harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tony Wu" <wuyue...@gmail.com>
Subject Re: [testing] assert message semantics Re: svn commit: r646259 - in /harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util: AbstractMapTest.java AbstractSequentialListTest.java CalendarTest.java
Date Thu, 10 Apr 2008 09:43:24 GMT
I agree on that we should provider as meaningful message as possible
if it could do some help.

On 4/10/08, Tim Ellison <t.p.ellison@gmail.com> wrote:
> Alexei Fedotov wrote:
> > Hello folks,
> >
> > Forgive my grumbling. Let me address the message of the assert:
> > "Should be equal to true."
> >
> >    assertEquals("Should be equal to true.", true, late.after(early));
> >
> > assert would say a message of the same nature automatically. The
> > message is helpful when it explains something JUnit checking facility
> > cannot deduce itself. E.g." 1.2.1 of JVM specification violated".
> >
>
> Agreed, that's why I flagged up the commit, and suggested just writing it
> as:
>
>  assertTrue(late.after(early));
>
> but I agree that where a further message is useful rather than a simple
> assertion it should be included,
>
>  assertTrue("1.2.1 of JVM specification violated", late.after(early));
>
> In some cases the message is probably overkill.
>
> Regards,
> Tim
>
>
> > On Wed, Apr 9, 2008 at 2:38 PM, Tim Ellison <t.p.ellison@gmail.com> wrote:
> >
> > > Tony,
> > >
> > >  I think these tests need a bit of tidy-up,
> > >
> > >  - lots of assertEquals true|false|null  where it would be more natural
> and
> > > readable to use assertTrue, assertNull etc.
> > >
> > >
> > >  Just write:
> > >  assertEquals("Should equal to be empty.", true, map.isEmpty());
> > >  as
> > >  assertTrue(map.isEmpty());
> > >
> > >  - contains unhelpful printing to stdout "System.out.println(map);"
> > >
> > >  Regards,
> > >  Tim
> > >
> > >
> > >
> > >  tonywu@apache.org wrote:
> > >
> > >
> > > > Author: tonywu
> > > > Date: Wed Apr  9 03:30:57 2008
> > > > New Revision: 646259
> > > >
> > > > URL: http://svn.apache.org/viewvc?rev=646259&view=rev
> > > > Log:
> > > > Apply patch for HARMONY-5699 ([classlib][luni][test] Add testcase to
> cover
> > > >
> > > untested methods of AbstractMap, AbstractSequencialList, Calender)
> > >
> > > > Modified:
> > > >
> > > >
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractMapTest.java
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractSequentialListTest.java
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CalendarTest.java
> > >
> > > > Modified:
> > > >
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractMapTest.java
> > >
> > > > URL:
> > > >
> > >
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractMapTest.java?rev=646259&r1=646258&r2=646259&view=diff
> > >
> ==============================================================================
> > >
> > > > ---
> > > >
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractMapTest.java
> > > (original)
> > >
> > > > +++
> > > >
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractMapTest.java
> > > Wed Apr  9 03:30:57 2008
> > >
> > > > @@ -132,10 +132,104 @@
> > > >               Object valueOut = aSpecialMap.remove(specialKey);
> > > >               assertSame("MyMap", valueOut, specialValue);
> > > >       }
> > > > +    +
> > > > +    /**
> > > > +     * @tests java.util.AbstractMap#clear()
> > > > +     */
> > > > +    public void test_clear() {
> > > > +        // normal clear()
> > > > +        AbstractMap map = new HashMap();
> > > > +        map.put(1, 1);
> > > > +        map.clear();
> > > > +        assertEquals("Should equal to be empty.", true,
> map.isEmpty());
> > > > +
> > > > +        // Special entrySet return a Set with no clear method.
> > > > +        AbstractMap myMap = new MocAbstractMap();
> > > > +        try {
> > > > +            myMap.clear();
> > > > +            fail("Should throw an unsupportedoprationexception");
> > > > +        } catch (UnsupportedOperationException e) {
> > > > +
> > > > +        }
> > > > +    }
> > > > +
> > > > +    class MocAbstractMap<K, V> extends AbstractMap {
> > > > +
> > > > +        public Set entrySet() {
> > > > +            Set set = new MySet();
> > > > +            return set;
> > > > +        }
> > > > +
> > > > +        class MySet extends HashSet {
> > > > +
> > > > +            public void clear() {
> > > > +                throw new
> UnsupportedOperationException();
> > > > +            }
> > > > +        }
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests
> java.util.AbstractMap#containsKey(Object)
> > > > +     */
> > > > +    public void test_containsKey() {
> > > > +        AbstractMap map = new AMT();
> > > > +
> > > > +        assertEquals("Shoule equal to be false.", false,
> > > >
> > > map.containsKey("k"));
> > >
> > > > +        assertEquals("Shoule equal to be false.", false,
> > > >
> > > map.containsKey(null));
> > >
> > > > +
> > > > +        map.put("k", "v");
> > > > +        map.put("key", null);
> > > > +        map.put(null, "value");
> > > > +        map.put(null, null);
> > > > +
> > > > +        assertEquals("Shoule equal to be true.", true,
> > > >
> > > map.containsKey("k"));
> > >
> > > > +        assertEquals("Shoule equal to be true.", true,
> > > >
> > > map.containsKey("key"));
> > >
> > > > +        assertEquals("Shoule equal to be true.", true,
> > > >
> > > map.containsKey(null));
> > >
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests
> java.util.AbstractMap#containsValue(Object)
> > > > +     */
> > > > +    public void test_containValue() {
> > > > +        AbstractMap map = new AMT();
> > > > +
> > > > +        assertEquals("Shoule equal to be false.", false,
> > > >
> > > map.containsValue("v"));
> > >
> > > > +        assertEquals("Shoule equal to be false.", false, map
> > > > +                .containsValue(null));
> > > > +        +        map.put("k", "v");
> > > > +        map.put("key", null);
> > > > +        map.put(null, "value");
> > > > +        +        System.out.println(map);
> > > > +        +        assertEquals("Shoule equal to be true.", true,
> > > >
> > > map.containsValue("v"));
> > >
> > > > +        assertEquals("Shoule equal to be true.", true, map
> > > > +                .containsValue("value"));
> > > > +        assertEquals("Shoule equal to be true.", true,
> > > >
> > > map.containsValue(null));
> > >
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests java.util.AbstractMap#get(Object)
> > > > +     */
> > > > +    public void test_get() {
> > > > +        AbstractMap map = new AMT();
> > > > +        assertEquals("Shoule equal to be null.", null,
> map.get("key"));
> > > > +        assertEquals("Shoule equal to be null.", null,
> map.get(null));
> > > > +
> > > > +        map.put("k", "v");
> > > > +        map.put("key", null);
> > > > +        map.put(null, "value");
> > > > +
> > > > +        assertEquals("Shoule equal to be v.", "v", map.get("k"));
> > > > +        assertEquals("Shoule equal to be null.", null,
> map.get("key"));
> > > > +        assertEquals("Shoule equal to be value.", "value",
> > > >
> > > map.get(null));
> > >
> > > > +    }
> > > >         /**
> > > > -        * @tests java.util.AbstractMap#values()
> > > > -        */
> > > > +     * @tests java.util.AbstractMap#values()
> > > > +     */
> > > >       public void test_values() {
> > > >               AbstractMap map1 = new HashMap(0);
> > > >               assertSame("HashMap(0)", map1.values(), map1.values());
> > > >
> > > > Modified:
> > > >
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractSequentialListTest.java
> > >
> > > > URL:
> > > >
> > >
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractSequentialListTest.java?rev=646259&r1=646258&r2=646259&view=diff
> > >
> ==============================================================================
> > >
> > > > ---
> > > >
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractSequentialListTest.java
> > > (original)
> > >
> > > > +++
> > > >
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractSequentialListTest.java
> > > Wed Apr  9 03:30:57 2008
> > >
> > > > @@ -52,7 +52,7 @@
> > > >    }
> > > >        /**
> > > > -     * @tests {@link
> java.util.AbstractSequentialList#addAll(int,
> > > >
> > > java.util.Collection)}
> > >
> > > > +     * @tests
> java.util.AbstractSequentialList#addAll(int,
> > > >
> > > java.util.Collection)
> > >
> > > >     */
> > > >    public void test_addAll_ILCollection() {
> > > >        AbstractSequentialList<String> al = new ASLT<String>();
> > > > @@ -65,6 +65,70 @@
> > > >        assertTrue("Should return true", al.addAll(2, c));
> //$NON-NLS-1$
> > > >    }
> > > >    +
> > > > +    /**
> > > > +     * @tests
> java.util.AbstractSequentialList#get(int)
> > > > +     */
> > > > +    public void test_get() {
> > > > +        AbstractSequentialList list = new MyAbstractSequentialList();
> > > > +        list.add(1);
> > > > +        list.add("value");
> > > > +        assertEquals("Should be equal to \"1\".", 1, list.get(0));
> > > > +        assertEquals("Should be equal to \"value\".", "value",
> > > >
> > > list.get(1));
> > >
> > > > +
> > > > +        // get value by index which is out of bounds
> > > > +        try {
> > > > +            list.get(list.size());
> > > > +            fail("Should throw IndexOutOfBoundsException.");
> > > > +        } catch (IndexOutOfBoundsException e) {
> > > > +            // expected
> > > > +        }
> > > > +        try {
> > > > +            list.get(-1);
> > > > +            fail("Should throw IndexOutOfBoundsException.");
> > > > +        } catch (IndexOutOfBoundsException e) {
> > > > +            // expected
> > > > +        }
> > > > +
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests
> java.util.AbstractSequentialList#remove(int)
> > > > +     */
> > > > +    public void test_remove() {
> > > > +        AbstractSequentialList list = new MyAbstractSequentialList();
> > > > +        list.add(1);
> > > > +
> > > > +        // normal test
> > > > +        assertEquals("Should be equal to \"1\".", 1, list.remove(0));
> > > > +
> > > > +        list.add("value");
> > > > +        assertEquals("Should be equal to \"value\".", "value",
> > > >
> > > list.remove(0));
> > >
> > > > +
> > > > +        // remove index is out of bounds
> > > > +        try {
> > > > +            list.remove(list.size());
> > > > +            fail("Should throw IndexOutOfBoundsException.");
> > > > +        } catch (IndexOutOfBoundsException e) {
> > > > +            // expected
> > > > +        }
> > > > +        try {
> > > > +            list.remove(-1);
> > > > +            fail("Should throw IndexOutOfBoundsException.");
> > > > +        } catch (IndexOutOfBoundsException e) {
> > > > +            // expected
> > > > +        }
> > > > +
> > > > +        // list dont't support remove operation
> > > > +        try {
> > > > +            AbstractSequentialList mylist = new
> > > >
> > > MockAbstractSequentialList();
> > >
> > > > +            mylist.remove(0);
> > > > +            fail("Should throw
> UnsupportedOperationException.");
> > > > +        } catch (UnsupportedOperationException e) {
> > > > +            // expected
> > > > +        }
> > > > +    }
> > > > +         public void test_set() throws Exception {
> > > >               MyAbstractSequentialList list = new
> > > >
> > > MyAbstractSequentialList();
> > >
> > > >               try {
> > > > @@ -80,10 +144,7 @@
> > > >               private LinkedList list = new LinkedList();
> > > >                 public ListIterator listIterator(int index) {
> > > > -                       ListIterator iter = list.listIterator();
> > > > -                       for (int i = 0; i < index; i++) {
> > > > -                               iter.next();
> > > > -                       }
> > > > +                       ListIterator iter = list.listIterator(index);
> > > >                       return iter;
> > > >               }
> > > >  @@ -92,4 +153,22 @@
> > > >                       return list.size();
> > > >               }
> > > >       }
> > > > +    +    class MockAbstractSequentialList<E> extends
> > > >
> > > AbstractSequentialList {
> > >
> > > > +        private LinkedList list = new LinkedList();
> > > > +
> > > > +        public ListIterator listIterator(int index) {
> > > > +            ListIterator iter = list.listIterator(index);
> > > > +            return iter;
> > > > +        }
> > > > +
> > > > +        @Override
> > > > +        public int size() {
> > > > +            return list.size();
> > > > +        }
> > > > +
> > > > +        public E remove(int location) {
> > > > +            throw new
> UnsupportedOperationException();
> > > > +        }
> > > > +    }
> > > >  }
> > > >
> > > > Modified:
> > > >
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CalendarTest.java
> > >
> > > > URL:
> > > >
> > >
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CalendarTest.java?rev=646259&r1=646258&r2=646259&view=diff
> > >
> ==============================================================================
> > >
> > > > ---
> > > >
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CalendarTest.java
> > > (original)
> > >
> > > > +++
> > > >
> > >
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CalendarTest.java
> > > Wed Apr  9 03:30:57 2008
> > >
> > > > @@ -19,9 +19,12 @@
> > > >  import java.util.Calendar;
> > > >  import java.util.Date;
> > > > +import java.util.GregorianCalendar;
> > > >  import java.util.Locale;
> > > >  import java.util.TimeZone;
> > > >  +import
> org.apache.harmony.testframework.serialization.SerializationTest;
> > > > +
> > > >  public class CalendarTest extends junit.framework.TestCase {
> > > >
> > > >       Locale defaultLocale;
> > > > @@ -471,7 +474,7 @@
> > > >    }
> > > >      /**
> > > > -     * @tests {@link
> java.util.Calendar#getActualMaximum(int)}
> > > > +     * @tests
> java.util.Calendar#getActualMaximum(int)
> > > >     */
> > > >    public void test_getActualMaximum_I() {
> > > >       Calendar c = new MockCalendar();
> > > > @@ -479,13 +482,283 @@
> > > >    }
> > > >        /**
> > > > -     * @tests {@link
> java.util.Calendar#getActualMinimum(int)}
> > > > +     * @tests
> java.util.Calendar#getActualMinimum(int)
> > > >     */
> > > >    public void test_getActualMinimum_I() {
> > > >       Calendar c = new MockCalendar();
> > > >       assertEquals("should be equal to 0", 0, c.getActualMinimum(0));
> > > >    }
> > > >  +    /**
> > > > +     * @tests java.util.Calendar#before(Object)
> > > > +     * @tests java.util.Calendar#after(Object)
> > > > +     */
> > > > +    public void test_before_after() {
> > > > +        Calendar early = Calendar.getInstance();
> > > > +        Calendar late = Calendar.getInstance();
> > > > +        // test by second
> > > > +        early.set(2008, 3, 20, 17, 28, 12);
> > > > +        late.set(2008, 3, 20, 17, 28, 22);
> > > > +        // test before()
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > early.before(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.before(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.before(early));
> > >
> > > > +        // test after();
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > late.after(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.after(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.after(late));
> > >
> > > > +
> > > > +        // test by minute
> > > > +        early.set(2008, 3, 20, 17, 18, 12);
> > > > +        late.set(2008, 3, 20, 17, 28, 12);
> > > > +        // test before()
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > early.before(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.before(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.before(early));
> > >
> > > > +        // test after();
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > late.after(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.after(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.after(late));
> > >
> > > > +
> > > > +        // test by hour
> > > > +        early.set(2008, 3, 20, 17, 28, 12);
> > > > +        late.set(2008, 3, 20, 27, 28, 12);
> > > > +        // test before()
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > early.before(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.before(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.before(early));
> > >
> > > > +        // test after();
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > late.after(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.after(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.after(late));
> > >
> > > > +
> > > > +        // test by day
> > > > +        early.set(2008, 3, 10, 17, 28, 12);
> > > > +        late.set(2008, 3, 20, 17, 28, 12);
> > > > +        // test before()
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > early.before(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.before(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.before(early));
> > >
> > > > +        // test after();
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > late.after(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.after(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.after(late));
> > >
> > > > +
> > > > +        // test by month
> > > > +        early.set(2008, 2, 20, 17, 28, 12);
> > > > +        late.set(2008, 3, 20, 17, 28, 12);
> > > > +        // test before()
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > early.before(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.before(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.before(early));
> > >
> > > > +        // test after();
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > late.after(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.after(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.after(late));
> > >
> > > > +
> > > > +        // test by year
> > > > +        early.set(2007, 3, 20, 17, 28, 12);
> > > > +        late.set(2008, 3, 20, 17, 28, 12);
> > > > +        // test before()
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > early.before(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.before(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.before(early));
> > >
> > > > +        // test after();
> > > > +        assertEquals("Should be equal to true.", true,
> > > >
> > > late.after(early));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > late.after(late));
> > >
> > > > +        assertEquals("Should be equal to false.", false,
> > > >
> > > early.after(late));
> > >
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests java.util.Calendar#clear()
> > > > +     * @tests java.util.Calendar#clear(int)
> > > > +     */
> > > > +    public void test_clear() {
> > > > +        Calendar calendar = Calendar.getInstance();
> > > > +
> > > > +        int count = 6;
> > > > +        int[] fields = new int[count];
> > > > +        int[] defaults = new int[count];
> > > > +
> > > > +        fields[0] = Calendar.YEAR;
> > > > +        fields[1] = Calendar.MONTH;
> > > > +        fields[2] = Calendar.DATE;
> > > > +        fields[3] = Calendar.HOUR_OF_DAY;
> > > > +        fields[4] = Calendar.MINUTE;
> > > > +        fields[5] = Calendar.SECOND;
> > > > +
> > > > +        defaults[0] = 1970;
> > > > +        defaults[1] = 0;
> > > > +        defaults[2] = 1;
> > > > +        defaults[3] = 0;
> > > > +        defaults[4] = 0;
> > > > +        defaults[5] = 0;
> > > > +
> > > > +        calendar.set(2008, 3, 20, 17, 28, 12);
> > > > +
> > > > +        // test clear(int)
> > > > +        for (int i = 0; i < fields.length; i++) {
> > > > +            int index = fields[i];
> > > > +            calendar.clear(index);
> > > > +            if (5 == index) {
> > > > +                // RI also don't change the value of DATE
> > > > +                assertEquals("Field " + index + " Should be equal to
> > > >
> > > 20.", 20,
> > >
> > > > +                        calendar.get(index));
> > > > +            } else if (11 == index) {
> > > > +                // RI also don't change the value of HOUR
> > > > +                assertEquals("Field " + index + " Should be equal to
> > > >
> > > 17.", 17,
> > >
> > > > +                        calendar.get(index));
> > > > +            } else {
> > > > +                // Other have been set to default values
> > > > +                assertEquals("Field " + index + " Should be equal to
> "
> > > > +                        + defaults[i] + ".", defaults[i],
> > > >
> > > calendar.get(index));
> > >
> > > > +            }
> > > > +        }
> > > > +
> > > > +        // test clear()
> > > > +        calendar.set(2008, 3, 20, 17, 28, 12);
> > > > +
> > > > +        calendar.clear();
> > > > +
> > > > +        for (int i = 0; i < fields.length; i++) {
> > > > +            int index = fields[i];
> > > > +            assertEquals("Field " + index + " Should be equal to "
> > > > +                    + defaults[i] + ".", defaults[i],
> > > >
> > > calendar.get(index));
> > >
> > > > +        }
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests java.util.Calendar#isSet(int)
> > > > +     */
> > > > +    public void test_isSet() {
> > > > +        Calendar calendar = Calendar.getInstance();
> > > > +        calendar.clear();
> > > > +        for (int i = 0; i < Calendar.FIELD_COUNT; i++) {
> > > > +            assertEquals("Should equal to be false.", false,
> > > >
> > > calendar.isSet(i));
> > >
> > > > +        }
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests
> java.util.Calendar#getAvailableLocales()
> > > > +     */
> > > > +    public void test_getAvailableLocales() {
> > > > +        Locale[] locales = Calendar.getAvailableLocales();
> > > > +        boolean exist = false;
> > > > +        for (int i = 0; i < locales.length; i++) {
> > > > +            Locale l = locales[i];
> > > > +            if (Locale.US.equals(l)) {
> > > > +                exist = true;
> > > > +                break;
> > > > +            }
> > > > +        }
> > > > +        assertEquals("Should at least contain Locale.US.", true,
> exist);
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests java.util.Calendar#getInstance(Locale)
> > > > +     * @tests
> java.util.Calendar#getInstance(TimeZone, Locale)
> > > > +     */
> > > > +    public void test_getInstance() {
> > > > +        // test getInstance(Locale)
> > > > +        Calendar us_calendar =
> Calendar.getInstance(Locale.US);
> > > > +        Calendar ch_calendar =
> Calendar.getInstance(Locale.CHINESE);
> > > > +        assertEquals("Should equal to be Sunday.", Calendar.SUNDAY,
> > > >
> > > us_calendar
> > >
> > > > +                .getFirstDayOfWeek());
> > > > +        assertEquals("Should equal to be Monday.", Calendar.MONDAY,
> > > >
> > > ch_calendar
> > >
> > > > +                .getFirstDayOfWeek());
> > > > +
> > > > +        // test getInstance(Locale, TimeZone)
> > > > +        Calendar gmt_calendar = Calendar.getInstance(TimeZone
> > > > +                .getTimeZone("GMT"), Locale.US);
> > > > +        assertEquals("Should equal to \"GMT\"",
> > > >
> > > TimeZone.getTimeZone("GMT"),
> > >
> > > > +                gmt_calendar.getTimeZone());
> > > > +        Calendar est_calendar = Calendar.getInstance(TimeZone
> > > > +                .getTimeZone("EST"), Locale.US);
> > > > +        assertEquals("Should equal to \"EST\"",
> > > >
> > > TimeZone.getTimeZone("EST")
> > >
> > > > +                .getID(),
> est_calendar.getTimeZone().getID());
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests java.util.Calendar#internalGet(int)
> > > > +     */
> > > > +    public void test_internalGet() {
> > > > +        MockGregorianCalendar c = new MockGregorianCalendar();
> > > > +        c.clear(Calendar.YEAR);
> > > > +        assertEquals("Should not be equal to 1970.", 0, c
> > > > +                .internal_get(Calendar.YEAR));
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests java.util.Calendar#hashCode()
> > > > +     */
> > > > +    public void test_hashcode() {
> > > > +        Calendar calendar =
> Calendar.getInstance(Locale.JAPAN);
> > > > +        assertEquals("Should equal to true.", true,
> > > > +                calendar.hashCode() == calendar.hashCode());
> > > > +        Calendar en_calendar =
> Calendar.getInstance(Locale.ENGLISH);
> > > > +        assertEquals("Should equal to false.", false,
> > > > +                en_calendar.hashCode() == calendar.hashCode());
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests java.util.Calendar#roll(int, int)
> > > > +     */
> > > > +    public void test_roll() {
> > > > +        Calendar calendar = Calendar.getInstance();
> > > > +        calendar.set(2008, 3, 20, 17, 28, 12);
> > > > +
> > > > +        // roll up
> > > > +        calendar.roll(Calendar.DATE, 5);
> > > > +        assertEquals("Shoule be equal to 25", 25,
> > > >
> > > calendar.get(Calendar.DATE));
> > >
> > > > +
> > > > +        // roll down
> > > > +        calendar.roll(Calendar.DATE, -5);
> > > > +        assertEquals("Shoule be equal to 20", 20,
> > > >
> > > calendar.get(Calendar.DATE));
> > >
> > > > +
> > > > +        // roll 0
> > > > +        calendar.roll(Calendar.DATE, 0);
> > > > +        assertEquals("Shoule be equal to 20", 20,
> > > >
> > > calendar.get(Calendar.DATE));
> > >
> > > > +
> > > > +        // roll overweight
> > > > +        calendar.set(2008, 1, 31, 17, 28, 12);
> > > > +        calendar.roll(Calendar.MONTH, 1);
> > > > +        assertEquals("Shoule be equal to 2", 2,
> > > >
> > > calendar.get(Calendar.DATE));
> > >
> > > > +
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests java.util.Calendar#toString()
> > > > +     */
> > > > +    public void test_toString() {
> > > > +        Calendar calendar = Calendar.getInstance();
> > > > +        assertEquals("Should be the current time with no '?' in the
> > > >
> > > string.",
> > >
> > > > +                true, (calendar.toString() != null)
> > > > +                        && (calendar.toString() instanceof String)
> > > > +                        &&
> (calendar.toString().indexOf("?") == -1));
> > > > +        calendar.clear();
> > > > +        assertEquals(
> > > > +                "Should be the empty but not null. With several '?'s
> in
> > > >
> > > the string.",
> > >
> > > > +                true, (calendar.toString() != null)
> > > > +                        && (calendar.toString() instanceof String)
> > > > +                        &&
> (calendar.toString().indexOf("?") != -1));
> > > > +    }
> > > > +
> > > > +    /**
> > > > +     * @tests serialization/deserialization.
> > > > +     */
> > > > +    public void testSerializationSelf() throws Exception {
> > > > +        Calendar calendar = Calendar.getInstance();
> > > > +        calendar.set(2008, 3, 20, 17, 28, 12);
> > > > +
> > > > +        SerializationTest.verifySelf(calendar);
> > > > +    }
> > > > +
> > > > +
> > > > +    private class MockGregorianCalendar extends GregorianCalendar {
> > > > +        public int internal_get(int field) {
> > > > +            return super.internalGet(field);
> > > > +        }
> > > > +    }
> > > >      private class MockCalendar extends Calendar {
> > > >
> > > >
> > > >
> > > >
> > > >
> > >
> >
> >
> >
> >
>


-- 
Tony Wu
China Software Development Lab, IBM

Mime
View raw message