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