001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing;
021:
022: @SuppressWarnings("unchecked")
023: public class SizeSequenceTest extends BasicSwingTestCase {
024: private SizeSequence sizeSequence;
025:
026: private int[] sizes;
027:
028: @Override
029: protected void setUp() throws Exception {
030: super .setUp();
031: sizes = new int[] { 1, 2, 3, 4, 5 };
032: sizeSequence = new SizeSequence(sizes);
033: }
034:
035: @Override
036: protected void tearDown() throws Exception {
037: super .tearDown();
038: sizeSequence = null;
039: sizes = null;
040: }
041:
042: public void testSizeSequence() throws Exception {
043: sizeSequence = new SizeSequence();
044: assertEquals(0, sizeSequence.getSizes().length);
045: sizeSequence = new SizeSequence(10);
046: assertEquals(10, sizeSequence.getSizes().length);
047: assertEquals(10, sizeSequence.getIndex(0));
048: for (int i = 0; i < 10; i++) {
049: assertEquals(0, sizeSequence.getSizes()[i]);
050: }
051: sizeSequence = new SizeSequence(10, 13);
052: assertEquals(10, sizeSequence.getSizes().length);
053: for (int i = 0; i < 10; i++) {
054: assertEquals(13, sizeSequence.getSizes()[i]);
055: }
056: int[] sizes = new int[] { 1, 2, 3, 4, 5 };
057: sizeSequence = new SizeSequence(sizes);
058: assertEquals(5, sizeSequence.getSizes().length);
059: assertNotSame(sizes, sizeSequence.getSizes());
060: }
061:
062: public void testGetIndex() {
063: assertEquals(2, sizeSequence.getIndex(3));
064: assertEquals(3, sizeSequence.getIndex(6));
065: assertEquals(4, sizeSequence.getIndex(11));
066: assertEquals(5, sizeSequence.getIndex(100));
067: assertEquals(0, sizeSequence.getIndex(-100));
068: }
069:
070: public void testGetPosition() {
071: assertEquals(0, sizeSequence.getPosition(0));
072: assertEquals(1, sizeSequence.getPosition(1));
073: assertEquals(3, sizeSequence.getPosition(2));
074: assertEquals(6, sizeSequence.getPosition(3));
075: assertEquals(10, sizeSequence.getPosition(4));
076: assertEquals(15, sizeSequence.getPosition(100));
077: assertEquals(0, sizeSequence.getPosition(-100));
078: sizeSequence = new SizeSequence();
079: assertEquals(0, sizeSequence.getPosition(5));
080: }
081:
082: public void testGetSetSize() {
083: assertEquals(1, sizeSequence.getSize(0));
084: sizeSequence.setSize(0, 10);
085: assertEquals(10, sizeSequence.getSize(0));
086: assertEquals(10, sizeSequence.getPosition(1));
087: assertEquals(12, sizeSequence.getPosition(2));
088: assertEquals(24, sizeSequence.getPosition(5));
089: assertEquals(1, sizeSequence.getIndex(11));
090: assertEquals(3, sizeSequence.getIndex(15));
091: sizeSequence.setSize(0, -10);
092: assertEquals(-10, sizeSequence.getSize(0));
093: assertEquals(-10, sizeSequence.getPosition(1));
094: sizeSequence.setSize(10, 1);
095: assertEquals(0, sizeSequence.getSize(10));
096: }
097:
098: public void testGetSetSizes() {
099: int[] first = sizeSequence.getSizes();
100: assertNotSame(first, sizeSequence.getSizes());
101: sizes[0] = 5;
102: assertEquals(1, sizeSequence.getSizes()[0]);
103: sizes = new int[] { 1, 2, 3, 4, 5, 6 };
104: sizeSequence.setSizes(sizes);
105: assertEquals(sizes.length, sizeSequence.getSizes().length);
106: assertNotSame(sizes, sizeSequence.getSizes());
107: for (int i = 0; i < sizes.length; i++) {
108: assertEquals(sizes[i], sizeSequence.getSizes()[i]);
109: assertEquals(sizes[i], sizeSequence.getSize(i));
110: }
111: }
112:
113: public void testInsertRemoveEntries() {
114: sizeSequence.removeEntries(0, 3);
115: assertEquals(2, sizeSequence.getSizes().length);
116: assertEquals(5, sizeSequence.getSize(1));
117: testExceptionalCase(new ExceptionalCase() {
118: @Override
119: public void exceptionalAction() throws Exception {
120: sizeSequence.removeEntries(10, 11);
121: }
122:
123: @Override
124: public Class expectedExceptionClass() {
125: return NegativeArraySizeException.class;
126: }
127: });
128: testExceptionalCase(new ExceptionalCase() {
129: @Override
130: public void exceptionalAction() throws Exception {
131: sizeSequence.removeEntries(10, -1);
132: }
133:
134: @Override
135: public Class expectedExceptionClass() {
136: return ArrayIndexOutOfBoundsException.class;
137: }
138: });
139: sizeSequence = new SizeSequence(sizes);
140: sizeSequence.insertEntries(2, 3, 2);
141: assertEquals(3 + sizes.length, sizeSequence.getSizes().length);
142: assertEquals(2, sizeSequence.getSize(2));
143: assertEquals(2, sizeSequence.getIndex(3));
144: assertEquals(5, sizeSequence.getSize(7));
145: testExceptionalCase(new ExceptionalCase() {
146: @Override
147: public void exceptionalAction() throws Exception {
148: sizeSequence.insertEntries(10, 11, 0);
149: }
150:
151: @Override
152: public Class expectedExceptionClass() {
153: return ArrayIndexOutOfBoundsException.class;
154: }
155: });
156: testExceptionalCase(new ExceptionalCase() {
157: @Override
158: public void exceptionalAction() throws Exception {
159: sizeSequence.insertEntries(0, -1, 0);
160: }
161:
162: @Override
163: public Class expectedExceptionClass() {
164: return ArrayIndexOutOfBoundsException.class;
165: }
166: });
167: }
168: }
|