View Javadoc
1   /*
2    * This file is part of dependency-check-core.
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   * Copyright (c) 2014 Jeremy Long. All Rights Reserved.
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   * @author Jeremy Long
33   */
34  public class DriverLoaderTest extends BaseTest {
35  
36      /**
37       * Test of load method, of class DriverLoader.
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       * Test of load method, of class DriverLoader; expecting an exception due to
54       * a bad driver class name.
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       * Test of load method, of class DriverLoader.
64       */
65      @Test
66      public void testLoad_String_String() throws Exception {
67          String className = "com.mysql.jdbc.Driver";
68          //we know this is in target/test-classes
69          //File testClassPath = (new File(this.getClass().getClassLoader().getResource("org.mortbay.jetty.jar").getPath())).getParentFile();
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       * Test of load method, of class DriverLoader.
88       */
89      @Test
90      public void testLoad_String_String_multiple_paths() throws Exception {
91          final String className = "com.mysql.jdbc.Driver";
92          //we know this is in target/test-classes
93          //final File testClassPath = (new File(this.getClass().getClassLoader().getResource("org.mortbay.jetty.jar").getPath())).getParentFile();
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      * Test of load method, of class DriverLoader with an incorrect class name.
111      */
112     @Test(expected = DriverLoadException.class)
113     public void testLoad_String_String_badClassName() throws Exception {
114         String className = "com.mybad.jdbc.Driver";
115         //we know this is in target/test-classes
116         //File testClassPath = (new File(this.getClass().getClassLoader().getResource("org.mortbay.jetty.jar").getPath())).getParentFile();
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      * Test of load method, of class DriverLoader with an incorrect class path.
126      */
127     @Test(expected = DriverLoadException.class)
128     public void testLoad_String_String_badPath() throws Exception {
129         String className = "com.mysql.jdbc.Driver";
130         //we know this is in target/test-classes
131         //File testClassPath = (new File(this.getClass().getClassLoader().getResource("org.mortbay.jetty.jar").getPath())).getParentFile();
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 }