KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > notifications > NodeMapDiffTest


1 package org.jboss.cache.notifications;
2
3 import junit.framework.TestCase;
4 import org.jboss.cache.util.Util;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8
9 /**
10  * Tests the diffs between maps.
11  *
12  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani</a>
13  * @since 2.0.0
14  */

15 public class NodeMapDiffTest extends TestCase
16 {
17    public void testDataAdded()
18    {
19       Util.MapModifications expected = new Util.MapModifications();
20       expected.addedEntries.put("key", "value");
21       expected.addedEntries.put("key1", "value1");
22
23       Map JavaDoc pre = new HashMap JavaDoc();
24       pre.put("oldKey", "oldValue");
25
26       Map JavaDoc post = new HashMap JavaDoc();
27       post.putAll(pre);
28       post.put("key", "value");
29       post.put("key1", "value1");
30
31       assertEquals(expected, Util.diffNodeData(pre, post));
32    }
33
34    public void testDataRemoved()
35    {
36       Util.MapModifications expected = new Util.MapModifications();
37       expected.removedEntries.put("key", "value");
38       expected.removedEntries.put("key1", "value1");
39
40       Map JavaDoc post = new HashMap JavaDoc();
41       post.put("oldKey", "oldValue");
42
43       Map JavaDoc pre = new HashMap JavaDoc();
44       pre.putAll(post);
45       pre.put("key", "value");
46       pre.put("key1", "value1");
47
48       assertEquals(expected, Util.diffNodeData(pre, post));
49    }
50
51    public void testDataChanged()
52    {
53       Util.MapModifications expected = new Util.MapModifications();
54       expected.modifiedEntries.put("key", "value");
55       expected.modifiedEntries.put("key1", "value1");
56
57       Map JavaDoc pre = new HashMap JavaDoc();
58       pre.put("oldKey", "oldValue");
59       pre.put("key", "valueOLD");
60       pre.put("key1", "value1OLD");
61
62       Map JavaDoc post = new HashMap JavaDoc();
63       post.putAll(pre);
64       post.put("key", "value");
65       post.put("key1", "value1");
66
67       assertEquals(expected, Util.diffNodeData(pre, post));
68    }
69
70    public void testNullMaps()
71    {
72       try
73       {
74          Util.diffNodeData(null, null);
75          fail("Expected NPE");
76       }
77       catch (NullPointerException JavaDoc npe)
78       {
79          // expected
80
}
81
82       try
83       {
84          Util.diffNodeData(new HashMap JavaDoc(), null);
85          fail("Expected NPE");
86       }
87       catch (NullPointerException JavaDoc npe)
88       {
89          // expected
90
}
91
92       try
93       {
94          Util.diffNodeData(null, new HashMap JavaDoc());
95          fail("Expected NPE");
96       }
97       catch (NullPointerException JavaDoc npe)
98       {
99          // expected
100
}
101
102    }
103
104    public void testEmptyMaps()
105    {
106       Util.MapModifications expected = new Util.MapModifications();
107
108       Map JavaDoc pre = new HashMap JavaDoc();
109       Map JavaDoc post = new HashMap JavaDoc();
110
111       assertEquals(expected, Util.diffNodeData(pre, post));
112    }
113
114    public void testNoChange()
115    {
116       Util.MapModifications expected = new Util.MapModifications();
117
118       Map JavaDoc pre = new HashMap JavaDoc();
119       pre.put("a", "b");
120       pre.put("c", "d");
121       pre.put("e", "f");
122
123       Map JavaDoc post = new HashMap JavaDoc();
124       post.put("a", "b");
125       post.put("c", "d");
126       post.put("e", "f");
127
128       assertEquals(expected, Util.diffNodeData(pre, post));
129    }
130
131    public void testMultipleChanges()
132    {
133       Util.MapModifications expected = new Util.MapModifications();
134       expected.modifiedEntries.put("key", "value");
135       expected.modifiedEntries.put("key1", "value1");
136       expected.addedEntries.put("key2", "value2");
137       expected.addedEntries.put("key3", "value3");
138       expected.removedEntries.put("key4", "value4");
139       expected.removedEntries.put("key5", "value5");
140
141       Map JavaDoc pre = new HashMap JavaDoc();
142       pre.put("oldKey", "oldValue");
143       pre.put("key", "valueOLD");
144       pre.put("key1", "value1OLD");
145       pre.put("key4", "value4");
146       pre.put("key5", "value5");
147
148       Map JavaDoc post = new HashMap JavaDoc();
149       post.put("oldKey", "oldValue");
150       post.put("key", "value");
151       post.put("key1", "value1");
152       post.put("key2", "value2");
153       post.put("key3", "value3");
154
155       assertEquals(expected, Util.diffNodeData(pre, post));
156    }
157
158 }
159
Popular Tags