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.jspecify.annotations.NonNull;
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.IOException;
26  import java.io.InputStream;
27  import java.net.MalformedURLException;
28  import java.net.URI;
29  import java.net.URISyntaxException;
30  import java.nio.charset.StandardCharsets;
31  import java.util.Objects;
32  
33  /**
34   *
35   * @author Jeremy Long
36   */
37  public abstract class BaseTest {
38  
39      /**
40       * The configured settings.
41       */
42      private Settings settings;
43  
44      /**
45       * Initialize the {@link Settings}.
46       */
47      @BeforeEach
48      public void setUp() throws Exception {
49          settings = new Settings();
50      }
51  
52      /**
53       * Clean the {@link Settings}.
54       */
55      @AfterEach
56      public void tearDown() throws Exception {
57          settings.cleanup(true);
58      }
59  
60      @AfterAll
61      public static void tearDownClass() {
62          File f = new File("./target/data/odc.mv.db");
63          if (f.exists() && f.isFile() && f.length() < 71680) {
64              System.err.println("------------------------------------------------");
65              System.err.println("------------------------------------------------");
66              System.err.println("Test referenced CveDB() and does not extend BaseDbTestCases?");
67              System.err.println("------------------------------------------------");
68              System.err.println("------------------------------------------------");
69          }
70      }
71  
72      /**
73       * Returns the given resource as an InputStream using the object's class loader.
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 @NonNull InputStream getResourceAsStream(Object o, String resource) {
80          return Objects.requireNonNull(o.getClass().getClassLoader().getResourceAsStream(resource), resource + " not found on classpath");
81      }
82  
83      /**
84       * Returns the given resource as a File using the object's class loader.
85       *
86       * @param o        the object used to obtain a reference to the class loader
87       * @param resource the name of the resource to load
88       * @return the resource as a File
89       */
90      public static @NonNull File getResourceAsFile(Object o, String resource) {
91          return new File(getResourceAsURI(o, resource).getPath());
92      }
93  
94      /**
95       * Returns the given resource as a URI using the object's class loader.
96       *
97       * @param o        the object used to obtain a reference to the class loader
98       * @param resource the name of the resource to load
99       * @return the resource as a URI
100      */
101     public static @NonNull URI getResourceAsURI(Object o, String resource) {
102         try {
103             return Objects.requireNonNull(o.getClass().getClassLoader().getResource(resource), resource + " not found on classpath").toURI();
104         } catch (URISyntaxException e) {
105             throw new UnsupportedOperationException(e);
106         }
107     }
108 
109     /**
110      * Returns the given resource as a URL string using the object's class loader.
111      *
112      * @param o        the object used to obtain a reference to the class loader
113      * @param resource the name of the resource to load
114      * @return the resource as a URL string
115      */
116     public static @NonNull String getResourceAsUrlString(Object o, String resource) {
117         try {
118             return getResourceAsURI(o, resource).toURL().toString();
119         } catch (MalformedURLException e) {
120             throw new UnsupportedOperationException(e);
121         }
122     }
123 
124     /**
125      * Returns the given resource content using the object's class loader.
126      *
127      * @param o        the object used to obtain a reference to the class loader
128      * @param resource the name of the resource to load
129      * @return the resource as a String
130      */
131     public static @NonNull String getResourceAsContentString(Object o, String resource) {
132         try (InputStream is = getResourceAsStream(o, resource)) {
133             return new String(is.readAllBytes(), StandardCharsets.UTF_8);
134         } catch (IOException e) {
135             throw new UnsupportedOperationException(e);
136         }
137     }
138 
139     /**
140      * @return the settings for the test cases.
141      */
142     protected Settings getSettings() {
143         return settings;
144     }
145 }