2 * (C) Copyright IBM Corp. 1998-2008. All Rights Reserved.
\r
4 * The program is provided "as is" without any warranty express or
\r
5 * implied, including the warranty of non-infringement and the implied
\r
6 * warranties of merchantibility and fitness for a particular purpose.
\r
7 * IBM will not be liable for any damages suffered by you as a result
\r
8 * of using the Program. In no event will IBM be liable for any
\r
9 * special, indirect or consequential damages or lost profits even if
\r
10 * IBM has been advised of the possibility of their occurrence. IBM
\r
11 * will not be liable for any third party claims against you.
\r
13 package com.ibm.richtext.test.unit;
\r
15 import com.ibm.icu.dev.test.TestFmwk;
\r
17 import com.ibm.richtext.textlayout.attributes.AttributeSet;
\r
18 import com.ibm.richtext.textlayout.attributes.TextAttribute;
\r
19 import com.ibm.richtext.textlayout.attributes.AttributeMap;
\r
20 import java.util.Enumeration;
\r
23 import java.util.Collection;
\r
24 import java.util.Iterator;
\r
25 import java.util.Set;
\r
26 import java.util.Map.Entry;
\r
29 public class TestAttributeMap extends TestFmwk {
\r
31 static final String COPYRIGHT =
\r
32 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
35 // There are JDK 1.1 versions of AttributeMap and AttributeSet.
\r
36 // Some of the tests in this class require Java 2 API's. I have
\r
37 // tried to isolate these tests by conditionalizing them on
\r
38 // this static variable. If you are back-porting to 1.1, remove
\r
39 // the Java 2 tests ONLY.
\r
40 private static final boolean gJDK11 = false;
\r
42 public static void main(String[] args) throws Exception {
\r
44 new TestAttributeMap().run(args);
\r
47 private AttributeSet maps; // A Set of AttributeMaps
\r
48 private AttributeSet sets; // A Set of Sets
\r
50 private static final class TestAttribute extends TextAttribute {
\r
55 private static final long serialVersionUID = 2092805803764400714L;
\r
57 TestAttribute(String name) {
\r
62 private static final TestAttribute[] attributes = {
\r
63 new TestAttribute("0"), new TestAttribute("1"), new TestAttribute("2")
\r
66 private static final Object[] values = {
\r
67 "Hello world", new Float(-42), new Object(), new AttributeMap(new TestAttribute("3"), "HH")
\r
71 * Returns lhs.equals(rhs) - but also checks for symmetry, and
\r
72 * consistency with hashCode().
\r
74 private boolean equalMaps(AttributeMap lhs, Object rhs) {
\r
76 boolean equal = lhs.equals(rhs);
\r
77 if (equal != (rhs.equals(lhs))) {
\r
78 errln("AttributeMap.equals is not symetric");
\r
81 if (lhs.hashCode() != rhs.hashCode()) {
\r
82 errln("AttributeMaps are equal but hashCodes differ");
\r
88 public TestAttributeMap() {
\r
90 maps = AttributeSet.EMPTY_SET;
\r
91 maps = maps.addElement(AttributeMap.EMPTY_ATTRIBUTE_MAP);
\r
92 maps.addElement(new AttributeMap(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB));
\r
93 maps.addElement(new AttributeMap(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER));
\r
95 for (int i=0; i < attributes.length; i++) {
\r
96 for (int j=0; j < values.length; j++) {
\r
97 maps = maps.addElement(new AttributeMap(attributes[i], values[j]));
\r
101 AttributeMap bigMap = new AttributeMap(new TestAttribute("4"), "value");
\r
102 for (int i=0; i < Math.min(attributes.length, values.length); i++) {
\r
103 bigMap = bigMap.addAttribute(attributes[i], values[values.length-i-1]);
\r
105 maps = maps.addElement(bigMap);
\r
107 sets = AttributeSet.EMPTY_SET;
\r
109 sets = new AttributeSet(AttributeSet.EMPTY_SET);
\r
111 for (int i=0; i < attributes.length; i++) {
\r
112 AttributeSet newSet = new AttributeSet(attributes[i]);
\r
113 sets = sets.addElement(newSet);
\r
116 AttributeSet allAttrs = AttributeSet.EMPTY_SET;
\r
117 for (int i=0; i < attributes.length; i++) {
\r
118 allAttrs = allAttrs.addElement(attributes[i]);
\r
121 sets = sets.addElement(allAttrs);
\r
125 * Run tests on AttributeMap. If a test fails an exception will propogate out
\r
128 public void test() {
\r
132 Enumeration mapIter = maps.elements();
\r
133 while (mapIter.hasMoreElements()) {
\r
135 AttributeMap testMap = (AttributeMap) mapIter.nextElement();
\r
137 _testModifiers(testMap);
\r
138 _testViews(testMap);
\r
140 Enumeration unionIter = maps.elements();
\r
141 while (unionIter.hasMoreElements()) {
\r
142 _testUnionWith(testMap, (AttributeMap) unionIter.nextElement());
\r
145 Enumeration setIter = sets.elements();
\r
146 while (setIter.hasMoreElements()) {
\r
147 AttributeSet testSet = (AttributeSet) setIter.nextElement();
\r
148 _testIntersectWith(testMap, testSet);
\r
149 _testRemoveAttributes(testMap, testSet);
\r
155 * Invoke modifiers on map. All should throw
\r
156 * UnsupportedOperationException, and leave map unmodified.
\r
158 void _testModifiers(AttributeMap map) {
\r
164 AttributeMap originalMap = new AttributeMap(map);
\r
167 map.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
\r
168 errln("Put should throw UnsupportedOperationException.");
\r
170 catch(UnsupportedOperationException e) {
\r
171 System.out.print("");
\r
175 Object key = TextAttribute.WEIGHT;
\r
176 Iterator iter = map.keySet().iterator();
\r
177 if (iter.hasNext()) {
\r
181 errln("Set should throw UnsupportedOperationException.");
\r
183 catch(UnsupportedOperationException e) {
\r
184 System.out.print("");
\r
189 errln("putAll should throw UnsupportedOperationException.");
\r
191 catch(UnsupportedOperationException e) {
\r
192 System.out.print("");
\r
197 errln("clear should throw UnsupportedOperationException.");
\r
199 catch(UnsupportedOperationException e) {
\r
200 System.out.print("");
\r
203 if (!originalMap.equals(map)) {
\r
204 errln("Modifiers changed map.");
\r
209 * Ensure that map.addAttributes(addMap) is equivalent to calling
\r
210 * map.add on all of addMap's entries.
\r
212 void _testUnionWith(AttributeMap map, AttributeMap addMap) {
\r
214 AttributeMap lhs = map.addAttributes(addMap);
\r
216 AttributeMap rhs = map;
\r
218 Enumeration iter = addMap.getKeySet().elements();
\r
219 while (iter.hasMoreElements()) {
\r
220 Object attr = iter.nextElement();
\r
221 Object value = addMap.get(attr);
\r
222 rhs = rhs.addAttribute(attr, value);
\r
225 if (!equalMaps(lhs, rhs)) {
\r
226 errln("Maps are not equal.");
\r
231 * Ensure that map.removeAttributes(remove) is equivalent to calling
\r
232 * map.removeAttribute on remove's elements.
\r
234 void _testRemoveAttributes(AttributeMap map, AttributeSet remove) {
\r
236 AttributeMap lhs = map.removeAttributes(remove);
\r
238 AttributeMap rhs = map;
\r
240 Enumeration iter = remove.elements();
\r
241 while (iter.hasMoreElements()) {
\r
242 Object attr = iter.nextElement();
\r
243 rhs = rhs.removeAttribute(attr);
\r
246 if (!equalMaps(lhs, rhs)) {
\r
247 errln("Maps are not equal.");
\r
252 * Ensure that map.intersectWith(intersect) is equivalent to
\r
253 * map.removeAttributes(map.keySet() - intersect);
\r
255 void _testIntersectWith(AttributeMap map, AttributeSet intersect) {
\r
257 AttributeMap lhs = map.intersectWith(intersect);
\r
259 AttributeSet keySet = map.getKeySet();
\r
260 AttributeSet removeSet = keySet.subtract(intersect);
\r
261 AttributeMap rhs = map.removeAttributes(removeSet);
\r
263 if (!equalMaps(lhs, rhs)) {
\r
264 map.intersectWith(intersect);
\r
265 logln("intersect: " + intersect);
\r
266 logln("keySet: " + keySet);
\r
267 logln("removeSet: " + removeSet);
\r
268 logln("map: " + map);
\r
269 logln("lhs: " + lhs);
\r
270 logln("rhs: " + rhs);
\r
271 errln("Maps are not equal.");
\r
277 * map, map.keySet(), and map.entrySet() are the same size;
\r
278 * map.containsKey() is true for every key in keySet();
\r
279 * map.containsValue() is true for every value in values;
\r
280 * every entry key is in keySet, every entry value is in map.values();
\r
281 * map.get() is consistent with entry's key, value;
\r
282 * sum of hashcodes of entries equals map.hashCode().
\r
284 void _testViews(AttributeMap map) {
\r
286 AttributeSet keySet = map.getKeySet();
\r
288 Enumeration keyIter = keySet.elements();
\r
289 while (keyIter.hasMoreElements()) {
\r
290 if (!map.containsKey(keyIter.nextElement())) {
\r
291 errln("keySet contains key not in map");
\r
299 Collection vals = map.values();
\r
300 Set entrySet = map.entrySet();
\r
302 if (keySet.size() != map.size() || entrySet.size() != map.size()) {
\r
303 errln("Set sizes are inconsistent with map size.");
\r
308 Iterator valueIter = vals.iterator();
\r
309 while (valueIter.hasNext()) {
\r
310 if (!map.containsValue(valueIter.next())) {
\r
311 errln("value set contains value not in map");
\r
315 Iterator entryIter = entrySet.iterator();
\r
316 while (entryIter.hasNext()) {
\r
318 Entry entry = (Entry) entryIter.next();
\r
320 Object key = entry.getKey();
\r
321 if (!keySet.contains(key)) {
\r
322 errln("Entry key is not in key set.");
\r
325 Object value = map.get(entry.getKey());
\r
326 if (!vals.contains(value)) {
\r
327 errln("Entry value is not in value set.");
\r
330 if (map.get(key) != value) {
\r
331 errln("map.get did not return entry value.");
\r
334 hashCode += entry.hashCode();
\r
337 if (hashCode != map.hashCode()) {
\r
338 errln("map hashcode is not sum of entry hashcodes.");
\r
343 * Look for correct behavior in obvious cases.
\r
347 AttributeMap map = new AttributeMap();
\r
348 if (!map.equals(AttributeMap.EMPTY_ATTRIBUTE_MAP)) {
\r
349 errln("Default-constructed map is not equal to empty map.");
\r
352 map = map.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
\r
353 Object otherMap = new AttributeMap(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
\r
354 if (!map.equals(otherMap)) {
\r
355 errln("Maps are inconsistent after map.add");
\r
358 otherMap = map.addAttributes(map);
\r
359 if (!equalMaps(map,otherMap)) {
\r
360 errln("Maps are inconsistent after addAttributes");
\r
363 map = map.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
\r
365 if (map.size() != 2) {
\r
366 errln("Map size is wrong. map="+map);
\r
369 if (equalMaps(map,otherMap)) {
\r
370 errln("Maps should not be equal");
\r
373 Object posture = new Float(0);
\r
374 map = map.addAttribute(TextAttribute.POSTURE, posture);
\r
376 if (map.size() != 2) {
\r
377 errln("Map size is wrong");
\r
380 if (!map.get(TextAttribute.POSTURE).equals(posture)) {
\r
381 errln("Map element is wrong");
\r
384 map = map.removeAttribute(TextAttribute.UNDERLINE);
\r
386 if (map.size() != 1) {
\r
387 errln("Map size is wrong");
\r
390 if (map.get(TextAttribute.UNDERLINE) != null) {
\r
391 errln("Map should not have element");
\r
394 // map has POSTURE_REGULAR. If we addAttributes a map with
\r
395 // POSTURE_ITALIC the new map should have POSTURE_ITALIC
\r
397 map = map.addAttributes(new AttributeMap(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE));
\r
398 if (map.get(TextAttribute.POSTURE) != TextAttribute.POSTURE_OBLIQUE) {
\r
399 errln("Map element is wrong");
\r
402 _testModifiers(map);
\r
405 Enumeration mapIter = maps.elements();
\r
406 while (mapIter.hasMoreElements()) {
\r
407 AttributeMap testMap = (AttributeMap) mapIter.nextElement();
\r
408 Object newValue = new Object();
\r
409 AttributeMap newMap = testMap.addAttribute(attributes[0], newValue);
\r
410 if (newMap.get(attributes[0]) != newValue) {
\r
411 errln("Did not get expected value back. map=" + map);
\r