svn commit: r1902 - in logback/trunk/logback-access/src/test/java/ch/qos/logback/access: jetty testUtil

Author: ceki Date: Wed Oct 29 15:12:39 2008 New Revision: 1902 Added: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/testUtil/ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/testUtil/NotifyingListAppender.java Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyFixture.java Log: reduce the probability of JettyBasicTest failing Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java ============================================================================== --- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java (original) +++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java Wed Oct 29 15:12:39 2008 @@ -1,6 +1,7 @@ package ch.qos.logback.access.jetty; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.io.OutputStreamWriter; import java.io.PrintWriter; @@ -13,69 +14,76 @@ import ch.qos.logback.access.spi.AccessEvent; import ch.qos.logback.access.spi.Util; -import ch.qos.logback.core.read.ListAppender; +import ch.qos.logback.access.testUtil.NotifyingListAppender; -public class JettyBasicTest { +public class JettyBasicTest { static RequestLogImpl requestLogImpl; static JettyFixture fixture; - + @BeforeClass static public void startServer() throws Exception { - //System.out.println("*** JettyBasicTest.startServer called"); + // System.out.println("*** JettyBasicTest.startServer called"); requestLogImpl = new RequestLogImpl(); JettyFixture fixture = new JettyFixture(requestLogImpl); fixture.start(); } - + @AfterClass - static public void stopServer() throws Exception { - //System.out.println("*** JettyBasicTest.stopServer called"); - if(fixture != null) { + static public void stopServer() throws Exception { + // System.out.println("*** JettyBasicTest.stopServer called"); + if (fixture != null) { fixture.stop(); } } - + @Test - public void teztGetRequest() throws Exception { - URL url = new URL("http://localhost:"+ JettyFixture.PORT + "/"); + public void getRequest() throws Exception { + URL url = new URL("http://localhost:" + JettyFixture.PORT + "/"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); String result = Util.readToString(connection.getInputStream()); - assertEquals("hello world", result); + assertEquals("hello world", result); - ListAppender appender = (ListAppender) requestLogImpl.getAppender("list"); - appender.list.clear(); + NotifyingListAppender listAppender = (NotifyingListAppender) requestLogImpl + .getAppender("list"); + listAppender.list.clear(); } @Test - public void teztEventGoesToAppenders() throws Exception { - URL url = new URL("http://localhost:"+ JettyFixture.PORT + "/"); + public void eventGoesToAppenders() throws Exception { + URL url = new URL("http://localhost:" + JettyFixture.PORT + "/"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); String result = Util.readToString(connection.getInputStream()); assertEquals("hello world", result); - //Thread.sleep(100); - ListAppender appender = (ListAppender) requestLogImpl.getAppender("list"); - assertTrue(appender.list.size()>0); - AccessEvent event = (AccessEvent) appender.list.get(0); + + NotifyingListAppender listAppender = (NotifyingListAppender) requestLogImpl + .getAppender("list"); + synchronized (listAppender) { + listAppender.wait(100); + } + + assertTrue(listAppender.list.size() > 0); + AccessEvent event = (AccessEvent) listAppender.list.get(0); assertEquals("127.0.0.1", event.getRemoteHost()); assertEquals("localhost", event.getServerName()); - appender.list.clear(); + listAppender.list.clear(); } @Test - public void teztPostContentConverter() throws Exception { - //System.out.println("into test"); - URL url = new URL("http://localhost:"+ JettyFixture.PORT + "/"); + public void postContentConverter() throws Exception { + // System.out.println("into test"); + URL url = new URL("http://localhost:" + JettyFixture.PORT + "/"); String msg = "test message"; - + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - //this line is necessary to make the stream aware of when the message is over. + // this line is necessary to make the stream aware of when the message is + // over. connection.setFixedLengthStreamingMode(msg.getBytes().length); ((HttpURLConnection) connection).setRequestMethod("POST"); connection.setDoOutput(true); @@ -83,7 +91,6 @@ connection.setUseCaches(false); connection.setRequestProperty("Content-Type", "text/plain"); - PrintWriter output = new PrintWriter(new OutputStreamWriter(connection .getOutputStream())); output.print(msg); @@ -92,12 +99,16 @@ // StatusPrinter.print(requestLogImpl.getStatusManager()); - ListAppender listAppender = (ListAppender) requestLogImpl + NotifyingListAppender listAppender = (NotifyingListAppender) requestLogImpl .getAppender("list"); - Thread.sleep(100); + + synchronized (listAppender) { + listAppender.wait(100); + } + @SuppressWarnings("unused") AccessEvent event = (AccessEvent) listAppender.list.get(0); - + // we should test the contents of the requests // assertEquals(msg, event.getRequestContent()); } Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyFixture.java ============================================================================== --- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyFixture.java (original) +++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyFixture.java Wed Oct 29 15:12:39 2008 @@ -20,8 +20,8 @@ import ch.qos.logback.access.PatternLayout; import ch.qos.logback.access.spi.AccessEvent; +import ch.qos.logback.access.testUtil.NotifyingListAppender; import ch.qos.logback.core.ConsoleAppender; -import ch.qos.logback.core.read.ListAppender; public class JettyFixture { RequestLogImpl requestLogImpl; @@ -76,7 +76,7 @@ private void buildContext() { - ListAppender<AccessEvent> appender = new ListAppender<AccessEvent>(); + NotifyingListAppender appender = new NotifyingListAppender(); appender.setContext(requestLogImpl); appender.setName("list"); appender.start(); Added: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/testUtil/NotifyingListAppender.java ============================================================================== --- (empty file) +++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/testUtil/NotifyingListAppender.java Wed Oct 29 15:12:39 2008 @@ -0,0 +1,28 @@ +/** + * Logback: the generic, reliable, fast and flexible logging framework. + * + * Copyright (C) 2000-2008, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ +package ch.qos.logback.access.testUtil; + +import java.util.ArrayList; +import java.util.List; + +import ch.qos.logback.access.spi.AccessEvent; +import ch.qos.logback.core.AppenderBase; + +public class NotifyingListAppender extends AppenderBase<AccessEvent> { + + public List<AccessEvent> list = new ArrayList<AccessEvent>(); + + protected void append(AccessEvent e) { + list.add(e); + synchronized (this) { + this.notify(); + } + } +}
participants (1)
-
noreply.ceki@qos.ch