View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.dbcp2;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import java.io.PrintWriter;
24  import java.sql.Connection;
25  import java.sql.SQLException;
26  import java.sql.SQLFeatureNotSupportedException;
27  import java.util.logging.Logger;
28  
29  import javax.sql.DataSource;
30  
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Test for DataSourceConnectionFactory.
36   */
37  public class TestDataSourceConnectionFactory {
38  
39      private static final class TestDataSource implements DataSource {
40  
41          @Override
42          public Connection getConnection() throws SQLException {
43              return new TesterConnection(null, null);
44          }
45  
46          @Override
47          public Connection getConnection(final String username, final String password) throws SQLException {
48              return new TesterConnection(username, password);
49          }
50  
51          @Override
52          public int getLoginTimeout() throws SQLException {
53              return 0;
54          }
55  
56          @Override
57          public PrintWriter getLogWriter() throws SQLException {
58              return null;
59          }
60  
61          @Override
62          public Logger getParentLogger() throws SQLFeatureNotSupportedException {
63              return null;
64          }
65  
66          @Override
67          public boolean isWrapperFor(final Class<?> iface) throws SQLException {
68              return false;
69          }
70  
71          @Override
72          public void setLoginTimeout(final int seconds) throws SQLException {
73              // noop
74          }
75  
76          @Override
77          public void setLogWriter(final PrintWriter out) throws SQLException {
78              // noop
79          }
80  
81          @Override
82          public <T> T unwrap(final Class<T> iface) throws SQLException {
83              return null;
84          }
85      }
86      private DataSource datasource;
87  
88      private DataSourceConnectionFactory factory;
89  
90      @BeforeEach
91      public void setUp() {
92          datasource = new TestDataSource();
93          factory = new DataSourceConnectionFactory(datasource);
94      }
95  
96      @Test
97      public void testCredentials() throws SQLException {
98          final DataSourceConnectionFactory factory = new DataSourceConnectionFactory(datasource, "foo", "bar");
99          try (Connection conn = factory.createConnection()) {
100             assertEquals("foo", ((TesterConnection) conn).getUserName());
101         }
102     }
103 
104     @Test
105     public void testDefaultValues() throws SQLException {
106         try (Connection conn = factory.createConnection()) {
107             assertNull(((TesterConnection) conn).getUserName());
108         }
109     }
110 
111     @Test
112     public void testEmptyPassword() throws SQLException {
113         final DataSourceConnectionFactory factory = new DataSourceConnectionFactory(datasource, "foo", (char[]) null);
114         try (Connection conn = factory.createConnection()) {
115             assertEquals("foo", ((TesterConnection) conn).getUserName());
116         }
117     }
118 
119     @Test
120     public void testEmptyUser() throws SQLException {
121         final DataSourceConnectionFactory factory = new DataSourceConnectionFactory(datasource, null, new char[] { 'a' });
122         try (Connection conn = factory.createConnection()) {
123             assertNull(((TesterConnection) conn).getUserName());
124         }
125     }
126 }