Java tutorial
/******************************************************************************* * Educational Online Test Delivery System * Copyright (c) 2013 American Institutes for Research * * Distributed under the AIR Open Source License, Version 1.0 * See accompanying file AIR-License-1_0.txt or at * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf ******************************************************************************/ package org.opentestsystem.shared.common.logging; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.core.StringStartsWith.startsWith; import static org.junit.Assert.assertThat; import java.io.IOException; import java.io.OutputStream; import java.io.StringWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; import org.apache.commons.io.output.WriterOutputStream; import org.opentestsystem.shared.mna.client.logging.MonitoringAlertingAppender; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/mna-test-context.xml" }) public class TestMonitoringAlertingAppender implements ApplicationContextAware { private static MockUrlStreamHandlerFactory urlStreamHandlerFactory = new TestMonitoringAlertingAppender.MockUrlStreamHandlerFactory(); private static final Logger LOGGER = LoggerFactory.getLogger(TestMonitoringAlertingAppender.class); private static String test1Json = "{\"severity\":\"DEBUG\",\"server\":\"test-server1\",\"node\":\"test-node1\",\"component\":\"junit:test\"," + "\"message\":\"this is a test message\",\"referenceNumber\":null,\"stackTrace\":null}"; private static String test2Json = "{\"severity\":\"ERROR\",\"server\":\"test-server1\",\"node\":\"test-node1\",\"component\":\"junit:test\",\"message\":\"this is another test message\",\"referenceNumber\":null,\"stackTrace\":\"java.lang.RuntimeException: fake runtime exception"; @BeforeClass public static void initStuff() { URL.setURLStreamHandlerFactory(urlStreamHandlerFactory); } @Test @Ignore public void writeLog() { LOGGER.debug("this is a test message"); String dataWritten = ((MockHttpUrlConnection) ((MockUrlStreamHandler) urlStreamHandlerFactory .getCurrentStreamHandler()).getCurrentConnection()).getWrittenString(); // assert things here assertThat(dataWritten, is(test1Json)); } @Test @Ignore public void writeLogWithException() { LOGGER.error("this is another test message", new RuntimeException("fake runtime exception")); // NOPMD String dataWritten = ((MockHttpUrlConnection) ((MockUrlStreamHandler) urlStreamHandlerFactory .getCurrentStreamHandler()).getCurrentConnection()).getWrittenString(); // assert things here assertThat(dataWritten, startsWith(test2Json)); } public static class MockUrlStreamHandlerFactory implements URLStreamHandlerFactory { private URLStreamHandler currentStreamHandler; @Override public URLStreamHandler createURLStreamHandler(final String arg0) { currentStreamHandler = new MockUrlStreamHandler(); return currentStreamHandler; } public URLStreamHandler getCurrentStreamHandler() { return currentStreamHandler; } } public static class MockUrlStreamHandler extends URLStreamHandler { private URLConnection currentConnection; @Override protected URLConnection openConnection(final URL url) throws IOException { currentConnection = new MockHttpUrlConnection(url); return currentConnection; } public URLConnection getCurrentConnection() { return currentConnection; } } public static class MockHttpUrlConnection extends HttpURLConnection { private final StringWriter swriter = new StringWriter(); protected MockHttpUrlConnection(final URL arg0) { super(arg0); } @Override public void disconnect() { // no impl } @Override public boolean usingProxy() { return false; } @Override public void connect() throws IOException { // no impl } @Override public OutputStream getOutputStream() { WriterOutputStream wos = new WriterOutputStream(swriter); return wos; } public String getWrittenString() { return swriter.toString(); } @Override public int getResponseCode() throws IOException { return 200; } } @Override public void setApplicationContext(final ApplicationContext inApplicationContext) throws BeansException { MonitoringAlertingAppender.autowireAppender(inApplicationContext); } }