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 {
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       * Test of load method, of class DriverLoader.
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       * Test of load method, of class DriverLoader; expecting an exception due to a bad driver class name.
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       * Test of load method, of class DriverLoader.
84       */
85      @Test
86      public void testLoad_String_String() throws Exception {
87          String className = "com.mysql.jdbc.Driver";
88          //we know this is in target/test-classes
89          //File testClassPath = (new File(this.getClass().getClassLoader().getResource("org.mortbay.jetty.jar").getPath())).getParentFile();
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      * Test of load method, of class DriverLoader.
108      */
109     @Test
110     public void testLoad_String_String_multiple_paths() throws Exception {
111         final String className = "com.mysql.jdbc.Driver";
112         //we know this is in target/test-classes
113         //final File testClassPath = (new File(this.getClass().getClassLoader().getResource("org.mortbay.jetty.jar").getPath())).getParentFile();
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      * Test of load method, of class DriverLoader with an incorrect class name.
131      */
132     @Test(expected = DriverLoadException.class)
133     public void testLoad_String_String_badClassName() throws Exception {
134         String className = "com.mybad.jdbc.Driver";
135         //we know this is in target/test-classes
136         //File testClassPath = (new File(this.getClass().getClassLoader().getResource("org.mortbay.jetty.jar").getPath())).getParentFile();
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      * Test of load method, of class DriverLoader with an incorrect class path.
146      */
147     @Test(expected = DriverLoadException.class)
148     public void testLoad_String_String_badPath() throws Exception {
149         String className = "com.mysql.jdbc.Driver";
150         //we know this is in target/test-classes
151         //File testClassPath = (new File(this.getClass().getClassLoader().getResource("org.mortbay.jetty.jar").getPath())).getParentFile();
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 }