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.Vector;
021:
022: import junit.framework.TestCase;
023:
024: public class AccessibleRelationSetTest extends TestCase {
025: private AccessibleRelationSet set;
026:
027: private AccessibleRelation[] relations;
028:
029: @Override
030: public void setUp() {
031: set = new AccessibleRelationSet();
032: relations = new AccessibleRelation[] {
033: new AccessibleRelation(AccessibleRelation.CONTROLLED_BY),
034: new AccessibleRelation(AccessibleRelation.MEMBER_OF) };
035: set.addAll(relations);
036: }
037:
038: @Override
039: public void tearDown() {
040: set = null;
041: relations = null;
042: }
043:
044: public void testAccessibleRelationSet() {
045: assertNotNull(set.relations);
046:
047: try {
048: new AccessibleRelationSet(null);
049: fail("expected null pointer exception");
050: } catch (NullPointerException e) {
051: }
052: }
053:
054: public void testAddContains() {
055: assertTrue("Should contain added item", set
056: .contains(AccessibleRelation.CONTROLLED_BY));
057: assertTrue("Should contain added item", set
058: .contains(AccessibleRelation.MEMBER_OF));
059:
060: AccessibleRelation relation = new AccessibleRelation(
061: AccessibleRelation.CONTROLLER_FOR);
062: assertTrue(set.add(relation));
063: assertTrue(set.add(relation));
064: assertTrue("Should contain added item", set
065: .contains(AccessibleRelation.CONTROLLER_FOR));
066: assertFalse("Should not contain not added item", set
067: .contains(AccessibleRelation.LABEL_FOR));
068:
069: }
070:
071: public void testNullOperations() {
072: set.relations = null;
073: assertFalse("Empty set should not contain any item", set
074: .contains(AccessibleRelation.LABEL_FOR));
075: assertNull("Empty set should not contain any item", set
076: .get(AccessibleRelation.LABEL_FOR));
077: assertFalse("Empty set should not contain any item", set
078: .remove(set.get(AccessibleRelation.LABEL_FOR)));
079: set.add(new AccessibleRelation(
080: AccessibleRelation.CONTROLLER_FOR));
081: set.relations = null;
082: set.addAll(relations);
083: }
084:
085: public void testDupes() {
086: AccessibleRelation dupeRelation = new AccessibleRelation(
087: AccessibleRelation.CONTROLLED_BY);
088: assertTrue("Add duplicate item", set.add(dupeRelation));
089: assertTrue("Add duplicate item", set.add(relations[0]));
090: assertNotSame(dupeRelation, set
091: .get(AccessibleRelation.CONTROLLED_BY));
092: assertSame(relations[0], set
093: .get(AccessibleRelation.CONTROLLED_BY));
094: set.remove(set.get(AccessibleRelation.CONTROLLED_BY));
095:
096: set.relations = null;
097: set.addAll(relations);
098: set.addAll(relations);
099: assertEquals("Should not add duplicate items in addAll",
100: relations.length, set.relations.size());
101: }
102:
103: public void testGet() {
104: assertSame("Get should return same value", relations[0], set
105: .get(AccessibleRelation.CONTROLLED_BY));
106: assertNull("Get should return null if no such element present",
107: set.get(AccessibleRelation.CONTROLLER_FOR));
108: }
109:
110: public void testClear() {
111: set.clear();
112: assertEquals("Cleared array should be empty", 0, set.relations
113: .size());
114: }
115:
116: public void testRemove() {
117: set.add(new AccessibleRelation(
118: AccessibleRelation.CONTROLLER_FOR));
119: assertTrue(set.remove(set
120: .get(AccessibleRelation.CONTROLLER_FOR)));
121: assertFalse(set.contains(AccessibleRelation.CONTROLLER_FOR));
122: }
123:
124: public void testToString() {
125: String stateSetString = set.toString();
126: assertTrue(
127: "String representation should contain elements representation",
128: stateSetString.indexOf(relations[0].toString()) >= 0);
129: assertTrue(
130: "String representation should contain elements representation",
131: stateSetString.indexOf(relations[1].toString()) >= 0);
132:
133: set.relations = null;
134: set.toString();
135: }
136:
137: // Regression for HARMONY-2457
138: public void test_constructor() {
139: TestAccessibleRelationSet obj = new TestAccessibleRelationSet();
140: assertNull(obj.relations);
141: }
142:
143: static class TestAccessibleRelationSet extends
144: AccessibleRelationSet {
145: Vector relations;
146:
147: TestAccessibleRelationSet() {
148: super();
149: relations = super.relations;
150: }
151: }
152: }
|