@Test public void shouldAllExceptionsExceptionQuietly() throws Exception { doThrow(new IOException()).when(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH)); try { doThrow(new RuntimeException("message")).when(pluginJarLocationMonitor).initialize(); agentPluginsInitializer.onApplicationEvent(null); } catch (Exception e) { fail("should have handled IOException"); } } }
@Test void shouldGiveAClearErrorIfTheArchiveAlreadyExists() throws Exception { doThrow( new FileAlreadyExistsException( "the-archive-path" ) ).when( dumper ).dump( any(), any(), any(), any() ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( "foo.db" ) ); assertEquals( "archive already exists: the-archive-path", commandFailed.getMessage() ); }
@Test public void testOnBodyPartReceivedWithResumableListenerThrowsException() throws Exception { ResumableAsyncHandler handler = new ResumableAsyncHandler(); ResumableListener resumableListener = mock(ResumableListener.class); doThrow(new IOException()).when(resumableListener).onBytesReceived(any()); handler.setResumableListener(resumableListener); HttpResponseBodyPart bodyPart = mock(HttpResponseBodyPart.class); State state = handler.onBodyPartReceived(bodyPart); assertEquals(state, AsyncHandler.State.ABORT, "State should be ABORT if the resumableListener threw an exception in onBodyPartReceived"); }
@Test void shouldGiveAClearMessageIfTheArchiveDoesntExist() throws IOException, IncorrectFormat { doThrow( new NoSuchFileException( archive.toString() ) ).when( loader ).load( any(), any(), any() ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( null ) ); assertEquals( "archive does not exist: " + archive, commandFailed.getMessage() ); }
@Test public void shouldReturnErrorResponseWhenFailedToUploadFile() throws Exception { when(systemEnvironment.get(PLUGIN_EXTERNAL_PROVIDED_PATH)).thenReturn(EXTERNAL_DIRECTORY_PATH); doThrow(new IOException()).when(goFileSystem).copyFile(any(File.class), any(File.class)); PluginUploadResponse response = pluginWriter.addPlugin(srcFile, srcFile.getName()); assertFalse(response.isSuccess()); assertTrue(response.errors().containsKey(HttpStatus.SC_INTERNAL_SERVER_ERROR)); }
private static TransactionToApply newTransactionThatFailsWith( Exception error ) throws IOException { TransactionRepresentation transaction = mock( TransactionRepresentation.class ); when( transaction.additionalHeader() ).thenReturn( new byte[0] ); // allow to build validated index updates but fail on actual tx application doThrow( error ).when( transaction ).accept( any() ); long txId = ThreadLocalRandom.current().nextLong( 0, 1000 ); TransactionToApply txToApply = new TransactionToApply( transaction ); FakeCommitment commitment = new FakeCommitment( txId, mock( TransactionIdStore.class ) ); commitment.setHasExplicitIndexChanges( false ); txToApply.commitment( commitment, txId ); return txToApply; }
@Test void shouldWrapIOExceptionsCarefullyBecauseCriticalInformationIsOftenEncodedInTheirNameButMissingFromTheirMessage() throws IOException, IncorrectFormat { doThrow( new FileSystemException( "the-message" ) ).when( loader ).load( any(), any(), any() ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( null ) ); assertEquals( "unable to load database: FileSystemException: the-message", commandFailed.getMessage() ); }
@Test public void closeAllAndRethrowException() throws Exception { doThrow( new IOException( "Faulty closable" ) ).when( faultyClosable ).close(); expectedException.expect( IOException.class ); expectedException.expectMessage( "Exception closing multiple resources" ); expectedException.expect( new NestedThrowableMatcher( IOException.class ) ); IOUtils.closeAll( goodClosable1, faultyClosable, goodClosable2 ); }
@Test void shouldMoveToFailedOnRun_fail() throws Throwable { BoltStateMachineV3 machine = getBoltStateMachineInTxReadyState(); // When BoltResponseHandler handler = mock( BoltResponseHandler.class ); doThrow( new RuntimeException( "Error!" ) ).when( handler ).onRecords( any(), anyBoolean() ); machine.process( new RunMessage( "A cypher query" ), handler ); // Then assertThat( machine.state(), instanceOf( FailedState.class ) ); }
@Test public void shouldContinueWithBuildingPluginInfoIfPluginSettingsIsNotProvidedByPlugin() { GoPluginDescriptor descriptor = new GoPluginDescriptor("plugin1", null, null, null, null, false); doThrow(new RuntimeException("foo")).when(extension).getPluginSettingsConfiguration("plugin1"); ArtifactPluginInfo artifactPluginInfo = new ArtifactPluginInfoBuilder(extension).pluginInfoFor(descriptor); assertThat(artifactPluginInfo.getDescriptor(), is(descriptor)); assertThat(artifactPluginInfo.getExtensionName(), is(PluginConstants.ARTIFACT_EXTENSION)); } }