Apply patch for HARMONY-6429: [test] Improve the method coverage rate of unit test for java.util
git-svn-id: https://svn.apache.org/repos/asf/harmony/enhanced/classlib/trunk@901111 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractListTest.java b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractListTest.java
index d302728..b659e64 100644
--- a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractListTest.java
+++ b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractListTest.java
@@ -430,4 +430,77 @@
holder.clear();
}
+
+ /**
+ * @tests {@link java.util.AbstractList#indexOf(Object)}
+ */
+ public void test_indexOf_Ljava_lang_Object() {
+ AbstractList<Integer> list = new MockArrayList<Integer>();
+ Integer[] array = { 1, 2, 3, 4, 5 };
+ list.addAll(Arrays.asList(array));
+
+ assertEquals("find 0 in the list do not contain 0", -1, list
+ .indexOf(new Integer(0)));
+ assertEquals("did not return the right location of element 3", 2, list
+ .indexOf(new Integer(3)));
+ assertEquals("find null in the list do not contain null element", -1,
+ list.indexOf(null));
+ list.add(null);
+ assertEquals("did not return the right location of element null", 5,
+ list.indexOf(null));
+ }
+
+ /**
+ * @add tests {@link java.util.AbstractList#lastIndexOf(Object)}
+ */
+ public void test_lastIndexOf_Ljava_lang_Object() {
+ AbstractList<Integer> list = new MockArrayList<Integer>();
+ Integer[] array = { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 };
+ list.addAll(Arrays.asList(array));
+
+ assertEquals("find 6 in the list do not contain 6", -1, list
+ .lastIndexOf(new Integer(6)));
+ assertEquals("did not return the right location of element 4", 6, list
+ .lastIndexOf(new Integer(4)));
+ assertEquals("find null in the list do not contain null element", -1,
+ list.lastIndexOf(null));
+ list.add(null);
+ assertEquals("did not return the right location of element null", 10,
+ list.lastIndexOf(null));
+ }
+
+ /**
+ * @add tests {@link java.util.AbstractList#remove(int)}
+ * @add tests {@link java.util.AbstractList#set(int, Object)}
+ */
+ public void test_remove_I() {
+ class MockList<E> extends AbstractList<E> {
+ private ArrayList<E> list;
+
+ @Override
+ public E get(int location) {
+ return list.get(location);
+ }
+
+ @Override
+ public int size() {
+ return list.size();
+ }
+
+ }
+ AbstractList<Integer> list = new MockList<Integer>();
+ try {
+ list.remove(0);
+ fail("should throw UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ // expected
+ }
+
+ try {
+ list.set(0, null);
+ fail("should throw UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ // expected
+ }
+ }
}
diff --git a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/InvalidPropertiesFormatExceptionTest.java b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/InvalidPropertiesFormatExceptionTest.java
index 68df0d5..63ab11e 100644
--- a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/InvalidPropertiesFormatExceptionTest.java
+++ b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/InvalidPropertiesFormatExceptionTest.java
@@ -36,4 +36,15 @@
}
}
+ /**
+ * @tests {@link java.util.InvalidPropertiesFormatException#InvalidPropertiesFormatException(Throwable)}
+ */
+ public void test_Constructor_Ljava_lang_Throwable() {
+ Throwable throwable = new Throwable();
+ InvalidPropertiesFormatException exception = new InvalidPropertiesFormatException(
+ throwable);
+ assertEquals("the casue did not equals argument passed in constructor",
+ throwable, exception.getCause());
+ }
+
}
diff --git a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ListResourceBundleTest.java b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ListResourceBundleTest.java
index afba601..04e0ba5 100644
--- a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ListResourceBundleTest.java
+++ b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ListResourceBundleTest.java
@@ -32,8 +32,8 @@
String name = "tests.support.Support_TestResource";
Locale.setDefault(new Locale("en", "US"));
bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
- Enumeration keys = bundle.getKeys();
- Vector result = new Vector();
+ Enumeration<String> keys = bundle.getKeys();
+ Vector<String> result = new Vector<String>();
while (keys.hasMoreElements()) {
result.addElement(keys.nextElement());
}
diff --git a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java
index a5c4d8b..c1bcb22 100644
--- a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java
+++ b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java
@@ -126,6 +126,8 @@
Properties myProps = new Properties();
myProps.setProperty("Abba", "Cadabra");
myProps.setProperty("Open", "Sesame");
+ myProps.setProperty("LongProperty",
+ "a long long long long long long long property");
myProps.list(ps);
ps.flush();
String propList = baos.toString();
@@ -133,6 +135,16 @@
propList.indexOf("Abba=Cadabra") >= 0);
assertTrue("Property list innacurate",
propList.indexOf("Open=Sesame") >= 0);
+ assertTrue("property list do not conatins \"...\"", propList
+ .indexOf("...") != -1);
+
+ ps = null;
+ try {
+ myProps.list(ps);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
}
/**
@@ -144,6 +156,8 @@
Properties myProps = new Properties();
myProps.setProperty("Abba", "Cadabra");
myProps.setProperty("Open", "Sesame");
+ myProps.setProperty("LongProperty",
+ "a long long long long long long long property");
myProps.list(pw);
pw.flush();
String propList = baos.toString();
@@ -151,6 +165,13 @@
propList.indexOf("Abba=Cadabra") >= 0);
assertTrue("Property list innacurate",
propList.indexOf("Open=Sesame") >= 0);
+ pw = null;
+ try {
+ myProps.list(pw);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
}
/**
@@ -286,7 +307,8 @@
* @tests java.util.Properties#propertyNames()
*/
public void test_propertyNames() {
- Enumeration names = tProps.propertyNames();
+ Properties myPro = new Properties(tProps);
+ Enumeration names = myPro.propertyNames();
while (names.hasMoreElements()) {
String p = (String) names.nextElement();
assertTrue("Incorrect names returned", p.equals("test.prop")
@@ -433,6 +455,13 @@
.getProperty("key2"));
assertEquals("Failed to load correct properties", "value1", prop
.getProperty("key1"));
+
+ try {
+ prop.loadFromXML(null);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
}
/**
@@ -492,6 +521,13 @@
assertTrue("Stored property list not equal to original", myProps2
.getProperty(nextKey).equals(myProps.getProperty(nextKey)));
}
+
+ try {
+ myProps.storeToXML(out, null, null);
+ fail("should throw nullPointerException");
+ } catch (NullPointerException ne) {
+ // expected
+ }
}
/**
diff --git a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertyResourceBundleTest.java b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertyResourceBundleTest.java
index 3c5aacb..b5089e7 100644
--- a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertyResourceBundleTest.java
+++ b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertyResourceBundleTest.java
@@ -17,9 +17,11 @@
package org.apache.harmony.luni.tests.java.util;
+import java.io.IOException;
import java.util.Enumeration;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
+import java.util.ResourceBundle;
import java.util.Vector;
public class PropertyResourceBundleTest extends junit.framework.TestCase {
@@ -95,4 +97,39 @@
*/
protected void tearDown() {
}
+
+ /**
+ * @add tests {@link java.util.PropertyResourceBundle#Enumeration}
+ */
+ public void test_access$0_Enumberation() throws IOException {
+ class MockResourceBundle extends PropertyResourceBundle {
+ MockResourceBundle(java.io.InputStream stream) throws IOException {
+ super(stream);
+ }
+
+ @Override
+ protected void setParent(ResourceBundle bundle) {
+ super.setParent(bundle);
+ }
+ }
+
+ java.io.InputStream localStream = new java.io.ByteArrayInputStream(
+ "p3=three\np4=four".getBytes());
+ MockResourceBundle localPrb = new MockResourceBundle(localStream);
+ localPrb.setParent(prb);
+ Enumeration<String> keys = localPrb.getKeys();
+ Vector<String> contents = new Vector<String>();
+ while (keys.hasMoreElements()) {
+ contents.add(keys.nextElement());
+ }
+
+ assertEquals("did not get the right number of properties", 4, contents
+ .size());
+ assertTrue("did not get the parent property p1", contents
+ .contains("p1"));
+ assertTrue("did not get the parent property p2", contents
+ .contains("p2"));
+ assertTrue("did not get the local property p3", contents.contains("p3"));
+ assertTrue("did not get the local property p4", contents.contains("p4"));
+ }
}
diff --git a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimeZoneTest.java b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimeZoneTest.java
index 2b98023..3364f52 100644
--- a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimeZoneTest.java
+++ b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimeZoneTest.java
@@ -17,10 +17,12 @@
package org.apache.harmony.luni.tests.java.util;
+import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.GregorianCalendar;
+import java.util.List;
import java.util.Locale;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
@@ -207,4 +209,67 @@
protected void tearDown() {
}
+
+ /**
+ * @add test {@link java.util.TimeZone#getAvailableIDs(int)}
+ */
+ public void test_getAvailableIDs_I() {
+ TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
+ int rawoffset = tz.getRawOffset();
+ String[] ids = TimeZone.getAvailableIDs(rawoffset);
+ List<String> idList = Arrays.asList(ids);
+ assertTrue("Asia/shanghai and Hongkong should have the same rawoffset",
+ idList.contains("Hongkong"));
+ }
+
+ /**
+ * @add test {@link java.util.TimeZone#getDisplayName()}
+ */
+ public void test_getDisplayName() {
+ TimeZone defaultZone = TimeZone.getDefault();
+ Locale defaulLocal = Locale.getDefault();
+ String defaultName = defaultZone.getDisplayName();
+ String expectedName = defaultZone.getDisplayName(defaulLocal);
+ assertEquals(
+ "getDispalyName() did not return the default Locale suitable name",
+ expectedName, defaultName);
+ }
+
+ /**
+ * @add test {@link java.util.TimeZone#getDisplayName(boolean, int)}
+ */
+ public void test_getDisplayName_ZI() {
+ TimeZone defaultZone = TimeZone.getDefault();
+ Locale defaultLocale = Locale.getDefault();
+ String actualName = defaultZone.getDisplayName(false, TimeZone.LONG);
+ String expectedName = defaultZone.getDisplayName(false, TimeZone.LONG,
+ defaultLocale);
+ assertEquals(
+ "getDisplayName(daylight,style) did not return the default locale suitable name",
+ expectedName, actualName);
+ }
+
+ /**
+ * @add test {@link java.util.TimeZone#hasSameRules(TimeZone)}
+ */
+ public void test_hasSameRules_Ljava_util_TimeZone() {
+ TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
+ int offset = tz.getRawOffset();
+
+ String[] ids = TimeZone.getAvailableIDs(offset);
+ int i = 0;
+ if (ids.length != 0) {
+ while (true) {
+ if (!(ids[i].equalsIgnoreCase(tz.getID()))) {
+ TimeZone sameZone = TimeZone.getTimeZone(ids[i]);
+ assertTrue(tz.hasSameRules(sameZone));
+ break;
+ } else {
+ i++;
+ }
+ }
+ }
+ assertFalse("should return false when parameter is null", tz
+ .hasSameRules(null));
+ }
}