harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r386087 [24/45] - in /incubator/harmony/enhanced/classlib/trunk: make/ make/patternsets/ modules/jndi/ modules/jndi/META-INF/ modules/jndi/make/ modules/jndi/make/common/ modules/jndi/src/ modules/jndi/src/main/ modules/jndi/src/main/java/ ...
Date Wed, 15 Mar 2006 14:57:17 GMT
Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FileHandlerTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FileHandlerTest.java?rev=386087&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FileHandlerTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FileHandlerTest.java Wed Mar 15 06:55:38 2006
@@ -0,0 +1,655 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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 tests.api.java.util.logging;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FilePermission;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.security.Permission;
+import java.util.Properties;
+import java.util.logging.FileHandler;
+import java.util.logging.Filter;
+import java.util.logging.Formatter;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogManager;
+import java.util.logging.LogRecord;
+import java.util.logging.LoggingPermission;
+import java.util.logging.XMLFormatter;
+
+import junit.framework.TestCase;
+import tests.api.java.util.logging.util.DefaultPropertyHelper;
+import tests.api.java.util.logging.util.EnvironmentHelper;
+
+/**
+ */
+public class FileHandlerTest extends TestCase {
+
+	static LogManager manager;
+
+	final static Properties props = new Properties();
+
+	final static String className = FileHandlerTest.class.getName();
+
+	final static StringWriter writer = new StringWriter();
+
+	final static SecurityManager securityManager = new MockLogSecurityManager();
+
+	final static String HOMEPATH = System.getProperty("user.home");
+
+	final static String TEMPPATH = System.getProperty("java.io.tmpdir");
+
+	final static String SEP = File.separator;
+
+	private static File bakPropFile = null;
+
+	static {
+		try {
+			bakPropFile = DefaultPropertyHelper.init();
+		} catch (Exception e) {
+		}
+		Runtime.getRuntime().addShutdownHook(new Thread() {
+			public void run() {
+				DefaultPropertyHelper.reset(bakPropFile);
+			}
+		});
+	}
+
+	FileHandler handler;
+
+	LogRecord r;
+
+	/*
+	 * @see TestCase#setUp()
+	 */
+	protected void setUp() throws Exception {
+		super.setUp();
+		manager = LogManager.getLogManager();
+		manager.reset();
+		initProps();
+		File file = new File(TEMPPATH + SEP + "log");
+		file.mkdir();
+		manager.readConfiguration(EnvironmentHelper
+				.PropertiesToInputStream(props));
+		handler = new FileHandler();
+		r = new LogRecord(Level.CONFIG, "msg");
+
+	}
+
+	/**
+	 * 
+	 */
+	private void initProps() {
+		props.clear();
+		props.put("java.util.logging.FileHandler.level", "FINE");
+		props.put("java.util.logging.FileHandler.filter", className
+				+ "$MockFilter");
+		props.put("java.util.logging.FileHandler.formatter", className
+				+ "$MockFormatter");
+		props.put("java.util.logging.FileHandler.encoding", "iso-8859-1");
+		// limit to only two message
+		props.put("java.util.logging.FileHandler.limit", "1000");
+		// rotation count is 2
+		props.put("java.util.logging.FileHandler.count", "2");
+		// using append mode
+		props.put("java.util.logging.FileHandler.append", "true");
+		props
+				.put("java.util.logging.FileHandler.pattern",
+						"%t/log/java%u.test");
+	}
+
+	/*
+	 * @see TestCase#tearDown()
+	 */
+	protected void tearDown() throws Exception {
+		if (null != handler) {
+			handler.close();
+		}
+		reset(TEMPPATH + SEP + "log", "");
+		super.tearDown();
+	}
+
+	public void testLock() throws Exception {
+		FileOutputStream output = new FileOutputStream(TEMPPATH + SEP + "log"
+				+ SEP + "java1.test.0");
+		FileHandler h = new FileHandler();
+		h.publish(r);
+		h.close();
+		assertFileContent(TEMPPATH + SEP + "log", "java1.test.0", h
+				.getFormatter());
+		output.close();
+	}
+
+	/*
+	 * Class under test for void FileHandler()
+	 */
+	public void testFileHandler() throws Exception {
+		assertEquals(handler.getEncoding(), "iso-8859-1");
+		assertTrue(handler.getFilter() instanceof MockFilter);
+		assertTrue(handler.getFormatter() instanceof MockFormatter);
+		assertEquals(handler.getLevel(), Level.FINE);
+		assertNotNull(handler.getErrorManager());
+		handler.publish(r);
+		handler.close();
+		// output 3 times, and all records left
+		// append mode is true
+		for (int i = 0; i < 3; i++) {
+			handler = new FileHandler();
+			handler.publish(r);
+			handler.close();
+		}
+		assertFileContent(TEMPPATH + SEP + "log", "java0.test.0",
+				new LogRecord[] { r, null, r, null, r, null, r },
+				new MockFormatter());
+	}
+
+	public void testDefaultValue() throws Exception {
+		handler.publish(r);
+		handler.close();
+		props.clear();
+		manager.readConfiguration(EnvironmentHelper
+				.PropertiesToInputStream(props));
+		handler = new FileHandler();
+		assertNull(handler.getEncoding());
+		assertNull(handler.getFilter());
+		assertTrue(handler.getFormatter() instanceof XMLFormatter);
+		assertEquals(handler.getLevel(), Level.ALL);
+		assertNotNull(handler.getErrorManager());
+		handler.publish(r);
+		handler.close();
+		// output 3 times, and only one record left
+		// default append mode is false
+		for (int i = 0; i < 3; i++) {
+			handler = new FileHandler();
+			handler.publish(r);
+			handler.close();
+		}
+		assertFileContent(HOMEPATH, "java0.log", new XMLFormatter());
+	}
+
+	private void assertFileContent(String homepath, String filename,
+			Formatter formatter) throws Exception {
+		assertFileContent(homepath, filename, new LogRecord[] { r }, formatter);
+	}
+
+	private void assertFileContent(String homepath, String filename,
+			LogRecord[] lr, Formatter formatter) throws Exception {
+		handler.close();
+		String msg = "";
+		// if formatter is null, the file content should be empty
+		// else the message should be formatted given records
+		if (null != formatter) {
+			StringBuffer sb = new StringBuffer();
+			sb.append(formatter.getHead(handler));
+			for (int i = 0; i < lr.length; i++) {
+				if (null == lr[i] && i < lr.length - 1) {
+					// if one record is null and is not the last record, means
+					// here is
+					// output completion point, should output tail, then output
+					// head
+					// (ready for next output)
+					sb.append(formatter.getTail(handler));
+					sb.append(formatter.getHead(handler));
+				} else {
+					sb.append(formatter.format(lr[i]));
+				}
+			}
+			sb.append(formatter.getTail(handler));
+			msg = sb.toString();
+		}
+		char[] chars = new char[msg.length()];
+		Reader reader = null;
+		try {
+			reader = new BufferedReader(new FileReader(homepath + SEP
+					+ filename));
+			reader.read(chars);
+			// System.out.println(new String(chars));
+			assertEquals(msg, new String(chars));
+			// assert has reached the end of the file
+			assertEquals(reader.read(), -1);
+		} finally {
+			try {
+				reader.close();
+				reader = null;
+			} catch (Exception e) {
+			}
+			reset(homepath, filename);
+		}
+	}
+
+	/**
+	 * @param homepath
+	 * @param filename
+	 */
+	private void reset(String homepath, String filename) {
+		File file = null;
+		try {
+			file = new File(homepath + SEP + filename);
+			if (file.isFile()) {
+				file.delete();
+			} else if (file.isDirectory()) {
+				File[] files = file.listFiles();
+				for (int i = 0; i < files.length; i++) {
+					files[i].delete();
+				}
+				file.delete();
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		try {
+			file = new File(homepath + SEP + filename + ".lck");
+			file.delete();
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+	}
+
+	public void testLimitAndCount() throws Exception {
+		handler.close();
+		// very small limit value, count=2
+		// output, rename current output file to the second generation file
+		// close it and open a new file as rotation output
+		handler = new FileHandler("%t/testLimitCount%g", 1, 2, false);
+		handler.publish(r);
+		handler.close();
+		assertFileContent(TEMPPATH, "testLimitCount1", handler.getFormatter());
+
+		// very small limit value, count=1
+		// output once, rotate(equals to nothing output)
+		handler = new FileHandler("%t/testLimitCount%g", 1, 1, false);
+		handler.publish(r);
+		handler.close();
+		assertFileContent(TEMPPATH, "testLimitCount0", new LogRecord[0],
+				handler.getFormatter());
+
+		// normal case, limit is 60(>2*msg length <3*msg length), append is
+		// false
+		handler = new FileHandler("%t/testLimitCount%u", 60, 3, false);
+		LogRecord[] rs = new LogRecord[10];
+		// batch output twice to test the append mode
+		for (int i = 0; i < 5; i++) {
+			rs[i] = new LogRecord(Level.SEVERE, "msg" + i);
+			handler.publish(rs[i]);
+		}
+		handler.close();
+		handler = new FileHandler("%t/testLimitCount%u", 60, 3, false);
+		for (int i = 5; i < 10; i++) {
+			rs[i] = new LogRecord(Level.SEVERE, "msg" + i);
+			handler.publish(rs[i]);
+		}
+
+		assertFileContent(TEMPPATH, "testLimitCount0.1", new LogRecord[] {
+				rs[5], rs[6], rs[7] }, handler.getFormatter());
+		assertFileContent(TEMPPATH, "testLimitCount0.0", new LogRecord[] {
+				rs[8], rs[9] }, handler.getFormatter());
+
+		// normal case, limit is 60(>2*msg length <3*msg length), append is true
+		handler = new FileHandler("%t/testLimitCount%u", 60, 3, false);
+		// batch output twice to test the append mode
+		for (int i = 0; i < 5; i++) {
+			rs[i] = new LogRecord(Level.SEVERE, "msg" + i);
+			handler.publish(rs[i]);
+		}
+		handler.close();
+		handler = new FileHandler("%t/testLimitCount%u", 60, 3, true);
+		for (int i = 5; i < 10; i++) {
+			rs[i] = new LogRecord(Level.SEVERE, "msg" + i);
+			handler.publish(rs[i]);
+		}
+		handler.close();
+		assertFileContent(TEMPPATH, "testLimitCount0.2", new LogRecord[] {
+				rs[3], rs[4], null, rs[5] }, handler.getFormatter());
+		assertFileContent(TEMPPATH, "testLimitCount0.1", new LogRecord[] {
+				rs[6], rs[7], rs[8] }, handler.getFormatter());
+		assertFileContent(TEMPPATH, "testLimitCount0.0",
+				new LogRecord[] { rs[9] }, handler.getFormatter());
+	}
+
+	public void testSecurity() throws IOException {
+		SecurityManager currentManager = System.getSecurityManager();
+
+		try {
+			System.setSecurityManager(new MockLogSecurityManager());
+			try {
+				handler.close();
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+
+			handler.publish(new LogRecord(Level.SEVERE, "msg"));
+			try {
+				handler = new FileHandler();
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+
+			try {
+				handler = new FileHandler("pattern1");
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+			try {
+				handler = new FileHandler("pattern2", true);
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+			try {
+				handler = new FileHandler("pattern3", 1000, 1);
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+			try {
+				handler = new FileHandler("pattern4", 1000, 1, true);
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+		} finally {
+			System.setSecurityManager(currentManager);
+		}
+
+	}
+
+	public void testFileSecurity() throws IOException {
+		SecurityManager currentManager = System.getSecurityManager();
+
+		try {
+			System.setSecurityManager(new MockFileSecurityManager());
+			handler.publish(new LogRecord(Level.SEVERE, "msg"));
+			handler.close();
+			try {
+				handler = new FileHandler();
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+
+			try {
+				handler = new FileHandler("pattern1");
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+			try {
+				handler = new FileHandler("pattern2", true);
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+			try {
+				handler = new FileHandler("pattern3", 1000, 1);
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+			try {
+				handler = new FileHandler("pattern4", 1000, 1, true);
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+		} finally {
+			System.setSecurityManager(currentManager);
+		}
+	}
+
+	public void testInvalidProperty() throws Exception {
+		props.put("java.util.logging.FileHandler.level", "null");
+		props.put("java.util.logging.FileHandler.filter", className
+				+ "$MockFilte");
+		props.put("java.util.logging.FileHandler.formatter", className
+				+ "$MockFormatte");
+		props.put("java.util.logging.FileHandler.encoding", "ut");
+		// limit to only two message
+		props.put("java.util.logging.FileHandler.limit", "-1");
+		// rotation count is 2
+		props.put("java.util.logging.FileHandler.count", "-1");
+		// using append mode
+		props.put("java.util.logging.FileHandler.append", "bad");
+
+		handler.close();
+
+		manager.readConfiguration(EnvironmentHelper
+				.PropertiesToInputStream(props));
+		handler = new FileHandler();
+		assertEquals(Level.ALL, handler.getLevel());
+		assertEquals(null, handler.getFilter());
+		assertTrue(handler.getFormatter() instanceof XMLFormatter);
+		assertNull(handler.getEncoding());
+		handler.close();
+
+		props.put("java.util.logging.FileHandler.pattern", "");
+		manager.readConfiguration(EnvironmentHelper
+				.PropertiesToInputStream(props));
+		try {
+			handler = new FileHandler();
+			fail("shouldn't open file with empty name");
+		} catch (NullPointerException e) {
+		}
+	}
+
+	public void testInvalidParams() throws IOException {
+
+		// %t and %p parsing can add file separator automatically
+		FileHandler h1 = new FileHandler("%taaa");
+		h1.close();
+		File file = new File(TEMPPATH + SEP + "aaa");
+		assertTrue(file.exists());
+		reset(TEMPPATH, "aaa");
+
+		// always parse special pattern
+		try {
+			h1 = new FileHandler("%t/%h");
+			fail("should throw null exception");
+		} catch (FileNotFoundException e) {
+		}
+		h1 = new FileHandler("%t%g");
+		h1.close();
+		file = new File(TEMPPATH + SEP + "0");
+		assertTrue(file.exists());
+		reset(TEMPPATH, "0");
+		h1 = new FileHandler("%t%u%g");
+		h1.close();
+		file = new File(TEMPPATH + SEP + "00");
+		assertTrue(file.exists());
+		reset(TEMPPATH, "00");
+
+		// this is normal case
+		h1 = new FileHandler("%t/%u%g%%g");
+		h1.close();
+		file = new File(TEMPPATH + SEP + "00%g");
+		assertTrue(file.exists());
+		reset(TEMPPATH, "00%g");
+
+		// multi separator has no effect
+		h1 = new FileHandler("//%t//multi%g");
+		h1.close();
+		file = new File(TEMPPATH + SEP + "multi0");
+		assertTrue(file.exists());
+		reset(TEMPPATH, "multi0");
+
+		// bad directory, IOException
+		try {
+			h1 = new FileHandler("%t/baddir/multi%g");
+			fail("should throw IO exception");
+		} catch (IOException e) {
+		}
+		file = new File(TEMPPATH + SEP + "baddir" + SEP + "multi0");
+		assertFalse(file.exists());
+
+		try {
+			FileHandler h = new FileHandler(null);
+			fail("should throw null exception");
+		} catch (NullPointerException e) {
+		}
+		try {
+			handler.publish(null);
+			fail("should throw null exception");
+		} catch (NullPointerException e) {
+		}
+		try {
+			FileHandler h = new FileHandler(null, false);
+			fail("should throw null exception");
+		} catch (NullPointerException e) {
+		}
+		try {
+			FileHandler h = new FileHandler("");
+			fail("should throw null exception");
+		} catch (NullPointerException e) {
+		}
+		try {
+			FileHandler h = new FileHandler("%t/java%u", 0, 0);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			FileHandler h = new FileHandler("%t/java%u", -1, 1);
+			fail("should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+	}
+
+	// set output stream still works, just like super StreamHandler
+	public void testSetOutputStream() throws Exception {
+		MockFileHandler handler = new MockFileHandler("%h/setoutput.log");
+		handler.setFormatter(new MockFormatter());
+		handler.publish(r);
+
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		handler.publicSetOutputStream(out);
+		handler.publish(r);
+		handler.close();
+		String msg = new String(out.toByteArray());
+		Formatter f = handler.getFormatter();
+		assertEquals(msg, f.getHead(handler) + f.format(r) + f.getTail(handler));
+		assertFileContent(HOMEPATH, "setoutput.log", handler.getFormatter());
+	}
+
+	/*
+	 * Class under test for void FileHandler(String)
+	 */
+	public void testFileHandlerString() throws Exception {
+		// test if unique ids not specified, it will append at the end
+		// no generation number is used
+		FileHandler h = new FileHandler("%t/log/string");
+		FileHandler h2 = new FileHandler("%t/log/string");
+		FileHandler h3 = new FileHandler("%t/log/string");
+		FileHandler h4 = new FileHandler("%t/log/string");
+		h.publish(r);
+		h2.publish(r);
+		h3.publish(r);
+		h4.publish(r);
+		h.close();
+		h2.close();
+		h3.close();
+		h4.close();
+		assertFileContent(TEMPPATH + SEP + "log", "string", h.getFormatter());
+		assertFileContent(TEMPPATH + SEP + "log", "string.1", h.getFormatter());
+		assertFileContent(TEMPPATH + SEP + "log", "string.2", h.getFormatter());
+		assertFileContent(TEMPPATH + SEP + "log", "string.3", h.getFormatter());
+
+		// default is append mode
+		FileHandler h6 = new FileHandler("%t/log/string%u.log");
+		h6.publish(r);
+		h6.close();
+		FileHandler h7 = new FileHandler("%t/log/string%u.log");
+		h7.publish(r);
+		h7.close();
+		try {
+			assertFileContent(TEMPPATH + SEP + "log", "string0.log", h
+					.getFormatter());
+			fail("should assertion failed");
+		} catch (Error e) {
+		}
+		File file = new File(TEMPPATH + SEP + "log");
+		assertTrue(file.list().length <= 2);
+
+		// test unique ids
+		FileHandler h8 = new FileHandler("%t/log/%ustring%u.log");
+		h8.publish(r);
+		FileHandler h9 = new FileHandler("%t/log/%ustring%u.log");
+		h9.publish(r);
+		h9.close();
+		h8.close();
+		assertFileContent(TEMPPATH + SEP + "log", "0string0.log", h
+				.getFormatter());
+		assertFileContent(TEMPPATH + SEP + "log", "1string1.log", h
+				.getFormatter());
+		file = new File(TEMPPATH + SEP + "log");
+		assertTrue(file.list().length <= 2);
+	}
+
+	/*
+	 * mock classes
+	 */
+	public static class MockFilter implements Filter {
+		public boolean isLoggable(LogRecord record) {
+			return !record.getMessage().equals("false");
+		}
+	}
+
+	public static class MockFormatter extends Formatter {
+		public String format(LogRecord r) {
+			if (null == r) {
+				return "";
+			}
+			return r.getMessage() + " by MockFormatter\n";
+		}
+
+		public String getTail(Handler h) {
+			return "tail\n";
+		}
+
+		public String getHead(Handler h) {
+			return "head\n";
+		}
+	}
+
+	public static class MockLogSecurityManager extends SecurityManager {
+		public void checkPermission(Permission perm) {
+			if (perm instanceof LoggingPermission) {
+				throw new SecurityException();
+			}
+			return;
+		}
+	}
+
+	public static class MockFileSecurityManager extends SecurityManager {
+		public void checkPermission(Permission perm) {
+			if (perm instanceof FilePermission) {
+				throw new SecurityException();
+			}
+		}
+	}
+
+	public static class MockFileHandler extends FileHandler {
+		public MockFileHandler() throws IOException {
+			super();
+		}
+
+		public MockFileHandler(String pattern) throws IOException {
+			super(pattern);
+		}
+
+		public void publicSetOutputStream(OutputStream stream) {
+			super.setOutputStream(stream);
+		}
+	}
+
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FilterTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FilterTest.java?rev=386087&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FilterTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FilterTest.java Wed Mar 15 06:55:38 2006
@@ -0,0 +1,44 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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 tests.api.java.util.logging;
+
+import java.util.logging.Filter;
+import java.util.logging.LogRecord;
+
+import junit.framework.TestCase;
+
+/**
+ * This testcase verifies the signature of the interface Filter.
+ * 
+ */
+public class FilterTest extends TestCase {
+	private static String className = FilterTest.class.getName();
+
+	public void testFilter() {
+		MockFilter f = new MockFilter();
+		f.isLoggable(null);
+	}
+
+	/*
+	 * This inner class implements the interface Filter to verify the signature.
+	 */
+	private class MockFilter implements Filter {
+
+		public boolean isLoggable(LogRecord record) {
+			return false;
+		}
+	}
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FormatterTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FormatterTest.java?rev=386087&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FormatterTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/FormatterTest.java Wed Mar 15 06:55:38 2006
@@ -0,0 +1,154 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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 tests.api.java.util.logging;
+
+import java.text.MessageFormat;
+import java.util.Locale;
+import java.util.ResourceBundle;
+import java.util.logging.Formatter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+import junit.framework.TestCase;
+
+/**
+ * 
+ */
+public class FormatterTest extends TestCase {
+	private static String className = FormatterTest.class.getName();
+
+	Formatter f;
+
+	LogRecord r;
+
+	static String MSG = "msg, pls. ignore it";
+
+	/*
+	 * @see TestCase#setUp()
+	 */
+	protected void setUp() throws Exception {
+		super.setUp();
+		f = new MockFormatter();
+		r = new LogRecord(Level.FINE, MSG);
+	}
+
+	/*
+	 * @see TestCase#tearDown()
+	 */
+	protected void tearDown() throws Exception {
+		super.tearDown();
+	}
+
+	/**
+	 * Constructor for FormatterTest.
+	 * 
+	 * @param arg0
+	 */
+	public FormatterTest(String arg0) {
+		super(arg0);
+	}
+
+	public void testFormat() {
+		assertEquals("format", f.format(r));
+	}
+
+	public void testGetHead() {
+		assertEquals("", f.getHead(null));
+	}
+
+	public void testGetTail() {
+		assertEquals("", f.getTail(null));
+	}
+
+	public void testFormatMessage() {
+		assertEquals(MSG, f.formatMessage(r));
+
+		String pattern = "test formatter {0, number}";
+		r.setMessage(pattern);
+		assertEquals(pattern, f.formatMessage(r));
+
+		Object[] oa = new Object[0];
+		r.setParameters(oa);
+		assertEquals(pattern, f.formatMessage(r));
+
+		oa = new Object[] { new Integer(100), new Float(1.1) };
+		r.setParameters(oa);
+		assertEquals(MessageFormat.format(pattern, oa), f.formatMessage(r));
+
+		r.setMessage(MSG);
+		assertEquals(MSG, f.formatMessage(r));
+
+		pattern = "wrong pattern {0, asdfasfd}";
+		r.setMessage(pattern);
+		assertEquals(pattern, f.formatMessage(r));
+
+		pattern = "pattern without 0 {1, number}";
+		r.setMessage(pattern);
+		assertEquals(pattern, f.formatMessage(r));
+
+		pattern = null;
+		r.setMessage(pattern);
+		assertNull(f.formatMessage(r));
+	}
+
+	public void testLocalizedFormatMessage() {
+		// normal case
+		r.setMessage("msg");
+		ResourceBundle rb = ResourceBundle
+				.getBundle("tests/api/java/util/logging/res");
+		r.setResourceBundle(rb);
+		assertEquals(rb.getString("msg"), f.formatMessage(r));
+
+		// local message is a pattern
+		r.setMessage("pattern");
+		Object[] oa = new Object[] { new Integer(3) };
+		r.setParameters(oa);
+		assertEquals(MessageFormat.format(rb.getString("pattern"), oa), f
+				.formatMessage(r));
+
+		// key is a pattern, but local message is not
+		r.setMessage("pattern{0,number}");
+		oa = new Object[] { new Integer(3) };
+		r.setParameters(oa);
+		assertEquals(rb.getString("pattern{0,number}"), f.formatMessage(r));
+
+		// another bundle
+		rb = ResourceBundle.getBundle("tests/api/java/util/logging/res",
+				Locale.US);
+		r.setMessage("msg");
+		r.setResourceBundle(rb);
+		assertEquals(rb.getString("msg"), f.formatMessage(r));
+
+		// cannot find local message in bundle
+		r.setMessage("msg without locale");
+		assertEquals("msg without locale", f.formatMessage(r));
+
+		// set bundle name but not bundle
+		r.setResourceBundle(null);
+		r.setResourceBundleName("tests/api/java/util/logging/res");
+		r.setMessage("msg");
+		assertEquals("msg", f.formatMessage(r));
+	}
+
+	public static class MockFormatter extends Formatter {
+
+		public String format(LogRecord arg0) {
+			return "format";
+		}
+
+	}
+
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/HandlerTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/HandlerTest.java?rev=386087&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/HandlerTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/HandlerTest.java Wed Mar 15 06:55:38 2006
@@ -0,0 +1,581 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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 tests.api.java.util.logging;
+
+import java.io.UnsupportedEncodingException;
+import java.security.Permission;
+import java.util.Properties;
+import java.util.logging.ErrorManager;
+import java.util.logging.Filter;
+import java.util.logging.Formatter;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogManager;
+import java.util.logging.LogRecord;
+import java.util.logging.LoggingPermission;
+import java.util.logging.SimpleFormatter;
+
+import junit.framework.TestCase;
+import tests.api.java.util.logging.util.EnvironmentHelper;
+import tests.util.CallVerificationStack;
+
+/**
+ * Test suite for the class java.util.logging.Handler.
+ * 
+ */
+public class HandlerTest extends TestCase {
+	private static String className = HandlerTest.class.getName();
+
+	/*
+	 * @see TestCase#setUp()
+	 */
+	protected void setUp() throws Exception {
+		super.setUp();
+	}
+
+	/*
+	 * @see TestCase#tearDown()
+	 */
+	protected void tearDown() throws Exception {
+		super.tearDown();
+	}
+
+	/**
+	 * Constructor for HandlerTest.
+	 * 
+	 * @param arg0
+	 */
+	public HandlerTest(String arg0) {
+		super(arg0);
+	}
+
+	/*
+	 * Test the constructor.
+	 */
+	public void testConstructor() {
+		MockHandler h = new MockHandler();
+		assertSame(h.getLevel(), Level.ALL);
+		assertNull(h.getFormatter());
+		assertNull(h.getFilter());
+		assertNull(h.getEncoding());
+		assertTrue(h.getErrorManager() instanceof ErrorManager);
+	}
+
+	/*
+	 * Test the constructor, with properties set
+	 */
+	public void testConstructor_Properties() throws Exception {
+		Properties p = new Properties();
+		p.put("java.util.logging.MockHandler.level", "FINE");
+		p
+				.put("java.util.logging.MockHandler.filter", className
+						+ "$MockFilter");
+		p.put("java.util.logging.Handler.formatter", className
+				+ "$MockFormatter");
+		p.put("java.util.logging.MockHandler.encoding", "utf-8");
+		LogManager.getLogManager().readConfiguration(
+				EnvironmentHelper.PropertiesToInputStream(p));
+
+		assertEquals(LogManager.getLogManager().getProperty(
+				"java.util.logging.MockHandler.level"), "FINE");
+		assertEquals(LogManager.getLogManager().getProperty(
+				"java.util.logging.MockHandler.encoding"), "utf-8");
+		MockHandler h = new MockHandler();
+		assertSame(h.getLevel(), Level.ALL);
+		assertNull(h.getFormatter());
+		assertNull(h.getFilter());
+		assertNull(h.getEncoding());
+		assertTrue(h.getErrorManager() instanceof ErrorManager);
+		LogManager.getLogManager().reset();
+	}
+
+	/*
+	 * Abstract method, no test needed.
+	 */
+	public void testClose() {
+		MockHandler h = new MockHandler();
+		h.close();
+	}
+
+	/*
+	 * Abstract method, no test needed.
+	 */
+	public void testFlush() {
+		MockHandler h = new MockHandler();
+		h.flush();
+	}
+
+	/*
+	 * Abstract method, no test needed.
+	 */
+	public void testPublish() {
+		MockHandler h = new MockHandler();
+		h.publish(null);
+	}
+
+	/*
+	 * Test getEncoding & setEncoding methods with supported encoding.
+	 */
+	public void testGetSetEncoding_Normal() throws Exception {
+		MockHandler h = new MockHandler();
+		h.setEncoding("iso-8859-1");
+		assertEquals("iso-8859-1", h.getEncoding());
+	}
+
+	/*
+	 * Test getEncoding & setEncoding methods with null.
+	 */
+	public void testGetSetEncoding_Null() throws Exception {
+		MockHandler h = new MockHandler();
+		h.setEncoding(null);
+		assertNull(h.getEncoding());
+	}
+
+	/*
+	 * Test getEncoding & setEncoding methods with unsupported encoding.
+	 */
+	public void testGetSetEncoding_Unsupported() {
+		MockHandler h = new MockHandler();
+		try {
+			h.setEncoding("impossible");
+			fail("Should throw UnsupportedEncodingException!");
+		} catch (UnsupportedEncodingException e) {
+		}
+		assertNull(h.getEncoding());
+	}
+
+	/*
+	 * Test setEncoding with insufficient privilege.
+	 */
+	public void testSetEncoding_InsufficientPrivilege() throws Exception {
+		MockHandler h = new MockHandler();
+		SecurityManager oldMan = System.getSecurityManager();
+		System.setSecurityManager(new MockSecurityManager());
+		// set a normal value
+		try {
+			h.setEncoding("iso-8859-1");
+			fail("Should throw SecurityException!");
+		} catch (SecurityException e) {
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+		assertNull(h.getEncoding());
+		System.setSecurityManager(new MockSecurityManager());
+		// set an invalid value
+		try {
+
+			h.setEncoding("impossible");
+			fail("Should throw SecurityException!");
+		} catch (SecurityException e) {
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+		assertNull(h.getEncoding());
+	}
+
+	/*
+	 * Test getErrorManager & setErrorManager methods with non-null value.
+	 */
+	public void testGetSetErrorManager_Normal() throws Exception {
+		MockHandler h = new MockHandler();
+		ErrorManager man = new ErrorManager();
+		h.setErrorManager(man);
+		assertSame(man, h.getErrorManager());
+	}
+
+	/*
+	 * Test getErrorManager & setErrorManager methods with null.
+	 */
+	public void testGetSetErrorManager_Null() throws Exception {
+		MockHandler h = new MockHandler();
+		// test set null
+		try {
+			h.setErrorManager(null);
+			fail("Should throw NullPointerException!");
+		} catch (NullPointerException e) {
+		}
+
+		// test reset null
+		try {
+			h.setErrorManager(new ErrorManager());
+			h.setErrorManager(null);
+			fail("Should throw NullPointerException!");
+		} catch (NullPointerException e) {
+		}
+	}
+
+	/*
+	 * Test getErrorManager with insufficient privilege.
+	 */
+	public void testGetErrorManager_InsufficientPrivilege() throws Exception {
+		MockHandler h = new MockHandler();
+		SecurityManager oldMan = System.getSecurityManager();
+		System.setSecurityManager(new MockSecurityManager());
+
+		try {
+			h.getErrorManager();
+			fail("Should throw SecurityException!");
+		} catch (SecurityException e) {
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+	}
+
+	/*
+	 * Test setErrorManager with insufficient privilege.
+	 */
+	public void testSetErrorManager_InsufficientPrivilege() throws Exception {
+		MockHandler h = new MockHandler();
+		SecurityManager oldMan = System.getSecurityManager();
+		System.setSecurityManager(new MockSecurityManager());
+
+		// set null
+		try {
+
+			h.setErrorManager(null);
+			fail("Should throw SecurityException!");
+		} catch (SecurityException e) {
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+		// set a normal value
+		System.setSecurityManager(new MockSecurityManager());
+		try {
+
+			h.setErrorManager(new ErrorManager());
+			fail("Should throw SecurityException!");
+		} catch (SecurityException e) {
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+	}
+
+	/*
+	 * Test getFilter & setFilter methods with non-null value.
+	 */
+	public void testGetSetFilter_Normal() throws Exception {
+		MockHandler h = new MockHandler();
+		Filter f = new MockFilter();
+		h.setFilter(f);
+		assertSame(f, h.getFilter());
+	}
+
+	/*
+	 * Test getFilter & setFilter methods with null.
+	 */
+	public void testGetSetFilter_Null() throws Exception {
+		MockHandler h = new MockHandler();
+		// test set null
+		h.setFilter(null);
+
+		// test reset null
+		h.setFilter(new MockFilter());
+		h.setFilter(null);
+	}
+
+	/*
+	 * Test setFilter with insufficient privilege.
+	 */
+	public void testSetFilter_InsufficientPrivilege() throws Exception {
+		MockHandler h = new MockHandler();
+		SecurityManager oldMan = System.getSecurityManager();
+		System.setSecurityManager(new MockSecurityManager());
+
+		// set null
+		try {
+
+			h.setFilter(null);
+			fail("Should throw SecurityException!");
+		} catch (SecurityException e) {
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+		// set a normal value
+		System.setSecurityManager(new MockSecurityManager());
+		try {
+
+			h.setFilter(new MockFilter());
+			fail("Should throw SecurityException!");
+		} catch (SecurityException e) {
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+	}
+
+	/*
+	 * Test getFormatter & setFormatter methods with non-null value.
+	 */
+	public void testGetSetFormatter_Normal() throws Exception {
+		MockHandler h = new MockHandler();
+		Formatter f = new SimpleFormatter();
+		h.setFormatter(f);
+		assertSame(f, h.getFormatter());
+	}
+
+	/*
+	 * Test getFormatter & setFormatter methods with null.
+	 */
+	public void testGetSetFormatter_Null() throws Exception {
+		MockHandler h = new MockHandler();
+		// test set null
+		try {
+			h.setFormatter(null);
+			fail("Should throw NullPointerException!");
+		} catch (NullPointerException e) {
+		}
+
+		// test reset null
+		try {
+			h.setFormatter(new SimpleFormatter());
+			h.setFormatter(null);
+			fail("Should throw NullPointerException!");
+		} catch (NullPointerException e) {
+		}
+	}
+
+	/*
+	 * Test setFormatter with insufficient privilege.
+	 */
+	public void testSetFormatter_InsufficientPrivilege() throws Exception {
+		MockHandler h = new MockHandler();
+		SecurityManager oldMan = System.getSecurityManager();
+		System.setSecurityManager(new MockSecurityManager());
+
+		// set null
+		try {
+
+			h.setFormatter(null);
+			fail("Should throw SecurityException!");
+		} catch (SecurityException e) {
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+		// set a normal value
+		System.setSecurityManager(new MockSecurityManager());
+		try {
+
+			h.setFormatter(new SimpleFormatter());
+			fail("Should throw SecurityException!");
+		} catch (SecurityException e) {
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+	}
+
+	/*
+	 * Test getLevel & setLevel methods with non-null value.
+	 */
+	public void testGetSetLevel_Normal() throws Exception {
+		MockHandler h = new MockHandler();
+		Level f = Level.CONFIG;
+		h.setLevel(f);
+		assertSame(f, h.getLevel());
+	}
+
+	/*
+	 * Test getLevel & setLevel methods with null.
+	 */
+	public void testGetSetLevel_Null() throws Exception {
+		MockHandler h = new MockHandler();
+		// test set null
+		try {
+			h.setLevel(null);
+			fail("Should throw NullPointerException!");
+		} catch (NullPointerException e) {
+		}
+
+		// test reset null
+		try {
+			h.setLevel(Level.CONFIG);
+			h.setLevel(null);
+			fail("Should throw NullPointerException!");
+		} catch (NullPointerException e) {
+		}
+	}
+
+	/*
+	 * Test setLevel with insufficient privilege.
+	 */
+	public void testSetLevel_InsufficientPrivilege() throws Exception {
+		MockHandler h = new MockHandler();
+		SecurityManager oldMan = System.getSecurityManager();
+		System.setSecurityManager(new MockSecurityManager());
+
+		// set null
+		try {
+
+			h.setLevel(null);
+			fail("Should throw NullPointerException!");
+		} catch (NullPointerException e) {
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+		// set a normal value
+		System.setSecurityManager(new MockSecurityManager());
+		try {
+
+			h.setLevel(Level.CONFIG);
+			fail("Should throw SecurityException!");
+		} catch (SecurityException e) {
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+	}
+
+	/*
+	 * Use no filter
+	 */
+	public void testIsLoggable_NoFilter() {
+		MockHandler h = new MockHandler();
+		LogRecord r = new LogRecord(Level.CONFIG, null);
+		assertTrue(h.isLoggable(r));
+
+		h.setLevel(Level.CONFIG);
+		assertTrue(h.isLoggable(r));
+
+		h.setLevel(Level.SEVERE);
+		assertFalse(h.isLoggable(r));
+
+		r.setLevel(Level.OFF);
+		h.setLevel(Level.OFF);
+		assertFalse(h.isLoggable(r));
+	}
+
+	/*
+	 * Use a filter
+	 */
+	public void testIsLoggable_WithFilter() {
+		MockHandler h = new MockHandler();
+		LogRecord r = new LogRecord(Level.CONFIG, null);
+		h.setFilter(new MockFilter());
+		assertFalse(h.isLoggable(r));
+
+		h.setLevel(Level.CONFIG);
+		assertFalse(h.isLoggable(r));
+		assertSame(r, CallVerificationStack.getInstance().pop());
+
+		h.setLevel(Level.SEVERE);
+		assertFalse(h.isLoggable(r));
+		assertSame(r, CallVerificationStack.getInstance().pop());
+	}
+
+	/*
+	 * Null log record, handler should call ErrorManager to handle
+	 * exceptional case
+	 */
+	public void testIsLoggable_Null() {
+		MockHandler h = new MockHandler();
+		h.isLoggable(null);
+	}
+
+	/*
+	 * Test whether the error manager is actually called with expected
+	 * parameters.
+	 */
+	public void testReportError() {
+		MockHandler h = new MockHandler();
+		h.setErrorManager(new MockErrorManager());
+		SecurityManager oldMan = System.getSecurityManager();
+		System.setSecurityManager(new MockSecurityManager());
+
+		try {
+			Exception ex = new Exception("test exception");
+			// with non-null parameters
+			h.reportError("test msg", ex, -1);
+			assertEquals(-1, CallVerificationStack.getInstance().popInt());
+			assertSame(ex, CallVerificationStack.getInstance().pop());
+			assertEquals("test msg", CallVerificationStack.getInstance().pop());
+			// with null parameters
+			h.reportError(null, null, 0);
+			assertEquals(0, CallVerificationStack.getInstance().popInt());
+			assertSame(null, CallVerificationStack.getInstance().pop());
+			assertEquals(null, CallVerificationStack.getInstance().pop());
+		} catch (SecurityException e) {
+			fail("Should not throw SecurityException!");
+		} finally {
+			System.setSecurityManager(oldMan);
+		}
+	}
+
+	/*
+	 * Used to enable the testing of Handler because Handler is an abstract
+	 * class.
+	 */
+	public static class MockHandler extends Handler {
+
+		public void close() {
+		}
+
+		public void flush() {
+		}
+
+		public void publish(LogRecord record) {
+		}
+
+		public void reportError(String msg, Exception ex, int code) {
+			super.reportError(msg, ex, code);
+		}
+	}
+
+	/*
+	 * Used to grant all permissions except logging control.
+	 */
+	public static class MockSecurityManager extends SecurityManager {
+
+		public MockSecurityManager() {
+		}
+
+		public void checkPermission(Permission perm) {
+			// grant all permissions except logging control
+			if (perm instanceof LoggingPermission) {
+				throw new SecurityException();
+			}
+		}
+
+		public void checkPermission(Permission perm, Object context) {
+			// grant all permissions except logging control
+			if (perm instanceof LoggingPermission) {
+				throw new SecurityException();
+			}
+		}
+	}
+
+	/*
+	 * A mock filter, always return false.
+	 */
+	public static class MockFilter implements Filter {
+
+		public boolean isLoggable(LogRecord record) {
+			CallVerificationStack.getInstance().push(record);
+			return false;
+		}
+	}
+
+	/*
+	 * A mock error manager, used to validate the expected method is called with
+	 * the expected parameters.
+	 */
+	public static class MockErrorManager extends ErrorManager {
+
+		public void error(String msg, Exception ex, int errorCode) {
+			CallVerificationStack.getInstance().push(msg);
+			CallVerificationStack.getInstance().push(ex);
+			CallVerificationStack.getInstance().push(errorCode);
+		}
+
+	}
+
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/Level.ser
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/Level.ser?rev=386087&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/Level.ser
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/LevelTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/LevelTest.java?rev=386087&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/LevelTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/LevelTest.java Wed Mar 15 06:55:38 2006
@@ -0,0 +1,408 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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 tests.api.java.util.logging;
+
+import java.io.Serializable;
+import java.util.Locale;
+import java.util.ResourceBundle;
+import java.util.logging.Level;
+
+import junit.framework.TestCase;
+import tests.api.java.util.logging.util.ProjectVersion;
+import tests.util.SerializationTester;
+
+/**
+ * Test the class java.util.logging.Level.
+ * 
+ */
+public class LevelTest extends TestCase implements Serializable {
+
+	private static final long serialVersionUID = 1L;
+
+	private static String className = LevelTest.class.getName();
+
+	transient ResourceBundle rb;
+
+	public LevelTest() {
+		System.out.println(ProjectVersion.IMPLEMENTATION);
+	}
+
+	/*
+	 * @see TestCase#setUp()
+	 */
+	protected void setUp() throws Exception {
+		super.setUp();
+		System.err.println(Locale.getDefault().toString());
+		rb = ResourceBundle.getBundle("tests/api/java/util/logging/res");
+	}
+
+	/*
+	 * @see TestCase#tearDown()
+	 */
+	protected void tearDown() throws Exception {
+		super.tearDown();
+	}
+
+	/*
+	 * Test the constructor without resource bundle parameter using normal
+	 * values. As byproducts, getName & intValue are also tested.
+	 */
+	public void testConstructorNoResBundle_Normal() {
+		MockLevel l = new MockLevel("level1", 1);
+		assertEquals(l.getName(), "level1");
+		assertEquals(l.intValue(), 1);
+		assertNull(l.getResourceBundleName());
+	}
+
+	/*
+	 * Test the constructor without resource bundle parameter using null name.
+	 * As byproducts, getName & intValue are also tested.
+	 */
+	public void testConstructorNoResBundle_NullName() {
+		MockLevel l = new MockLevel(null, -2);
+		assertEquals(l.getName(), null);
+		assertEquals(l.intValue(), -2);
+		assertNull(l.getResourceBundleName());
+	}
+
+	/*
+	 * Test the constructor without resource bundle parameter using empty name.
+	 * As byproducts, getName & intValue are also tested.
+	 */
+	// public void testConstructorNoResBundle_EmptyName() {
+	// MockLevel l = new MockLevel("", -3);
+	// assertEquals(l.getName(), "");
+	// assertEquals(l.intValue(), -3);
+	// assertNull(l.getResourceBundleName());
+	// }
+	/*
+	 * Test the constructor having resource bundle parameter using normal
+	 * values. As byproducts, getName & intValue are also tested.
+	 */
+	public void testConstructorHavingResBundle_Normal() {
+		MockLevel l = new MockLevel("level1", 1, "resourceBundle");
+		assertEquals(l.getName(), "level1");
+		assertEquals(l.intValue(), 1);
+		assertEquals(l.getResourceBundleName(), "resourceBundle");
+	}
+
+	/*
+	 * Test the constructor having resource bundle parameter using null names.
+	 * As byproducts, getName & intValue are also tested.
+	 */
+	public void testConstructorHavingResBundle_NullName() {
+		MockLevel l = new MockLevel(null, -123, null);
+		assertNull(l.getName());
+		assertEquals(l.intValue(), -123);
+		assertNull(l.getResourceBundleName());
+	}
+
+	// /*
+	// * Test the constructor having resource bundle parameter using empty
+	// names.
+	// * As byproducts, getName & intValue are also tested.
+	// */
+	// public void testConstructorHavingResBundle_EmptyName() {
+	// MockLevel l = new MockLevel("", -1000, "");
+	// assertEquals(l.getName(), "");
+	// assertEquals(l.intValue(), -1000);
+	// assertEquals(l.getResourceBundleName(), "");
+	// }
+
+	/*
+	 * Test method parse, with the pre-defined string consts.
+	 */
+	public void testParse_PredefinedConstStrings() {
+		assertSame(Level.parse("SEVERE"), Level.SEVERE);
+		assertSame(Level.parse("WARNING"), Level.WARNING);
+		assertSame(Level.parse("INFO"), Level.INFO);
+		assertSame(Level.parse("CONFIG"), Level.CONFIG);
+		assertSame(Level.parse("FINE"), Level.FINE);
+		assertSame(Level.parse("FINER"), Level.FINER);
+		assertSame(Level.parse("FINEST"), Level.FINEST);
+		assertSame(Level.parse("OFF"), Level.OFF);
+		assertSame(Level.parse("ALL"), Level.ALL);
+	}
+
+	/*
+	 * Test method parse, with an undefined string.
+	 */
+	public void testParse_IllegalConstString() {
+		try {
+			Level.parse("SEVERe");
+			fail("Should throw IllegalArgumentException if undefined string.");
+		} catch (IllegalArgumentException e) {
+		}
+	}
+
+	/*
+	 * Test method parse, with an empty string.
+	 */
+	public void testParse_EmptyString() {
+		try {
+			Level l = Level.parse("");
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+		}
+
+	}
+
+	/*
+	 * Test method parse, with a null string.
+	 */
+	public void testParse_NullString() {
+		try {
+			Level.parse(null);
+			fail("Should throw NullPointerException.");
+		} catch (NullPointerException e) {
+		}
+	}
+
+	/*
+	 * Test method parse, with pre-defined valid number strings.
+	 */
+	public void testParse_PredefinedNumber() {
+		assertSame(Level.parse("SEVERE"), Level.SEVERE);
+		assertSame(Level.parse("WARNING"), Level.WARNING);
+		assertSame(Level.parse("INFO"), Level.INFO);
+		assertSame(Level.parse("CONFIG"), Level.CONFIG);
+		assertSame(Level.parse("FINE"), Level.FINE);
+		assertSame(Level.parse("FINER"), Level.FINER);
+		assertSame(Level.parse("FINEST"), Level.FINEST);
+		assertSame(Level.parse("OFF"), Level.OFF);
+		assertSame(Level.parse("ALL"), Level.ALL);
+		assertSame(Level.parse("1000"), Level.SEVERE);
+		assertSame(Level.parse("900"), Level.WARNING);
+		assertSame(Level.parse("800"), Level.INFO);
+		assertSame(Level.parse("700"), Level.CONFIG);
+		assertSame(Level.parse("500"), Level.FINE);
+		assertSame(Level.parse("400"), Level.FINER);
+		assertSame(Level.parse("300"), Level.FINEST);
+		assertSame(Level.parse(String.valueOf(Integer.MAX_VALUE)), Level.OFF);
+		assertSame(Level.parse(String.valueOf(Integer.MIN_VALUE)), Level.ALL);
+	}
+
+	/*
+	 * Test method parse, with an undefined valid number strings.
+	 */
+	public void testParse_UndefinedNumber() {
+		Level l = Level.parse("0");
+		assertEquals(l.intValue(), 0);
+		assertEquals(l.getName(), "0");
+		assertNull(l.getResourceBundleName());
+	}
+
+	/*
+	 * Test method parse, with an undefined valid number strings with spaces.
+	 */
+	public void testParse_UndefinedNumberWithSpaces() {
+		try {
+			Level.parse(" 0");
+		} catch (IllegalArgumentException e) {
+		}
+	}
+
+	public void testParse_NegativeNumber() {
+		Level l = Level.parse("-4");
+		assertEquals(l.intValue(), -4);
+		assertEquals("-4", l.getName());
+		assertNull(l.getResourceBundleName());
+	}
+
+	/*
+	 * Test method parse, expecting the same objects will be returned given the
+	 * same name, even for non-predefined levels.
+	 */
+	public void testParse_SameObject() {
+		Level l = Level.parse("-100");
+		assertSame(l, Level.parse("-100"));
+	}
+
+	/*
+	 * Test method hashCode, with normal fields.
+	 */
+	public void testHashCode_Normal() {
+		assertEquals(Level.parse("100").hashCode(), 100);
+		assertEquals(Level.parse("-1").hashCode(), -1);
+		assertEquals(Level.parse("0").hashCode(), 0);
+		assertEquals(Level.parse("ALL").hashCode(), Integer.MIN_VALUE);
+	}
+
+	/*
+	 * Test equals when two objects are equal.
+	 */
+	public void testEquals_Equal() {
+		MockLevel l1 = new MockLevel("level1", 1);
+		MockLevel l2 = new MockLevel("level2", 1);
+		assertEquals(l1, l2);
+		assertEquals(l2, l1);
+	}
+
+	/*
+	 * Test equals when two objects are not equal.
+	 */
+	public void testEquals_NotEqual() {
+		MockLevel l1 = new MockLevel("level1", 1);
+		MockLevel l2 = new MockLevel("level1", 2);
+		assertFalse(l1.equals(l2));
+		assertFalse(l2.equals(l1));
+	}
+
+	/*
+	 * Test equals when the other object is null.
+	 */
+	public void testEquals_Null() {
+		assertFalse(Level.ALL.equals(null));
+	}
+
+	/*
+	 * Test equals when the other object is not an instance of Level.
+	 */
+	public void testEquals_NotLevel() {
+		assertFalse(Level.ALL.equals(new Object()));
+	}
+
+	/*
+	 * Test equals when the other object is itself.
+	 */
+	public void testEquals_Itself() {
+		assertTrue(Level.ALL.equals(Level.ALL));
+	}
+
+	/*
+	 * Test toString of a normal Level.
+	 */
+	public void testToString_Normal() {
+		assertEquals(Level.ALL.toString(), "ALL");
+
+		MockLevel l = new MockLevel("name", 2);
+		assertEquals("name", l.toString());
+
+		MockLevel emptyLevel = new MockLevel("", 3);
+		assertEquals("", emptyLevel.toString());
+	}
+
+	/*
+	 * Test toString of a Level with null name.
+	 */
+	public void testToString_Null() {
+		MockLevel l = new MockLevel(null, 2);
+		assertEquals(null, l.toString());
+	}
+
+	/*
+	 * Test serilaziation of pre-defined const levels. It is expected that the
+	 * deserialized cost level should be the same instance as the existing one.
+	 */
+	public void testSerialization_ConstLevel() throws Exception {
+		Level.ALL.toString();
+		SerializationTester.assertSame(Level.ALL);
+	}
+
+	/*
+	 * Test serilaziation of normal instance of Level. It is expected that the
+	 * deserialized level object should be equal to the original one.
+	 */
+	public void testSerialization_InstanceLevel() throws Exception {
+		SerializationTester.assertSame(Level.parse("550"));
+		Level l = Level.parse("");
+		SerializationTester.assertSame(l);
+		SerializationTester.assertEquals(Level.parse("-1"));
+		SerializationTester.assertEquals(new MockLevel("123", 123, "bundle"));
+		SerializationTester.assertEquals(new MockLevel(null, 123, null));
+	}
+
+	public void testSerializationCompability() throws Exception {
+		SerializationTester.assertCompabilityEquals(Level.ALL,
+				"tests/api/java/util/logging/Level.ser");
+	}
+
+	public void testGetLocalName() {
+		Level l = new MockLevel("level1", 120,
+				"tests/api/java/util/logging/res");
+		assertEquals(rb.getString("level1"), l.getLocalizedName());
+
+		l = new MockLevel("bad name", 120, "res");
+		assertEquals("bad name", l.getLocalizedName());
+
+		l = new MockLevel("level1", 11120, "bad name");
+		assertEquals("level1", l.getLocalizedName());
+
+		l = new MockLevel("level1", 1120);
+		assertEquals("level1", l.getLocalizedName());
+
+		// System.out.println(Level.SEVERE.getResourceBundleName());
+		// assertEquals("SEVERE", Level.SEVERE.getLocalizedName());
+	}
+
+	/*
+	 * Test defining new levels in subclasses of Level
+	 */
+	public void testSubclassNewLevel() {
+		MyLevel.DUPLICATENAME.getName();// just to load MyLevel class
+
+		// test duplicated name and num
+		assertEquals(MyLevel.parse("800").getName(), "INFO");
+		assertEquals(MyLevel.parse("INFO").intValue(), 800);
+		// test duplicated name
+		assertEquals(MyLevel.parse("499").getName(), "FINE");
+		assertEquals(MyLevel.parse("500").getName(), "FINE");
+		assertEquals(MyLevel.parse("FINE").intValue(), 500);
+		// test duplicated number
+		assertEquals(MyLevel.parse("300").getName(), "FINEST");
+		assertEquals(MyLevel.parse("FINEST").intValue(), 300);
+		assertEquals(MyLevel.parse("MYLEVEL1").intValue(), 300);
+		// test a normal new level, without duplicated elements
+		assertEquals(MyLevel.parse("299").getName(), "MYLEVEL2");
+		assertEquals(MyLevel.parse("MYLEVEL2").intValue(), 299);
+	}
+
+	/*
+	 * This subclass is to test whether subclasses of Level can add new defined
+	 * levels.
+	 */
+	static class MyLevel extends Level implements Serializable {
+		private static final long serialVersionUID = 1L;
+
+		public MyLevel(String name, int value) {
+			super(name, value);
+		}
+
+		public static final Level DUPLICATENAMENUM = new MyLevel("INFO", 800);
+
+		public static final Level DUPLICATENAME = new MyLevel("FINE", 499);
+
+		public static final Level DUPLICATENUM = new MyLevel("MYLEVEL1", 300);
+
+		public static final Level NORMAL = new MyLevel("MYLEVEL2", 299);
+	}
+
+	/*
+	 * This Mock is used to expose the protected constructors.
+	 */
+	public class MockLevel extends Level implements Serializable {
+
+		private static final long serialVersionUID = 1L;
+
+		public MockLevel(String name, int value) {
+			super(name, value);
+		}
+
+		public MockLevel(String name, int value, String resourceBundleName) {
+			super(name, value, resourceBundleName);
+		}
+	}
+}



Mime
View raw message