]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/test/unit/TestAttributeMap.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / test / unit / TestAttributeMap.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2008.  All Rights Reserved.\r
3  *\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
12  */\r
13 package com.ibm.richtext.test.unit;\r
14 \r
15 import com.ibm.icu.dev.test.TestFmwk;\r
16 \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
21 \r
22 // Java2 imports\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
27 \r
28 \r
29 public class TestAttributeMap extends TestFmwk  {\r
30 \r
31     static final String COPYRIGHT =\r
32                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
33 \r
34 \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
41 \r
42     public static void main(String[] args) throws Exception {\r
43 \r
44         new TestAttributeMap().run(args);\r
45     }\r
46 \r
47     private AttributeSet maps;  // A Set of AttributeMaps\r
48     private AttributeSet sets;  // A Set of Sets\r
49 \r
50     private static final class TestAttribute extends TextAttribute {\r
51 \r
52         /**\r
53          * For serialization\r
54          */\r
55         private static final long serialVersionUID = 2092805803764400714L;\r
56 \r
57         TestAttribute(String name) {\r
58             super(name);\r
59         }\r
60     }\r
61 \r
62     private static final TestAttribute[] attributes = {\r
63         new TestAttribute("0"), new TestAttribute("1"), new TestAttribute("2")\r
64     };\r
65 \r
66     private static final Object[] values = {\r
67         "Hello world", new Float(-42), new Object(), new AttributeMap(new TestAttribute("3"), "HH")\r
68     };\r
69 \r
70     /**\r
71      * Returns lhs.equals(rhs) - but also checks for symmetry, and\r
72      * consistency with hashCode().\r
73      */\r
74     private boolean equalMaps(AttributeMap lhs, Object rhs) {\r
75 \r
76         boolean equal = lhs.equals(rhs);\r
77         if (equal != (rhs.equals(lhs))) {\r
78             errln("AttributeMap.equals is not symetric");\r
79         }\r
80         if (equal) {\r
81             if (lhs.hashCode() != rhs.hashCode()) {\r
82                 errln("AttributeMaps are equal but hashCodes differ");\r
83             }\r
84         }\r
85         return equal;\r
86     }\r
87 \r
88     public TestAttributeMap() {\r
89 \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
94 \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
98             }\r
99         }\r
100 \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
104         }\r
105         maps = maps.addElement(bigMap);\r
106 \r
107         sets = AttributeSet.EMPTY_SET;\r
108 \r
109         sets = new AttributeSet(AttributeSet.EMPTY_SET);\r
110 \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
114         }\r
115 \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
119         }\r
120 \r
121         sets = sets.addElement(allAttrs);\r
122     }\r
123 \r
124     /**\r
125      * Run tests on AttributeMap.  If a test fails an exception will propogate out\r
126      * of this method.\r
127      */\r
128     public void test() {\r
129 \r
130         easyTests();\r
131 \r
132         Enumeration mapIter = maps.elements();\r
133         while (mapIter.hasMoreElements()) {\r
134 \r
135             AttributeMap testMap = (AttributeMap) mapIter.nextElement();\r
136 \r
137             _testModifiers(testMap);\r
138             _testViews(testMap);\r
139 \r
140             Enumeration unionIter = maps.elements();\r
141             while (unionIter.hasMoreElements()) {\r
142                 _testUnionWith(testMap, (AttributeMap) unionIter.nextElement());\r
143             }\r
144 \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
150             }\r
151         }\r
152     }\r
153 \r
154     /**\r
155      * Invoke modifiers on map.  All should throw\r
156      * UnsupportedOperationException, and leave map unmodified.\r
157      */\r
158     void _testModifiers(AttributeMap map) {\r
159 \r
160         if (gJDK11) {\r
161             return;\r
162         }\r
163         \r
164         AttributeMap originalMap = new AttributeMap(map);\r
165 \r
166         try {\r
167             map.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);\r
168             errln("Put should throw UnsupportedOperationException.");\r
169         }\r
170         catch(UnsupportedOperationException e) {\r
171             System.out.print("");\r
172         }\r
173 \r
174         try {\r
175             Object key = TextAttribute.WEIGHT;\r
176             Iterator iter = map.keySet().iterator();\r
177             if (iter.hasNext()) {\r
178                 key = iter.next();\r
179             }\r
180             map.remove(key);\r
181             errln("Set should throw UnsupportedOperationException.");\r
182         }\r
183         catch(UnsupportedOperationException e) {\r
184             System.out.print("");\r
185         }\r
186 \r
187         try {\r
188             map.putAll(map);\r
189             errln("putAll should throw UnsupportedOperationException.");\r
190         }\r
191         catch(UnsupportedOperationException e) {\r
192             System.out.print("");\r
193         }\r
194 \r
195         try {\r
196             map.clear();\r
197             errln("clear should throw UnsupportedOperationException.");\r
198         }\r
199         catch(UnsupportedOperationException e) {\r
200             System.out.print("");\r
201         }\r
202 \r
203         if (!originalMap.equals(map)) {\r
204             errln("Modifiers changed map.");\r
205         }\r
206     }\r
207 \r
208     /**\r
209      * Ensure that map.addAttributes(addMap) is equivalent to calling\r
210      * map.add on all of addMap's entries.\r
211      */\r
212     void _testUnionWith(AttributeMap map, AttributeMap addMap) {\r
213 \r
214         AttributeMap lhs = map.addAttributes(addMap);\r
215 \r
216         AttributeMap rhs = map;\r
217 \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
223         }\r
224 \r
225         if (!equalMaps(lhs, rhs)) {\r
226             errln("Maps are not equal.");\r
227         }\r
228     }\r
229 \r
230     /**\r
231      * Ensure that map.removeAttributes(remove) is equivalent to calling\r
232      * map.removeAttribute on remove's elements.\r
233      */\r
234     void _testRemoveAttributes(AttributeMap map, AttributeSet remove) {\r
235 \r
236         AttributeMap lhs = map.removeAttributes(remove);\r
237 \r
238         AttributeMap rhs = map;\r
239 \r
240         Enumeration iter = remove.elements();\r
241         while (iter.hasMoreElements()) {\r
242             Object attr = iter.nextElement();\r
243             rhs = rhs.removeAttribute(attr);\r
244         }\r
245 \r
246         if (!equalMaps(lhs, rhs)) {\r
247             errln("Maps are not equal.");\r
248         }\r
249     }\r
250 \r
251     /**\r
252      * Ensure that map.intersectWith(intersect) is equivalent to\r
253      * map.removeAttributes(map.keySet() - intersect);\r
254      */\r
255     void _testIntersectWith(AttributeMap map, AttributeSet intersect) {\r
256 \r
257         AttributeMap lhs = map.intersectWith(intersect);\r
258 \r
259         AttributeSet keySet = map.getKeySet();\r
260         AttributeSet removeSet = keySet.subtract(intersect);\r
261         AttributeMap rhs = map.removeAttributes(removeSet);\r
262 \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
272         }\r
273     }\r
274 \r
275     /**\r
276      * Ensure that:\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
283      */\r
284     void _testViews(AttributeMap map) {\r
285 \r
286         AttributeSet keySet = map.getKeySet();\r
287 \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
292             }\r
293         }\r
294 \r
295         if (gJDK11) {\r
296             return;\r
297         }\r
298         \r
299         Collection vals = map.values();\r
300         Set entrySet = map.entrySet();\r
301 \r
302         if (keySet.size() != map.size() || entrySet.size() != map.size()) {\r
303             errln("Set sizes are inconsistent with map size.");\r
304         }\r
305 \r
306         int hashCode = 0;\r
307 \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
312             }\r
313         }\r
314 \r
315         Iterator entryIter = entrySet.iterator();\r
316         while (entryIter.hasNext()) {\r
317 \r
318             Entry entry = (Entry) entryIter.next();\r
319 \r
320             Object key = entry.getKey();\r
321             if (!keySet.contains(key)) {\r
322                 errln("Entry key is not in key set.");\r
323             }\r
324 \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
328             }\r
329 \r
330             if (map.get(key) != value) {\r
331                 errln("map.get did not return entry value.");\r
332             }\r
333 \r
334             hashCode += entry.hashCode();\r
335         }\r
336 \r
337         if (hashCode != map.hashCode()) {\r
338             errln("map hashcode is not sum of entry hashcodes.");\r
339         }\r
340     }\r
341 \r
342     /**\r
343      * Look for correct behavior in obvious cases.\r
344      */\r
345     void easyTests() {\r
346 \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
350         }\r
351 \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
356         }\r
357 \r
358         otherMap = map.addAttributes(map);\r
359         if (!equalMaps(map,otherMap)) {\r
360             errln("Maps are inconsistent after addAttributes");\r
361         }\r
362 \r
363         map = map.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);\r
364 \r
365         if (map.size() != 2) {\r
366             errln("Map size is wrong.  map="+map);\r
367         }\r
368 \r
369         if (equalMaps(map,otherMap)) {\r
370             errln("Maps should not be equal");\r
371         }\r
372 \r
373         Object posture = new Float(0);\r
374         map = map.addAttribute(TextAttribute.POSTURE, posture);\r
375 \r
376         if (map.size() != 2) {\r
377             errln("Map size is wrong");\r
378         }\r
379 \r
380         if (!map.get(TextAttribute.POSTURE).equals(posture)) {\r
381             errln("Map element is wrong");\r
382         }\r
383 \r
384         map = map.removeAttribute(TextAttribute.UNDERLINE);\r
385 \r
386         if (map.size() != 1) {\r
387             errln("Map size is wrong");\r
388         }\r
389 \r
390         if (map.get(TextAttribute.UNDERLINE) != null) {\r
391             errln("Map should not have element");\r
392         }\r
393 \r
394         // map has POSTURE_REGULAR.  If we addAttributes a map with\r
395         // POSTURE_ITALIC the new map should have POSTURE_ITALIC\r
396 \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
400         }\r
401 \r
402         _testModifiers(map);\r
403         _testViews(map);\r
404 \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
412             }\r
413         }\r
414     }\r
415 }