View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.utils;
19  
20  import java.io.File;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  import org.junit.Test;
26  
27  /**
28   *
29   * @author Jeremy Long
30   */
31  public class FileUtilsTest extends BaseTest {
32  
33      /**
34       * Test of getFileExtension method, of class FileUtils.
35       */
36      @Test
37      public void testGetFileExtension() {
38          String[] fileName = {"something-0.9.5.jar", "lib2-1.1.js", "dir.tmp/noext"};
39          String[] expResult = {"jar", "js", null};
40  
41          for (int i = 0; i < fileName.length; i++) {
42              String result = FileUtils.getFileExtension(fileName[i]);
43              assertEquals("Failed extraction on \"" + fileName[i] + "\".", expResult[i], result);
44          }
45      }
46  
47      /**
48       * Test of delete method, of class FileUtils.
49       */
50      @Test
51      public void testDelete() throws Exception {
52  
53          File file = File.createTempFile("tmp", "deleteme", Settings.getTempDirectory());
54          if (!file.exists()) {
55              fail("Unable to create a temporary file.");
56          }
57          boolean status = FileUtils.delete(file);
58          assertTrue("delete returned a failed status", status);
59          assertFalse("Temporary file exists after attempting deletion", file.exists());
60      }
61  }