From c0543d2d71be8714f73e3c97a982323c723f9089 Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Thu, 25 Jun 2009 05:30:15 +0000 Subject: [PATCH] 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 --- .../handler/dataimport/JdbcDataSource.java | 2 +- .../dataimport/MockInitialContextFactory.java | 63 ++++++++++ .../dataimport/TestJdbcDataSource.java | 112 +++++++++++++++++- 3 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/MockInitialContextFactory.java diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java index 4162dea550f..8347df18409 100644 --- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java +++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java @@ -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); diff --git a/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/MockInitialContextFactory.java b/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/MockInitialContextFactory.java new file mode 100644 index 00000000000..30a86cbe43e --- /dev/null +++ b/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/MockInitialContextFactory.java @@ -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 objects = new HashMap(); + 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() { + @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); + } +} diff --git a/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestJdbcDataSource.java b/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestJdbcDataSource.java index ae9f699ebc6..ac41c85524e 100644 --- a/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestJdbcDataSource.java +++ b/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestJdbcDataSource.java @@ -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.*; + /** *

* 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> fields = new ArrayList>(); + + 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()); } - }