1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37 public abstract class BaseTest {
38
39
40
41
42 private Settings settings;
43
44
45
46
47 @BeforeEach
48 public void setUp() throws Exception {
49 settings = new Settings();
50 }
51
52
53
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
74
75
76
77
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
85
86
87
88
89
90 public static @NonNull File getResourceAsFile(Object o, String resource) {
91 return new File(getResourceAsURI(o, resource).getPath());
92 }
93
94
95
96
97
98
99
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
111
112
113
114
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
126
127
128
129
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
141
142 protected Settings getSettings() {
143 return settings;
144 }
145 }