View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.chukwa.util;
19  
20  import java.sql.Connection;
21  import java.sql.SQLException;
22  
23  import org.apache.hadoop.chukwa.util.DriverManagerUtil.ConnectionInfo;
24  
25  import junit.framework.TestCase;
26  
27  public class DriverManagerUtilTest extends TestCase {
28  
29    public void testLoadDriver() throws ClassNotFoundException {
30      Class<?> clazz = DriverManagerUtil.loadDriver();
31      System.out.println(clazz);
32    }
33  
34    public void testGetConnectionInfo() {
35      {
36        String url = "jdbc:mysql://localhost:3306/demo";
37        ConnectionInfo ci = new ConnectionInfo(url);
38        assertEquals("jdbc:mysql://localhost:3306/demo", ci.getUri());
39        assertEquals(0, ci.getProperties().size());
40      }
41      
42      {
43        String url = "jdbc:mysql://localhost:3306/demo?user=example";
44        ConnectionInfo ci = new ConnectionInfo(url);
45        assertEquals("jdbc:mysql://localhost:3306/demo", ci.getUri());
46        assertEquals(1, ci.getProperties().size());
47        assertEquals("example", ci.getProperties().get("user"));
48      }
49      
50      {
51        String url = "jdbc:mysql://localhost:3306/demo?user=example&";
52        ConnectionInfo ci = new ConnectionInfo(url);
53        assertEquals("jdbc:mysql://localhost:3306/demo", ci.getUri());
54        assertEquals(1, ci.getProperties().size());
55        assertEquals("example", ci.getProperties().get("user"));
56      }
57      
58      {
59        String url = "jdbc:mysql://localhost:3306/demo?user=example&pwd";
60        ConnectionInfo ci = new ConnectionInfo(url);
61        assertEquals("jdbc:mysql://localhost:3306/demo", ci.getUri());
62        assertEquals(2, ci.getProperties().size());
63        assertEquals("example", ci.getProperties().get("user"));
64        assertEquals("", ci.getProperties().get("pwd"));
65      }
66      
67      {
68        String url = "jdbc:mysql://localhost:3306/demo?user=example&pwd=";
69        ConnectionInfo ci = new ConnectionInfo(url);
70        assertEquals("jdbc:mysql://localhost:3306/demo", ci.getUri());
71        assertEquals(2, ci.getProperties().size());
72        assertEquals("example", ci.getProperties().get("user"));
73        assertEquals("", ci.getProperties().get("pwd"));
74      }
75      
76      {
77        String url = "jdbc:mysql://localhost:3306/demo?user=example&pwd=ppppp";
78        ConnectionInfo ci = new ConnectionInfo(url);
79        assertEquals("jdbc:mysql://localhost:3306/demo", ci.getUri());
80        assertEquals(2, ci.getProperties().size());
81        assertEquals("example", ci.getProperties().get("user"));
82        assertEquals("ppppp", ci.getProperties().get("pwd"));
83      }
84    }
85  
86    public void testGetConnection() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
87      if(false) {
88        DriverManagerUtil.loadDriver().newInstance();
89        String url = "jdbc:mysql://localhost:3306/test?user=root&pwd=";
90        Connection conn = DriverManagerUtil.getConnection(url);
91      }
92    } 
93  }