View Javadoc
1   /*
2    * Copyright 2014 OWASP.
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  package org.owasp.dependencycheck;
17  
18  import java.io.File;
19  import java.io.InputStream;
20  import org.junit.AfterClass;
21  import org.junit.Assume;
22  import org.junit.BeforeClass;
23  import org.owasp.dependencycheck.utils.Settings;
24  
25  /**
26   *
27   * @author Jeremy Long
28   */
29  public class BaseTest {
30  
31      @BeforeClass
32      public static void setUpClass() throws Exception {
33          Settings.initialize();
34      }
35  
36      @AfterClass
37      public static void tearDownClass() throws Exception {
38          File f = new File("./target/data/dc.h2.db");
39          if (f.exists() && f.isFile() && f.length() < 71680) {
40              System.err.println("------------------------------------------------");
41              System.err.println("------------------------------------------------");
42              System.err.println("Test referenced CveDB() and does not extend BaseDbTestCases?");
43              System.err.println("------------------------------------------------");
44              System.err.println("------------------------------------------------");
45          }
46  
47          Settings.cleanup(true);
48      }
49  
50      /**
51       * Returns the given resource as an InputStream using the object's class loader. The org.junit.Assume API is used so that test
52       * cases are skipped if the resource is not available.
53       *
54       * @param o the object used to obtain a reference to the class loader
55       * @param resource the name of the resource to load
56       * @return the resource as an InputStream
57       */
58      public static InputStream getResourceAsStream(Object o, String resource) {
59          getResourceAsFile(o, resource);
60          return o.getClass().getClassLoader().getResourceAsStream(resource);
61      }
62  
63      /**
64       * Returns the given resource as a File using the object's class loader. The org.junit.Assume API is used so that test cases
65       * are skipped if the resource is not available.
66       *
67       * @param o the object used to obtain a reference to the class loader
68       * @param resource the name of the resource to load
69       * @return the resource as an File
70       */
71      public static File getResourceAsFile(Object o, String resource) {
72          File f = new File(o.getClass().getClassLoader().getResource(resource).getPath());
73          Assume.assumeTrue(String.format("%n%n[SEVERE] Unable to load resource for test case: %s%n%n", resource), f.exists());
74          return f;
75      }
76  }