This is an automated email from the ASF dual-hosted git repository. dschneider pushed a commit to branch feature/GEODE-6459 in repository https://gitbox.apache.org/repos/asf/geode.git
The following commit(s) were added to refs/heads/feature/GEODE-6459 by this push: new f5c7585 added more coverage f5c7585 is described below commit f5c7585e71f7992efe54bed901f329f110fc2253 Author: Darrel Schneider <dschnei...@pivotal.io> AuthorDate: Mon Mar 4 17:03:35 2019 -0800 added more coverage --- .../CreateMappingPreconditionCheckFunction.java | 24 +++---- ...CreateMappingPreconditionCheckFunctionTest.java | 80 +++++++++++++++++++++- 2 files changed, 90 insertions(+), 14 deletions(-) diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunction.java b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunction.java index 184c444..bb55af4 100644 --- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunction.java +++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunction.java @@ -229,7 +229,7 @@ public class CreateMappingPreconditionCheckFunction extends CliFunction<Object[] } } - private Path createTemporaryDirectory(String prefix) { + Path createTemporaryDirectory(String prefix) { try { return createTempDirectory(prefix); } catch (IOException ex) { @@ -240,7 +240,7 @@ public class CreateMappingPreconditionCheckFunction extends CliFunction<Object[] } - private void deleteDirectory(Path tempDir) { + void deleteDirectory(Path tempDir) { try { FileUtils.deleteDirectory(tempDir.toFile()); } catch (IOException ioe) { @@ -277,25 +277,18 @@ public class CreateMappingPreconditionCheckFunction extends CliFunction<Object[] } try { Path tempPdxClassFile = Paths.get(tempDir.toString(), remoteInputStreamName); - try (FileOutputStream fos = new FileOutputStream(tempPdxClassFile.toString()); - InputStream input = RemoteInputStreamClient.wrap(remoteInputStream)) { - IOUtils.copyLarge(input, fos); + try (InputStream input = RemoteInputStreamClient.wrap(remoteInputStream); + FileOutputStream output = new FileOutputStream(tempPdxClassFile.toString())) { + copyFile(input, output); } return tempPdxClassFile.toFile(); } catch (IOException iox) { - closeIgnoringException(remoteInputStream); throw new JdbcConnectorException( "The pdx class file \"" + remoteInputStreamName + "\" could not be copied to a temporary file, because: " + iox); } } - private void closeIgnoringException(RemoteInputStream remoteInputStream) { - try { - remoteInputStream.close(true); - } catch (IOException ignore) { - } - } // unit test mocks this method DataSource getDataSource(String dataSourceName) { @@ -308,7 +301,7 @@ public class CreateMappingPreconditionCheckFunction extends CliFunction<Object[] } // unit test mocks this method - private Class<?> loadClass(String className, URL url) throws ClassNotFoundException { + Class<?> loadClass(String className, URL url) throws ClassNotFoundException { return URLClassLoader.newInstance(new URL[] {url}).loadClass(className); } @@ -332,4 +325,9 @@ public class CreateMappingPreconditionCheckFunction extends CliFunction<Object[] return Files.createTempDirectory(prefix); } + // unit test mocks this method + void copyFile(InputStream input, FileOutputStream output) throws IOException { + IOUtils.copyLarge(input, output); + } + } diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunctionTest.java b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunctionTest.java index 31ee166..c37c1cd 100644 --- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunctionTest.java +++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/CreateMappingPreconditionCheckFunctionTest.java @@ -19,12 +19,15 @@ import static org.assertj.core.api.Assertions.catchThrowable; import static org.mockito.Mockito.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.same; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.io.IOException; import java.io.Serializable; import java.sql.Connection; import java.sql.JDBCType; @@ -484,10 +487,12 @@ public class CreateMappingPreconditionCheckFunctionTest { } @Test - public void executeFunctionThrowsClassNotFoundExceptionGivenDummyRemoteInputStream() { + public void executeFunctionThrowsGivenRemoteInputStreamAndLoadClassThatThrowsClassNotFound() + throws ClassNotFoundException { remoteInputStreamName = "remoteInputStreamName"; remoteInputStream = mock(RemoteInputStream.class); setupInputArgs(); + doThrow(ClassNotFoundException.class).when(function).loadClass(eq(PDX_CLASS_NAME), any()); Throwable throwable = catchThrowable(() -> function.executeFunction(context)); @@ -495,5 +500,78 @@ public class CreateMappingPreconditionCheckFunctionTest { .hasMessageContaining( "The pdx class \"" + PDX_CLASS_NAME + "\" could not be loaded because: ") .hasMessageContaining("ClassNotFoundException"); + verify(function).createTemporaryDirectory(any()); + verify(function).deleteDirectory(any()); } + + @Test + public void executeFunctionThrowsGivenRemoteInputStreamAndcreateTempDirectoryException() + throws IOException { + remoteInputStreamName = "remoteInputStreamName"; + remoteInputStream = mock(RemoteInputStream.class); + setupInputArgs(); + doThrow(IOException.class).when(function).createTempDirectory(any()); + + Throwable throwable = catchThrowable(() -> function.executeFunction(context)); + + assertThat(throwable).isInstanceOf(JdbcConnectorException.class) + .hasMessageContaining( + "Could not create a temporary directory with the prefix \"pdx-class-dir-\" because: ") + .hasMessageContaining("IOException"); + verify(function, never()).deleteDirectory(any()); + verify(remoteInputStream, never()).close(true); + } + + @Test + public void executeFunctionThrowsGivenRemoteInputStreamAndCopyFileIOException() + throws IOException { + remoteInputStreamName = "remoteInputStreamName"; + remoteInputStream = mock(RemoteInputStream.class); + setupInputArgs(); + doThrow(IOException.class).when(function).copyFile(any(), any()); + + Throwable throwable = catchThrowable(() -> function.executeFunction(context)); + + assertThat(throwable).isInstanceOf(JdbcConnectorException.class) + .hasMessageContaining( + "The pdx class file \"" + remoteInputStreamName + + "\" could not be copied to a temporary file, because: ") + .hasMessageContaining("IOException"); + verify(function).createTemporaryDirectory(any()); + verify(function).deleteDirectory(any()); + verify(remoteInputStream).close(true); + } + + @Test + public void executeFunctionReturnsSuccessGivenRemoteInputStreamClassAndPackageName() + throws ClassNotFoundException { + remoteInputStreamName = "remoteInputStreamName.class"; + remoteInputStream = mock(RemoteInputStream.class); + setupInputArgs(); + String PDX_CLASS_NAME_WITH_PACKAGE = "foo.bar.MyPdxClassName"; + when(regionMapping.getPdxName()).thenReturn(PDX_CLASS_NAME_WITH_PACKAGE); + doReturn(PdxClassDummy.class).when(function).loadClass(eq(PDX_CLASS_NAME_WITH_PACKAGE), any()); + + CliFunctionResult result = function.executeFunction(context); + + assertThat(result.isSuccessful()).isTrue(); + verify(function).createTemporaryDirectory(any()); + verify(function).deleteDirectory(any()); + } + + @Test + public void executeFunctionReturnsSuccessGivenRemoteInputStreamJar() + throws ClassNotFoundException { + remoteInputStreamName = "remoteInputStreamName.jar"; + remoteInputStream = mock(RemoteInputStream.class); + setupInputArgs(); + doReturn(PdxClassDummy.class).when(function).loadClass(eq(PDX_CLASS_NAME), any()); + + CliFunctionResult result = function.executeFunction(context); + + assertThat(result.isSuccessful()).isTrue(); + verify(function).createTemporaryDirectory(any()); + verify(function).deleteDirectory(any()); + } + }