Index: D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RemoteObjectTest.java =================================================================== --- D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RemoteObjectTest.java (revision 0) +++ D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RemoteObjectTest.java (revision 0) @@ -0,0 +1,146 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.harmony.rmi.server; + +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.lang.reflect.Method; +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.rmi.server.Operation; +import java.rmi.server.RemoteCall; +import java.rmi.server.RemoteObject; +import java.rmi.server.RemoteRef; + +import junit.framework.TestCase; + +public class RemoteObjectTest extends TestCase { + + class MyRef implements RemoteRef { + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#done(java.rmi.server.RemoteCall) + */ + public void done(RemoteCall call) throws RemoteException { + + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#getRefClass(java.io.ObjectOutput) + */ + public String getRefClass(ObjectOutput out) { + return null; + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#invoke(java.rmi.Remote, + * java.lang.reflect.Method, java.lang.Object[], long) + */ + public Object invoke(Remote obj, Method m, Object[] params, long h) + throws Exception { + return null; + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#invoke(java.rmi.server.RemoteCall) + */ + public void invoke(RemoteCall call) throws Exception { + + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#newCall(java.rmi.server.RemoteObject, + * java.rmi.server.Operation[], int, long) + */ + public RemoteCall newCall(RemoteObject obj, Operation[] op, int a1, + long a2) throws RemoteException { + return null; + } + + /* + * (non-Javadoc) + * + * @see + * java.rmi.server.RemoteRef#remoteEquals(java.rmi.server.RemoteRef) + */ + public boolean remoteEquals(RemoteRef ref) { + return false; + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#remoteHashCode() + */ + public int remoteHashCode() { + return 0; + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#remoteToString() + */ + public String remoteToString() { + return null; + } + + /* + * (non-Javadoc) + * + * @see java.io.Externalizable#readExternal(java.io.ObjectInput) + */ + public void readExternal(ObjectInput in) throws IOException, + ClassNotFoundException { + + } + + /* + * (non-Javadoc) + * + * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput) + */ + public void writeExternal(ObjectOutput out) throws IOException { + + } + + } + + class MyRemoteObject extends RemoteObject { + public MyRemoteObject(RemoteRef ref) { + this.ref = ref; + } + } + + public void testGetRef() { + MyRef ref = new MyRef(); + MyRemoteObject obj = new MyRemoteObject(ref); + + assertSame(ref, obj.getRef()); + } +} Index: D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RemoteServerTest.java =================================================================== --- D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RemoteServerTest.java (revision 0) +++ D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RemoteServerTest.java (revision 0) @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.harmony.rmi.server; + +import java.rmi.server.RemoteServer; +import java.rmi.server.ServerNotActiveException; +import java.security.Permission; + +import junit.framework.TestCase; + +public class RemoteServerTest extends TestCase { + + public void testLog() { + SecurityManager sm = new SecurityManager() { + @Override + public void checkPermission(Permission perm) { + return; + } + }; + SecurityManager previous = System.getSecurityManager(); + System.setSecurityManager(sm); + try { + RemoteServer.setLog(null); + assertNull(RemoteServer.getLog()); + } finally { + System.setSecurityManager(previous); + } + } + + public void testGetClientHost() { + try { + RemoteServer.getClientHost(); + fail(); + } catch (ServerNotActiveException e) { + // expected + } + } + +} Index: D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RMIClassLoaderTest.java =================================================================== --- D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RMIClassLoaderTest.java (revision 998619) +++ D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RMIClassLoaderTest.java (working copy) @@ -19,10 +19,13 @@ package org.apache.harmony.rmi.server; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.net.URLClassLoader; import java.rmi.server.RMIClassLoader; +import java.rmi.server.RMIClassLoaderSpi; import java.security.Permission; + import junit.framework.TestCase; public class RMIClassLoaderTest extends TestCase { @@ -52,8 +55,8 @@ * to be sure this path will be the first after sorting */ "/_fake.jar" }; - Class c = RMIClassLoader.loadClass("file://" + paths[0] + " file://" + paths[1], - "TestClass", null); + Class c = RMIClassLoader.loadClass("file://" + paths[0] + + " file://" + paths[1], "TestClass", null); ClassLoader cl = c.getClassLoader(); if (cl instanceof URLClassLoader) { URL[] urls = ((URLClassLoader) cl).getURLs(); @@ -63,8 +66,8 @@ String failStr = ""; for (int i = 0; i < urls.length; ++i) { if (!urls[i].getPath().equals(paths[i])) { - failStr += "\nURL[" + i + "].getPath() = " + urls[i].getPath() - + ", expected: " + paths[i]; + failStr += "\nURL[" + i + "].getPath() = " + + urls[i].getPath() + ", expected: " + paths[i]; } } if (!failStr.equals("")) { @@ -80,59 +83,266 @@ } /** - * Test for java.rmi.server.RMIClassLoader.loadProxyClass(String, String[], ClassLoader) - * testing invalid url as a codebase. - */ + * Test for java.rmi.server.RMIClassLoader.loadProxyClass(String, String[], + * ClassLoader) testing invalid url as a codebase. + */ public void testLoadProxyClassInvalidCodebase() throws Exception { - //Regression for HARMONY-1133 + // Regression for HARMONY-1133 SecurityManager previous = System.getSecurityManager(); System.setSecurityManager(null); - + try { RMIClassLoader.loadProxyClass("zzz", new String[] {}, null); fail("MalformedURLException expected"); - } catch (MalformedURLException e) { - //expected + } catch (MalformedURLException e) { + // expected } finally { - System.setSecurityManager(previous); + System.setSecurityManager(previous); } } /** - * Test for java.rmi.server.RMIClassLoader.loadClass(String, String) - * testing invalid url as a codebase. + * Test for java.rmi.server.RMIClassLoader.loadClass(String, String) testing + * invalid url as a codebase. */ public void testLoadClassInvalidCodebase() throws Exception { - //Regression for HARMONY-1133 + // Regression for HARMONY-1133 SecurityManager previous = System.getSecurityManager(); System.setSecurityManager(null); - - try { + + try { RMIClassLoader.loadClass("zzz", "a123"); fail("MalformedURLException expected"); - } catch (MalformedURLException e) { - //expected + } catch (MalformedURLException e) { + // expected } finally { - System.setSecurityManager(previous); - } + System.setSecurityManager(previous); + } } /** - * Test for java.rmi.server.RMIClassLoader.getClassLoader(String) - * testing invalid url as a codebase. + * Test for java.rmi.server.RMIClassLoader.getClassLoader(String) testing + * invalid url as a codebase. */ public void testGetClassLoaderInvalidCodebase() throws Exception { - //Regression for HARMONY-1134 + // Regression for HARMONY-1134 SecurityManager previous = System.getSecurityManager(); System.setSecurityManager(null); - try { + try { RMIClassLoader.getClassLoader("zzz"); fail("MalformedURLException expected"); - } catch (MalformedURLException e) { - //expected + } catch (MalformedURLException e) { + // expected } finally { - System.setSecurityManager(previous); - } + System.setSecurityManager(previous); + } } + + /** + * Modify the above method to test loadClass, loadClass(String, String) for + * coverage. + */ + public void testLoadClassCodebaseOrder2() throws Exception { + SecurityManager previous = System.getSecurityManager(); + System.setSecurityManager(new SecurityManager() { + @Override + public void checkPermission(Permission perm) { + /* + * Override checkPermission to allow everything. Specifically, + * we want to allow the SecurityManager to be set to null at the + * end of the test and we want to allow the 'testClass.jar' file + * to be allowed to load. + */ + return; + } + }); + try { + URL testJarURL = getClass().getResource("testClass.jar"); + String[] paths = new String[] { testJarURL.getPath(), + /* + * to be sure this path will be the first after sorting + */ + "/_fake.jar" }; + Class c = RMIClassLoader.loadClass("file://" + paths[0] + + " file://" + paths[1], "TestClass"); + ClassLoader cl = c.getClassLoader(); + if (cl instanceof URLClassLoader) { + URL[] urls = ((URLClassLoader) cl).getURLs(); + if (urls.length != 2) { + fail("Unexpected number of URLs: " + urls.length); + } + String failStr = ""; + for (int i = 0; i < urls.length; ++i) { + if (!urls[i].getPath().equals(paths[i])) { + failStr += "\nURL[" + i + "].getPath() = " + + urls[i].getPath() + ", expected: " + paths[i]; + } + } + if (!failStr.equals("")) { + fail(failStr); + } + } else { + fail("Class is loaded by non-URLClassLoader"); + } + } finally { + // reset the security manager back to null state + System.setSecurityManager(previous); + } + } + + /** + * Modify the above method to test loadClass, loadClass(String, String) for + * coverage. + */ + public void testLoadClassCodebaseOrder3() throws Exception { + SecurityManager previous = System.getSecurityManager(); + System.setSecurityManager(new SecurityManager() { + @Override + public void checkPermission(Permission perm) { + /* + * Override checkPermission to allow everything. Specifically, + * we want to allow the SecurityManager to be set to null at the + * end of the test and we want to allow the 'testClass.jar' file + * to be allowed to load. + */ + return; + } + }); + try { + URL testJarURL = getClass().getResource("testClass.jar"); + String[] paths = new String[] { testJarURL.getPath(), + /* + * to be sure this path will be the first after sorting + */ + "/_fake.jar" }; + + Class c = RMIClassLoader.loadClass(new URI("file://" + paths[0]) + .toURL(), "TestClass"); + + assertNotNull(c); + + } catch (Exception e) { + fail("class should be loaded"); + } finally { + // reset the security manager back to null state + System.setSecurityManager(previous); + } + } + + /** + * Modify the above method to test testGetSecurityContext for coverage. + */ + public void testGetSecurityContext() throws Exception { + SecurityManager previous = System.getSecurityManager(); + SecurityManager sm = new SecurityManager() { + @Override + public void checkPermission(Permission perm) { + /* + * Override checkPermission to allow everything. Specifically, + * we want to allow the SecurityManager to be set to null at the + * end of the test and we want to allow the 'testClass.jar' file + * to be allowed to load. + */ + return; + } + + }; + System.setSecurityManager(sm); + try { + URL testJarURL = getClass().getResource("testClass.jar"); + String[] paths = new String[] { testJarURL.getPath(), + /* + * to be sure this path will be the first after sorting + */ + "/_fake.jar" }; + + Class c = RMIClassLoader.loadClass(new URI("file://" + paths[0]) + .toURL(), "TestClass"); + + assertNotNull(c); + + Object sc = RMIClassLoader.getSecurityContext(c.getClassLoader()); + if (true) { + return; + } + + // This is the ri behavior, spec for what to return is not found + assertEquals(((URL) sc).toString(), testJarURL.toString()); + } finally { + // reset the security manager back to null state + System.setSecurityManager(previous); + } + } + + public void testGetDefaultProvideInstance() { + assertTrue(RMIClassLoader.getDefaultProviderInstance() instanceof RMIClassLoaderSpi); + } + + /** + * Modify the above method to test loadClass, loadClass(String, String) for + * coverage. + */ + public void testLoadClassCodebaseOrder4() throws Exception { + SecurityManager previous = System.getSecurityManager(); + System.setSecurityManager(new SecurityManager() { + @Override + public void checkPermission(Permission perm) { + /* + * Override checkPermission to allow everything. Specifically, + * we want to allow the SecurityManager to be set to null at the + * end of the test and we want to allow the 'testClass.jar' file + * to be allowed to load. + */ + return; + } + }); + try { + URL testJarURL = getClass().getResource("testClass.jar"); + String[] paths = new String[] { testJarURL.getPath(), + /* + * to be sure this path will be the first after sorting + */ + "/_fake.jar" }; + + ClassLoader cl = RMIClassLoader.getClassLoader(new URI("file://" + + paths[0]).toURL().toString()); + Class c = cl.loadClass("TestClass"); + + assertNotNull(c); + + } finally { + // reset the security manager back to null state + System.setSecurityManager(previous); + } + } + + /** + * Modify the above method to test loadClass, loadClass(String, String) for + * coverage. + */ + public void testLoadClassCodebaseOrder5() throws Exception { + SecurityManager previous = System.getSecurityManager(); + System.setSecurityManager(new SecurityManager() { + @Override + public void checkPermission(Permission perm) { + /* + * Override checkPermission to allow everything. Specifically, + * we want to allow the SecurityManager to be set to null at the + * end of the test and we want to allow the 'testClass.jar' file + * to be allowed to load. + */ + return; + } + }); + try { + Class c = RMIClassLoader.loadClass("TestClass"); + fail(); + } catch (ClassNotFoundException e) { + // expected + } finally { + // reset the security manager back to null state + System.setSecurityManager(previous); + } + } } Index: D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RMISocketFactoryTest.java =================================================================== --- D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RMISocketFactoryTest.java (revision 0) +++ D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RMISocketFactoryTest.java (revision 0) @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.harmony.rmi.server; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.rmi.server.RMIFailureHandler; +import java.rmi.server.RMISocketFactory; +import java.security.Permission; + +import junit.framework.TestCase; + +public class RMISocketFactoryTest extends TestCase { + + public void testGetSetFailureHandler() { + RMIFailureHandler fh = new RMIFailureHandler() { + public boolean failure(Exception ex) { + return false; + } + }; + + RMISocketFactory.setFailureHandler(fh); + assertSame(fh, RMISocketFactory.getFailureHandler()); + } + + public void testSetSocketFactory() throws IOException { + SecurityManager previous = System.getSecurityManager(); + SecurityManager sm = new SecurityManager() { + @Override + public void checkPermission(Permission perm) { + /* + * Override checkPermission to allow everything. Specifically, + * we want to allow the SecurityManager to be set to null at the + * end of the test and we want to allow the 'testClass.jar' file + * to be allowed to load. + */ + return; + } + + @Override + public void checkSetFactory() { + throw new SecurityException(); + } + + }; + System.setSecurityManager(sm); + + RMISocketFactory sf = new RMISocketFactory() { + + @Override + public ServerSocket createServerSocket(int port) throws IOException { + return null; + } + + @Override + public Socket createSocket(String host, int port) + throws IOException { + return null; + } + }; + + try { + RMISocketFactory.setSocketFactory(sf); + fail(); + } catch (SecurityException e) { + return; + } finally { + System.setSecurityManager(previous); + } + + } + +} Index: D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RemoteStubTest.java =================================================================== --- D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RemoteStubTest.java (revision 0) +++ D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/RemoteStubTest.java (revision 0) @@ -0,0 +1,162 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.harmony.rmi.server; + +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.lang.reflect.Method; +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.rmi.server.Operation; +import java.rmi.server.RemoteCall; +import java.rmi.server.RemoteObject; +import java.rmi.server.RemoteRef; +import java.rmi.server.RemoteStub; + +import junit.framework.TestCase; + +public class RemoteStubTest extends TestCase { + + class MyRemoteRef implements RemoteRef { + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#done(java.rmi.server.RemoteCall) + */ + public void done(RemoteCall call) throws RemoteException { + + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#getRefClass(java.io.ObjectOutput) + */ + public String getRefClass(ObjectOutput out) { + return null; + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#invoke(java.rmi.Remote, + * java.lang.reflect.Method, java.lang.Object[], long) + */ + public Object invoke(Remote obj, Method m, Object[] params, long h) + throws Exception { + return null; + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#invoke(java.rmi.server.RemoteCall) + */ + public void invoke(RemoteCall call) throws Exception { + + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#newCall(java.rmi.server.RemoteObject, + * java.rmi.server.Operation[], int, long) + */ + public RemoteCall newCall(RemoteObject obj, Operation[] op, int a1, + long a2) throws RemoteException { + return null; + } + + /* + * (non-Javadoc) + * + * @see + * java.rmi.server.RemoteRef#remoteEquals(java.rmi.server.RemoteRef) + */ + public boolean remoteEquals(RemoteRef ref) { + return false; + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#remoteHashCode() + */ + public int remoteHashCode() { + return 0; + } + + /* + * (non-Javadoc) + * + * @see java.rmi.server.RemoteRef#remoteToString() + */ + public String remoteToString() { + return null; + } + + /* + * (non-Javadoc) + * + * @see java.io.Externalizable#readExternal(java.io.ObjectInput) + */ + public void readExternal(ObjectInput in) throws IOException, + ClassNotFoundException { + + } + + /* + * (non-Javadoc) + * + * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput) + */ + public void writeExternal(ObjectOutput out) throws IOException { + + } + + } + + public void testSetRef() { + MyRemoteStub stub = new MyRemoteStub(); + RemoteRef ref = new MyRemoteRef(); + + MyRemoteStub.setRef(stub, ref); + + assertEquals(ref, stub.getRef()); + } + +} + +class MyRemoteStub extends RemoteStub { + public RemoteRef getRef() { + return ref; + } + + public MyRemoteStub() { + super(); + } + + public MyRemoteStub(RemoteRef ref) { + super(ref); + } + + public static void setRef(RemoteStub stub, RemoteRef ref) { + RemoteStub.setRef(stub, ref); + } +} Index: D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/LogStreamTest.java =================================================================== --- D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/LogStreamTest.java (revision 998619) +++ D:/trunks/Harmony_community/trunk/classlib/modules/rmi/src/test/api/java/org/apache/harmony/rmi/server/LogStreamTest.java (working copy) @@ -18,8 +18,10 @@ package org.apache.harmony.rmi.server; import java.io.ByteArrayOutputStream; +import java.io.OutputStream; import java.io.PrintStream; import java.rmi.server.LogStream; + import junit.framework.TestCase; public class LogStreamTest extends TestCase { @@ -27,13 +29,13 @@ * Test for java.rmi.server.LogStream.write(int b) */ public void testWriteI() throws Exception { - //regression test for HARMONY-1271 - LogStream.log("tst").write((int)'\n'); + // regression test for HARMONY-1271 + LogStream.log("tst").write((int) '\n'); - //regression test for HARMONY-994 + // regression test for HARMONY-994 LogStream.log("tst").write(0); } - + public void testSetOutputStreamBad() throws Exception { // Regression test HARMONY-1198 try { @@ -46,41 +48,54 @@ } /** - * Test for java.rmi.server.LogStream.write(byte[], int, int) - * testing invalid offsets/lengths. + * Test for java.rmi.server.LogStream.write(byte[], int, int) testing + * invalid offsets/lengths. */ public void testWriteArrInvalidOffLen() throws Exception { // Regression test for HARMONY-1691 // list of invalid offsets/lengths pairs - int[][] invalidPairs = new int[][] { - { -2, 1 }, - { 0, -6 }, - { 6, 1 }, - { 0, 6 } }; + int[][] invalidPairs = new int[][] { { -2, 1 }, { 0, -6 }, { 6, 1 }, + { 0, 6 } }; // store original default stream for LogStream PrintStream prevOut = LogStream.getDefaultStream(); try { // set empty default stream to not print garbage to System.out/err - LogStream.setDefaultStream( - new PrintStream(new ByteArrayOutputStream())); + LogStream.setDefaultStream(new PrintStream( + new ByteArrayOutputStream())); LogStream ls = LogStream.log("test"); for (int i = 0; i < invalidPairs.length; ++i) { try { - ls.write(new byte[] { 1, 1 }, - invalidPairs[i][0], invalidPairs[i][1]); + ls.write(new byte[] { 1, 1 }, invalidPairs[i][0], + invalidPairs[i][1]); fail("IndexOutOfBoundsException " + "is not thrown when off = " + invalidPairs[i][0] + ", len = " + invalidPairs[i][1]); } catch (IndexOutOfBoundsException e) { - //expected + // expected } } } finally { // restore original stream LogStream.setDefaultStream(prevOut); } + } + + public void testParseLevel() { + assertEquals(LogStream.parseLevel("BRIEF"), LogStream.BRIEF); + assertEquals(LogStream.parseLevel("SILENT"), LogStream.SILENT); + assertEquals(LogStream.parseLevel("VERBOSE"), LogStream.VERBOSE); + } + + public void testGetOutputStream() { + LogStream log = LogStream.log("abc"); + OutputStream output = log.getOutputStream(); + PrintStream defaultPrintStream = LogStream.getDefaultStream(); + + assertEquals(output.toString(), defaultPrintStream.toString()); + } + }