@Override public IOException getCause() { return (IOException)super.getCause(); } }
@Test public void canonicalPath_fails_to_get_canonical_path() throws Exception { File file = mock(File.class); when(file.getCanonicalPath()).thenThrow(new IOException()); try { PathUtils.canonicalPath(file); fail(); } catch (IllegalStateException e) { assertThat(e.getCause()).isInstanceOf(IOException.class); } }
@Test public void non_existing_file_should_throw_exception() { try { createInputFileWithMetadata(Paths.get(""), "non_existing"); Assert.fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).endsWith("Unable to read file " + Paths.get("").resolve("non_existing").toAbsolutePath()); assertThat(e.getCause()).isInstanceOf(IllegalStateException.class); } }
private void assertBogusActiveTestGroupBehavior(String testGroups) { // Should result in something similar to the following: // // java.lang.IllegalStateException: Failed to parse 'testGroups' system property: // Unable to find test group 'bogus' when parsing testGroups value: 'all-bogus'. // Available groups include: [LONG_RUNNING,PERFORMANCE,JMXMP,CI] setTestGroups(testGroups); try { Assume.group(JMXMP); fail("assumption should have failed"); } catch (IllegalStateException ex) { assertThat(ex.getMessage(), startsWith("Failed to parse '" + TEST_GROUPS_SYSTEM_PROPERTY + "' system property: ")); assertThat(ex.getCause(), instanceOf(IllegalArgumentException.class)); assertThat(ex.getCause().getMessage(), equalTo("Unable to find test group 'bogus' when parsing testGroups value: '" + testGroups + "'. Available groups include: [LONG_RUNNING,PERFORMANCE,JMXMP,CI]")); } }
@Test public void illegalArgumentException() { this.resolvers.add(stubResolver(1)); Method method = ResolvableMethod.on(TestController.class).mockCall(o -> o.singleArg(null)).method(); Mono<HandlerResult> mono = invoke(new TestController(), method); try { mono.block(); fail("Expected IllegalStateException"); } catch (IllegalStateException ex) { assertNotNull("Exception not wrapped", ex.getCause()); assertTrue(ex.getCause() instanceof IllegalArgumentException); assertTrue(ex.getMessage().contains("Controller [")); assertTrue(ex.getMessage().contains("Method [")); assertTrue(ex.getMessage().contains("with argument values:")); assertTrue(ex.getMessage().contains("[0] [type=java.lang.Integer] [value=1]")); } }
@Test public void illegalArgumentException() throws Exception { this.composite.addResolver(new StubArgumentResolver(Integer.class, "__not_an_int__")); this.composite.addResolver(new StubArgumentResolver("value")); try { getInvocable(Integer.class, String.class).invokeForRequest(request, null); fail("Expected exception"); } catch (IllegalStateException ex) { assertNotNull("Exception not wrapped", ex.getCause()); assertTrue(ex.getCause() instanceof IllegalArgumentException); assertTrue(ex.getMessage().contains("Controller [")); assertTrue(ex.getMessage().contains("Method [")); assertTrue(ex.getMessage().contains("with argument values:")); assertTrue(ex.getMessage().contains("[0] [type=java.lang.String] [value=__not_an_int__]")); assertTrue(ex.getMessage().contains("[1] [type=java.lang.String] [value=value")); } }
@Test public void close_throws_exception_on_error() { Closeable closeable = new Closeable() { @Override public void close() throws IOException { throw new IOException("expected"); } }; try { System2.INSTANCE.close(closeable); fail(); } catch (IllegalStateException e) { assertThat(e.getCause().getMessage()).isEqualTo("expected"); } }
@Test public void failOnError() throws Exception { HttpsTrust.Ssl context = mock(HttpsTrust.Ssl.class); KeyManagementException cause = new KeyManagementException("foo"); when(context.newFactory(any(TrustManager.class))).thenThrow(cause); try { new HttpsTrust(context); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).isEqualTo("Fail to build SSL factory"); assertThat(e.getCause()).isSameAs(cause); } }
@SuppressWarnings("rawtypes") @Test public void testInvokeConstructorThatFails() throws NoSuchMethodException { final Constructor<Dictionary> constructor = Dictionary.class.getConstructor(); try { CommonUtil.invokeConstructor(constructor); fail("IllegalStateException is expected"); } catch (IllegalStateException expected) { assertSame("Invalid exception cause", InstantiationException.class, expected.getCause().getClass()); } }
public void testFailOnExceptionFromStartUp() { TestService service = new TestService(); service.startUpException = new Exception(); try { service.startAsync().awaitRunning(); fail(); } catch (IllegalStateException e) { assertEquals(service.startUpException, e.getCause()); } assertEquals(0, service.numberOfTimesRunCalled.get()); assertEquals(Service.State.FAILED, service.state()); }
@Test public void illegalArgumentException() throws Exception { this.resolvers.addResolver(new StubArgumentResolver(Integer.class, "__not_an_int__")); this.resolvers.addResolver(new StubArgumentResolver("value")); try { Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method(); invoke(new Handler(), method); fail("Expected exception"); } catch (IllegalStateException ex) { assertNotNull("Exception not wrapped", ex.getCause()); assertTrue(ex.getCause() instanceof IllegalArgumentException); assertTrue(ex.getMessage().contains("Endpoint [")); assertTrue(ex.getMessage().contains("Method [")); assertTrue(ex.getMessage().contains("with argument values:")); assertTrue(ex.getMessage().contains("[0] [type=java.lang.String] [value=__not_an_int__]")); assertTrue(ex.getMessage().contains("[1] [type=java.lang.String] [value=value")); } }
public void testFailOnExceptionFromShutDown() throws Exception { TestService service = new TestService(); service.shutDownException = new Exception(); service.startAsync().awaitRunning(); service.runFirstBarrier.await(); service.stopAsync(); service.runSecondBarrier.await(); try { service.awaitTerminated(); fail(); } catch (IllegalStateException e) { assertEquals(service.shutDownException, e.getCause()); } assertEquals(Service.State.FAILED, service.state()); }
@Test public void testGetNonExistentConstructor() { try { CommonUtil.getConstructor(Math.class); fail("IllegalStateException is expected"); } catch (IllegalStateException expected) { assertSame("Invalid exception cause", NoSuchMethodException.class, expected.getCause().getClass()); } }
@Test public void testInvalidClassName() throws Exception { String bogusClassName = "foobar"; DriverManagerDataSource ds = new DriverManagerDataSource(); try { ds.setDriverClassName(bogusClassName); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // OK assertTrue(ex.getCause() instanceof ClassNotFoundException); } }
@Test public void fail_to_read_row() throws Exception { dbTester.prepareDbUnit(getClass(), "feed.xml"); try (Connection connection = dbTester.openConnection()) { PreparedStatement stmt = connection.prepareStatement("select * from issues order by id"); FailIterator iterator = new FailIterator(stmt); assertThat(iterator.hasNext()).isTrue(); try { iterator.next(); fail(); } catch (IllegalStateException e) { assertThat(e.getCause()).isInstanceOf(SQLException.class); } iterator.close(); } }
@Test @Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"}) public void runAndExpectExceptionAndCauseTypes_wrongExceptionTypeFails(Execution execution) { concurrencyRule.add(callWithExceptionAndCause) .expectExceptionType(NullPointerException.class) .expectExceptionCauseType(expectedExceptionWithCause.getCause().getClass()); assertThatThrownBy(() -> execution.execute(concurrencyRule)).isInstanceOf(AssertionError.class); }
@Test @Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"}) public void runAndExpectExceptionAndCauseTypes(Execution execution) { concurrencyRule.add(callWithExceptionAndCause) .expectExceptionType(expectedExceptionWithCause.getClass()) .expectExceptionCauseType(expectedExceptionWithCause.getCause().getClass()); execution.execute(concurrencyRule); }
@Test @Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"}) public void runAndExpectExceptionCauseType(Execution execution) { concurrencyRule.add(callWithExceptionAndCause) .expectExceptionCauseType(expectedExceptionWithCause.getCause().getClass()); execution.execute(concurrencyRule); }