private Matcher<String> messageMatcher( @Nonnull String message ) { return allOf( startsWith( "[AsyncLog @ " ), endsWith( "] " + message ) ); } }
@Test void shouldLogWithAbbreviatedClassNameAsContext() { // Given StringWriter writer = new StringWriter(); FormattedLogProvider logProvider = newFormattedLogProvider( writer ); FormattedLog log = logProvider.getLog( StringWriter.class ); // When log.info( "Terminator 2" ); // Then assertThat( writer.toString(), endsWith( format( "INFO [j.i.StringWriter] Terminator 2%n" ) ) ); }
@Test public void testLogToFile() throws Exception { final Logger logger = this.ctx.getLogger(FILE_LOGGER_NAME); final long random = this.random.nextLong(); logger.debug("This is test message number {}", random); int count = 0; String line = Strings.EMPTY; try (final BufferedReader in = new BufferedReader(new FileReader(this.logFileName))) { while (in.ready()) { ++count; line = in.readLine(); } } assertThat(count, is(equalTo(1))); assertThat(line, endsWith(Long.toString(random))); }
/** * @since 4.1 */ @Test public void loadContextWithLocationsAndConfigurationClasses() throws Exception { expectedException.expect(IllegalStateException.class); expectedException.expectMessage(startsWith("Neither")); expectedException.expectMessage(endsWith("declare either 'locations' or 'classes' but not both.")); MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), new String[] {"test.xml"}, new Class<?>[] {getClass()}, EMPTY_STRING_ARRAY, loader); loader.loadContext(mergedConfig); }
@Test public void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception { int port = SocketUtils.findAvailableUdpPort(); DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost")); try { exception.expect(IllegalStateException.class); exception.expectMessage(startsWith("Could not find an available UDP port")); exception.expectMessage(endsWith("after 1 attempts")); // will only look for the exact port SocketUtils.findAvailableUdpPort(port, port); } finally { socket.close(); } }
@Test void disabledWithDefaultReason() { assertResult(condition.evaluateExecutionCondition(buildExtensionContext("defaultReason")), true, endsWith("defaultReason() is disabled because @DisabledIf(\"#{1 + 1 eq 2}\") evaluated to true")); }
@Test void missingDisabledIf() { assertResult(condition.evaluateExecutionCondition(buildExtensionContext("missingDisabledIf")), false, endsWith("missingDisabledIf() is enabled since @DisabledIf is not present")); }
@Test void notDisabledWithDefaultReason() { assertResult(condition.evaluateExecutionCondition(buildExtensionContext("neverDisabledWithDefaultReason")), false, endsWith( "neverDisabledWithDefaultReason() is enabled because @DisabledIf(\"false\") did not evaluate to true")); }
@Test public void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception { int port = SocketUtils.findAvailableTcpPort(); ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost")); try { exception.expect(IllegalStateException.class); exception.expectMessage(startsWith("Could not find an available TCP port")); exception.expectMessage(endsWith("after 1 attempts")); // will only look for the exact port SocketUtils.findAvailableTcpPort(port, port); } finally { socket.close(); } }
@Test public void testLogIoException() throws Exception { //I can't put wrong file here. Checkstyle fails before check started. //I saw some usage of file or handling of wrong file in Checker, or somewhere //in checks running part. So I had to do it with reflection to improve coverage. final TranslationCheck check = new TranslationCheck(); final DefaultConfiguration checkConfig = createModuleConfig(TranslationCheck.class); final MessageDispatcher dispatcher = mock(MessageDispatcher.class); check.configure(checkConfig); check.setMessageDispatcher(dispatcher); final Method logException = check.getClass().getDeclaredMethod("logException", Exception.class, File.class); logException.setAccessible(true); final File file = new File(""); logException.invoke(check, new IOException("test exception"), file); Mockito.verify(dispatcher, times(1)).fireErrors(any(String.class), captor.capture()); final String actual = captor.getValue().first().getMessage(); assertThat("Invalid message: " + actual, actual, endsWith("test exception")); }
@Test public void maxAge() { Duration maxAge = Duration.ofDays(365); String expires = HttpHeaders.formatDate(System.currentTimeMillis() + maxAge.toMillis()); expires = expires.substring(0, expires.indexOf(":") + 1); assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge).build().toString(), allOf( startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires), endsWith(" GMT"))); assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge.getSeconds()).build().toString(), allOf( startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires), endsWith(" GMT"))); }
for (final String line : lines) { assertThat(line, startsWith("{\"thread\":")); assertThat(line, endsWith("\"threadPriority\":5}"));
for (final String line : lines) { assertThat(line, startsWith("{\"version\":\"1.1\",\"host\":\"myself\",\"timestamp\":")); assertThat(line, endsWith("\"}"));
@Test public void buildResponseHeaders() throws Exception { this.response.addHeader("Content-Type", "text/html"); this.response.addHeader("X-Test", "value"); Cookie cookie = new Cookie("cookieA", "valueA"); cookie.setDomain("domain"); cookie.setPath("/path"); cookie.setMaxAge(1800); cookie.setSecure(true); cookie.setHttpOnly(true); this.response.addCookie(cookie); WebResponse webResponse = this.responseBuilder.build(); List<NameValuePair> responseHeaders = webResponse.getResponseHeaders(); assertThat(responseHeaders.size(), equalTo(3)); NameValuePair header = responseHeaders.get(0); assertThat(header.getName(), equalTo("Content-Type")); assertThat(header.getValue(), equalTo("text/html")); header = responseHeaders.get(1); assertThat(header.getName(), equalTo("X-Test")); assertThat(header.getValue(), equalTo("value")); header = responseHeaders.get(2); assertThat(header.getName(), equalTo("Set-Cookie")); assertThat(header.getValue(), startsWith("cookieA=valueA; Path=/path; Domain=domain; Max-Age=1800; Expires=")); assertThat(header.getValue(), endsWith("; Secure; HttpOnly")); }
@Test public void shouldBeAbleToReturnPaths() throws Exception { long n = functionalTestHelper.getGraphDbHelper() .createNode(); long r = functionalTestHelper.getGraphDbHelper() .getFirstNode(); functionalTestHelper.getGraphDbHelper() .createRelationship( "friend", n, r ); String methodUri = getPluginMethodUri( functionalTestHelper.nodeUri( n ), "pathToReference" ); Map<String, Object> maps = PluginFunctionalTestHelper.makePostMap( methodUri ); assertThat( (String) maps.get( "start" ), endsWith( Long.toString( r ) ) ); assertThat( (String) maps.get( "end" ), endsWith( Long.toString( n ) ) ); }
@Test public void flowTracingVoidReturn() { final EntryMessage msg = logger.traceEntry("doFoo(a={}, b={})", 1, 2); logger.traceExit(msg); assertEquals(2, results.size()); assertThat("Incorrect Entry", results.get(0), startsWith("ENTER[ FLOW ] TRACE Enter")); assertThat("Missing entry data", results.get(0), containsString("doFoo(a=1, b=2)")); assertThat("Incorrect Exit", results.get(1), startsWith("EXIT[ FLOW ] TRACE Exit")); assertThat("Missing exit data", results.get(1), endsWith("doFoo(a=1, b=2)")); }
@Test public void testWrongUserSpecifiedLanguageCodes() { final TranslationCheck check = new TranslationCheck(); try { check.setRequiredTranslations("11"); fail("IllegalArgumentException is expected. Specified language code is incorrect."); } catch (IllegalArgumentException ex) { final String exceptionMessage = ex.getMessage(); assertThat("Error message is unexpected", exceptionMessage, containsString("11")); assertThat("Error message is unexpected", exceptionMessage, endsWith("[TranslationCheck]")); } }
allOf(startsWith("<MyBean"), endsWith("><string>foo</string></MyBean>")));
assertEquals(1, remoteWALStatus.length); Path remoteWAL = remoteWALStatus[0].getPath(); assertThat(remoteWAL.getName(), endsWith(ReplicationUtils.SYNC_WAL_SUFFIX)); writeAndVerifyReplication(UTIL1, UTIL2, 0, 100); assertEquals(1, remoteWALStatus.length); remoteWAL = remoteWALStatus[0].getPath(); assertThat(remoteWAL.getName(), endsWith(ReplicationUtils.SYNC_WAL_SUFFIX));
@Test public void entityStream() { FluxExchangeResult<Person> result = this.client.get() .accept(TEXT_EVENT_STREAM) .exchange() .expectStatus().isOk() .expectHeader().contentTypeCompatibleWith(TEXT_EVENT_STREAM) .returnResult(Person.class); StepVerifier.create(result.getResponseBody()) .expectNext(new Person("N0"), new Person("N1"), new Person("N2")) .expectNextCount(4) .consumeNextWith(person -> assertThat(person.getName(), endsWith("7"))) .thenCancel() .verify(); }