001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.util;
016:
017: import java.util.Iterator;
018:
019: import org.testng.Assert;
020: import org.testng.annotations.Test;
021:
022: public class IntegerRangeTest extends Assert {
023: @Test
024: public void start_less_than_finish() {
025: IntegerRange r = new IntegerRange(1, 3);
026:
027: assertEquals(r.toString(), "1..3");
028:
029: assertEquals(r.getStart(), 1);
030: assertEquals(r.getFinish(), 3);
031:
032: Iterator<Integer> i = r.iterator();
033:
034: assertEquals(i.next().intValue(), 1);
035: assertEquals(i.next().intValue(), 2);
036:
037: assertTrue(i.hasNext());
038:
039: assertEquals(i.next().intValue(), 3);
040:
041: assertFalse(i.hasNext());
042:
043: try {
044: i.next();
045: unreachable();
046: } catch (IllegalStateException ex) {
047: }
048: }
049:
050: @Test
051: public void start_same_as_finish() {
052: IntegerRange r = new IntegerRange(3, 3);
053:
054: Iterator<Integer> i = r.iterator();
055:
056: assertTrue(i.hasNext());
057:
058: assertEquals(i.next().intValue(), 3);
059:
060: assertFalse(i.hasNext());
061: }
062:
063: @Test
064: public void finish_less_than_start() {
065: IntegerRange r = new IntegerRange(3, 1);
066:
067: assertEquals(r.toString(), "3..1");
068:
069: Iterator<Integer> i = r.iterator();
070:
071: assertEquals(i.next().intValue(), 3);
072: assertEquals(i.next().intValue(), 2);
073:
074: assertTrue(i.hasNext());
075:
076: assertEquals(i.next().intValue(), 1);
077:
078: assertFalse(i.hasNext());
079:
080: try {
081: i.next();
082: unreachable();
083: } catch (IllegalStateException ex) {
084: }
085: }
086:
087: @Test
088: public void hash_code_and_equals() {
089: IntegerRange r1 = new IntegerRange(1, 100);
090: IntegerRange r2 = new IntegerRange(1, 100);
091: IntegerRange r3 = new IntegerRange(1, 10);
092:
093: assertEquals(r1.hashCode(), r2.hashCode());
094: assertFalse(r1.hashCode() == r3.hashCode());
095:
096: assertTrue(r1.equals(r1));
097: assertEquals(r1, r2);
098:
099: assertFalse(r1.equals(r3));
100: assertFalse(r1.equals(this ));
101: assertFalse(r1.equals(null));
102: assertFalse(r1.equals(new IntegerRange(3, 30)));
103: }
104:
105: @Test
106: public void iterator_remove_not_supported() {
107: IntegerRange r = new IntegerRange(1, 3);
108:
109: Iterator<Integer> i = r.iterator();
110:
111: assertEquals(i.next(), new Integer(1));
112:
113: try {
114: i.remove();
115: unreachable();
116: } catch (UnsupportedOperationException ex) {
117: // Expected, ignored.
118: }
119:
120: assertEquals(i.next(), new Integer(2));
121: }
122:
123: private final void unreachable() {
124: throw new AssertionError("This code should be unreachable.");
125: }
126: }
|