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
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import org.junit.Test;
28 import org.owasp.dependencycheck.BaseTest;
29
30
31
32
33
34 public class DriverLoaderTest extends BaseTest {
35
36
37
38
39 @Test
40 public void testLoad_String() throws Exception {
41 String className = "org.h2.Driver";
42 Driver d = null;
43 try {
44 d = DriverLoader.load(className);
45 } finally {
46 if (d != null) {
47 DriverManager.deregisterDriver(d);
48 }
49 }
50 }
51
52
53
54
55
56 @Test(expected = DriverLoadException.class)
57 public void testLoad_String_ex() throws Exception {
58 String className = "bad.Driver";
59 Driver d = DriverLoader.load(className);
60 }
61
62
63
64
65 @Test
66 public void testLoad_String_String() throws Exception {
67 String className = "com.mysql.jdbc.Driver";
68
69
70 File testClassPath = BaseTest.getResourceAsFile(this, "org.mortbay.jetty.jar").getParentFile();
71 File driver = new File(testClassPath, "../../src/test/resources/mysql-connector-java-5.1.27-bin.jar");
72 assertTrue("MySQL Driver JAR file not found in src/test/resources?", driver.isFile());
73
74 Driver d = null;
75 try {
76 d = DriverLoader.load(className, driver.getAbsolutePath());
77 d = DriverManager.getDriver("jdbc:mysql://localhost:3306/dependencycheck");
78 assertNotNull(d);
79 } finally {
80 if (d != null) {
81 DriverManager.deregisterDriver(d);
82 }
83 }
84 }
85
86
87
88
89 @Test
90 public void testLoad_String_String_multiple_paths() throws Exception {
91 final String className = "com.mysql.jdbc.Driver";
92
93
94 final File testClassPath = BaseTest.getResourceAsFile(this, "org.mortbay.jetty.jar").getParentFile();
95 final File dir1 = new File(testClassPath, "../../src/test/");
96 final File dir2 = new File(testClassPath, "../../src/test/resources/");
97 final String paths = String.format("%s" + File.pathSeparator + "%s", dir1.getAbsolutePath(), dir2.getAbsolutePath());
98
99 Driver d = null;
100 try {
101 d = DriverLoader.load(className, paths);
102 } finally {
103 if (d != null) {
104 DriverManager.deregisterDriver(d);
105 }
106 }
107 }
108
109
110
111
112 @Test(expected = DriverLoadException.class)
113 public void testLoad_String_String_badClassName() throws Exception {
114 String className = "com.mybad.jdbc.Driver";
115
116
117 File testClassPath = BaseTest.getResourceAsFile(this, "org.mortbay.jetty.jar").getParentFile();
118 File driver = new File(testClassPath, "../../src/test/resources/mysql-connector-java-5.1.27-bin.jar");
119 assertTrue("MySQL Driver JAR file not found in src/test/resources?", driver.isFile());
120
121 Driver d = DriverLoader.load(className, driver.getAbsolutePath());
122 }
123
124
125
126
127 @Test(expected = DriverLoadException.class)
128 public void testLoad_String_String_badPath() throws Exception {
129 String className = "com.mysql.jdbc.Driver";
130
131
132 File testClassPath = BaseTest.getResourceAsFile(this, "org.mortbay.jetty.jar").getParentFile();
133 File driver = new File(testClassPath, "../../src/test/bad/mysql-connector-java-5.1.27-bin.jar");
134 Driver d = DriverLoader.load(className, driver.getAbsolutePath());
135 }
136 }