SOLR-1244 JdbcDataSource uses wrong overload of getConnection on JNDI DataSource

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@788265 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Noble Paul 2009-06-25 05:30:15 +00:00
parent 57a2a11ac8
commit c0543d2d71
3 changed files with 171 additions and 6 deletions

View File

@ -142,7 +142,7 @@ public class JdbcDataSource extends
javax.sql.DataSource dataSource = (javax.sql.DataSource) jndival;
String user = (String) initProps.get("user");
String pass = (String) initProps.get("password");
if(user != null){
if(user == null || user.trim().equals("")){
c = dataSource.getConnection();
} else {
c = dataSource.getConnection(user, pass);

View File

@ -0,0 +1,63 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.handler.dataimport;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
import org.easymock.IMocksControl;
public class MockInitialContextFactory implements InitialContextFactory {
private static final Map<String, Object> objects = new HashMap<String, Object>();
private final IMocksControl mockControl;
private final javax.naming.Context context;
public MockInitialContextFactory() {
mockControl = EasyMock.createStrictControl();
context = mockControl.createMock(javax.naming.Context.class);
try {
EasyMock.expect(context.lookup((String) EasyMock.anyObject())).andAnswer(
new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
return objects.get(EasyMock.getCurrentArguments()[0]);
}
}).anyTimes();
} catch (NamingException e) {
throw new RuntimeException(e);
}
mockControl.replay();
}
@SuppressWarnings("unchecked")
public javax.naming.Context getInitialContext(Hashtable env) {
return context;
}
public static void bind(String name, Object obj) {
objects.put(name, obj);
}
}

View File

@ -16,12 +16,17 @@
*/
package org.apache.solr.handler.dataimport;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.*;
import javax.sql.DataSource;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.*;
/**
* <p>
* Test for JdbcDataSource
@ -35,6 +40,104 @@ import java.util.*;
* @since solr 1.3
*/
public class TestJdbcDataSource {
Driver driver;
DataSource dataSource;
Connection connection;
IMocksControl mockControl;
JdbcDataSource jdbcDataSource = new JdbcDataSource();
List<Map<String, String>> fields = new ArrayList<Map<String, String>>();
Context context = AbstractDataImportHandlerTest.getContext(null, null,
jdbcDataSource, Context.FULL_DUMP, fields, null);
Properties props = new Properties();
String sysProp = System.getProperty("java.naming.factory.initial");
@Before
public void SetUp() throws ClassNotFoundException {
System.setProperty("java.naming.factory.initial",
MockInitialContextFactory.class.getName());
mockControl = EasyMock.createStrictControl();
driver = mockControl.createMock(Driver.class);
dataSource = mockControl.createMock(DataSource.class);
connection = mockControl.createMock(Connection.class);
}
@After
public void tearDown() {
if (sysProp == null) {
System.getProperties().remove("java.naming.factory.initial");
} else {
System.setProperty("java.naming.factory.initial", sysProp);
}
}
@Test
public void retrieveFromJndi() throws Exception {
MockInitialContextFactory.bind("java:comp/env/jdbc/JndiDB", dataSource);
props.put(JdbcDataSource.JNDI_NAME, "java:comp/env/jdbc/JndiDB");
EasyMock.expect(dataSource.getConnection()).andReturn(connection);
connection.setAutoCommit(false);
connection.setHoldability(1);
mockControl.replay();
Connection conn = jdbcDataSource.createConnectionFactory(context, props)
.call();
mockControl.verify();
Assert.assertSame("connection", conn, connection);
}
@Test
public void retrieveFromJndiWithCredentials() throws Exception {
MockInitialContextFactory.bind("java:comp/env/jdbc/JndiDB", dataSource);
props.put(JdbcDataSource.JNDI_NAME, "java:comp/env/jdbc/JndiDB");
props.put("user", "Fred");
props.put("password", "4r3d");
EasyMock.expect(dataSource.getConnection("Fred", "4r3d")).andReturn(
connection);
connection.setAutoCommit(false);
connection.setHoldability(1);
mockControl.replay();
Connection conn = jdbcDataSource.createConnectionFactory(context, props)
.call();
mockControl.verify();
Assert.assertSame("connection", conn, connection);
}
@Test
public void retrieveFromDriverManager() throws Exception {
DriverManager.registerDriver(driver);
EasyMock.expect(
driver.connect((String) EasyMock.notNull(), (Properties) EasyMock
.notNull())).andReturn(connection);
connection.setAutoCommit(false);
connection.setHoldability(1);
props.put(JdbcDataSource.DRIVER, driver.getClass().getName());
props.put(JdbcDataSource.URL, "jdbc:fakedb");
mockControl.replay();
Connection conn = jdbcDataSource.createConnectionFactory(context, props)
.call();
mockControl.verify();
Assert.assertSame("connection", conn, connection);
}
@Test
@Ignore
@ -74,5 +177,4 @@ public class TestJdbcDataSource {
Assert.assertEquals(Float.class, msrp.getClass());
Assert.assertEquals(Long.class, trim_id.getClass());
}
}