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