001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.util;
032:
033: import java.util.*;
034: import junit.framework.TestCase;
035:
036: /**
037: * @author Taras Puchko
038: */
039: public class EnumSet_TestCase extends TestCase {
040:
041: private static enum Day {
042: SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIEDAY, SATURDAY
043: }
044:
045: public void testAllOf() {
046: EnumSet<Day> days = EnumSet.allOf(Day.class);
047: assertEquals(7, days.size());
048: assertContent(days, Day.values());
049: }
050:
051: public void testComplementOf() {
052: EnumSet<Day> days = EnumSet.complementOf(EnumSet.of(Day.SUNDAY,
053: Day.TUESDAY, Day.THURSDAY, Day.SATURDAY));
054: assertContent(days, Day.MONDAY, Day.WEDNESDAY, Day.FRIEDAY);
055: }
056:
057: public void testCopyOf() {
058: EnumSet<Day> enumSetCopy = EnumSet.copyOf(EnumSet.of(
059: Day.SUNDAY, Day.SATURDAY));
060: assertContent(enumSetCopy, Day.SUNDAY, Day.SATURDAY);
061:
062: EnumSet<Day> collectionCopy = EnumSet
063: .copyOf((Collection<Day>) EnumSet.of(Day.SUNDAY,
064: Day.SATURDAY));
065: assertContent(collectionCopy, Day.SUNDAY, Day.SATURDAY);
066:
067: EnumSet<Day> listCopy = EnumSet.copyOf(Arrays.asList(
068: Day.SATURDAY, Day.SUNDAY));
069: assertContent(listCopy, Day.SUNDAY, Day.SATURDAY);
070:
071: try {
072: EnumSet.copyOf(Collections.<Day> emptyList());
073: fail();
074: } catch (IllegalArgumentException e) {
075: //ok
076: }
077: }
078:
079: public void testNoneOf() {
080: EnumSet<Day> days = EnumSet.noneOf(Day.class);
081: assertTrue(days.isEmpty());
082: }
083:
084: public void testOf() {
085: assertContent(EnumSet.of(Day.FRIEDAY), Day.FRIEDAY);
086: assertContent(EnumSet.of(Day.SUNDAY, Day.MONDAY), Day.SUNDAY,
087: Day.MONDAY);
088: assertContent(EnumSet.of(Day.SUNDAY, Day.MONDAY, Day.TUESDAY),
089: Day.SUNDAY, Day.MONDAY, Day.TUESDAY);
090: assertContent(EnumSet.of(Day.SUNDAY, Day.MONDAY, Day.TUESDAY,
091: Day.WEDNESDAY), Day.SUNDAY, Day.MONDAY, Day.TUESDAY,
092: Day.WEDNESDAY);
093: assertContent(EnumSet.of(Day.SUNDAY, Day.MONDAY, Day.TUESDAY,
094: Day.WEDNESDAY, Day.THURSDAY), Day.SUNDAY, Day.MONDAY,
095: Day.TUESDAY, Day.WEDNESDAY, Day.THURSDAY);
096: assertContent(EnumSet.of(Day.SUNDAY, Day.MONDAY, Day.TUESDAY,
097: Day.WEDNESDAY, Day.THURSDAY, Day.FRIEDAY), Day.SUNDAY,
098: Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY, Day.THURSDAY,
099: Day.FRIEDAY);
100: }
101:
102: public void testRange() throws Exception {
103: assertContent(EnumSet.range(Day.TUESDAY, Day.THURSDAY),
104: Day.TUESDAY, Day.WEDNESDAY, Day.THURSDAY);
105: try {
106: EnumSet.range(Day.WEDNESDAY, Day.SUNDAY);
107: } catch (IllegalArgumentException e) {
108: //ok
109: }
110: }
111:
112: public void testAdd() {
113: EnumSet<Day> days = EnumSet.of(Day.SUNDAY);
114: try {
115: days.add(null);
116: fail();
117: } catch (NullPointerException e) {
118: //ok
119: }
120: }
121:
122: public void testIterator() {
123: EnumSet<Day> days = EnumSet.of(Day.FRIEDAY, Day.WEDNESDAY,
124: Day.SUNDAY, Day.SATURDAY, Day.TUESDAY);
125: Iterator<Day> iterator = days.iterator();
126: days.add(Day.THURSDAY);
127: assertEquals(Day.SUNDAY, iterator.next());
128: days.add(Day.MONDAY);
129: assertEquals(Day.TUESDAY, iterator.next());
130: assertEquals(Day.WEDNESDAY, iterator.next());
131: assertEquals(Day.FRIEDAY, iterator.next());
132: assertEquals(Day.SATURDAY, iterator.next());
133: assertFalse(iterator.hasNext());
134: }
135:
136: public void testClone() throws Exception {
137: EnumSet<Day> days = EnumSet.of(Day.SUNDAY);
138: EnumSet<Day> clone = days.clone();
139: assertContent(clone, Day.SUNDAY);
140: }
141:
142: private void assertContent(EnumSet<Day> set, Day... days) {
143: assertEquals(days.length, set.size());
144: List<Day> list = Arrays.asList(days);
145: for (Day day : Day.values()) {
146: if (list.contains(day)) {
147: assertTrue(set.contains(day));
148: } else {
149: assertFalse(set.contains(day));
150: }
151: }
152: }
153: }
|