1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.data.nvdcve;
19
20 import java.io.File;
21 import java.sql.Driver;
22 import java.sql.DriverManager;
23 import org.junit.After;
24 import org.junit.AfterClass;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.owasp.dependencycheck.BaseTest;
31
32
33
34
35
36 public class DriverLoaderTest {
37
38 public DriverLoaderTest() {
39 }
40
41 @BeforeClass
42 public static void setUpClass() {
43 }
44
45 @AfterClass
46 public static void tearDownClass() {
47 }
48
49 @Before
50 public void setUp() {
51 }
52
53 @After
54 public void tearDown() {
55 }
56
57
58
59
60 @Test
61 public void testLoad_String() throws Exception {
62 String className = "org.h2.Driver";
63 Driver d = null;
64 try {
65 d = DriverLoader.load(className);
66 } finally {
67 if (d != null) {
68 DriverManager.deregisterDriver(d);
69 }
70 }
71 }
72
73
74
75
76 @Test(expected = DriverLoadException.class)
77 public void testLoad_String_ex() throws Exception {
78 String className = "bad.Driver";
79 Driver d = DriverLoader.load(className);
80 }
81
82
83
84
85 @Test
86 public void testLoad_String_String() throws Exception {
87 String className = "com.mysql.jdbc.Driver";
88
89
90 File testClassPath = BaseTest.getResourceAsFile(this, "org.mortbay.jetty.jar").getParentFile();
91 File driver = new File(testClassPath, "../../src/test/resources/mysql-connector-java-5.1.27-bin.jar");
92 assertTrue("MySQL Driver JAR file not found in src/test/resources?", driver.isFile());
93
94 Driver d = null;
95 try {
96 d = DriverLoader.load(className, driver.getAbsolutePath());
97 d = DriverManager.getDriver("jdbc:mysql://localhost:3306/dependencycheck");
98 assertNotNull(d);
99 } finally {
100 if (d != null) {
101 DriverManager.deregisterDriver(d);
102 }
103 }
104 }
105
106
107
108
109 @Test
110 public void testLoad_String_String_multiple_paths() throws Exception {
111 final String className = "com.mysql.jdbc.Driver";
112
113
114 final File testClassPath = BaseTest.getResourceAsFile(this, "org.mortbay.jetty.jar").getParentFile();
115 final File dir1 = new File(testClassPath, "../../src/test/");
116 final File dir2 = new File(testClassPath, "../../src/test/resources/");
117 final String paths = String.format("%s" + File.pathSeparator + "%s", dir1.getAbsolutePath(), dir2.getAbsolutePath());
118
119 Driver d = null;
120 try {
121 d = DriverLoader.load(className, paths);
122 } finally {
123 if (d != null) {
124 DriverManager.deregisterDriver(d);
125 }
126 }
127 }
128
129
130
131
132 @Test(expected = DriverLoadException.class)
133 public void testLoad_String_String_badClassName() throws Exception {
134 String className = "com.mybad.jdbc.Driver";
135
136
137 File testClassPath = BaseTest.getResourceAsFile(this, "org.mortbay.jetty.jar").getParentFile();
138 File driver = new File(testClassPath, "../../src/test/resources/mysql-connector-java-5.1.27-bin.jar");
139 assertTrue("MySQL Driver JAR file not found in src/test/resources?", driver.isFile());
140
141 Driver d = DriverLoader.load(className, driver.getAbsolutePath());
142 }
143
144
145
146
147 @Test(expected = DriverLoadException.class)
148 public void testLoad_String_String_badPath() throws Exception {
149 String className = "com.mysql.jdbc.Driver";
150
151
152 File testClassPath = BaseTest.getResourceAsFile(this, "org.mortbay.jetty.jar").getParentFile();
153 File driver = new File(testClassPath, "../../src/test/bad/mysql-connector-java-5.1.27-bin.jar");
154 Driver d = DriverLoader.load(className, driver.getAbsolutePath());
155 }
156 }