KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > utils > DirtyFlagMapTest


1 /*
2  * Copyright 2004-2006 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package org.quartz.utils;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.quartz.utils.DirtyFlagMap;
25
26 import junit.framework.TestCase;
27
28 /**
29  * Unit test for DirtyFlagMap. These tests focus on making
30  * sure the isDirty flag is set correctly.
31  */

32 public class DirtyFlagMapTest extends TestCase {
33
34     public void testClear() {
35         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
36         assertFalse(dirtyFlagMap.isDirty());
37         
38         dirtyFlagMap.clear();
39         assertFalse(dirtyFlagMap.isDirty());
40         dirtyFlagMap.put("X", "Y");
41         dirtyFlagMap.clearDirtyFlag();
42         dirtyFlagMap.clear();
43         assertTrue(dirtyFlagMap.isDirty());
44     }
45     
46     public void testPut() {
47         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
48         dirtyFlagMap.put("a", "Y");
49         assertTrue(dirtyFlagMap.isDirty());
50     }
51     
52     public void testRemove() {
53         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
54         dirtyFlagMap.put("a", "Y");
55         dirtyFlagMap.clearDirtyFlag();
56         
57         dirtyFlagMap.remove("b");
58         assertFalse(dirtyFlagMap.isDirty());
59
60         dirtyFlagMap.remove("a");
61         assertTrue(dirtyFlagMap.isDirty());
62     }
63     
64     public void testEntrySetRemove() {
65         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
66         Set JavaDoc entrySet = dirtyFlagMap.entrySet();
67         dirtyFlagMap.remove("a");
68         assertFalse(dirtyFlagMap.isDirty());
69         dirtyFlagMap.put("a", "Y");
70         dirtyFlagMap.clearDirtyFlag();
71         entrySet.remove("b");
72         assertFalse(dirtyFlagMap.isDirty());
73         entrySet.remove(entrySet.iterator().next());
74         assertTrue(dirtyFlagMap.isDirty());
75     }
76
77     public void testEntrySetRetainAll() {
78         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
79         Set JavaDoc entrySet = dirtyFlagMap.entrySet();
80         entrySet.retainAll(Collections.EMPTY_LIST);
81         assertFalse(dirtyFlagMap.isDirty());
82         dirtyFlagMap.put("a", "Y");
83         dirtyFlagMap.clearDirtyFlag();
84         entrySet.retainAll(Collections.singletonList(entrySet.iterator().next()));
85         assertFalse(dirtyFlagMap.isDirty());
86         entrySet.retainAll(Collections.EMPTY_LIST);
87         assertTrue(dirtyFlagMap.isDirty());
88     }
89     
90     public void testEntrySetRemoveAll() {
91         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
92         Set JavaDoc entrySet = dirtyFlagMap.entrySet();
93         entrySet.removeAll(Collections.EMPTY_LIST);
94         assertFalse(dirtyFlagMap.isDirty());
95         dirtyFlagMap.put("a", "Y");
96         dirtyFlagMap.clearDirtyFlag();
97         entrySet.removeAll(Collections.EMPTY_LIST);
98         assertFalse(dirtyFlagMap.isDirty());
99         entrySet.removeAll(Collections.singletonList(entrySet.iterator().next()));
100         assertTrue(dirtyFlagMap.isDirty());
101     }
102     
103     public void testEntrySetClear() {
104         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
105         Set JavaDoc entrySet = dirtyFlagMap.entrySet();
106         entrySet.clear();
107         assertFalse(dirtyFlagMap.isDirty());
108         dirtyFlagMap.put("a", "Y");
109         dirtyFlagMap.clearDirtyFlag();
110         entrySet.clear();
111         assertTrue(dirtyFlagMap.isDirty());
112     }
113
114     public void testEntrySetIterator() {
115         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
116         Set JavaDoc entrySet = dirtyFlagMap.entrySet();
117         dirtyFlagMap.put("a", "A");
118         dirtyFlagMap.put("b", "B");
119         dirtyFlagMap.put("c", "C");
120         dirtyFlagMap.clearDirtyFlag();
121         Iterator JavaDoc entrySetIter = entrySet.iterator();
122         Map.Entry JavaDoc entryToBeRemoved = (Map.Entry JavaDoc)entrySetIter.next();
123         String JavaDoc removedKey = (String JavaDoc)entryToBeRemoved.getKey();
124         entrySetIter.remove();
125         assertEquals(2, dirtyFlagMap.size());
126         assertTrue(dirtyFlagMap.isDirty());
127         assertFalse(dirtyFlagMap.containsKey(removedKey));
128         dirtyFlagMap.clearDirtyFlag();
129         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entrySetIter.next();
130         entry.setValue("BB");
131         assertTrue(dirtyFlagMap.isDirty());
132         assertTrue(dirtyFlagMap.containsValue("BB"));
133     }
134
135     public void testEntrySetToArray() {
136         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
137         Set JavaDoc entrySet = dirtyFlagMap.entrySet();
138         dirtyFlagMap.put("a", "A");
139         dirtyFlagMap.put("b", "B");
140         dirtyFlagMap.put("c", "C");
141         dirtyFlagMap.clearDirtyFlag();
142         Object JavaDoc[] array = entrySet.toArray();
143         assertEquals(3, array.length);
144         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)array[0];
145         entry.setValue("BB");
146         assertTrue(dirtyFlagMap.isDirty());
147         assertTrue(dirtyFlagMap.containsValue("BB"));
148     }
149
150     public void testEntrySetToArrayWithArg() {
151         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
152         Set JavaDoc entrySet = dirtyFlagMap.entrySet();
153         dirtyFlagMap.put("a", "A");
154         dirtyFlagMap.put("b", "B");
155         dirtyFlagMap.put("c", "C");
156         dirtyFlagMap.clearDirtyFlag();
157         Object JavaDoc[] array = entrySet.toArray(new Map.Entry JavaDoc[] {});
158         assertEquals(3, array.length);
159         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)array[0];
160         entry.setValue("BB");
161         assertTrue(dirtyFlagMap.isDirty());
162         assertTrue(dirtyFlagMap.containsValue("BB"));
163     }
164     
165     public void testKeySetClear() {
166         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
167         Set JavaDoc keySet = dirtyFlagMap.keySet();
168         keySet.clear();
169         assertFalse(dirtyFlagMap.isDirty());
170         dirtyFlagMap.put("a", "Y");
171         dirtyFlagMap.clearDirtyFlag();
172         keySet.clear();
173         assertTrue(dirtyFlagMap.isDirty());
174         assertEquals(0, dirtyFlagMap.size());
175     }
176         
177     public void testValuesClear() {
178         DirtyFlagMap dirtyFlagMap = new DirtyFlagMap();
179         Collection JavaDoc values = dirtyFlagMap.values();
180         values.clear();
181         assertFalse(dirtyFlagMap.isDirty());
182         dirtyFlagMap.put("a", "Y");
183         dirtyFlagMap.clearDirtyFlag();
184         values.clear();
185         assertTrue(dirtyFlagMap.isDirty());
186         assertEquals(0, dirtyFlagMap.size());
187     }
188 }
189
Popular Tags