1 16 package org.quartz.utils; 17 18 import java.util.Collection ; 19 import java.util.Collections ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import org.quartz.utils.DirtyFlagMap; 25 26 import junit.framework.TestCase; 27 28 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 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 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 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 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 entrySet = dirtyFlagMap.entrySet(); 117 dirtyFlagMap.put("a", "A"); 118 dirtyFlagMap.put("b", "B"); 119 dirtyFlagMap.put("c", "C"); 120 dirtyFlagMap.clearDirtyFlag(); 121 Iterator entrySetIter = entrySet.iterator(); 122 Map.Entry entryToBeRemoved = (Map.Entry )entrySetIter.next(); 123 String removedKey = (String )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 entry = (Map.Entry )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 entrySet = dirtyFlagMap.entrySet(); 138 dirtyFlagMap.put("a", "A"); 139 dirtyFlagMap.put("b", "B"); 140 dirtyFlagMap.put("c", "C"); 141 dirtyFlagMap.clearDirtyFlag(); 142 Object [] array = entrySet.toArray(); 143 assertEquals(3, array.length); 144 Map.Entry entry = (Map.Entry )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 entrySet = dirtyFlagMap.entrySet(); 153 dirtyFlagMap.put("a", "A"); 154 dirtyFlagMap.put("b", "B"); 155 dirtyFlagMap.put("c", "C"); 156 dirtyFlagMap.clearDirtyFlag(); 157 Object [] array = entrySet.toArray(new Map.Entry [] {}); 158 assertEquals(3, array.length); 159 Map.Entry entry = (Map.Entry )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 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 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 |