/** * Returns true if {@code e} is due to a firmware bug fixed after Android 4.2.2. * https://code.google.com/p/android/issues/detail?id=54072 */ public static boolean isAndroidGetsocknameError(AssertionError e) { return e.getCause() != null && e.getMessage() != null && e.getMessage().contains("getsockname failed"); }
/** * Returns true if {@code e} is due to a firmware bug fixed after Android 4.2.2. * https://code.google.com/p/android/issues/detail?id=54072 */ public static boolean isAndroidGetsocknameError(AssertionError e) { return e.getCause() != null && e.getMessage() != null && e.getMessage().contains("getsockname failed"); }
/** * Returns true if {@code e} is due to a firmware bug fixed after Android 4.2.2. * https://code.google.com/p/android/issues/detail?id=54072 */ static boolean isAndroidGetsocknameError(AssertionError e) { return e.getCause() != null && e.getMessage() != null && e.getMessage().contains("getsockname failed"); } }
/** * Returns true if {@code e} is due to a firmware bug fixed after Android 4.2.2. * https://code.google.com/p/android/issues/detail?id=54072 */ public static boolean isAndroidGetsocknameError(AssertionError e) { return e.getCause() != null && e.getMessage() != null && e.getMessage().contains("getsockname failed"); }
@Override public void fail(AssertionError failure) { ThrowableAssumptionViolatedException assumptionViolated = new ThrowableAssumptionViolatedException(failure.getMessage(), failure.getCause()); assumptionViolated.setStackTrace(failure.getStackTrace()); throw assumptionViolated; } };
@Test public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() { NullPointerException npe = new NullPointerException("inner-message"); try { expectThrows(IOException.class, throwingRunnable(npe)); } catch (AssertionError ex) { assertSame(npe, ex.getCause()); assertEquals("inner-message", ex.getCause().getMessage()); return; } fail(); }
@Test public void exists() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); HeaderAssertions assertions = headerAssertions(headers); // Success assertions.exists("Content-Type"); try { assertions.exists("Framework"); fail("Header should not exist"); } catch (AssertionError error) { Throwable cause = error.getCause(); assertNotNull(cause); assertEquals("Response header 'Framework' does not exist", cause.getMessage()); } }
@Test public void doesNotExist() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); HeaderAssertions assertions = headerAssertions(headers); // Success assertions.doesNotExist("Framework"); try { assertions.doesNotExist("Content-Type"); fail("Existing header expected"); } catch (AssertionError error) { Throwable cause = error.getCause(); assertNotNull(cause); assertEquals("Response header 'Content-Type' exists with " + "value=[application/json;charset=UTF-8]", cause.getMessage()); } }
@Test public void valueMatches() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); HeaderAssertions assertions = headerAssertions(headers); // Success assertions.valueMatches("Content-Type", ".*UTF-8.*"); try { assertions.valueMatches("Content-Type", ".*ISO-8859-1.*"); fail("Wrong pattern expected"); } catch (AssertionError error) { Throwable cause = error.getCause(); assertNotNull(cause); assertEquals("Response header 'Content-Type'=[application/json;charset=UTF-8] " + "does not match [.*ISO-8859-1.*]", cause.getMessage()); } }
@Test public void contentTypeCompatibleWith() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_XML); HeaderAssertions assertions = headerAssertions(headers); // Success assertions.contentTypeCompatibleWith(MediaType.parseMediaType("application/*")); try { assertions.contentTypeCompatibleWith(MediaType.TEXT_XML); fail("MediaTypes not compatible expected"); } catch (AssertionError error) { Throwable cause = error.getCause(); assertNotNull(cause); assertEquals("Response header 'Content-Type'=[application/xml] " + "is not compatible with [text/xml]", cause.getMessage()); } }
@Test public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() { NullPointerException npe = new NullPointerException(); try { expectThrows(IOException.class, throwingRunnable(npe)); } catch (AssertionError error) { assertEquals("Expected IOException to be thrown, but NullPointerException was thrown", error.getMessage()); assertSame(npe, error.getCause()); return; } fail(); }
@Test public void testFailingCacheService() throws InitializationException, IOException { service.setFailOnCalls(true); runner.setProperty(Wait.RELEASE_SIGNAL_IDENTIFIER, "${releaseSignalAttribute}"); final Map<String, String> props = new HashMap<>(); props.put("releaseSignalAttribute", "2"); runner.enqueue(new byte[]{}, props); try { runner.run(); fail("Expect the processor to receive an IO exception from the cache service and throws ProcessException."); } catch (final AssertionError e) { assertTrue(e.getCause() instanceof ProcessException); assertTrue(e.getCause().getCause() instanceof IOException); } finally { service.setFailOnCalls(false); } }
@Test public void testNoTerminalEventBut2Errors() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); ts.onSubscribe(EmptySubscription.INSTANCE); ts.onError(new TestException()); ts.onError(new TestException()); try { ts.assertNotTerminated(); throw new RuntimeException("Failed to report there were terminal event(s)!"); } catch (AssertionError ex) { // expected Throwable e = ex.getCause(); if (!(e instanceof CompositeException)) { fail("Multiple Error present but the reported error doesn't have a composite cause!"); } CompositeException ce = (CompositeException)e; assertEquals(2, ce.size()); } }
@Test public void testFailingCacheService() throws InitializationException, IOException { service.setFailOnCalls(true); runner.setProperty(Notify.RELEASE_SIGNAL_IDENTIFIER, "${releaseSignalAttribute}"); final Map<String, String> props = new HashMap<>(); props.put("releaseSignalAttribute", "2"); runner.enqueue(new byte[] {}, props); try { runner.run(); fail("Processor should throw RuntimeException in case it receives an IO exception from the cache service and yield for a while."); } catch (final AssertionError e) { assertTrue(e.getCause() instanceof RuntimeException); } service.setFailOnCalls(false); }
@Test public void testMultipleErrors2() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); ts.onSubscribe(EmptySubscription.INSTANCE); ts.onError(new TestException()); ts.onError(new TestException()); try { ts.assertError(TestException.class); } catch (AssertionError ex) { Throwable e = ex.getCause(); if (!(e instanceof CompositeException)) { fail("Multiple Error present but the reported error doesn't have a composite cause!"); } CompositeException ce = (CompositeException)e; assertEquals(2, ce.size()); // expected return; } fail("Multiple Error present but no assertion error!"); }
@Test public void testFailInMiddleWithBadStatementRollbackOnFailure() throws InitializationException, ProcessException, SQLException, IOException { final TestRunner runner = TestRunners.newTestRunner(PutSQL.class); runner.addControllerService("dbcp", service); runner.enableControllerService(service); runner.setProperty(PutSQL.OBTAIN_GENERATED_KEYS, "false"); runner.setProperty(PutSQL.CONNECTION_POOL, "dbcp"); runner.setProperty(RollbackOnFailure.ROLLBACK_ON_FAILURE, "true"); runner.enqueue("INSERT INTO PERSONS_AI (NAME, CODE) VALUES ('Mark', 84)".getBytes()); runner.enqueue("INSERT INTO PERSONS_AI".getBytes()); // intentionally wrong syntax runner.enqueue("INSERT INTO PERSONS_AI (NAME, CODE) VALUES ('Tom', 3)".getBytes()); runner.enqueue("INSERT INTO PERSONS_AI (NAME, CODE) VALUES ('Harry', 44)".getBytes()); try { runner.run(); fail("ProcessException should be thrown"); } catch (AssertionError e) { assertTrue(e.getCause() instanceof ProcessException); runner.assertTransferCount(PutSQL.REL_FAILURE, 0); runner.assertTransferCount(PutSQL.REL_SUCCESS, 0); } }
@Test public void testMultipleErrors3() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); ts.onSubscribe(EmptySubscription.INSTANCE); ts.onError(new TestException()); ts.onError(new TestException()); try { ts.assertError(new TestException()); } catch (AssertionError ex) { Throwable e = ex.getCause(); if (!(e instanceof CompositeException)) { fail("Multiple Error present but the reported error doesn't have a composite cause!"); } CompositeException ce = (CompositeException)e; assertEquals(2, ce.size()); // expected return; } fail("Multiple Error present but no assertion error!"); }
@Test public void testMultipleErrors4() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); ts.onSubscribe(EmptySubscription.INSTANCE); ts.onError(new TestException()); ts.onError(new TestException()); try { ts.assertError(Functions.<Throwable>alwaysTrue()); } catch (AssertionError ex) { Throwable e = ex.getCause(); if (!(e instanceof CompositeException)) { fail("Multiple Error present but the reported error doesn't have a composite cause!"); } CompositeException ce = (CompositeException)e; assertEquals(2, ce.size()); // expected return; } fail("Multiple Error present but no assertion error!"); }
@Test public void testMultipleErrors() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); ts.onSubscribe(EmptySubscription.INSTANCE); ts.onError(new TestException()); ts.onError(new TestException()); try { ts.assertNoErrors(); } catch (AssertionError ex) { Throwable e = ex.getCause(); if (!(e instanceof CompositeException)) { fail("Multiple Error present but the reported error doesn't have a composite cause!"); } CompositeException ce = (CompositeException)e; if (ce.size() != 2) { ce.printStackTrace(); } assertEquals(2, ce.size()); // expected return; } fail("Multiple Error present but no assertion error!"); }