[rfc][icedtea-web] TeeOutputStream Dependency Fix
Omair Majid
omajid at redhat.com
Tue Sep 9 15:58:06 UTC 2014
* Jie Kang <jkang at redhat.com> [2014-09-04 11:32]:
> This patch adds a logger dependency injection to TeeOutputStream so
> that the tests no longer require a protected function in order to run.
>
> Thoughts?
I like the approach. Comments in-line below.
> +++ b/netx/net/sourceforge/jnlp/util/logging/JavaConsole.java
> - System.setErr(new TeeOutputStream(System.err, true));
> - System.setOut(new TeeOutputStream(System.out, false));
> + System.setErr(new TeeOutputStream(System.err, true, OutputController.getLogger()));
> + System.setOut(new TeeOutputStream(System.out, false, OutputController.getLogger()));
You can avoid this change by creating a public constructor:
public TeeOutputStream(PrintStream stdStream, boolean isError) {
this(stdStream, isError, OutputController.getLogger());
}
// maybe even package-private constructor is okay?
public TeeOutputStream(PrintStream stdStream, boolean isError, MessageLogger logger) {
// your new implementation
Now, existing code will not see an API change, but your test can use the
3-arg variant.
> +++ b/netx/net/sourceforge/jnlp/util/logging/MessageLogger.java
> @@ -0,0 +1,7 @@
> +package net.sourceforge.jnlp.util.logging;
Missing license.
> +++ b/tests/netx/unit/net/sourceforge/jnlp/util/logging/TeeOutputStreamTest.java
> + private class TestLogger implements MessageLogger {
> + private ByteArrayOutputStream baos = new ByteArrayOutputStream();
> +
> + @Override
> + public void log(MessageWithHeader jm) {
> + baos.write(jm.getMessage().getBytes(), 0, jm.getMessage().getBytes().length);
> + }
> +
Now that you have a custom logger, you can even do things like record
individual `jm` messages in a list and count how many times logging was
triggered. I like it! :)
Thanks,
Omair
--
PGP Key: 66484681 (http://pgp.mit.edu/)
Fingerprint = F072 555B 0A17 3957 4E95 0056 F286 F14F 6648 4681
More information about the distro-pkg-dev
mailing list