From bbcf8411d9ef361a20f5d60a322b154dbb9ca184 Mon Sep 17 00:00:00 2001 From: John Vu Date: Tue, 7 Jul 2009 16:30:09 +0000 Subject: [PATCH] ICU-6958 Increased code coverage for com.ibm.icu.text especially adding test cases in DecompressionTest, DateIntervalFormatTest, BreakIteratorTest, and SearchTest. X-SVN-Rev: 26232 --- .../com/ibm/icu/text/CollatorServiceShim.java | 4 +- .../com/ibm/icu/text/DateIntervalInfo.java | 2 + .../test/compression/DecompressionTest.java | 3 - .../test/format/DateIntervalFormatTest.java | 104 ++++++++++++++++++ .../icu/dev/test/rbbi/BreakIteratorTest.java | 45 +++++++- .../ibm/icu/dev/test/search/SearchTest.java | 29 ++++- 6 files changed, 179 insertions(+), 8 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/CollatorServiceShim.java b/icu4j/main/classes/core/src/com/ibm/icu/text/CollatorServiceShim.java index e5e2e278ec1..e64df7896fe 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/CollatorServiceShim.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/CollatorServiceShim.java @@ -26,12 +26,14 @@ final class CollatorServiceShim extends Collator.ServiceShim { // if (service.isDefault()) { // return new RuleBasedCollator(locale); // } - try { ULocale[] actualLoc = new ULocale[1]; Collator coll = (Collator)service.get(locale, actualLoc); if (coll == null) { + ///CLOVER:OFF + //Can't really change coll after it's been initialized throw new MissingResourceException("Could not locate Collator data", "", ""); + ///CLOVER:ON } coll = (Collator) coll.clone(); coll.setLocale(actualLoc[0], actualLoc[0]); // services make no distinction between actual & valid diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalInfo.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalInfo.java index 6e851d8c214..ad74b4a10dc 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalInfo.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalInfo.java @@ -764,7 +764,9 @@ public class DateIntervalInfo implements Cloneable, Freezable, Serializable { other.frozen = false; return other; } catch ( CloneNotSupportedException e ) { + ///CLOVER:OFF throw new IllegalStateException("clone is not supported"); + ///CLOVER:ON } } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/compression/DecompressionTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/compression/DecompressionTest.java index 1cfa1aa7deb..3eca66765eb 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/compression/DecompressionTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/compression/DecompressionTest.java @@ -166,9 +166,6 @@ public class DecompressionTest extends TestFmwk { ud.decompress(null, 0, 0, null, charValid, 0, 1); errln("UnicodeDecompressor.decompress was suppose to return an exception."); } catch(Exception e){} - - - } } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java index fd588d78459..5341fc56a96 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java @@ -22,6 +22,7 @@ import com.ibm.icu.text.DateFormat; import com.ibm.icu.text.DateIntervalFormat; import com.ibm.icu.text.DateIntervalInfo; import com.ibm.icu.text.SimpleDateFormat; +import com.ibm.icu.text.DateIntervalInfo.PatternInfo; import com.ibm.icu.util.Calendar; import com.ibm.icu.util.DateInterval; import com.ibm.icu.util.ULocale; @@ -1024,4 +1025,107 @@ public class DateIntervalFormatTest extends com.ibm.icu.dev.test.TestFmwk { errln("FAIL: Exception - " + e.getClass().getName()); } } + + /* Tests the method + * public boolean equals(Object a) + */ + public void TestDateIntervalInfoEquals(){ + // Tests when "if ( a instanceof PatternInfo )" is false + DateIntervalInfo diia = new DateIntervalInfo(); + if(diia.equals("dummy") != false){ + errln("DateIntervalInfo.equals(Object a) was suppose to return " + + "false for a String object."); + } + if(diia.equals(0) != false){ + errln("DateIntervalInfo.equals(Object a) was suppose to return " + + "false for an Integer object."); + } + if(diia.equals(0.0) != false){ + errln("DateIntervalInfo.equals(Object a) was suppose to return " + + "false for an Integer object."); + } + if(diia.equals(new Object()) != false){ + errln("DateIntervalInfo.equals(Object a) was suppose to return " + + "false for an Integer object."); + } + } + + /* Tests the method + * public Object cloneAsThawed() + */ + public void TestCloseAsThawed(){ + DateIntervalInfo dii = new DateIntervalInfo(); + try{ + dii.cloneAsThawed(); + } catch(Exception e){ + errln("DateIntervalInfo.closeAsThawed() was not suppose to return " + + "an exception."); + } + } + + /* Tests the method + * public boolean isFrozen() + */ + public void TestIsFrozen(){ + DateIntervalInfo dii = new DateIntervalInfo(); + if(dii.isFrozen() != false){ + errln("DateIntervalInfo.isFrozen() is suppose to return false."); + } + dii.freeze(); + + if(dii.isFrozen() != true){ + errln("DateIntervalInfo.isFrozen() is suppose to return true."); + } + } + + /* Tests the method + * public boolean clone() + */ + public void TestClone(){ + DateIntervalInfo dii = new DateIntervalInfo(new ULocale("en_US")); + DateIntervalInfo dii_clone = (DateIntervalInfo) dii.clone(); + dii_clone.freeze(); + + // Tests when "if ( frozen )" is true + if(!dii.equals(dii_clone)){ + errln("DateIntervalInfo.clone() is suppose to return true for " + + "an original DateIntervalInfo object and a clone of the " + + "original DateIntervalInfo object."); + } + } + + /* Tests the method + * public void setFallbackIntervalPattern(String fallbackPattern) + */ + public void TestSetFallbackIntervalPattern(){ + DateIntervalInfo dii = new DateIntervalInfo(new ULocale("en_US")); + // Tests when "if ( frozen )" is true + try{ + dii.freeze(); + dii.setFallbackIntervalPattern(""); + errln("DateIntervalInfo.setFallbackIntervalPattern(String fallbackPattern) " + + "was suppose to return an exception for a frozen object."); + } catch (Exception e){} + + // Tests when "if ( firstPatternIndex == -1 || secondPatternIndex == -1 )" is true + dii = (DateIntervalInfo) dii.cloneAsThawed(); + try{ + dii.setFallbackIntervalPattern(""); + errln("DateIntervalInfo.setFallbackIntervalPattern(String fallbackPattern) " + + "was suppose to return an exception for a string object of ''."); + } catch(Exception e){} + + try{ + dii.setFallbackIntervalPattern("0"); + errln("DateIntervalInfo.setFallbackIntervalPattern(String fallbackPattern) " + + "was suppose to return an exception for a string object of 0."); + } catch(Exception e){} + + // Tests when "if ( firstPatternIndex > secondPatternIndex )" is true + dii.setFallbackIntervalPattern("{1}{0}"); + if(dii.getDefaultOrder() != true) + errln("DateIntervalInfo.setFallbackIntervalPattern(String fallbackPattern) " + + "was suppose to change the variable 'fFirstDateInPtnIsLaterDate' " + + "to true."); + } } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/rbbi/BreakIteratorTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/rbbi/BreakIteratorTest.java index 8bd628e15ae..c191e683a2e 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/rbbi/BreakIteratorTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/rbbi/BreakIteratorTest.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 1996-2007, International Business Machines Corporation and * + * Copyright (C) 1996-2009, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -8,6 +8,10 @@ package com.ibm.icu.dev.test.rbbi; import com.ibm.icu.dev.test.*; import com.ibm.icu.text.BreakIterator; +import com.ibm.icu.text.DictionaryBasedBreakIterator; + +import java.io.IOException; +import java.io.InputStream; import java.text.StringCharacterIterator; import java.util.Locale; import java.util.Vector; @@ -23,7 +27,6 @@ public class BreakIteratorTest extends TestFmwk public static void main(String[] args) throws Exception { new BreakIteratorTest().run(args); } - public BreakIteratorTest() { @@ -842,5 +845,41 @@ public class BreakIteratorTest extends TestFmwk errln("ERR: Failed to create an instance type: " + type + " / locale: " + loc + " / exception: " + e.getMessage()); } } -} + + /* Tests the constructors + * public DictionaryBasedBreakIterator(String rules, ... + * public DictionaryBasedBreakIterator(InputStream compiledRules, ... + */ + public void TestDictionaryBasedBreakIterator() throws IOException{ + // The following class allows the testing of the constructor + // public DictionaryBasedBreakIterator(String rules, ... + class TestDictionaryBasedBreakIterator extends DictionaryBasedBreakIterator{ + public TestDictionaryBasedBreakIterator() throws IOException{ + super("",null); + } + } + try{ + @SuppressWarnings("unused") + TestDictionaryBasedBreakIterator td = new TestDictionaryBasedBreakIterator(); + errln("DictionaryBasedBreakIterator constructor is suppose to return an " + + "exception for an empty string."); + } catch(Exception e){} + // The following class allows the testing of the constructor + // public DictionaryBasedBreakIterator(InputStream compiledRules, ... + class TestDictionaryBasedBreakIterator1 extends DictionaryBasedBreakIterator{ + public TestDictionaryBasedBreakIterator1() throws IOException{ + super((InputStream)null,null); + } + + } + try{ + @SuppressWarnings("unused") + TestDictionaryBasedBreakIterator1 td1 = new TestDictionaryBasedBreakIterator1(); + errln("DictionaryBasedBreakIterator constructor is suppose to return an " + + "exception for an null input stream."); + } catch(Exception e){} + + + } +} \ No newline at end of file diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/search/SearchTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/search/SearchTest.java index 98b24f8a418..9cd159218ff 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/search/SearchTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/search/SearchTest.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2000-2008, International Business Machines Corporation and * + * Copyright (C) 2000-2009, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -885,6 +885,17 @@ public class SearchTest extends TestFmwk { if (!assertEqualWithStringSearch(strsrch, COLLATOR[0])) { errln("Error searching collator test"); } + + /* Tests the method + * public void setCollator(RuleBasedCollator collator) + */ + // Tests when "if (collator == null)" is true + try{ + StringSearch ss = new StringSearch(CONTRACTIONRULE, CONTRACTIONRULE); + ss.setCollator(null); + errln("StringSearch.setCollator(RuleBasedCollator collator) was " + + "suppose to return an exception for a null RuleBasedCollator"); + } catch(Exception e){} } public void TestCollatorCanonical() { @@ -1648,6 +1659,22 @@ public class SearchTest extends TestFmwk { if (strsrch != null) { strsrch = null; } + + /* Tests the method + * public void setPattern(String pattern) + */ + // Tests when "if (pattern == null || pattern.length() <= 0)" is true + StringSearch ss = new StringSearch(IGNORABLERULE, IGNORABLERULE); + try{ + ss.setPattern(null); + errln("StringSearch.setPattern(String pattern) is suppose to " + + "return an exception for a null parameter."); + } catch(Exception e){} + try{ + ss.setPattern(""); + errln("StringSearch.setPattern(String pattern) is suppose to " + + "return an exception for an empty string parameter."); + } catch(Exception e){} } public void TestPatternCanonical() {