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 io.github.jeremylong.jcs3.slf4j.Slf4jAdapter;
19  import java.io.File;
20  import java.io.InputStream;
21  import java.net.URISyntaxException;
22  import org.junit.After;
23  
24  import org.junit.AfterClass;
25  import org.junit.Assume;
26  import org.junit.Before;
27  import org.owasp.dependencycheck.utils.Settings;
28  
29  /**
30   *
31   * @author Jeremy Long
32   */
33  public abstract class BaseTest {
34  
35      /**
36       * The configured settings.
37       */
38      private Settings settings;
39  
40      /**
41       * Initialize the {@link Settings}.
42       */
43      @Before
44      public void setUp() throws Exception {
45          System.setProperty("jcs.logSystem", "slf4j");
46          Slf4jAdapter.muteLogging(true);
47          settings = new Settings();
48      }
49  
50      /**
51       * Clean the {@link Settings}.
52       */
53      @After
54      public void tearDown() throws Exception {
55          settings.cleanup(true);
56      }
57  
58      @AfterClass
59      public static void tearDownClass() throws Exception {
60          File f = new File("./target/data/odc.mv.db");
61          if (f.exists() && f.isFile() && f.length() < 71680) {
62              System.err.println("------------------------------------------------");
63              System.err.println("------------------------------------------------");
64              System.err.println("Test referenced CveDB() and does not extend BaseDbTestCases?");
65              System.err.println("------------------------------------------------");
66              System.err.println("------------------------------------------------");
67          }
68      }
69  
70      /**
71       * Returns the given resource as an InputStream using the object's class
72       * loader. The org.junit.Assume API is used so that test cases are skipped
73       * if the resource is not available.
74       *
75       * @param o the object used to obtain a reference to the class loader
76       * @param resource the name of the resource to load
77       * @return the resource as an InputStream
78       */
79      public static InputStream getResourceAsStream(Object o, String resource) {
80          getResourceAsFile(o, resource);
81          return o.getClass().getClassLoader().getResourceAsStream(resource);
82      }
83  
84      /**
85       * Returns the given resource as a File using the object's class loader. The
86       * org.junit.Assume API is used so that test cases are skipped if the
87       * resource is not available.
88       *
89       * @param o the object used to obtain a reference to the class loader
90       * @param resource the name of the resource to load
91       * @return the resource as an File
92       */
93      public static File getResourceAsFile(Object o, String resource) {
94          try {
95              File f = new File(o.getClass().getClassLoader().getResource(resource).toURI().getPath());
96              Assume.assumeTrue(String.format("%n%n[SEVERE] Unable to load resource for test case: %s%n%n", resource), f.exists());
97              return f;
98          } catch (URISyntaxException e) {
99              throw new UnsupportedOperationException(e);
100         }
101     }
102 
103     /**
104      * Returns the settings for the test cases.
105      *
106      * @return
107      */
108     protected Settings getSettings() {
109         return settings;
110     }
111 }