private static void checkThreadPoolName(Thread thread, int threadId) { assertThat(thread.getName()).matches("^pool-\\d+-thread-" + threadId + "$"); }
public void testToString_misbehaving() throws Exception { assertThat( new AbstractFuture<Object>() { @Override public String pendingToString() { throw new RuntimeException("I'm a misbehaving implementation"); } }.toString()) .matches( "[^\\[]+\\[status=PENDING, info=\\[Exception thrown from implementation: " + "class java.lang.RuntimeException\\]\\]"); }
public void testToString_failed() { assertThat(Futures.immediateFailedFuture(new RuntimeException("foo")).toString()) .matches("[^\\[]+\\[status=FAILURE, cause=\\[java.lang.RuntimeException: foo\\]\\]"); }
public void testToString_cancelled() throws Exception { assertThat(Futures.immediateCancelledFuture().toString()) .matches("[^\\[]+\\[status=CANCELLED\\]"); }
@GwtIncompatible // getStackTraceAsString(Throwable) public void testGetStackTraceAsString() { class StackTraceException extends Exception { StackTraceException(String message) { super(message); } } StackTraceException e = new StackTraceException("my message"); String firstLine = quote(e.getClass().getName() + ": " + e.getMessage()); String secondLine = "\\s*at " + ThrowablesTest.class.getName() + "\\..*"; String moreLines = "(?:.*\n?)*"; String expected = firstLine + "\n" + secondLine + "\n" + moreLines; assertThat(getStackTraceAsString(e)).matches(expected); }
public void testLenientFormat_badArgumentToString_gwtFriendly() { assertThat(Strings.lenientFormat("boiler %s plate", new ThrowsOnToString())) .matches( "boiler <.*> plate"); }
@GwtIncompatible // Doubles.tryParse private static void checkTryParse(double expected, String input) { assertEquals(Double.valueOf(expected), Doubles.tryParse(input)); assertThat(input) .matches( Pattern.compile( Doubles.FLOATING_POINT_PATTERN.pattern(), Doubles.FLOATING_POINT_PATTERN.flags())); }
public void testToString_completed() throws Exception { AbstractFuture<Object> testFuture2 = new AbstractFuture<Object>() { @Override public String pendingToString() { return "cause=[Someday...]"; } }; AbstractFuture<Object> testFuture3 = new AbstractFuture<Object>() {}; testFuture3.setFuture(testFuture2); assertThat(testFuture3.toString()) .matches( "[^\\[]+\\[status=PENDING, info=\\[setFuture=" + "\\[[^\\[]+\\[status=PENDING, info=\\[cause=\\[Someday...\\]\\]\\]\\]\\]\\]"); testFuture2.set("result string"); assertThat(testFuture3.toString()) .matches("[^\\[]+\\[status=SUCCESS, result=\\[result string\\]\\]"); }
@GwtIncompatible // GWT reflection includes less data public void testLenientFormat_badArgumentToString() { assertThat(Strings.lenientFormat("boiler %s plate", new ThrowsOnToString())) .matches( "boiler <com\\.google\\.common\\.base\\.StringsTest\\$ThrowsOnToString@[0-9a-f]+ " + "threw java\\.lang\\.UnsupportedOperationException> plate"); }
public void testToString_notDone() throws Exception { AbstractFuture<Object> testFuture = new AbstractFuture<Object>() { @Override public String pendingToString() { return "cause=[Because this test isn't done]"; } }; assertThat(testFuture.toString()) .matches( "[^\\[]+\\[status=PENDING, info=\\[cause=\\[Because this test isn't done\\]\\]\\]"); try { testFuture.get(1, TimeUnit.NANOSECONDS); fail(); } catch (TimeoutException e) { assertThat(e.getMessage()).contains("1 nanoseconds"); assertThat(e.getMessage()).contains("Because this test isn't done"); } }
.that(thread.exception) .hasMessageThat() .matches( "Waited 2 seconds \\(plus " + (longWait ? "3" : "1")
@Test public void checkFileExistsAndReadable_nonExistent_fail() throws Exception { Path nonExistent = Paths.get("non-existent"); IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> checkFileExistsAndReadable(nonExistent)); assertThat(exception).hasMessageThat().matches("File '.*' was not found."); }
@Test public void checkDirectoryExists_nonExistent_fail() { Path nonExistent = Paths.get("non-existent"); IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> checkDirectoryExists(nonExistent)); assertThat(exception).hasMessageThat().matches("Directory '.*' was not found."); }
@Test public void checkDirectoryExists_notADirectory_fail() throws Exception { Path notDirectory = tmp.newFile().toPath(); IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> checkDirectoryExists(notDirectory)); assertThat(exception).hasMessageThat().matches("'.*' is not a directory."); }
@Test public void checkFileDoesNotExist_existing_fail() throws Exception { Path validFile = tmp.newFile().toPath(); IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> checkFileDoesNotExist(validFile)); assertThat(exception).hasMessageThat().matches("File '.*' already exists."); }
@Test public void checkFileExistsAndReadable_notAFile_fail() throws Exception { Path directory = tmp.newFolder().toPath(); IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> checkFileExistsAndReadable(directory)); assertThat(exception).hasMessageThat().matches("File '.*' is a directory."); }
@Test public void checkFileHasExtension_failure() throws Exception { Path xmlFile = tmp.newFile("AndroidManifest.xml").toPath(); IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, () -> checkFileHasExtension("Module", xmlFile, ".zip")); assertThat(exception) .hasMessageThat() .matches("Module 'AndroidManifest.xml' is expected to have '.zip' extension."); }
@Test public void missingBundleFile_throws() throws Exception { ParsedFlags flags = new FlagParser().parse("--bundle=doesnt-exist"); ValidateBundleCommand command = ValidateBundleCommand.fromFlags(flags); IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> command.execute()); assertThat(exception.getMessage()).matches("File '.*' was not found."); }
@Test public void dumpInvalidTarget() { ValidationException exception = assertThrows( ValidationException.class, () -> DumpCommand.fromFlags( new FlagParser().parse("dump", "blah", "--bundle=" + bundlePath))); assertThat(exception) .hasMessageThat() .matches("Unrecognized dump target: 'blah'. Accepted values are: .*"); }
@Test public void testExtractFromDirectoryNoTableOfContents_throws() throws Exception { Files.createFile(tmpDir.resolve("base-master.apk")); Exception e = assertThrows( IllegalArgumentException.class, () -> ExtractApksCommand.builder() .setApksArchivePath(tmpDir) .setDeviceSpec(DeviceSpec.getDefaultInstance()) .build() .execute()); assertThat(e).hasMessageThat().matches("File '.*toc.pb' was not found."); }