diff options
Diffstat (limited to 'tests')
9 files changed, 794 insertions, 67 deletions
diff --git a/tests/netx/unit/net/sourceforge/jnlp/DefaultLaunchHandlerTest.java b/tests/netx/unit/net/sourceforge/jnlp/DefaultLaunchHandlerTest.java index e5d1e44..7a68c74 100644 --- a/tests/netx/unit/net/sourceforge/jnlp/DefaultLaunchHandlerTest.java +++ b/tests/netx/unit/net/sourceforge/jnlp/DefaultLaunchHandlerTest.java @@ -42,70 +42,94 @@ import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import net.sourceforge.jnlp.util.logging.OutputController; import org.junit.Test; public class DefaultLaunchHandlerTest { + private static class LocalLogger extends OutputController { + + private static class AccessibleStream extends PrintStream { + + public AccessibleStream(ByteArrayOutputStream out) { + super(out); + } + + public ByteArrayOutputStream getOut() { + return (ByteArrayOutputStream) out; + } + } + + LocalLogger() { + super(new AccessibleStream(new ByteArrayOutputStream()), new AccessibleStream(new ByteArrayOutputStream())); + } + + public String getStream1() { + return ((AccessibleStream) (super.getOut())).getOut().toString(); + } + + public String getStream2() { + return ((AccessibleStream) (super.getErr())).getOut().toString(); + } + } + @Test public void testBasicLaunch() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DefaultLaunchHandler handler = new DefaultLaunchHandler(new PrintStream(baos)); + LocalLogger l = new LocalLogger(); + DefaultLaunchHandler handler = new DefaultLaunchHandler(l); // all no-ops with no output handler.launchInitialized(null); handler.launchStarting(null); handler.launchCompleted(null); - String output = baos.toString(); - assertEquals("", output); + assertEquals("", l.getStream1()); + assertEquals("", l.getStream2()); } @Test public void testLaunchWarning() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DefaultLaunchHandler handler = new DefaultLaunchHandler(new PrintStream(baos)); + LocalLogger l = new LocalLogger(); + DefaultLaunchHandler handler = new DefaultLaunchHandler(l); LaunchException warning = new LaunchException(null, null, "severe", "warning type", "test warning", "this is a test of the warning"); boolean continueLaunch = handler.launchWarning(warning); assertTrue(continueLaunch); - String output = baos.toString(); - assertEquals("netx: warning type: test warning\n", output); + assertEquals("netx: warning type: test warning\n", l.getStream1()); } @Test public void testLaunchError() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DefaultLaunchHandler handler = new DefaultLaunchHandler(new PrintStream(baos)); + LocalLogger l = new LocalLogger(); + DefaultLaunchHandler handler = new DefaultLaunchHandler(l); LaunchException error = new LaunchException(null, null, "severe", "error type", "test error", "this is a test of the error"); handler.launchError(error); - String output = baos.toString(); - assertEquals("netx: error type: test error\n", output); + assertEquals("netx: error type: test error\n", l.getStream1()); } @Test public void testLaunchErrorWithCause() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DefaultLaunchHandler handler = new DefaultLaunchHandler(new PrintStream(baos)); + LocalLogger l = new LocalLogger(); + DefaultLaunchHandler handler = new DefaultLaunchHandler(l); ParseException parse = new ParseException("no information element"); LaunchException error = new LaunchException(null, parse, "severe", "error type", "test error", "this is a test of the error"); handler.launchError(error); - String output = baos.toString(); - assertEquals("netx: error type: test error (no information element)\n", output); + assertEquals("netx: error type: test error (no information element)\n", l.getStream1()); } @Test public void testLaunchErrorWithNestedCause() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DefaultLaunchHandler handler = new DefaultLaunchHandler(new PrintStream(baos)); + LocalLogger l = new LocalLogger(); + DefaultLaunchHandler handler = new DefaultLaunchHandler(l); ParseException parse = new ParseException("no information element"); RuntimeException runtime = new RuntimeException("programmer made a mistake", parse); @@ -113,21 +137,18 @@ public class DefaultLaunchHandlerTest { "severe", "error type", "test error", "this is a test of the error"); handler.launchError(error); - String output = baos.toString(); - assertEquals("netx: error type: test error (programmer made a mistake (no information element))\n", output); + assertEquals("netx: error type: test error (programmer made a mistake (no information element))\n", l.getStream1()); } - @Test public void testValidationError() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DefaultLaunchHandler handler = new DefaultLaunchHandler(new PrintStream(baos)); + LocalLogger l = new LocalLogger(); + DefaultLaunchHandler handler = new DefaultLaunchHandler(l); LaunchException error = new LaunchException(null, null, "severe", "validation-error type", "test validation-error", "this is a test of a validation error"); handler.validationError(error); - String output = baos.toString(); - assertEquals("netx: validation-error type: test validation-error\n", output); + assertEquals("netx: validation-error type: test validation-error\n", l.getStream1()); } } diff --git a/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java b/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java index 03d942b..b4c0079 100644 --- a/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java +++ b/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java @@ -41,6 +41,7 @@ import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.io.UnsupportedEncodingException; +import java.lang.reflect.Method; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URISyntaxException; @@ -50,6 +51,7 @@ import net.sourceforge.jnlp.ServerAccess; import net.sourceforge.jnlp.ServerLauncher; import net.sourceforge.jnlp.Version; import net.sourceforge.jnlp.runtime.JNLPRuntime; +import net.sourceforge.jnlp.util.logging.OutputController; import net.sourceforge.jnlp.util.UrlUtils; import org.junit.AfterClass; import org.junit.Assert; @@ -60,7 +62,7 @@ public class ResourceTrackerTest { public static ServerLauncher testServer; public static ServerLauncher testServerWithBrokenHead; - private static PrintStream backedUpStream; + private static PrintStream[] backedUpStream = new PrintStream[4]; private static ByteArrayOutputStream currentErrorStream; private static final String nameStub1 = "itw-server"; private static final String nameStub2 = "test-file"; @@ -112,21 +114,47 @@ public class ResourceTrackerTest { return n; } - + @BeforeClass - public static void redirectErr() { - if (backedUpStream == null) { - backedUpStream = System.err; + //keeping silent outputs from launched jvm + public static void redirectErr() throws IOException { + for (int i = 0; i < backedUpStream.length; i++) { + if (backedUpStream[i] == null) { + switch (i) { + case 0: + backedUpStream[i] = System.out; + break; + case 1: + backedUpStream[i] = System.err; + break; + case 2: + backedUpStream[i] = OutputController.getLogger().getOut(); + break; + case 3: + backedUpStream[i] = OutputController.getLogger().getErr(); + break; + } + + } + } currentErrorStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(currentErrorStream)); System.setErr(new PrintStream(currentErrorStream)); + OutputController.getLogger().setOut(new PrintStream(currentErrorStream)); + OutputController.getLogger().setErr(new PrintStream(currentErrorStream)); + } @AfterClass - public static void redirectErrBack() throws UnsupportedEncodingException { + public static void redirectErrBack() throws IOException { ServerAccess.logErrorReprint(currentErrorStream.toString("utf-8")); - System.setErr(backedUpStream); + System.setOut(backedUpStream[0]); + System.setErr(backedUpStream[1]); + OutputController.getLogger().setOut(backedUpStream[2]); + OutputController.getLogger().setErr(backedUpStream[3]); + } @@ -141,14 +169,18 @@ public class ResourceTrackerTest { } @BeforeClass - public static void startServer() throws IOException { + public static void startServer() throws Exception { + redirectErr(); testServer = ServerAccess.getIndependentInstance(System.getProperty("java.io.tmpdir"), ServerAccess.findFreePort()); + redirectErrBack(); } @BeforeClass - public static void startServer2() throws IOException { + public static void startServer2() throws Exception { + redirectErr(); testServerWithBrokenHead = ServerAccess.getIndependentInstance(System.getProperty("java.io.tmpdir"), ServerAccess.findFreePort()); testServerWithBrokenHead.setSupportingHeadRequest(false); + redirectErrBack(); } @AfterClass @@ -162,7 +194,7 @@ public class ResourceTrackerTest { } @Test - public void getUrlResponseCodeTestWorkingHeadRequest() throws IOException { + public void getUrlResponseCodeTestWorkingHeadRequest() throws Exception { redirectErr(); try { File f = File.createTempFile(nameStub1, nameStub2); @@ -177,7 +209,7 @@ public class ResourceTrackerTest { } @Test - public void getUrlResponseCodeTestNotWorkingHeadRequest() throws IOException { + public void getUrlResponseCodeTestNotWorkingHeadRequest() throws Exception { redirectErr(); try { File f = File.createTempFile(nameStub1, nameStub2); @@ -192,7 +224,7 @@ public class ResourceTrackerTest { } @Test - public void getUrlResponseCodeTestGetRequestOnNotWorkingHeadRequest() throws IOException { + public void getUrlResponseCodeTestGetRequestOnNotWorkingHeadRequest() throws Exception { redirectErr(); try { File f = File.createTempFile(nameStub1, nameStub2); @@ -207,7 +239,7 @@ public class ResourceTrackerTest { } @Test - public void getUrlResponseCodeTestGetRequest() throws IOException { + public void getUrlResponseCodeTestGetRequest() throws Exception { redirectErr(); try { File f = File.createTempFile(nameStub1, nameStub2); @@ -222,7 +254,7 @@ public class ResourceTrackerTest { } @Test - public void getUrlResponseCodeTestWrongRequest() throws IOException { + public void getUrlResponseCodeTestWrongRequest() throws Exception { redirectErr(); try { File f = File.createTempFile(nameStub1, nameStub2); @@ -248,7 +280,7 @@ public class ResourceTrackerTest { } @Test - public void findBestUrltest() throws IOException { + public void findBestUrltest() throws Exception { redirectErr(); try { File fileForServerWithHeader = File.createTempFile(nameStub1, nameStub2); diff --git a/tests/netx/unit/net/sourceforge/jnlp/util/HttpUtilsTest.java b/tests/netx/unit/net/sourceforge/jnlp/util/HttpUtilsTest.java index 08a6060..c585eed 100644 --- a/tests/netx/unit/net/sourceforge/jnlp/util/HttpUtilsTest.java +++ b/tests/netx/unit/net/sourceforge/jnlp/util/HttpUtilsTest.java @@ -44,27 +44,57 @@ import java.net.HttpURLConnection; import java.net.URL; import net.sourceforge.jnlp.ServerAccess; import net.sourceforge.jnlp.ServerLauncher; +import net.sourceforge.jnlp.util.logging.OutputController; +import org.junit.AfterClass; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; public class HttpUtilsTest { - private static PrintStream backedUpStream; - private static ByteArrayOutputStream nwErrorStream; + private static PrintStream[] backedUpStream = new PrintStream[4]; + private static ByteArrayOutputStream currentErrorStream; - public static void redirectErr() { - if (backedUpStream == null) { - backedUpStream = System.err; + @BeforeClass + //keeping silent outputs from launched jvm + public static void redirectErr() throws IOException { + for (int i = 0; i < backedUpStream.length; i++) { + if (backedUpStream[i] == null) { + switch (i) { + case 0: + backedUpStream[i] = System.out; + break; + case 1: + backedUpStream[i] = System.err; + break; + case 2: + backedUpStream[i] = OutputController.getLogger().getOut(); + break; + case 3: + backedUpStream[i] = OutputController.getLogger().getErr(); + break; + } + + } + } - nwErrorStream = new ByteArrayOutputStream(); - System.setErr(new PrintStream(nwErrorStream)); + currentErrorStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(currentErrorStream)); + System.setErr(new PrintStream(currentErrorStream)); + OutputController.getLogger().setOut(new PrintStream(currentErrorStream)); + OutputController.getLogger().setErr(new PrintStream(currentErrorStream)); + } - - public static void redirectErrBack() throws UnsupportedEncodingException { - ServerAccess.logErrorReprint(nwErrorStream.toString("utf-8")); - System.setErr(backedUpStream); + @AfterClass + public static void redirectErrBack() throws IOException { + ServerAccess.logErrorReprint(currentErrorStream.toString("utf-8")); + System.setOut(backedUpStream[0]); + System.setErr(backedUpStream[1]); + OutputController.getLogger().setOut(backedUpStream[2]); + OutputController.getLogger().setErr(backedUpStream[3]); + } diff --git a/tests/netx/unit/net/sourceforge/jnlp/util/XDesktopEntryTest.java b/tests/netx/unit/net/sourceforge/jnlp/util/XDesktopEntryTest.java index 1cd343c..8985085 100644 --- a/tests/netx/unit/net/sourceforge/jnlp/util/XDesktopEntryTest.java +++ b/tests/netx/unit/net/sourceforge/jnlp/util/XDesktopEntryTest.java @@ -41,11 +41,7 @@ import java.io.IOException; import java.io.StringReader; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Set; import net.sourceforge.jnlp.ServerAccess; diff --git a/tests/netx/unit/net/sourceforge/jnlp/util/logging/FileLogTest.java b/tests/netx/unit/net/sourceforge/jnlp/util/logging/FileLogTest.java new file mode 100644 index 0000000..ed16780 --- /dev/null +++ b/tests/netx/unit/net/sourceforge/jnlp/util/logging/FileLogTest.java @@ -0,0 +1,178 @@ +/*Copyright (C) 2013 Red Hat, Inc. + + This file is part of IcedTea. + + IcedTea is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 2. + + IcedTea is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with IcedTea; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. + */ +package net.sourceforge.jnlp.util.logging; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import net.sourceforge.jnlp.closinglisteners.RulesFolowingClosingListener; +import net.sourceforge.jnlp.util.StreamUtils; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class FileLogTest { + + private static File[] loggingTargets = new File[12]; + private static String line1 = "I'm logged line one"; + private static String line2 = "I'm logged line two"; + private static String line3 = "I'm logged line three"; + private static RulesFolowingClosingListener.ContainsRule r1 = new RulesFolowingClosingListener.ContainsRule(line1); + private static RulesFolowingClosingListener.ContainsRule r2 = new RulesFolowingClosingListener.ContainsRule(line2); + private static RulesFolowingClosingListener.ContainsRule r3 = new RulesFolowingClosingListener.ContainsRule(line3); + + @BeforeClass + public static void prepareTmpFiles() throws IOException { + for (int i = 0; i < loggingTargets.length; i++) { + loggingTargets[i] = File.createTempFile("fileLogger", "iteTest"); + loggingTargets[i].deleteOnExit(); + } + //delete first half of the files, logger should handle both casses + for (int i = 0; i < loggingTargets.length / 2; i++) { + loggingTargets[i].delete(); + } + + } + + @AfterClass + public static void cleanTmpFiles() throws IOException { + for (int i = 0; i < loggingTargets.length; i++) { + loggingTargets[i].delete(); + } + } + + @Test + public void isAppendingLoggerLoggingOnNotExisitngFile() throws Exception { + int i = 0; + FileLog l = new FileLog(loggingTargets[i].getAbsolutePath(), true); + l.log(line1); + String s = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertTrue(r1.evaluate(s)); + } + + @Test + public void isRewritingLoggerLoggingOnNotExisitngFile() throws Exception { + int i = 1; + FileLog l = new FileLog(loggingTargets[i].getAbsolutePath(), false); + l.log(line1); + String s = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertTrue(r1.evaluate(s)); + } + + @Test + public void isRewritingLoggerRewritingOnNotExisitngFile() throws Exception { + int i = 2; + FileLog l1 = new FileLog(loggingTargets[i].getAbsolutePath(), false); + l1.log(line2); + String s1 = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertTrue(r2.evaluate(s1)); + l1.close(); + FileLog l2 = new FileLog(loggingTargets[i].getAbsolutePath(), false); + l2.log(line3); + String s2 = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertFalse(r2.evaluate(s2)); + Assert.assertTrue(r3.evaluate(s2)); + + } + + @Test + public void isAppendingLoggerAppendingOnNotExisitngFile() throws Exception { + int i = 4; + FileLog l1 = new FileLog(loggingTargets[i].getAbsolutePath(), true); + l1.log(line2); + String s1 = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertTrue(r2.evaluate(s1)); + l1.close(); + FileLog l2 = new FileLog(loggingTargets[i].getAbsolutePath(), true); + l2.log(line3); + String s2 = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertTrue(r2.evaluate(s2)); + Assert.assertTrue(r3.evaluate(s2)); + + } + + //************ + @Test + public void isAppendingLoggerLoggingOnExisitngFile() throws Exception { + int i = 6; + FileLog l = new FileLog(loggingTargets[i].getAbsolutePath(), true); + l.log(line1); + String s = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertTrue(r1.evaluate(s)); + } + + @Test + public void isRewritingLoggerLoggingOnExisitngFile() throws Exception { + int i = 7; + FileLog l = new FileLog(loggingTargets[i].getAbsolutePath(), false); + l.log(line1); + String s = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertTrue(r1.evaluate(s)); + } + + @Test + public void isRewritingLoggerRewritingOnExisitngFile() throws Exception { + int i = 8; + FileLog l1 = new FileLog(loggingTargets[i].getAbsolutePath(), false); + l1.log(line2); + String s1 = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertTrue(r2.evaluate(s1)); + l1.close(); + FileLog l2 = new FileLog(loggingTargets[i].getAbsolutePath(), false); + l2.log(line3); + String s2 = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertFalse(r2.evaluate(s2)); + Assert.assertTrue(r3.evaluate(s2)); + + } + + @Test + public void isAppendingLoggerAppendingOnExisitngFile() throws Exception { + int i = 10; + FileLog l1 = new FileLog(loggingTargets[i].getAbsolutePath(), true); + l1.log(line2); + String s1 = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertTrue(r2.evaluate(s1)); + l1.close(); + FileLog l2 = new FileLog(loggingTargets[i].getAbsolutePath(), true); + l2.log(line3); + String s2 = StreamUtils.readStreamAsString(new FileInputStream(loggingTargets[i]), true); + Assert.assertTrue(r2.evaluate(s2)); + Assert.assertTrue(r3.evaluate(s2)); + + } +} diff --git a/tests/netx/unit/net/sourceforge/jnlp/util/logging/OutputControllerTest.java b/tests/netx/unit/net/sourceforge/jnlp/util/logging/OutputControllerTest.java new file mode 100644 index 0000000..8c08f45 --- /dev/null +++ b/tests/netx/unit/net/sourceforge/jnlp/util/logging/OutputControllerTest.java @@ -0,0 +1,367 @@ +/*Copyright (C) 2013 Red Hat, Inc. + + This file is part of IcedTea. + + IcedTea is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 2. + + IcedTea is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with IcedTea; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. + */ +package net.sourceforge.jnlp.util.logging; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.PrintStream; +import java.util.Random; +import net.sourceforge.jnlp.closinglisteners.RulesFolowingClosingListener; +import net.sourceforge.jnlp.util.StreamUtils; +import org.junit.Assert; +import org.junit.Test; + +public class OutputControllerTest { + + private static String line1 = "I'm logged line one"; + private static String line2 = "I'm logged line two"; + private static String line3 = "I'm logged line three"; + private static String line4 = "I'm logged line four"; + private static String line5 = "I'm logged line five"; + private static String line6 = "I'm logged line six"; + private static RulesFolowingClosingListener.ContainsRule r1 = new RulesFolowingClosingListener.ContainsRule(line1); + private static RulesFolowingClosingListener.ContainsRule r2 = new RulesFolowingClosingListener.ContainsRule(line2); + private static RulesFolowingClosingListener.ContainsRule r3 = new RulesFolowingClosingListener.ContainsRule(line3); + private static RulesFolowingClosingListener.ContainsRule r4 = new RulesFolowingClosingListener.ContainsRule(line4); + private static RulesFolowingClosingListener.ContainsRule r5 = new RulesFolowingClosingListener.ContainsRule(line5); + private static RulesFolowingClosingListener.ContainsRule r6 = new RulesFolowingClosingListener.ContainsRule(line6); + + private static class AccessiblePrintStream extends PrintStream { + + public AccessiblePrintStream(ByteArrayOutputStream out) { + super(out); + } + + public ByteArrayOutputStream getOut() { + return (ByteArrayOutputStream) out; + } + } + + @Test + public void isLoggingStdStreams() throws Exception { + ByteArrayOutputStream os1 = new ByteArrayOutputStream(); + ByteArrayOutputStream os2 = new ByteArrayOutputStream(); + OutputController oc = new OutputController(new PrintStream(os1), new PrintStream(os2)); + LogConfig.getLogConfig().setEnableLogging(false); + LogConfig.getLogConfig().setLogToFile(false); + LogConfig.getLogConfig().setLogToStreams(true); + LogConfig.getLogConfig().setLogToSysLog(false); + oc.log(OutputController.Level.MESSAGE_DEBUG, line1); + oc.log(OutputController.Level.ERROR_DEBUG, line1); + oc.flush(); + Assert.assertFalse(r1.evaluate(os1.toString("utf-8"))); + Assert.assertFalse(r1.evaluate(os2.toString("utf-8"))); + oc.log(OutputController.Level.MESSAGE_ALL, line1); + oc.log(OutputController.Level.ERROR_DEBUG, line1); + oc.flush(); + Assert.assertTrue(r1.evaluate(os1.toString("utf-8"))); + Assert.assertFalse(r1.evaluate(os2.toString("utf-8"))); + oc.log(OutputController.Level.ERROR_ALL, line1); + oc.flush(); + Assert.assertTrue(r1.evaluate(os2.toString("utf-8"))); + + LogConfig.getLogConfig().setEnableLogging(true); + oc.log(OutputController.Level.MESSAGE_DEBUG, line2); + oc.flush(); + Assert.assertTrue(r2.evaluate(os1.toString("utf-8"))); + Assert.assertFalse(r2.evaluate(os2.toString("utf-8"))); + oc.log(OutputController.Level.ERROR_DEBUG, line2); + oc.flush(); + Assert.assertTrue(r2.evaluate(os1.toString("utf-8"))); + Assert.assertTrue(r2.evaluate(os2.toString("utf-8"))); + + oc.log(OutputController.Level.ERROR_DEBUG, line3); + oc.flush(); + Assert.assertFalse(r3.evaluate(os1.toString("utf-8"))); + Assert.assertTrue(r3.evaluate(os2.toString("utf-8"))); + oc.log(OutputController.Level.MESSAGE_DEBUG, line3); + oc.flush(); + Assert.assertTrue(r3.evaluate(os1.toString("utf-8"))); + Assert.assertTrue(r3.evaluate(os2.toString("utf-8"))); + + LogConfig.getLogConfig().setEnableLogging(false); + oc.log(OutputController.Level.WARNING_DEBUG, line4); + oc.flush(); + Assert.assertFalse(r4.evaluate(os1.toString("utf-8"))); + Assert.assertFalse(r4.evaluate(os2.toString("utf-8"))); + oc.log(OutputController.Level.WARNING_ALL, line5); + oc.flush(); + Assert.assertTrue(r5.evaluate(os1.toString("utf-8"))); + Assert.assertTrue(r5.evaluate(os2.toString("utf-8"))); + LogConfig.getLogConfig().setEnableLogging(true); + oc.log(OutputController.Level.WARNING_DEBUG, line4); + oc.flush(); + Assert.assertTrue(r4.evaluate(os1.toString("utf-8"))); + Assert.assertTrue(r4.evaluate(os2.toString("utf-8"))); + + } + private static final Random random = new Random(); + private int delayable = 0; + + private class IdedRunnable implements Runnable { + + private final int id; + private final OutputController oc; + private boolean done = false; + private final int iterations; + + public IdedRunnable(int id, OutputController oc, int iterations) { + this.id = id; + this.oc = oc; + this.iterations = iterations; + } + + @Override + public void run() { + for (int i = 0; i < iterations; i++) { + try { + //be sure this pattern is kept in assers + oc.log(OutputController.Level.WARNING_ALL, "thread " + id + " line " + i); + Thread.sleep(random.nextInt(delayable)); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + done = true; + } + + public boolean isDone() { + return done; + } + } + + /** + * todo - include syslog once implemented + */ + @Test + public void isParalelLogingWorking() throws Exception { + LogConfig.getLogConfig().setEnableLogging(true); + LogConfig.getLogConfig().setLogToStreams(true); + LogConfig.getLogConfig().setLogToSysLog(false); + String s = ""; + //this was tested with 1-100 iterations and 100 threads. But can couse OutOfMemoryError + int maxi = 90; + int minits = 70; + int maxt = 10; + //tested with delayable 1-10, but took minutes then + for (delayable = 1; delayable < 1; delayable++) { + for (int iterations = minits; iterations < maxi; iterations++) { + for (int threads = 1; threads < maxt; threads++) { + LogConfig.getLogConfig().setLogToFile(false); + System.gc(); + ByteArrayOutputStream os1 = new ByteArrayOutputStream(); + ByteArrayOutputStream os2 = new ByteArrayOutputStream(); + OutputController oc = new OutputController(new PrintStream(os1), new PrintStream(os2)); + + File f = File.createTempFile("replacedFilelogger", "itwTest"); + f.deleteOnExit(); + oc.setFileLog(new FileLog(f.getAbsolutePath(), false)); + LogConfig.getLogConfig().setLogToFile(true); + + ThreadGroup tg = new ThreadGroup("TerribleGroup"); + IdedRunnable[] idedRunnables = new IdedRunnable[threads]; + Thread[] xt = new Thread[threads]; + for (int i = 0; i < threads; i++) { + Thread.sleep(random.nextInt(delayable)); + idedRunnables[i] = new IdedRunnable(i, oc, iterations); + xt[i] = new Thread(tg, idedRunnables[i], "iterations = " + iterations + "; threads = " + threads + "; delayable = " + delayable); + xt[i].start(); + } + while (true) { + boolean ok = true; + for (IdedRunnable idedRunnable : idedRunnables) { + if (!idedRunnable.isDone()) { + ok = false; + break; + } + } + if (ok) { + break; + } + } + oc.flush(); + String s1 = os1.toString("utf-8"); + String s2 = os2.toString("utf-8"); + String s3 = StreamUtils.readStreamAsString(new FileInputStream(f), true); + for (int i = minits; i < maxi; i++) { + for (int t = 0; t < maxt; t++) { + //be sure this pattern is kept in IdedRunnable + String expected = "thread " + t + " line " + i; + if (i >= iterations || t >= threads) { + Assert.assertFalse(s1.contains(expected)); + Assert.assertFalse(s2.contains(expected)); + Assert.assertFalse(s3.contains(expected)); + } else { + Assert.assertTrue(s1.contains(expected)); + Assert.assertTrue(s2.contains(expected)); + Assert.assertTrue(s3.contains(expected)); + } + + } + } + tg.destroy(); + + } + } + } + + + + } + + @Test + public void isChangingOfStreasmWorking() throws Exception { + LogConfig.getLogConfig().setEnableLogging(true); + LogConfig.getLogConfig().setLogToFile(false); + LogConfig.getLogConfig().setLogToStreams(true); + LogConfig.getLogConfig().setLogToSysLog(false); + ByteArrayOutputStream os1 = new ByteArrayOutputStream(); + ByteArrayOutputStream os2 = new ByteArrayOutputStream(); + OutputController oc = new OutputController(new PrintStream(os1), new PrintStream(os2)); + oc.log(OutputController.Level.MESSAGE_ALL, line1); + oc.log(OutputController.Level.ERROR_ALL, line1); + oc.flush(); + ByteArrayOutputStream os3 = new ByteArrayOutputStream(); + ByteArrayOutputStream os4 = new ByteArrayOutputStream(); + oc.setOut(new PrintStream(os3)); + oc.log(OutputController.Level.MESSAGE_ALL, line2); + oc.log(OutputController.Level.ERROR_ALL, line2); + oc.flush(); + oc.setErr(new PrintStream(os4)); + oc.log(OutputController.Level.MESSAGE_ALL, line3); + oc.log(OutputController.Level.ERROR_ALL, line3); + oc.flush(); + + Assert.assertTrue(r1.evaluate(os1.toString("utf-8"))); + Assert.assertTrue(r1.evaluate(os2.toString("utf-8"))); + Assert.assertFalse(r1.evaluate(os3.toString("utf-8"))); + Assert.assertFalse(r1.evaluate(os4.toString("utf-8"))); + + + Assert.assertFalse(r2.evaluate(os1.toString("utf-8"))); + Assert.assertTrue(r2.evaluate(os2.toString("utf-8"))); + Assert.assertTrue(r2.evaluate(os3.toString("utf-8"))); + Assert.assertFalse(r2.evaluate(os4.toString("utf-8"))); + + Assert.assertFalse(r3.evaluate(os1.toString("utf-8"))); + Assert.assertFalse(r3.evaluate(os2.toString("utf-8"))); + Assert.assertTrue(r3.evaluate(os3.toString("utf-8"))); + Assert.assertTrue(r3.evaluate(os4.toString("utf-8"))); + + LogConfig.getLogConfig().setLogToStreams(false); + + oc.log(OutputController.Level.MESSAGE_ALL, line4); + oc.log(OutputController.Level.ERROR_ALL, line4); + + Assert.assertFalse(r4.evaluate(os1.toString("utf-8"))); + Assert.assertFalse(r4.evaluate(os2.toString("utf-8"))); + Assert.assertFalse(r4.evaluate(os3.toString("utf-8"))); + Assert.assertFalse(r4.evaluate(os4.toString("utf-8"))); + + } + + @Test + public void isFileLoggerWorking() throws Exception { + String s1 = ""; + String s2 = ""; + LogConfig.getLogConfig().setEnableLogging(true); + LogConfig.getLogConfig().setLogToFile(false); + LogConfig.getLogConfig().setLogToStreams(false); + LogConfig.getLogConfig().setLogToSysLog(false); + + ByteArrayOutputStream os1 = new ByteArrayOutputStream(); + ByteArrayOutputStream os2 = new ByteArrayOutputStream(); + OutputController oc = new OutputController(new PrintStream(os1), new PrintStream(os2)); + File f1 = File.createTempFile("replacedFilelogger", "itwTest"); + File f2 = File.createTempFile("replacedFilelogger", "itwTest"); + f1.deleteOnExit(); + f2.deleteOnExit(); + oc.setFileLog(new FileLog(f1.getAbsolutePath(), false)); + LogConfig.getLogConfig().setLogToFile(true); + oc.log(OutputController.Level.MESSAGE_ALL, line1); + oc.log(OutputController.Level.ERROR_ALL, line2); + oc.log(OutputController.Level.MESSAGE_ALL, line3); + oc.flush(); + s1 = StreamUtils.readStreamAsString(new FileInputStream(f1), true); + s2 = StreamUtils.readStreamAsString(new FileInputStream(f2), true); + + Assert.assertTrue(r1.evaluate(s1)); + Assert.assertFalse(r1.evaluate(s2)); + Assert.assertTrue(r2.evaluate(s1)); + Assert.assertFalse(r2.evaluate(s2)); + Assert.assertTrue(r3.evaluate(s1)); + Assert.assertFalse(r3.evaluate(s2)); + + oc.setFileLog(new FileLog(f2.getAbsolutePath(), false)); + oc.log(OutputController.Level.ERROR_ALL, line5); + oc.log(OutputController.Level.MESSAGE_ALL, line5); + oc.flush(); + + s1 = StreamUtils.readStreamAsString(new FileInputStream(f1), true); + s2 = StreamUtils.readStreamAsString(new FileInputStream(f2), true); + + Assert.assertTrue(r1.evaluate(s1)); + Assert.assertFalse(r1.evaluate(s2)); + Assert.assertTrue(r2.evaluate(s1)); + Assert.assertFalse(r2.evaluate(s2)); + Assert.assertTrue(r3.evaluate(s1)); + Assert.assertFalse(r3.evaluate(s2)); + + Assert.assertFalse(r5.evaluate(s1)); + Assert.assertTrue(r5.evaluate(s2)); + + LogConfig.getLogConfig().setLogToFile(false); + oc.log(OutputController.Level.ERROR_ALL, line6); + oc.log(OutputController.Level.MESSAGE_ALL, line6); + oc.flush(); + + s1 = StreamUtils.readStreamAsString(new FileInputStream(f1), true); + s2 = StreamUtils.readStreamAsString(new FileInputStream(f2), true); + + Assert.assertFalse(r6.evaluate(s1)); + Assert.assertFalse(r6.evaluate(s2)); + + + } + + /** + * add syslog once implemented + */ + @Test + public void isSysLoggerWorking() throws Exception { + } +} diff --git a/tests/netx/unit/net/sourceforge/jnlp/util/logging/PrintStreamLoggerTest.java b/tests/netx/unit/net/sourceforge/jnlp/util/logging/PrintStreamLoggerTest.java new file mode 100644 index 0000000..f56099c --- /dev/null +++ b/tests/netx/unit/net/sourceforge/jnlp/util/logging/PrintStreamLoggerTest.java @@ -0,0 +1,113 @@ +/*Copyright (C) 2013 Red Hat, Inc. + + This file is part of IcedTea. + + IcedTea is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 2. + + IcedTea is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with IcedTea; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. + */ +package net.sourceforge.jnlp.util.logging; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import net.sourceforge.jnlp.closinglisteners.RulesFolowingClosingListener; +import org.junit.Assert; +import org.junit.Test; + +public class PrintStreamLoggerTest { + + private static String line1 = "I'm logged line one"; + private static String line2 = "I'm logged line two"; + private static RulesFolowingClosingListener.ContainsRule r1 = new RulesFolowingClosingListener.ContainsRule(line1); + private static RulesFolowingClosingListener.ContainsRule r2 = new RulesFolowingClosingListener.ContainsRule(line2); + + private static class AccessiblePrintStream extends PrintStream{ + + public AccessiblePrintStream(ByteArrayOutputStream out) { + super(out); + } + + + + public ByteArrayOutputStream getOut() { + return (ByteArrayOutputStream) out; + } + } + + @Test + public void isLoggingAtAll() throws Exception { + int i = 0; + ByteArrayOutputStream output = new ByteArrayOutputStream(); + PrintStreamLogger l = new PrintStreamLogger(new PrintStream(output)); + l.log(line1); + Assert.assertTrue(r1.evaluate(output.toString("utf-8"))); + l.log(line2); + Assert.assertTrue(r1.evaluate(output.toString("utf-8"))); + Assert.assertTrue(r2.evaluate(output.toString("utf-8"))); + } + + @Test + public void isReturningStream() throws Exception { + int i = 0; + ByteArrayOutputStream output = new ByteArrayOutputStream(); + AccessiblePrintStream ps = new AccessiblePrintStream(output); + PrintStreamLogger l = new PrintStreamLogger(ps); + l.log(line1); + Assert.assertTrue(r1.evaluate(output.toString("utf-8"))); + AccessiblePrintStream got = (AccessiblePrintStream) l.getStream(); + Assert.assertTrue(r1.evaluate(got.getOut().toString("utf-8"))); + l.log(line2); + Assert.assertTrue(r1.evaluate(output.toString("utf-8"))); + Assert.assertTrue(r2.evaluate(output.toString("utf-8"))); + Assert.assertTrue(r1.evaluate(got.getOut().toString("utf-8"))); + Assert.assertTrue(r2.evaluate(got.getOut().toString("utf-8"))); + Assert.assertTrue(got == ps); + } + + @Test + public void isSettingStream() throws Exception { + int i = 0; + ByteArrayOutputStream output1 = new ByteArrayOutputStream(); + ByteArrayOutputStream output2 = new ByteArrayOutputStream(); + AccessiblePrintStream ps = new AccessiblePrintStream(output1); + PrintStreamLogger l = new PrintStreamLogger(ps); + l.log(line1); + Assert.assertTrue(r1.evaluate(output1.toString("utf-8"))); + AccessiblePrintStream set = new AccessiblePrintStream(output2); + l.setStream(set); + l.log(line2); + Assert.assertFalse(r1.evaluate(output2.toString("utf-8"))); + Assert.assertTrue(r2.evaluate(output2.toString("utf-8"))); + Assert.assertFalse(r1.evaluate(set.getOut().toString("utf-8"))); + Assert.assertTrue(r2.evaluate(set.getOut().toString("utf-8"))); + Assert.assertTrue(set != ps); + Assert.assertTrue(set == l.getStream()); + } +} diff --git a/tests/reproducers/simple/simpletest1/testcases/XDGspecificationTests.java b/tests/reproducers/simple/simpletest1/testcases/XDGspecificationTests.java index 7bfc58a..176d417 100644 --- a/tests/reproducers/simple/simpletest1/testcases/XDGspecificationTests.java +++ b/tests/reproducers/simple/simpletest1/testcases/XDGspecificationTests.java @@ -205,7 +205,6 @@ public class XDGspecificationTests extends BrowserTest { Entry<String, String> entry = it.next(); String v = entry.getValue(); String s = entry.getKey() + "=" + v; - //System.out.println(s); if (entry.getKey().equals(XDG_CACHE_HOME) || entry.getKey().equals(XDG_CONFIG_HOME)) { ServerAccess.logOutputReprint("ignoring " + s); c++; @@ -243,7 +242,6 @@ public class XDGspecificationTests extends BrowserTest { Entry<String, String> entry = it.next(); String v = entry.getValue(); String s = entry.getKey() + "=" + v; - //System.out.println(s); if (entry.getKey().equals(XDG_CACHE_HOME)) { ServerAccess.logOutputReprint(entry.getKey() + " was " + v); v = cacheF.getAbsolutePath(); diff --git a/tests/test-extensions/net/sourceforge/jnlp/ServerAccess.java b/tests/test-extensions/net/sourceforge/jnlp/ServerAccess.java index e9f4238..31e81ad 100644 --- a/tests/test-extensions/net/sourceforge/jnlp/ServerAccess.java +++ b/tests/test-extensions/net/sourceforge/jnlp/ServerAccess.java @@ -61,6 +61,7 @@ import net.sourceforge.jnlp.browsertesting.Browsers; import net.sourceforge.jnlp.closinglisteners.AutoErrorClosingListener; import net.sourceforge.jnlp.closinglisteners.AutoOkClosingListener; import net.sourceforge.jnlp.util.FileUtils; +import net.sourceforge.jnlp.util.logging.OutputController; import org.junit.Assert; /** @@ -756,16 +757,7 @@ public class ServerAccess { logException(t, true); } public static void logException(Throwable t, boolean print){ - try{ - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - t.printStackTrace(pw); - log(sw.toString(), false, print); - pw.close(); - sw.close(); - }catch(Exception ex){ - throw new RuntimeException(ex); - } + log(OutputController.exceptionToString(t), false, print); } private static StackTraceElement getTestMethod() { |