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: package javax.accessibility;
019:
020: import java.util.Arrays;
021: import java.util.Vector;
022:
023: import junit.framework.TestCase;
024:
025: public class AccessibleStateSetTest extends TestCase {
026: private AccessibleStateSet stateSet;
027:
028: private AccessibleState[] statesArray;
029:
030: @Override
031: public void setUp() {
032: stateSet = new AccessibleStateSet();
033: statesArray = new AccessibleState[] { AccessibleState.ACTIVE,
034: AccessibleState.ARMED };
035: stateSet.addAll(statesArray);
036: }
037:
038: @Override
039: public void tearDown() {
040: stateSet = null;
041: statesArray = null;
042: }
043:
044: public void testAccessibleStateSet() throws Exception {
045: AccessibleState[] statesArray = { AccessibleState.ACTIVE,
046: AccessibleState.ARMED };
047: stateSet = new AccessibleStateSet(statesArray);
048: assertNotNull(stateSet.states);
049:
050: try {
051: new AccessibleStateSet(null);
052: fail("expected null pointer exception");
053: } catch (NullPointerException e) {
054: }
055: }
056:
057: public void testAddContains() throws Exception {
058: assertTrue("Must contain added state", stateSet
059: .contains(AccessibleState.ACTIVE));
060: assertTrue("Must contain added state", stateSet
061: .contains(AccessibleState.ARMED));
062: boolean added = stateSet.add(AccessibleState.ACTIVE);
063: assertEquals("Should not add duplicate item", 2,
064: stateSet.states.size());
065: assertFalse("Should not add duplicate item", added);
066: assertFalse(stateSet.contains(null));
067:
068: assertTrue(stateSet.add(null));
069: assertTrue(stateSet.contains(null));
070:
071: stateSet.states = null;
072: assertFalse(stateSet.contains(null));
073: assertNull(stateSet.states);
074:
075: stateSet.states = null;
076: stateSet.add(AccessibleState.ACTIVE);
077: }
078:
079: public void testAddAll() {
080: stateSet.addAll(statesArray);
081: stateSet.addAll(statesArray);
082: assertEquals("Should not add duplicate items",
083: statesArray.length, stateSet.states.size());
084:
085: try {
086: stateSet.addAll(null);
087: fail("expected null pointer exception");
088: } catch (NullPointerException e) {
089: }
090: }
091:
092: public void testRemove() throws Exception {
093: boolean removed = stateSet.remove(AccessibleState.ICONIFIED);
094: assertFalse("Should not remove non-existing item", removed);
095: removed = stateSet.remove(AccessibleState.ACTIVE);
096: assertFalse("Should remove existing item", stateSet
097: .contains(AccessibleState.ACTIVE));
098: assertTrue("Should remove existing item", removed);
099: }
100:
101: public void testClear() throws Exception {
102:
103: stateSet.clear();
104: assertEquals("Cleared set should be empty", 0, stateSet.states
105: .size());
106:
107: stateSet.states = null;
108: stateSet.clear();
109: }
110:
111: public void testToString() throws Exception {
112: String stateSetString = stateSet.toString();
113: assertTrue(
114: "String representation should contain elements representation",
115: stateSetString.indexOf(AccessibleState.ACTIVE
116: .toString()) >= 0);
117: assertTrue(
118: "String representation should contain elements representation",
119: stateSetString
120: .indexOf(AccessibleState.ARMED.toString()) >= 0);
121:
122: stateSet.states = null;
123: stateSet.toString();
124:
125: // regression test for HARMONY-1190
126: try {
127: new AccessibleStateSet(new AccessibleState[2]).toString();
128: fail("NullPointerException expected");
129: } catch (NullPointerException e) {
130: // expected
131: }
132: }
133:
134: public void testToArray() throws Exception {
135: AccessibleState[] statesReturnedArray = stateSet.toArray();
136: assertEquals("Returned array size don't match",
137: statesArray.length, statesReturnedArray.length);
138: for (int i = 0; i < statesReturnedArray.length; i++)
139: assertEquals("Returned element mismatch:" + i,
140: statesArray[i], statesReturnedArray[i]);
141: stateSet.states = null;
142: Arrays.asList(stateSet.toArray());
143: }
144:
145: // Regression for HARMONY-2457
146: public void test_constructor() {
147: TestAccessibleStateSet obj = new TestAccessibleStateSet();
148: assertNull(obj.states);
149: }
150:
151: static class TestAccessibleStateSet extends AccessibleStateSet {
152: Vector states;
153:
154: TestAccessibleStateSet() {
155: super();
156: states = super.states;
157: }
158: }
159:
160: }
|