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