You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rave.apache.org by mf...@apache.org on 2011/08/22 18:38:27 UTC
svn commit: r1160327 - in /incubator/rave/trunk/rave-commons/src:
main/java/org/apache/rave/jdbc/util/
main/java/org/apache/rave/persistence/jpa/ test/java/org/apache/rave/jdbc/
test/java/org/apache/rave/jdbc/util/ test/resources/
Author: mfranklin
Date: Mon Aug 22 16:38:27 2011
New Revision: 1160327
URL: http://svn.apache.org/viewvc?rev=1160327&view=rev
Log:
Added test coverage for rave-commons (Supports RAVE-174)
Added:
incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/jdbc/
incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/jdbc/util/
incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/jdbc/util/DataSourcePopulatorTest.java
incubator/rave/trunk/rave-commons/src/test/resources/test-child.sql
incubator/rave/trunk/rave-commons/src/test/resources/test-data.sql
Modified:
incubator/rave/trunk/rave-commons/src/main/java/org/apache/rave/jdbc/util/DataSourcePopulator.java
incubator/rave/trunk/rave-commons/src/main/java/org/apache/rave/persistence/jpa/PopulatedLocalContainerEntityManagerFactory.java
Modified: incubator/rave/trunk/rave-commons/src/main/java/org/apache/rave/jdbc/util/DataSourcePopulator.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-commons/src/main/java/org/apache/rave/jdbc/util/DataSourcePopulator.java?rev=1160327&r1=1160326&r2=1160327&view=diff
==============================================================================
--- incubator/rave/trunk/rave-commons/src/main/java/org/apache/rave/jdbc/util/DataSourcePopulator.java (original)
+++ incubator/rave/trunk/rave-commons/src/main/java/org/apache/rave/jdbc/util/DataSourcePopulator.java Mon Aug 22 16:38:27 2011
@@ -25,6 +25,7 @@ import org.springframework.core.io.Resou
import javax.sql.DataSource;
import java.io.IOException;
+import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -53,7 +54,8 @@ import java.util.List;
* </code>
* <p/>
*/
-public class DataSourcePopulator {
+public class DataSourcePopulator implements Serializable {
+ private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory.getLogger(DataSourcePopulator.class);
@@ -75,8 +77,9 @@ public class DataSourcePopulator {
* @param executeScriptQuery {@see setExecuteScriptQuery}
*/
public DataSourcePopulator(List<Resource> scriptLocations, String executeScriptQuery) {
- setScriptLocations(scriptLocations);
- setExecuteScriptQuery(executeScriptQuery);
+ this.scriptLocations = scriptLocations;
+ this.executeScriptQuery = executeScriptQuery;
+
}
/**
@@ -158,7 +161,7 @@ public class DataSourcePopulator {
boolean result;
try {
//If the ResultSet has any rows, the first method will return true
- result = !executeQuery(conn, executeScriptQuery).first();
+ result = executeScriptQuery == null || !executeQuery(conn, executeScriptQuery).first();
} catch (SQLException e) {
//Only return true if the execption we got is that the table was not found
result = e.getMessage().toLowerCase().matches("table \".*\" not found.*\n*.*");
Modified: incubator/rave/trunk/rave-commons/src/main/java/org/apache/rave/persistence/jpa/PopulatedLocalContainerEntityManagerFactory.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-commons/src/main/java/org/apache/rave/persistence/jpa/PopulatedLocalContainerEntityManagerFactory.java?rev=1160327&r1=1160326&r2=1160327&view=diff
==============================================================================
--- incubator/rave/trunk/rave-commons/src/main/java/org/apache/rave/persistence/jpa/PopulatedLocalContainerEntityManagerFactory.java (original)
+++ incubator/rave/trunk/rave-commons/src/main/java/org/apache/rave/persistence/jpa/PopulatedLocalContainerEntityManagerFactory.java Mon Aug 22 16:38:27 2011
@@ -33,6 +33,8 @@ import javax.persistence.spi.Persistence
* DataSource immediately after the factory is initialized
*/
public class PopulatedLocalContainerEntityManagerFactory extends LocalContainerEntityManagerFactoryBean {
+ private static final long serialVersionUID = 1L;
+
private DataSourcePopulator populator;
public PopulatedLocalContainerEntityManagerFactory() {
Added: incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/jdbc/util/DataSourcePopulatorTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/jdbc/util/DataSourcePopulatorTest.java?rev=1160327&view=auto
==============================================================================
--- incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/jdbc/util/DataSourcePopulatorTest.java (added)
+++ incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/jdbc/util/DataSourcePopulatorTest.java Mon Aug 22 16:38:27 2011
@@ -0,0 +1,192 @@
+/*
+ * 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.rave.jdbc.util;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.easymock.EasyMock.*;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class DataSourcePopulatorTest {
+
+ public static final String INSERT = "INSERT INTO BAR VALUES('FOO'); INSERT INTO FOO VALUES ('BAR'); ";
+ private static final String CHECK_QUERY = "QUERY FOR TABLES";
+ private static final String TABLE_NOT_FOUND_MSG = "TABLE \"BOO\" NOT FOUND";
+
+ private DataSource dataSource;
+ private Connection connection;
+ private DataSourcePopulator populator;
+ private List<Resource> resourceList;
+
+ @Before
+ public void setup() throws SQLException {
+ dataSource = createNiceMock(DataSource.class);
+ connection = createNiceMock(Connection.class);
+ expect(dataSource.getConnection()).andReturn(connection);
+ replay(dataSource);
+ resourceList = createScriptList();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void invalid_config() {
+ populator = new DataSourcePopulator(null, CHECK_QUERY);
+ populator.initialize(dataSource);
+ }
+
+ @Test
+ public void null_check() throws SQLException {
+ Statement populate = createNiceMock(Statement.class);
+ expect(populate.execute(INSERT)).andReturn(true).once();
+ replay(populate);
+
+ expect(connection.createStatement()).andReturn(populate).once();
+ connection.close();
+ expectLastCall();
+ replay(connection);
+
+ populator = new DataSourcePopulator(resourceList, null);
+ populator.initialize(dataSource);
+ verify(populate);
+ }
+
+ @Test
+ public void valid_execute() throws SQLException {
+ Statement check = createNiceMock(Statement.class);
+ expect(check.executeQuery(CHECK_QUERY)).andThrow(new SQLException(TABLE_NOT_FOUND_MSG));
+ replay(check);
+
+ Statement populate = createNiceMock(Statement.class);
+ expect(populate.execute(INSERT)).andReturn(true).once();
+ replay(populate);
+
+ expect(connection.createStatement()).andReturn(check).once();
+ expect(connection.createStatement()).andReturn(populate).once();
+ connection.close();
+ expectLastCall();
+ replay(connection);
+
+ populator = new DataSourcePopulator(resourceList, CHECK_QUERY);
+ populator.initialize(dataSource);
+ verify(populate);
+ }
+
+ @Test
+ public void valid_execute_nodata() throws SQLException {
+ ResultSet rs = createNiceMock(ResultSet.class);
+ expect(rs.first()).andReturn(false);
+ replay(rs);
+
+ Statement check = createNiceMock(Statement.class);
+ expect(check.executeQuery(CHECK_QUERY)).andReturn(rs);
+ replay(check);
+
+ Statement populate = createNiceMock(Statement.class);
+ expect(populate.execute(INSERT)).andReturn(true).once();
+ replay(populate);
+
+ expect(connection.createStatement()).andReturn(check).once();
+ expect(connection.createStatement()).andReturn(populate).once();
+ connection.close();
+ expectLastCall();
+ replay(connection);
+
+ populator = new DataSourcePopulator(resourceList, CHECK_QUERY);
+ populator.initialize(dataSource);
+ verify(populate);
+ }
+
+ @Test
+ public void invalid_SQL() throws SQLException {
+ Statement check = createNiceMock(Statement.class);
+ expect(check.executeQuery(CHECK_QUERY)).andThrow(new SQLException(TABLE_NOT_FOUND_MSG));
+ replay(check);
+
+ Statement populate = createNiceMock(Statement.class);
+ expect(populate.execute(INSERT)).andThrow(new SQLException());
+ replay(populate);
+
+ expect(connection.createStatement()).andReturn(check).once();
+ expect(connection.createStatement()).andReturn(populate).once();
+ connection.close();
+ expectLastCall();
+ replay(connection);
+
+ populator = new DataSourcePopulator(resourceList, CHECK_QUERY);
+ try {
+ populator.initialize(dataSource);
+ } catch (Exception e) {
+ verify(connection);
+ }
+ }
+
+ @Test
+ public void failed_close() throws SQLException {
+ ResultSet rs = createNiceMock(ResultSet.class);
+ expect(rs.first()).andReturn(true);
+ replay(rs);
+
+ Statement check = createNiceMock(Statement.class);
+ expect(check.executeQuery(CHECK_QUERY)).andReturn(rs);
+ replay(check);
+
+ expect(connection.createStatement()).andReturn(check).once();
+ connection.close();
+ expectLastCall().andThrow(new SQLException());
+ replay(connection);
+
+ populator = new DataSourcePopulator(resourceList, CHECK_QUERY);
+ populator.initialize(dataSource);
+ //Test passes because no exception was bubbled up on the failed connection closing
+ assertThat(true, is(true));
+ }
+
+ @Test (expected = RuntimeException.class)
+ public void failedConnection() throws SQLException {
+
+ populator = new DataSourcePopulator();
+ populator.setExecuteScriptQuery(CHECK_QUERY);
+ populator.setScriptLocations(resourceList);
+
+ dataSource = createNiceMock(DataSource.class);
+ expect(dataSource.getConnection()).andThrow(new SQLException());
+ replay(dataSource);
+
+ populator.initialize(dataSource);
+
+ }
+
+ private List<Resource> createScriptList() {
+ List<Resource> resourceList = new ArrayList<Resource>();
+ resourceList.add(new ClassPathResource("test-data.sql"));
+ return resourceList;
+ }
+}
Added: incubator/rave/trunk/rave-commons/src/test/resources/test-child.sql
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-commons/src/test/resources/test-child.sql?rev=1160327&view=auto
==============================================================================
--- incubator/rave/trunk/rave-commons/src/test/resources/test-child.sql (added)
+++ incubator/rave/trunk/rave-commons/src/test/resources/test-child.sql Mon Aug 22 16:38:27 2011
@@ -0,0 +1 @@
+INSERT INTO BAR VALUES('FOO');
\ No newline at end of file
Added: incubator/rave/trunk/rave-commons/src/test/resources/test-data.sql
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-commons/src/test/resources/test-data.sql?rev=1160327&view=auto
==============================================================================
--- incubator/rave/trunk/rave-commons/src/test/resources/test-data.sql (added)
+++ incubator/rave/trunk/rave-commons/src/test/resources/test-data.sql Mon Aug 22 16:38:27 2011
@@ -0,0 +1,2 @@
+@@test-child.sql
+INSERT INTO FOO VALUES ('BAR');
\ No newline at end of file