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