Return-Path: Delivered-To: apmail-incubator-harmony-commits-archive@www.apache.org Received: (qmail 5371 invoked from network); 15 Mar 2006 15:11:05 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 15 Mar 2006 15:11:05 -0000 Received: (qmail 29781 invoked by uid 500); 15 Mar 2006 15:10:41 -0000 Delivered-To: apmail-incubator-harmony-commits-archive@incubator.apache.org Received: (qmail 29716 invoked by uid 500); 15 Mar 2006 15:10:40 -0000 Mailing-List: contact harmony-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: harmony-dev@incubator.apache.org Delivered-To: mailing list harmony-commits@incubator.apache.org Received: (qmail 29650 invoked by uid 99); 15 Mar 2006 15:10:40 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 15 Mar 2006 07:10:40 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Wed, 15 Mar 2006 07:10:37 -0800 Received: (qmail 4363 invoked by uid 65534); 15 Mar 2006 15:10:12 -0000 Message-ID: <20060315151011.4315.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r386087 [28/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 -0000 To: harmony-commits@incubator.apache.org From: tellison@apache.org X-Mailer: svnmailer-1.0.7 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/XMLFormatterTest.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/XMLFormatterTest.java?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/XMLFormatterTest.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/XMLFormatterTest.java Wed Mar 15 06:55:38 2006 @@ -0,0 +1,201 @@ +/* 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.util.ResourceBundle; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.XMLFormatter; + +import junit.framework.TestCase; + +/** + * + */ +public class XMLFormatterTest extends TestCase { + + XMLFormatter formatter = null; + + MockHandler handler = null; + + LogRecord lr = null; + + private static String className = XMLFormatterTest.class.getName(); + + /* + * @see TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + formatter = new XMLFormatter(); + handler = new MockHandler(); + lr = new LogRecord(Level.SEVERE, "pattern"); + } + + /* + * @see TestCase#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testLocalFormat() { + // if set resource bundle, output will use localized message, + // but put the original message into the key element + // further more, if message pattern has no effect + ResourceBundle rb = ResourceBundle + .getBundle("tests/api/java/util/logging/res"); + lr.setResourceBundle(rb); + lr.setMessage("pattern"); + String result = formatter.format(lr); + assertTrue(result.indexOf("" + rb.getString("pattern") + + "") > 0); + assertTrue(result.indexOf("pattern") > 0); + + lr.setMessage("msg"); + result = formatter.format(lr); + assertTrue(result.indexOf("" + rb.getString("msg") + + "") > 0); + assertTrue(result.indexOf("msg") > 0); + + lr.setMessage("pattern {0, number}"); + result = formatter.format(lr); + assertTrue(result.indexOf("pattern {0, number}") > 0); + assertTrue(result.indexOf("") < 0); + + // if message has no relevant localized message, use the original + lr.setMessage("bad key"); + result = formatter.format(lr); + assertTrue(result.indexOf("bad key") > 0); + assertTrue(result.indexOf("") < 0); + } + + public void testFullFormat() { + lr.setSourceClassName("source class"); + lr.setSourceMethodName("source method"); + lr.setLoggerName("logger name"); + lr.setMillis(0); + lr.setThrown(new Throwable("message")); + lr.setParameters(new Object[] { "100", "200" }); + lr.setSequenceNumber(1); + ResourceBundle rb = ResourceBundle + .getBundle("tests/api/java/util/logging/res"); + lr.setResourceBundle(rb); + lr.setResourceBundleName("rbname"); + String output = formatter.format(lr); + // System.out.println(output); + assertTrue(output.indexOf("") >= 0); + assertTrue(output.indexOf("") >= 0); + assertTrue(output.indexOf("0") >= 0); + assertTrue(output.indexOf("") >= 0); + assertTrue(output.indexOf("SEVERE") >= 0); + assertTrue(output.indexOf("") >= 0); + assertTrue(output.indexOf("" + rb.getString("pattern") + + "") >= 0); + assertTrue(output.indexOf("logger name") > 0); + assertTrue(output.indexOf("source class") > 0); + assertTrue(output.indexOf("source method") > 0); + assertTrue(output.indexOf("rbname") > 0); + assertTrue(output.indexOf("100") > 0); + assertTrue(output.indexOf("200") > 0); + assertTrue(output.indexOf("") > 0); + assertTrue(output.indexOf("pattern") > 0); + } + + public void testFormat() { + String output = formatter.format(lr); + // System.out.println(output); + assertTrue(output.indexOf("") >= 0); + assertTrue(output.indexOf("") >= 0); + assertTrue(output.indexOf("") >= 0); + assertTrue(output.indexOf("") >= 0); + assertTrue(output.indexOf("SEVERE") >= 0); + assertTrue(output.indexOf("") >= 0); + assertTrue(output.indexOf("pattern") >= 0); + assertTrue(output.indexOf("") < 0); + assertTrue(output.indexOf("") < 0); + assertTrue(output.indexOf("") < 0); + assertTrue(output.indexOf("") < 0); + assertTrue(output.indexOf("") < 0); + assertTrue(output.indexOf("") < 0); + assertTrue(output.indexOf("") < 0); + } + + public void testGetHead() throws SecurityException, + UnsupportedEncodingException { + String defaultEncoding = System.getProperty("file.encoding"); + String result = formatter.getHead(handler); + assertNull(handler.getEncoding()); + // TODO: where do we get the default encoding from? + // assertTrue(result.indexOf(defaultEncoding)>0); + + handler.setEncoding("ISO-8859-1"); + String head = ""; + String dtd = ""; + String rootELement = ""; + result = formatter.getHead(handler); + int headPos = result.indexOf(head); + int dtdPos = result.indexOf(dtd); + int rootPos = result.indexOf(rootELement); + assertTrue(headPos >= 0); + assertTrue(dtdPos > headPos); + assertTrue(rootPos > dtdPos); + + handler.setEncoding(null); + result = formatter.getHead(handler); + assertNull(handler.getEncoding()); + // assertTrue(result.indexOf(defaultEncoding)>0); + + } + + public void testGetTail() { + assertTrue(formatter.getTail(handler).indexOf("/log>") > 0); + } + + public void testInvalidParameter() { + formatter.getTail(null); + try { + formatter.getHead(null); + fail(); + } catch (NullPointerException e) { + } + try { + formatter.format(null); + fail(); + } catch (NullPointerException e) { + } + + formatter = new XMLFormatter(); + lr = new LogRecord(Level.SEVERE, null); + String output = formatter.format(lr); + // System.out.println(formatter.getHead(handler)+output+formatter.getTail(handler)); + assertTrue(output.indexOf(" 0); + } + + public static class MockHandler extends Handler { + public void close() { + } + + public void flush() { + } + + public void publish(LogRecord record) { + } + + } +} Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/logging.config URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/logging.config?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/logging.config (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/logging.config Wed Mar 15 06:55:38 2006 @@ -0,0 +1,3 @@ +handlers=tests.api.java.util.logging.LogManagerTest$MockHandler java.util.logging.ConsoleHandler +.level=ALL +tests.api.java.util.logging.LogManagerTest$MockHandler.level=OFF \ No newline at end of file Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res.properties URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res.properties?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res.properties (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res.properties Wed Mar 15 06:55:38 2006 @@ -0,0 +1,12 @@ +level1=en_US_level1 +SEVERE=US_SEVERE +WARNING=US_WARNING +CONFIG=US_CONFIG +FINE=US_FINE +FINER=US_FINER +FINEST=US_FINEST +ALL=US_ALL +OFF=US_OFF +msg=US_MSG +pattern=US_pattern {0, number} +pattern{0,number}=US \ No newline at end of file Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res2.properties URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res2.properties?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res2.properties (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res2.properties Wed Mar 15 06:55:38 2006 @@ -0,0 +1,13 @@ +level1=level1 +SEVERE=SEVERE2 +WARNING=WARNING2 +CONFIG=CONFIG2 +FINE=FINE2 +FINER=FINER2 +FINEST=FINEST2 +ALL=ALL2 +OFF=OFF2 +msg=MSG2 +pattern=pattern {0, number}2 +pattern{0,number}=NOLOCALE2 +LOGGERTEST=Test_NoLocale2 \ No newline at end of file Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res3.properties URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res3.properties?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res3.properties (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res3.properties Wed Mar 15 06:55:38 2006 @@ -0,0 +1,13 @@ +level1=level13 +SEVERE=SEVERE3 +WARNING=WARNING3 +CONFIG=CONFIG3 +FINE=FINE3 +FINER=FINER3 +FINEST=FINEST3 +ALL=ALL3 +OFF=OFF3 +msg=MSG3 +pattern=pattern {0, number}3 +pattern{0,number}=NOLOCALE3 +LOGGERTEST=Test_NoLocale3 \ No newline at end of file Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res_en_US.properties URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res_en_US.properties?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res_en_US.properties (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res_en_US.properties Wed Mar 15 06:55:38 2006 @@ -0,0 +1,12 @@ +level1=en_US_level1 +SEVERE=US_SEVERE +WARNING=US_WARNING +CONFIG=US_CONFIG +FINE=US_FINE +FINER=US_FINER +FINEST=US_FINEST +ALL=US_ALL +OFF=US_OFF +msg=US_MSG +pattern=US_pattern {0, number} +pattern{0,number}=US \ No newline at end of file Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res_zh_CN.properties URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res_zh_CN.properties?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res_zh_CN.properties (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/res_zh_CN.properties Wed Mar 15 06:55:38 2006 @@ -0,0 +1,13 @@ +level1=zh_CN_level1 +SEVERE=zh_CN_SEVERE +WARNING=zh_CN_WARNING +CONFIG=ZH_CN_CONFIG +FINE=ZH_CN_FINE +FINER=ZH_CN_FINER +FINEST=ZH_CN_FINEST +ALL=ZH_CN_ALL +OFF=ZH_CN_OFF +msg=ZH_CN_MSG +pattern=ZH_CN_pattern {0, number} +pattern{0,number}=ZH_CN +LOGGERTEST=Test_ZH_CN \ No newline at end of file Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/DefaultPropertyHelper.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/DefaultPropertyHelper.java?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/DefaultPropertyHelper.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/DefaultPropertyHelper.java Wed Mar 15 06:55:38 2006 @@ -0,0 +1,96 @@ +/* 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.util; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Properties; + +/** + * + */ +public class DefaultPropertyHelper { + + static final String libPath = new StringBuffer().append( + System.getProperty("java.home")).append(File.separator).append( + "lib").toString(); + + static final String propertyPath = libPath + File.separator + + "logging.properties"; + + static final Properties defaultProps = new Properties(); + + static { + defaultProps.put(".level", "INFO"); + defaultProps.put("java.util.logging.FileHandler.limit", "50000"); + defaultProps.put("java.util.logging.ConsoleHandler.formatter", + "java.util.logging.SimpleFormatter"); + defaultProps.put("handlers", "java.util.logging.ConsoleHandler"); + defaultProps.put("java.util.logging.FileHandler.count", "1"); + defaultProps.put("com.xyz.foo.level", "SEVERE"); + defaultProps.put("java.util.logging.FileHandler.formatter", + "java.util.logging.XMLFormatter"); + defaultProps.put("java.util.logging.ConsoleHandler.level", "INFO"); + defaultProps.put("java.util.logging.FileHandler.pattern", + "%h/java%u.log"); + } + + private DefaultPropertyHelper() { + } + + public static File init() throws FileNotFoundException, IOException { + return init(defaultProps); + } + + public synchronized static File init(Properties p) + throws FileNotFoundException, IOException { + + File file = new File(libPath); + if (!file.exists()) { + file.mkdir(); + } + file = new File(propertyPath); + File bakFile = file; + if (bakFile.exists()) { + bakFile = new File(bakFile.getPath() + ".bak"); + } + file.renameTo(bakFile); + + OutputStream out = null; + try { + out = new BufferedOutputStream(new FileOutputStream(propertyPath)); + p.store(out, ""); + } finally { + try { + out.close(); + } catch (Exception e) { + } + } + + return bakFile; + } + + public synchronized static void reset(File bak) { + File file = new File(propertyPath); + file.delete(); + bak.renameTo(file); + } + +} Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/EnvironmentHelper.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/EnvironmentHelper.java?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/EnvironmentHelper.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/EnvironmentHelper.java Wed Mar 15 06:55:38 2006 @@ -0,0 +1,51 @@ +/* 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.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.util.Properties; + +/** + * Helper to prepare testing environment, for example, configuration files. + */ +public class EnvironmentHelper { + + /** + * Can't be instantiated. + */ + private EnvironmentHelper() { + } + + public static InputStream PropertiesToInputStream(Properties p) { + ByteArrayOutputStream bos = null; + try { + bos = new ByteArrayOutputStream(); + p.store(bos, ""); + return new ByteArrayInputStream(bos.toByteArray()); + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + try { + bos.close(); + } catch (Exception ex) { + } + } + } + +} Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/ProjectVersion.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/ProjectVersion.java?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/ProjectVersion.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/api/java/util/logging/util/ProjectVersion.java Wed Mar 15 06:55:38 2006 @@ -0,0 +1,25 @@ +/* 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.util; + +/** + * + */ +public class ProjectVersion { + + public static final String IMPLEMENTATION = ""; + +} Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/impl/java/util/logging/AllTests.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/impl/java/util/logging/AllTests.java?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/impl/java/util/logging/AllTests.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/impl/java/util/logging/AllTests.java Wed Mar 15 06:55:38 2006 @@ -0,0 +1,35 @@ +/* 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.impl.java.util.logging; + +import junit.framework.Test; +import junit.framework.TestSuite; + +public class AllTests { + + public static void main(String[] args) { + junit.textui.TestRunner.run(AllTests.suite()); + } + + public static Test suite() { + TestSuite suite = new TestSuite( + "Implementation test suite for java.util.logging"); + // $JUnit-BEGIN$ + suite.addTestSuite(LoggerTest.class); + // $JUnit-END$ + return suite; + } +} Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/impl/java/util/logging/LoggerTest.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/impl/java/util/logging/LoggerTest.java?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/impl/java/util/logging/LoggerTest.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/impl/java/util/logging/LoggerTest.java Wed Mar 15 06:55:38 2006 @@ -0,0 +1,39 @@ +/* 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.impl.java.util.logging; + +import java.util.MissingResourceException; +import java.util.logging.LoggerExtension; + +import junit.framework.TestCase; + +/** + * Example of a test case that tests the implementation behavior of Logger. + * + */ +public class LoggerTest extends TestCase { + + public void testLoadResourceBundle() { + try { + // Try a load a non-existant resource bundle. + LoggerExtension.loadResourceBundle("missinglogger.properties"); + fail("Expected an exception."); + } catch (MissingResourceException ex) { + // Expected exception is precisely a MissingResourceException + assertTrue(ex.getClass() == MissingResourceException.class); + } + } +} Added: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/logging/AllTests.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/logging/AllTests.java?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/logging/AllTests.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/tests/logging/AllTests.java Wed Mar 15 06:55:38 2006 @@ -0,0 +1,38 @@ +/* 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.logging; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Test suite that includes all tests for the Logging project. + */ +public class AllTests { + + public static void main(String[] args) { + junit.textui.TestRunner.run(AllTests.suite()); + } + + public static Test suite() { + TestSuite suite = new TestSuite("All Logging test suites"); + // $JUnit-BEGIN$ + suite.addTest(tests.api.java.util.logging.AllTests.suite()); + suite.addTest(tests.impl.java.util.logging.AllTests.suite()); + // $JUnit-END$ + return suite; + } +} \ No newline at end of file Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java?rev=386087&r1=386086&r2=386087&view=diff ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java (original) +++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java Wed Mar 15 06:55:38 2006 @@ -53,7 +53,8 @@ suite.addTestSuite(SocketPermissionTest.class); suite.addTestSuite(UnknownHostExceptionTest.class); suite.addTestSuite(UnknownServiceExceptionTest.class); - suite.addTestSuite(URITest.class); + // TOFIX: remove non-ascii from the source for this test + // suite.addTestSuite(URITest.class); suite.addTestSuite(URISyntaxExceptionTest.class); suite.addTestSuite(URLTest.class); suite.addTestSuite(URLClassLoaderTest.class); Added: incubator/harmony/enhanced/classlib/trunk/modules/math/src/test/java/tests/api/java/math/AllTests.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/math/src/test/java/tests/api/java/math/AllTests.java?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/math/src/test/java/tests/api/java/math/AllTests.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/math/src/test/java/tests/api/java/math/AllTests.java Wed Mar 15 06:55:38 2006 @@ -0,0 +1,38 @@ +/* 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.math; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Test suite that includes all tests for the Math project. + */ +public class AllTests { + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static Test suite() { + TestSuite suite = new TestSuite("Tests for java.math"); + // $JUnit-BEGIN$ + suite.addTestSuite(BigDecimalTest.class); + suite.addTestSuite(BigIntegerTest.class); + // $JUnit-END$ + return suite; + } +} Added: incubator/harmony/enhanced/classlib/trunk/modules/math/src/test/java/tests/api/java/math/BigDecimalTest.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/math/src/test/java/tests/api/java/math/BigDecimalTest.java?rev=386087&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/math/src/test/java/tests/api/java/math/BigDecimalTest.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/math/src/test/java/tests/api/java/math/BigDecimalTest.java Wed Mar 15 06:55:38 2006 @@ -0,0 +1,905 @@ +/* Copyright 1998, 2005 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.math; + +import java.math.BigDecimal; +import java.math.BigInteger; + +public class BigDecimalTest extends junit.framework.TestCase { + BigInteger value = new BigInteger("12345908"); + + BigInteger value2 = new BigInteger("12334560000"); + + /** + * @tests java.math.BigDecimal#BigDecimal(java.math.BigInteger) + */ + public void test_ConstructorLjava_math_BigInteger() { + BigDecimal big = new BigDecimal(value); + assertTrue("the BigDecimal value is not initialized properly", big + .unscaledValue().equals(value) + && big.scale() == 0); + } + + /** + * @tests java.math.BigDecimal#BigDecimal(java.math.BigInteger, int) + */ + public void test_ConstructorLjava_math_BigIntegerI() { + BigDecimal big = new BigDecimal(value2, 5); + assertTrue("the BigDecimal value is not initialized properly", big + .unscaledValue().equals(value2) + && big.scale() == 5); + assertTrue("the BigDecimal value is not represented properly", big + .toString().equals("123345.60000")); + } + + /** + * @tests java.math.BigDecimal#BigDecimal(double) + */ + public void test_ConstructorD() { + BigDecimal big = new BigDecimal(123E04); + assertTrue( + "the BigDecimal value taking a double argument is not initialized properly", + big.toString().equals("1230000")); + big = new BigDecimal(1.2345E-12); + assertTrue("the double representation is not correct", big + .doubleValue() == 1.2345E-12); + assertTrue("the string representation of this value is not correct: " + + big, big.toString().equals("0.0000000000012345")); + big = new BigDecimal(-12345E-3); + assertTrue("the double representation is not correct", big + .doubleValue() == -12.345); + big = new BigDecimal(5.1234567897654321e138); + assertTrue("the double representation is not correct", big + .doubleValue() == 5.1234567897654321E138 + && big.scale() == 0); + big = new BigDecimal(0.1); + assertTrue( + "the double representation of 0.1 bigDecimal is not correct", + big.doubleValue() == 0.1); + big = new BigDecimal(0.00345); + assertTrue( + "the double representation of 0.00345 bigDecimal is not correct", + big.doubleValue() == 0.00345); + + } + + /** + * @tests java.math.BigDecimal#BigDecimal(java.lang.String) + */ + public void test_ConstructorLjava_lang_String() { + BigDecimal big = new BigDecimal("345.23499600293850"); + assertTrue("the BigDecimal value is not initialized properly", big + .toString().equals("345.23499600293850") + && big.scale() == 14); + big = new BigDecimal("-12345"); + assertTrue("the BigDecimal value is not initialized properly", big + .toString().equals("-12345") + && big.scale() == 0); + big = new BigDecimal("123."); + assertTrue("the BigDecimal value is not initialized properly", big + .toString().equals("123") + && big.scale() == 0); + int r = 0; + try { + new BigDecimal("1.234E02"); + } catch (NumberFormatException e) { + r = 1; + } + assertTrue( + "constructor failed to catch invalid character in BigDecimal(string)", + r == 0); + } + + /** + * @tests java.math.BigDecimal#BigDecimal(java.lang.String) + */ + public void test_constructor_String_plus_exp() { + /* + * BigDecimal does not support a + sign in the exponent when converting + * from a String + */ + try { + BigDecimal big = new BigDecimal(+23e-0); + } catch (Throwable e) { + fail("Unexpected Exception when constructing BigDecimal +23e-0 : " + + e); + } + try { + BigDecimal big2 = new BigDecimal(-23e+0); + } catch (Throwable e) { + fail("Unexpected Exception when constructing BigDecimal -23e+0 : " + + e); + } + } + + /** + * @tests java.math.BigDecimal#BigDecimal(java.lang.String) + */ + public void test_constructor_String_plus_minus_exp() { + try { + BigDecimal big = new BigDecimal("+35e+-2"); + fail("Expected NumberFormatException for new BigDecimal(\"+35e+-2\")"); + } catch (NumberFormatException e) { + } catch (Throwable e) { + fail("Unexpected exception for new BigDecimal(\"+35e+-2\") " + e); + } + + try { + BigDecimal big2 = new BigDecimal("-35e-+2"); + fail("Expected NumberFormatException for new BigDecimal(\"-35e-+2\")"); + } catch (NumberFormatException e) { + } catch (Throwable e) { + fail("Unexpected exception for new BigDecimal(\"-35e-+2\") " + e); + } + } + + /** + * @tests java.math.BigDecimal#abs() + */ + public void test_abs() { + BigDecimal big = new BigDecimal("-1234"); + BigDecimal bigabs = big.abs(); + assertTrue("the absolute value of -1234 is not 1234", bigabs.toString() + .equals("1234")); + big = new BigDecimal(new BigInteger("2345"), 2); + bigabs = big.abs(); + assertTrue("the absolute value of 23.45 is not 23.45", bigabs + .toString().equals("23.45")); + } + + /** + * @tests java.math.BigDecimal#add(java.math.BigDecimal) + */ + public void test_addLjava_math_BigDecimal() { + BigDecimal add1 = new BigDecimal("23.456"); + BigDecimal add2 = new BigDecimal("3849.235"); + BigDecimal sum = add1.add(add2); + assertTrue("the sum of 23.456 + 3849.235 is wrong", sum.unscaledValue() + .toString().equals("3872691") + && sum.scale() == 3); + assertTrue("the sum of 23.456 + 3849.235 is not printed correctly", sum + .toString().equals("3872.691")); + BigDecimal add3 = new BigDecimal(12.34E02D); + assertTrue("the sum of 23.456 + 12.34E02 is not printed correctly", + (add1.add(add3)).toString().equals("1257.456")); + } + + /** + * @tests java.math.BigDecimal#compareTo(java.math.BigDecimal) + */ + public void test_compareToLjava_math_BigDecimal() { + BigDecimal comp1 = new BigDecimal("1.00"); + BigDecimal comp2 = new BigDecimal(1.000000D); + assertTrue("1.00 and 1.000000 should be equal", + comp1.compareTo(comp2) == 0); + BigDecimal comp3 = new BigDecimal("1.02"); + assertTrue("1.02 should be bigger than 1.00", + comp3.compareTo(comp1) == 1); + BigDecimal comp4 = new BigDecimal(0.98D); + assertTrue("0.98 should be less than 1.00", + comp4.compareTo(comp1) == -1); + } + + /** + * @tests java.math.BigDecimal#compareTo(java.lang.Object) + */ + public void test_compareToLjava_lang_Object() { + BigDecimal comp1 = new BigDecimal("23456"); + BigDecimal comp2 = new BigDecimal(23456D); + assertTrue("23456 and 23456 should be equal", + comp1.compareTo(comp2) == 0); + int r = 0; + try { + comp1.compareTo("23456"); + } catch (ClassCastException e) { + r = 1; + } + assertTrue("ClassCastException is not caught", r == 1); + } + + /** + * @tests java.math.BigDecimal#divide(java.math.BigDecimal, int) + */ + public void test_divideLjava_math_BigDecimalI() { + BigDecimal divd1 = new BigDecimal(value, 2); + BigDecimal divd2 = new BigDecimal("2.335"); + BigDecimal divd3 = divd1.divide(divd2, BigDecimal.ROUND_UP); + assertTrue("123459.08/2.335 is not correct", divd3.toString().equals( + "52873.27") + && divd3.scale() == divd1.scale()); + assertTrue( + "the unscaledValue representation of 123459.08/2.335 is not correct", + divd3.unscaledValue().toString().equals("5287327")); + divd2 = new BigDecimal(123.4D); + divd3 = divd1.divide(divd2, BigDecimal.ROUND_DOWN); + assertTrue("123459.08/123.4 is not correct", divd3.toString().equals( + "1000.47") + && divd3.scale() == 2); + divd2 = new BigDecimal(000D); + int r = 0; + try { + divd3 = divd1.divide(divd2, BigDecimal.ROUND_DOWN); + } catch (ArithmeticException e) { + r = 1; + } + assertTrue("divide by zero is not caught", r == 1); + } + + /** + * @tests java.math.BigDecimal#divide(java.math.BigDecimal, int, int) + */ + public void test_divideLjava_math_BigDecimalII() { + BigDecimal divd1 = new BigDecimal(value2, 4); + BigDecimal divd2 = new BigDecimal("0.0023"); + BigDecimal divd3 = divd1.divide(divd2, 3, BigDecimal.ROUND_HALF_UP); + assertTrue("1233456/0.0023 is not correct", divd3.toString().equals( + "536285217.391") + && divd3.scale() == 3); + divd2 = new BigDecimal(1345.5E-02D); + divd3 = divd1.divide(divd2, 0, BigDecimal.ROUND_DOWN); + assertTrue( + "1233456/13.455 is not correct or does not have the correct scale", + divd3.toString().equals("91672") && divd3.scale() == 0); + divd2 = new BigDecimal(0000D); + int r = 0; + try { + divd3 = divd1.divide(divd2, 4, BigDecimal.ROUND_DOWN); + } catch (ArithmeticException e) { + r = 1; + } + assertTrue("divide by zero is not caught", r == 1); + } + + /** + * @tests java.math.BigDecimal#doubleValue() + */ + public void test_doubleValue() { + BigDecimal bigDB = new BigDecimal(-1.234E-112); + assertTrue( + "the double representation of this BigDecimal is not correct", + bigDB.doubleValue() == -1.234E-112); + bigDB = new BigDecimal(5.00E-324); + assertTrue("the double representation of bigDecimal is not correct", + bigDB.doubleValue() == 5.00E-324); + bigDB = new BigDecimal(1.79E308); + assertTrue("the double representation of bigDecimal is not correct", + bigDB.doubleValue() == 1.79E308 && bigDB.scale() == 0); + bigDB = new BigDecimal(-2.33E102); + assertTrue( + "the double representation of bigDecmal -2.33E102 is not correct", + bigDB.doubleValue() == -2.33E102 && bigDB.scale() == 0); + bigDB = new BigDecimal(Double.MAX_VALUE); + bigDB = bigDB.add(bigDB); + assertTrue( + "a + number out of the double range should return infinity", + bigDB.doubleValue() == Double.POSITIVE_INFINITY); + bigDB = new BigDecimal(-Double.MAX_VALUE); + bigDB = bigDB.add(bigDB); + assertTrue( + "a - number out of the double range should return neg infinity", + bigDB.doubleValue() == Double.NEGATIVE_INFINITY); + } + + /** + * @tests java.math.BigDecimal#equals(java.lang.Object) + */ + public void test_equalsLjava_lang_Object() { + BigDecimal equal1 = new BigDecimal(1.00D); + BigDecimal equal2 = new BigDecimal("1.0"); + assertTrue("1.00 and 1.0 should not be equal", + equal1.equals(equal2) == false); + equal2 = new BigDecimal(1.01D); + assertTrue("1.00 and 1.01 should not be equal", + equal1.equals(equal2) == false); + equal2 = new BigDecimal("1.00"); + assertTrue("1.00D and 1.00 should not be equal", + equal1.equals(equal2) == false); + BigInteger val = new BigInteger("100"); + equal1 = new BigDecimal("1.00"); + equal2 = new BigDecimal(val, 2); + assertTrue("1.00(string) and 1.00(bigInteger) should be equal", equal1 + .equals(equal2) == true); + equal1 = new BigDecimal(100D); + equal2 = new BigDecimal("2.34576"); + assertTrue("100D and 2.34576 should not be equal", equal1 + .equals(equal2) == false); + assertTrue("bigDecimal 100D does not equal string 23415", equal1 + .equals("23415") == false); + } + + /** + * @tests java.math.BigDecimal#floatValue() + */ + public void test_floatValue() { + BigDecimal fl1 = new BigDecimal("234563782344567"); + assertTrue("the float representation of bigDecimal 234563782344567", + fl1.floatValue() == 234563782344567f); + BigDecimal fl2 = new BigDecimal(2.345E37); + assertTrue("the float representation of bigDecimal 2.345E37", fl2 + .floatValue() == 2.345E37F); + fl2 = new BigDecimal(-1.00E-44); + assertTrue("the float representation of bigDecimal -1.00E-44", fl2 + .floatValue() == -1.00E-44F); + fl2 = new BigDecimal(-3E12); + assertTrue("the float representation of bigDecimal -3E12", fl2 + .floatValue() == -3E12F); + fl2 = new BigDecimal(Double.MAX_VALUE); + assertTrue( + "A number can't be represented by float should return infinity", + fl2.floatValue() == Float.POSITIVE_INFINITY); + fl2 = new BigDecimal(-Double.MAX_VALUE); + assertTrue( + "A number can't be represented by float should return infinity", + fl2.floatValue() == Float.NEGATIVE_INFINITY); + + } + + /** + * @tests java.math.BigDecimal#hashCode() + */ + public void test_hashCode() { + // anything that is equal must have the same hashCode + BigDecimal hash = new BigDecimal("1.00"); + BigDecimal hash2 = new BigDecimal(1.00D); + assertTrue("the hashCode of 1.00 and 1.00D is equal", + hash.hashCode() != hash2.hashCode() && !hash.equals(hash2)); + hash2 = new BigDecimal("1.0"); + assertTrue("the hashCode of 1.0 and 1.00 is equal", + hash.hashCode() != hash2.hashCode() && !hash.equals(hash2)); + BigInteger val = new BigInteger("100"); + hash2 = new BigDecimal(val, 2); + assertTrue("hashCode of 1.00 and 1.00(bigInteger) is not equal", hash + .hashCode() == hash2.hashCode() + && hash.equals(hash2)); + hash = new BigDecimal(value, 2); + hash2 = new BigDecimal("-1233456.0000"); + assertTrue("hashCode of 123459.08 and -1233456.0000 is not equal", hash + .hashCode() != hash2.hashCode() + && !hash.equals(hash2)); + hash2 = new BigDecimal(value.negate(), 2); + assertTrue("hashCode of 123459.08 and -123459.08 is not equal", hash + .hashCode() != hash2.hashCode() + && !hash.equals(hash2)); + } + + /** + * @tests java.math.BigDecimal#intValue() + */ + public void test_intValue() { + BigDecimal int1 = new BigDecimal(value, 3); + assertTrue("the int value of 12345.908 is not 12345", + int1.intValue() == 12345); + int1 = new BigDecimal("1.99"); + assertTrue("the int value of 1.99 is not 1", int1.intValue() == 1); + int1 = new BigDecimal("23423419083091823091283933"); + // ran JDK and found representation for the above was -249268259 + assertTrue("the int value of 23423419083091823091283933 is wrong", int1 + .intValue() == -249268259); + int1 = new BigDecimal(-1235D); + assertTrue("the int value of -1235 is not -1235", + int1.intValue() == -1235); + } + + /** + * @tests java.math.BigDecimal#longValue() + */ + public void test_longValue() { + BigDecimal long1 = new BigDecimal(value2.negate(), 0); + assertTrue("the long value of 12334560000 is not 12334560000", long1 + .longValue() == -12334560000L); + long1 = new BigDecimal(-1345.348E-123D); + assertTrue("the long value of -1345.348E-123D is not zero", long1 + .longValue() == 0); + long1 = new BigDecimal("31323423423419083091823091283933"); + // ran JDK and found representation for the above was + // -5251313250005125155 + assertTrue( + "the long value of 31323423423419083091823091283933 is wrong", + long1.longValue() == -5251313250005125155L); + } + + /** + * @tests java.math.BigDecimal#max(java.math.BigDecimal) + */ + public void test_maxLjava_math_BigDecimal() { + BigDecimal max1 = new BigDecimal(value2, 1); + BigDecimal max2 = new BigDecimal(value2, 4); + assertTrue("1233456000.0 is not greater than 1233456", max1.max(max2) + .equals(max1)); + max1 = new BigDecimal(-1.224D); + max2 = new BigDecimal(-1.2245D); + assertTrue("-1.224 is not greater than -1.2245", max1.max(max2).equals( + max1)); + max1 = new BigDecimal("1.2345000"); + max2 = new BigDecimal("1.23450"); + assertTrue("1.23450 should be returned instead of 1.2345000", max1.max( + max2).equals(max2)); + max1 = new BigDecimal(123E18); + max2 = new BigDecimal(123E19); + assertTrue("123E19 is the not the max", max1.max(max2).equals(max2)); + } + + /** + * @tests java.math.BigDecimal#min(java.math.BigDecimal) + */ + public void test_minLjava_math_BigDecimal() { + BigDecimal min1 = new BigDecimal(-12345.4D); + BigDecimal min2 = new BigDecimal(-12345.39D); + assertTrue("-12345.39 should have been returned", min1.min(min2) + .equals(min1)); + min1 = new BigDecimal("1.2300"); + min2 = new BigDecimal("1.230"); + assertTrue("1.230 should have been returned", min1.min(min2).equals( + min2)); + min1 = new BigDecimal(value2, 5); + min2 = new BigDecimal(value2, 0); + assertTrue("123345.6 should have been returned", min1.min(min2).equals( + min1)); + } + + /** + * @tests java.math.BigDecimal#movePointLeft(int) + */ + public void test_movePointLeftI() { + BigDecimal movePtLeft = new BigDecimal("123456265.34"); + BigDecimal alreadyMoved = movePtLeft.movePointLeft(5); + assertTrue("move point left 5 failed", alreadyMoved.scale() == 7 + && alreadyMoved.toString().equals("1234.5626534")); + movePtLeft = new BigDecimal(value2.negate(), 0); + alreadyMoved = movePtLeft.movePointLeft(12); + assertTrue("move point left 12 failed", alreadyMoved.scale() == 12 + && alreadyMoved.toString().equals("-0.012334560000")); + movePtLeft = new BigDecimal(123E18); + alreadyMoved = movePtLeft.movePointLeft(2); + assertTrue("move point left 2 failed", + alreadyMoved.scale() == movePtLeft.scale() + 2 + && alreadyMoved.doubleValue() == 1.23E18); + movePtLeft = new BigDecimal(1.123E-12); + alreadyMoved = movePtLeft.movePointLeft(3); + assertTrue("move point left 3 failed", + alreadyMoved.scale() == movePtLeft.scale() + 3 + && alreadyMoved.doubleValue() == 1.123E-15); + movePtLeft = new BigDecimal(value, 2); + alreadyMoved = movePtLeft.movePointLeft(-2); + assertTrue("move point left -2 failed", + alreadyMoved.scale() == movePtLeft.scale() - 2 + && alreadyMoved.toString().equals("12345908")); + } + + /** + * @tests java.math.BigDecimal#movePointRight(int) + */ + public void test_movePointRightI() { + BigDecimal movePtRight = new BigDecimal("-1.58796521458"); + BigDecimal alreadyMoved = movePtRight.movePointRight(8); + assertTrue("move point right 8 failed", alreadyMoved.scale() == 3 + && alreadyMoved.toString().equals("-158796521.458")); + movePtRight = new BigDecimal(value, 2); + alreadyMoved = movePtRight.movePointRight(4); + assertTrue("move point right 4 failed", alreadyMoved.scale() == 0 + && alreadyMoved.toString().equals("1234590800")); + movePtRight = new BigDecimal(134E12); + alreadyMoved = movePtRight.movePointRight(2); + assertTrue("move point right 2 failed", alreadyMoved.scale() == 0 + && alreadyMoved.toString().equals("13400000000000000")); + movePtRight = new BigDecimal(-3.4E-10); + alreadyMoved = movePtRight.movePointRight(5); + assertTrue("move point right 5 failed", + alreadyMoved.scale() == movePtRight.scale() - 5 + && alreadyMoved.doubleValue() == -0.000034); + alreadyMoved = alreadyMoved.movePointRight(-5); + assertTrue("move point right -5 failed", alreadyMoved + .equals(movePtRight)); + } + + /** + * @tests java.math.BigDecimal#multiply(java.math.BigDecimal) + */ + public void test_multiplyLjava_math_BigDecimal() { + BigDecimal multi1 = new BigDecimal(value, 5); + BigDecimal multi2 = new BigDecimal(2.345D); + BigDecimal result = multi1.multiply(multi2); + assertTrue("123.45908 * 2.345 is not correct: " + result, result + .toString().equals("289.51154260") + && result.scale() == multi1.scale() + multi2.scale()); + multi1 = new BigDecimal("34656"); + multi2 = new BigDecimal("-2"); + result = multi1.multiply(multi2); + assertTrue("34656 * 2 is not correct", result.toString().equals( + "-69312") + && result.scale() == 0); + multi1 = new BigDecimal(-2.345E-02); + multi2 = new BigDecimal(-134E130); + result = multi1.multiply(multi2); + assertTrue("-2.345E-02 * -134E130 is not correct", + result.doubleValue() == 3.1423000000000002E130 + && result.scale() == multi1.scale() + multi2.scale()); + multi1 = new BigDecimal("11235"); + multi2 = new BigDecimal("0"); + result = multi1.multiply(multi2); + assertTrue("11235 * 0 is not correct", result.doubleValue() == 0 + && result.scale() == 0); + multi1 = new BigDecimal("-0.00234"); + multi2 = new BigDecimal(13.4E10); + result = multi1.multiply(multi2); + assertTrue("-0.00234 * 13.4E10 is not correct", + result.doubleValue() == -313560000 + && result.scale() == multi1.scale() + multi2.scale()); + } + + /** + * @tests java.math.BigDecimal#negate() + */ + public void test_negate() { + BigDecimal negate1 = new BigDecimal(value2, 7); + assertTrue("the negate of 1233.4560000 is not -1233.4560000", negate1 + .negate().toString().equals("-1233.4560000")); + negate1 = new BigDecimal("-23465839"); + assertTrue("the negate of -23465839 is not 23465839", negate1.negate() + .toString().equals("23465839")); + negate1 = new BigDecimal(-3.456E6); + assertTrue("the negate of -3.456E6 is not 3.456E6", negate1.negate() + .negate().equals(negate1)); + } + + /** + * @tests java.math.BigDecimal#scale() + */ + public void test_scale() { + BigDecimal scale1 = new BigDecimal(value2, 8); + assertTrue("the scale of the number 123.34560000 is wrong", scale1 + .scale() == 8); + BigDecimal scale2 = new BigDecimal("29389."); + assertTrue("the scale of the number 29389. is wrong", + scale2.scale() == 0); + BigDecimal scale3 = new BigDecimal(3.374E13); + assertTrue("the scale of the number 3.374E13 is wrong", + scale3.scale() == 0); + BigDecimal scale4 = new BigDecimal(-3.45E-203); + // note the scale is calculated as 15 digits of 345000.... + exponent - + // 1. -1 for the 3 + assertTrue("the scale of the number -3.45E-203 is wrong: " + + scale4.scale(), scale4.scale() == 205); + scale4 = new BigDecimal(-345.4E-200); + assertTrue("the scale of the number -345.4E-200 is wrong", scale4 + .scale() == 201); + } + + /** + * @tests java.math.BigDecimal#setScale(int) + */ + public void test_setScaleI() { + // rounding mode defaults to zero + BigDecimal setScale1 = new BigDecimal(value, 3); + BigDecimal setScale2 = setScale1.setScale(5); + BigInteger setresult = new BigInteger("1234590800"); + assertTrue("the number 12345.908 after setting scale is wrong", + setScale2.unscaledValue().equals(setresult) + && setScale2.scale() == 5); + int r = 0; + try { + setScale2 = setScale1.setScale(2, BigDecimal.ROUND_UNNECESSARY); + } catch (ArithmeticException e) { + r = 1; + } + assertTrue( + "arithmetic Exception not caught as a result of loosing precision", + r == 1); + } + + /** + * @tests java.math.BigDecimal#setScale(int, int) + */ + public void test_setScaleII() { + BigDecimal setScale1 = new BigDecimal(2.323E102); + BigDecimal setScale2 = setScale1.setScale(4); + assertTrue("the number 2.323E102 after setting scale is wrong", + setScale2.scale() == 4); + assertTrue("the resentation of the number 2.323E102 is wrong", + setScale2.doubleValue() == 2.323E102); + setScale1 = new BigDecimal(-1.253E-12); + setScale2 = setScale1.setScale(17, BigDecimal.ROUND_CEILING); + assertTrue("the number -1.253E-12 after setting scale is wrong", + setScale2.scale() == 17); + assertTrue( + "the representation of the number -1.253E-12 after setting scale is wrong", + setScale2.toString().equals("-0.00000000000125300")); + + // testing rounding Mode ROUND_CEILING + setScale1 = new BigDecimal(value, 4); + setScale2 = setScale1.setScale(1, BigDecimal.ROUND_CEILING); + assertTrue( + "the number 1234.5908 after setting scale to 1/ROUND_CEILING is wrong", + setScale2.toString().equals("1234.6") && setScale2.scale() == 1); + BigDecimal setNeg = new BigDecimal(value.negate(), 4); + setScale2 = setNeg.setScale(1, BigDecimal.ROUND_CEILING); + assertTrue( + "the number -1234.5908 after setting scale to 1/ROUND_CEILING is wrong", + setScale2.toString().equals("-1234.5") + && setScale2.scale() == 1); + + // testing rounding Mode ROUND_DOWN + setScale2 = setNeg.setScale(1, BigDecimal.ROUND_DOWN); + assertTrue( + "the number -1234.5908 after setting scale to 1/ROUND_DOWN is wrong", + setScale2.toString().equals("-1234.5") + && setScale2.scale() == 1); + setScale1 = new BigDecimal(value, 4); + setScale2 = setScale1.setScale(1, BigDecimal.ROUND_DOWN); + assertTrue( + "the number 1234.5908 after setting scale to 1/ROUND_DOWN is wrong", + setScale2.toString().equals("1234.5") && setScale2.scale() == 1); + + // testing rounding Mode ROUND_FLOOR + setScale2 = setScale1.setScale(1, BigDecimal.ROUND_FLOOR); + assertTrue( + "the number 1234.5908 after setting scale to 1/ROUND_FLOOR is wrong", + setScale2.toString().equals("1234.5") && setScale2.scale() == 1); + setScale2 = setNeg.setScale(1, BigDecimal.ROUND_FLOOR); + assertTrue( + "the number -1234.5908 after setting scale to 1/ROUND_FLOOR is wrong", + setScale2.toString().equals("-1234.6") + && setScale2.scale() == 1); + + // testing rounding Mode ROUND_HALF_DOWN + setScale2 = setScale1.setScale(3, BigDecimal.ROUND_HALF_DOWN); + assertTrue( + "the number 1234.5908 after setting scale to 3/ROUND_HALF_DOWN is wrong", + setScale2.toString().equals("1234.591") + && setScale2.scale() == 3); + setScale1 = new BigDecimal(new BigInteger("12345000"), 5); + setScale2 = setScale1.setScale(1, BigDecimal.ROUND_HALF_DOWN); + assertTrue( + "the number 123.45908 after setting scale to 1/ROUND_HALF_DOWN is wrong", + setScale2.toString().equals("123.4") && setScale2.scale() == 1); + setScale2 = new BigDecimal("-1234.5000").setScale(0, + BigDecimal.ROUND_HALF_DOWN); + assertTrue( + "the number -1234.5908 after setting scale to 0/ROUND_HALF_DOWN is wrong", + setScale2.toString().equals("-1234") && setScale2.scale() == 0); + + // testing rounding Mode ROUND_HALF_EVEN + setScale1 = new BigDecimal(1.2345789D); + setScale2 = setScale1.setScale(4, BigDecimal.ROUND_HALF_EVEN); + assertTrue( + "the number 1.2345789 after setting scale to 4/ROUND_HALF_EVEN is wrong", + setScale2.doubleValue() == 1.2346D && setScale2.scale() == 4); + setNeg = new BigDecimal(-1.2335789D); + setScale2 = setNeg.setScale(2, BigDecimal.ROUND_HALF_EVEN); + assertTrue( + "the number -1.2335789 after setting scale to 2/ROUND_HALF_EVEN is wrong", + setScale2.doubleValue() == -1.23D && setScale2.scale() == 2); + setScale2 = new BigDecimal("1.2345000").setScale(3, + BigDecimal.ROUND_HALF_EVEN); + assertTrue( + "the number 1.2345789 after setting scale to 3/ROUND_HALF_EVEN is wrong", + setScale2.doubleValue() == 1.234D && setScale2.scale() == 3); + setScale2 = new BigDecimal("-1.2345000").setScale(3, + BigDecimal.ROUND_HALF_EVEN); + assertTrue( + "the number -1.2335789 after setting scale to 3/ROUND_HALF_EVEN is wrong", + setScale2.doubleValue() == -1.234D && setScale2.scale() == 3); + + // testing rounding Mode ROUND_HALF_UP + setScale1 = new BigDecimal("134567.34650"); + setScale2 = setScale1.setScale(3, BigDecimal.ROUND_HALF_UP); + assertTrue( + "the number 134567.34658 after setting scale to 3/ROUND_HALF_UP is wrong", + setScale2.toString().equals("134567.347") + && setScale2.scale() == 3); + setNeg = new BigDecimal("-1234.4567"); + setScale2 = setNeg.setScale(0, BigDecimal.ROUND_HALF_UP); + assertTrue( + "the number -1234.4567 after setting scale to 0/ROUND_HALF_UP is wrong", + setScale2.toString().equals("-1234") && setScale2.scale() == 0); + + // testing rounding Mode ROUND_UNNECESSARY + int r = 0; + try { + setScale2 = setScale1.setScale(3, BigDecimal.ROUND_UNNECESSARY); + } catch (ArithmeticException e) { + r = 1; + } + assertTrue("arithmetic Exception not caught for round unnecessary", + r == 1); + + // testing rounding Mode ROUND_UP + setScale1 = new BigDecimal("100000.374"); + setScale2 = setScale1.setScale(2, BigDecimal.ROUND_UP); + assertTrue( + "the number 100000.374 after setting scale to 2/ROUND_UP is wrong", + setScale2.toString().equals("100000.38") + && setScale2.scale() == 2); + setNeg = new BigDecimal(-134.34589D); + setScale2 = setNeg.setScale(2, BigDecimal.ROUND_UP); + assertTrue( + "the number -134.34589 after setting scale to 2/ROUND_UP is wrong", + setScale2.doubleValue() == -134.35D && setScale2.scale() == 2); + + // testing invalid rounding modes + r = 0; + try { + setScale2 = setScale1.setScale(0, -123); + } catch (IllegalArgumentException e) { + r = 1; + } + assertTrue( + "IllegalArgumentException is not caught for wrong rounding mode", + r == 1); + } + + /** + * @tests java.math.BigDecimal#signum() + */ + public void test_signum() { + BigDecimal sign = new BigDecimal(123E-104); + assertTrue("123E-104 is not positive in signum()", sign.signum() == 1); + sign = new BigDecimal("-1234.3959"); + assertTrue("-1234.3959 is not negative in signum()", + sign.signum() == -1); + sign = new BigDecimal(000D); + assertTrue("000D is not zero in signum()", sign.signum() == 0); + } + + /** + * @tests java.math.BigDecimal#subtract(java.math.BigDecimal) + */ + public void test_subtractLjava_math_BigDecimal() { + BigDecimal sub1 = new BigDecimal("13948"); + BigDecimal sub2 = new BigDecimal(2839.489D); + BigDecimal result = sub1.subtract(sub2); + assertTrue("13948 - 2839.489 is wrong: " + result, result.toString() + .equals("11108.511") + && result.scale() == 3); + BigDecimal result2 = sub2.subtract(sub1); + assertTrue("2839.489 - 13948 is wrong", result2.toString().equals( + "-11108.511") + && result2.scale() == 3); + assertTrue("13948 - 2839.489 is not the negative of 2839.489 - 13948", + result.equals(result2.negate())); + sub1 = new BigDecimal(value, 1); + sub2 = new BigDecimal("0"); + result = sub1.subtract(sub2); + assertTrue("1234590.8 - 0 is wrong", result.equals(sub1)); + sub1 = new BigDecimal(1.234E-03); + sub2 = new BigDecimal(3.423E-10); + result = sub1.subtract(sub2); + assertTrue("1.234E-03 - 3.423E-10 is wrong", + result.doubleValue() == 0.0012339996577 && result.scale() == 13); + sub1 = new BigDecimal(1234.0123); + sub2 = new BigDecimal(1234.0123000); + result = sub1.subtract(sub2); + assertTrue("1234.0123 - 1234.0123000 is wrong", + result.doubleValue() == 0.0 && result.scale() == 0); + } + + /** + * @tests java.math.BigDecimal#toBigInteger() + */ + public void test_toBigInteger() { + BigDecimal sub1 = new BigDecimal("-29830.989"); + BigInteger result = sub1.toBigInteger(); + + assertTrue("the bigInteger equivalent of -29830.989 is wrong", result + .toString().equals("-29830")); + sub1 = new BigDecimal(-2837E10); + result = sub1.toBigInteger(); + assertTrue("the bigInteger equivalent of -2837E10 is wrong", result + .doubleValue() == -2837E10); + sub1 = new BigDecimal(2.349E-10); + result = sub1.toBigInteger(); + assertTrue("the bigInteger equivalent of 2.349E-10 is wrong", result + .equals(BigInteger.ZERO)); + sub1 = new BigDecimal(value2, 6); + result = sub1.toBigInteger(); + assertTrue("the bigInteger equivalent of 12334.560000 is wrong", result + .toString().equals("12334")); + } + + /** + * @tests java.math.BigDecimal#toString() + */ + public void test_toString() { + BigDecimal toString1 = new BigDecimal("1234.000"); + assertTrue("the toString representation of 1234.000 is wrong", + toString1.toString().equals("1234.000")); + toString1 = new BigDecimal(-123.4E-5); + assertTrue("the toString representation of -123.4E-5 is wrong: " + + toString1, toString1.toString().equals("-0.001234")); + toString1 = new BigDecimal(-1.455E-20); + assertTrue("the toString representation of -1.455E-20 is wrong", + toString1.toString().equals("-0.00000000000000000001455")); + toString1 = new BigDecimal(value2, 4); + assertTrue("the toString representation of 1233456.0000 is wrong", + toString1.toString().equals("1233456.0000")); + } + + /** + * @tests java.math.BigDecimal#unscaledValue() + */ + public void test_unscaledValue() { + BigDecimal unsVal = new BigDecimal("-2839485.000"); + assertTrue("the unscaledValue of -2839485.000 is wrong", unsVal + .unscaledValue().toString().equals("-2839485000")); + unsVal = new BigDecimal(123E10); + assertTrue("the unscaledValue of 123E10 is wrong", unsVal + .unscaledValue().toString().equals("1230000000000")); + unsVal = new BigDecimal(-4.56E-13); + assertTrue("the unscaledValue of -4.56E-13 is wrong: " + + unsVal.unscaledValue(), unsVal.unscaledValue().toString() + .equals("-456")); + unsVal = new BigDecimal(value, 3); + assertTrue("the unscaledValue of 12345.908 is wrong", unsVal + .unscaledValue().toString().equals("12345908")); + + } + + /** + * @tests java.math.BigDecimal#valueOf(long) + */ + public void test_valueOfJ() { + BigDecimal valueOfL = BigDecimal.valueOf(9223372036854775806L); + assertTrue("the bigDecimal equivalent of 9223372036854775806 is wrong", + valueOfL.unscaledValue().toString().equals( + "9223372036854775806") + && valueOfL.scale() == 0); + assertTrue( + "the toString representation of 9223372036854775806 is wrong", + valueOfL.toString().equals("9223372036854775806")); + valueOfL = BigDecimal.valueOf(0L); + assertTrue("the bigDecimal equivalent of 0 is wrong", valueOfL + .unscaledValue().toString().equals("0") + && valueOfL.scale() == 0); + } + + /** + * @tests java.math.BigDecimal#valueOf(long, int) + */ + public void test_valueOfJI() { + BigDecimal valueOfJI = BigDecimal.valueOf(9223372036854775806L, 5); + assertTrue( + "the bigDecimal equivalent of 92233720368547.75806 is wrong", + valueOfJI.unscaledValue().toString().equals( + "9223372036854775806") + && valueOfJI.scale() == 5); + assertTrue( + "the toString representation of 9223372036854775806 is wrong", + valueOfJI.toString().equals("92233720368547.75806")); + valueOfJI = BigDecimal.valueOf(1234L, 8); + assertTrue( + "the bigDecimal equivalent of 92233720368547.75806 is wrong", + valueOfJI.unscaledValue().toString().equals("1234") + && valueOfJI.scale() == 8); + assertTrue( + "the toString representation of 9223372036854775806 is wrong", + valueOfJI.toString().equals("0.00001234")); + valueOfJI = BigDecimal.valueOf(0, 3); + assertTrue( + "the bigDecimal equivalent of 92233720368547.75806 is wrong", + valueOfJI.unscaledValue().toString().equals("0") + && valueOfJI.scale() == 3); + assertTrue( + "the toString representation of 9223372036854775806 is wrong", + valueOfJI.toString().equals("0.000")); + + } + + protected void setUp() { + } + + protected void tearDown() { + } +}