@Test public void testIsValueNeedsLookup() { assertTrue("with ${ as value", Property.createProperty("", "${").isValueNeedsLookup()); assertTrue("with ${ in value", Property.createProperty("", "blah${blah").isValueNeedsLookup()); assertFalse("empty value", Property.createProperty("", "").isValueNeedsLookup()); assertFalse("without ${ in value", Property.createProperty("", "blahblah").isValueNeedsLookup()); assertFalse("without $ in value", Property.createProperty("", "blahb{sys:lah").isValueNeedsLookup()); assertFalse("without { in value", Property.createProperty("", "blahb$sys:lah").isValueNeedsLookup()); } }
@Test public void testNullValueIsConvertedToEmptyString() { // LOG4J2-1313 <Property name="x" /> support assertEquals("", Property.createProperty("name", null).getValue()); }
@Test public void testH2Properties() throws SQLException { Property[] properties = new Property[] { // @formatter:off Property.createProperty("username", JdbcH2TestHelper.USER_NAME), Property.createProperty("password", JdbcH2TestHelper.PASSWORD), // @formatter:on }; // @formatter:off DriverManagerConnectionSource source = DriverManagerConnectionSource.newBuilder() .setConnectionString(JdbcH2TestHelper.CONNECTION_STRING) .setProperties(properties) .build(); // @formatter:on try (Connection conn = source.getConnection()) { Assert.assertFalse(conn.isClosed()); } }
@Test public void testPropertiesWithSubstitution() { final Property[] all = new Property[] { Property.createProperty("key1", "value1-${sys:user.name}"), Property.createProperty("key2", "value2-${sys:user.name}"), }; final LoggerConfig loggerConfig = createForProperties(all);
@SuppressWarnings({"deprecation"}) @Test public void testPropertiesWithoutSubstitution() { assertNull("null propertiesList", createForProperties(null).getPropertyList()); assertNull("null property Map", createForProperties(null).getProperties()); final Property[] all = new Property[] { Property.createProperty("key1", "value1"), Property.createProperty("key2", "value2"), }; final LoggerConfig loggerConfig = createForProperties(all); final List<Property> list = loggerConfig.getPropertyList(); assertEquals("map and list contents equal", new HashSet<>(list), new HashSet<>(loggerConfig.getPropertyList())); final Object[] actualList = new Object[1]; loggerConfig.setLogEventFactory(new LogEventFactory() { @Override public LogEvent createEvent(final String loggerName, final Marker marker, final String fqcn, final Level level, final Message data, final List<Property> properties, final Throwable t) { actualList[0] = properties; return new Builder().setTimeMillis(System.currentTimeMillis()).build(); } }); loggerConfig.log("name", "fqcn", null, Level.INFO, new SimpleMessage("msg"), null); assertSame("propertiesList passed in as is if no substitutions required", list, actualList[0]); }
@Test public void testAppendCustomHeader() throws Exception { wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")) .willReturn(SUCCESS_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/")) .setHeaders(new Property[] { Property.createProperty("X-Test", "header value"), Property.createProperty("X-Runtime", "${java:runtime}") }) .build(); appender.append(createLogEvent()); wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/")) .withHeader("Host", containing("localhost")) .withHeader("X-Test", equalTo("header value")) .withHeader("X-Runtime", equalTo(JAVA_LOOKUP.getRuntime())) .withHeader("Content-Type", containing("application/json")) .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\""))); }
@Test public void insertConfigurationProperties() { assertThat( new OpenCensusTraceContextDataInjector() .injectContextData( Lists.newArrayList( Property.createProperty("property1", "value1"), Property.createProperty("property2", "value2")), new SortedArrayStringMap()) .toMap()) .containsExactly( "property1", "value1", "property2", "value2", "traceId", "00000000000000000000000000000000", "spanId", "0000000000000000", "traceSampled", "false"); }
props.add(Property.createProperty(prop.getName(), value));