[667] | 1 | package de.ugoe.cs.util.console; |
---|
[530] | 2 | |
---|
[666] | 3 | import static org.junit.Assert.assertEquals; |
---|
| 4 | import static org.junit.Assert.assertFalse; |
---|
| 5 | import static org.junit.Assert.assertNotNull; |
---|
| 6 | import static org.junit.Assert.assertNull; |
---|
| 7 | import static org.junit.Assert.assertSame; |
---|
| 8 | import static org.junit.Assert.assertTrue; |
---|
| 9 | import static org.junit.Assert.fail; |
---|
| 10 | |
---|
[530] | 11 | import java.io.ByteArrayInputStream; |
---|
| 12 | import java.io.ByteArrayOutputStream; |
---|
| 13 | import java.io.InputStream; |
---|
| 14 | import java.io.ObjectInputStream; |
---|
| 15 | import java.io.ObjectOutputStream; |
---|
| 16 | import java.lang.reflect.Constructor; |
---|
| 17 | import java.lang.reflect.Modifier; |
---|
| 18 | import java.util.Collection; |
---|
| 19 | import java.util.LinkedList; |
---|
| 20 | |
---|
[666] | 21 | import org.junit.Before; |
---|
| 22 | import org.junit.Test; |
---|
[530] | 23 | |
---|
[667] | 24 | import de.ugoe.cs.util.console.GlobalDataContainer; |
---|
| 25 | |
---|
[530] | 26 | /** |
---|
| 27 | * The class <code>WindowsEventTest</code> contains tests for the class |
---|
| 28 | * <code>{@link WindowsEvent}</code>. |
---|
| 29 | * |
---|
| 30 | * @author Steffen Herbold |
---|
| 31 | * @version 1.0 |
---|
| 32 | */ |
---|
| 33 | public class GlobalDataContainerTest { |
---|
| 34 | |
---|
[555] | 35 | private static void assertCollectionContent(Collection<?> c1, Collection<?> c2) { |
---|
| 36 | assertEquals(c1.size(), c2.size()); |
---|
| 37 | for (Object obj : c1) { |
---|
| 38 | assertTrue(c2.contains(obj)); |
---|
| 39 | } |
---|
| 40 | } |
---|
[530] | 41 | |
---|
[555] | 42 | @Test |
---|
| 43 | public void testGlobalDataContainer_1() throws Exception { |
---|
| 44 | // assure that all constructors are private |
---|
| 45 | Constructor<?>[] constructors = GlobalDataContainer.class.getDeclaredConstructors(); |
---|
| 46 | for (int i = 0; i < constructors.length; i++) { |
---|
| 47 | int modifier = constructors[i].getModifiers(); |
---|
| 48 | if (modifier != Modifier.PRIVATE) { |
---|
| 49 | fail(constructors[i].getName() + " - " + Modifier.toString(modifier)); |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | } |
---|
[530] | 53 | |
---|
[555] | 54 | @Test |
---|
| 55 | public void testGetInstance_1() throws Exception { |
---|
| 56 | GlobalDataContainer instance1 = GlobalDataContainer.getInstance(); |
---|
| 57 | GlobalDataContainer instance2 = GlobalDataContainer.getInstance(); |
---|
| 58 | assertNotNull(instance1); |
---|
| 59 | assertSame(instance1, instance2); |
---|
| 60 | } |
---|
[530] | 61 | |
---|
[555] | 62 | @Test |
---|
| 63 | public void testDatahandling_1() throws Exception { |
---|
| 64 | // test is data can be added and retrieved as expected |
---|
| 65 | GlobalDataContainer instance = GlobalDataContainer.getInstance(); |
---|
[530] | 66 | |
---|
[555] | 67 | String key1 = "key1"; |
---|
| 68 | String data1 = "data1"; |
---|
| 69 | String key2 = "key2"; |
---|
| 70 | String data2 = "data2"; |
---|
| 71 | String key3 = "key3"; |
---|
| 72 | Integer data3 = 1; |
---|
| 73 | String key4 = "key4"; |
---|
| 74 | Integer data4 = 2; |
---|
| 75 | String key5 = "key5"; |
---|
| 76 | Integer data5 = 3; |
---|
[530] | 77 | |
---|
[555] | 78 | assertFalse(instance.addData(key1, data1)); |
---|
| 79 | assertFalse(instance.addData(key2, data2)); |
---|
| 80 | assertFalse(instance.addData(key3, data3)); |
---|
| 81 | assertFalse(instance.addData(key4, data4)); |
---|
| 82 | assertFalse(instance.addData(key5, data5)); |
---|
[530] | 83 | |
---|
[555] | 84 | assertEquals(data1, instance.getData(key1)); |
---|
| 85 | assertEquals(data2, instance.getData(key2)); |
---|
| 86 | assertEquals(data3, instance.getData(key3)); |
---|
| 87 | assertEquals(data4, instance.getData(key4)); |
---|
| 88 | assertEquals(data5, instance.getData(key5)); |
---|
| 89 | } |
---|
[530] | 90 | |
---|
[555] | 91 | @Test |
---|
| 92 | public void testDatahandling_2() throws Exception { |
---|
| 93 | // test is data can be added, overwritten, and retrieved as expected |
---|
| 94 | GlobalDataContainer instance = GlobalDataContainer.getInstance(); |
---|
[530] | 95 | |
---|
[555] | 96 | String key1 = "key1"; |
---|
| 97 | String data1 = "data1"; |
---|
| 98 | String data1changed = "data1changed"; |
---|
| 99 | String key2 = "key2"; |
---|
| 100 | String data2 = "data2"; |
---|
| 101 | String data2changed = "data2changed"; |
---|
| 102 | String key3 = "key3"; |
---|
| 103 | Integer data3 = 1; |
---|
| 104 | Integer data3changed = 11; |
---|
| 105 | String key4 = "key4"; |
---|
| 106 | Integer data4 = 2; |
---|
| 107 | Integer data4changed = 22; |
---|
| 108 | String key5 = "key5"; |
---|
| 109 | Integer data5 = 3; |
---|
| 110 | Integer data5changed = 33; |
---|
[530] | 111 | |
---|
[555] | 112 | assertFalse(instance.addData(key1, data1)); |
---|
| 113 | assertFalse(instance.addData(key2, data2)); |
---|
| 114 | assertFalse(instance.addData(key3, data3)); |
---|
| 115 | assertFalse(instance.addData(key4, data4)); |
---|
| 116 | assertFalse(instance.addData(key5, data5)); |
---|
[530] | 117 | |
---|
[555] | 118 | assertTrue(instance.addData(key1, data1changed)); |
---|
| 119 | assertTrue(instance.addData(key2, data2changed)); |
---|
| 120 | assertTrue(instance.addData(key3, data3changed)); |
---|
| 121 | assertTrue(instance.addData(key4, data4changed)); |
---|
| 122 | assertTrue(instance.addData(key5, data5changed)); |
---|
[530] | 123 | |
---|
[555] | 124 | assertEquals(data1changed, instance.getData(key1)); |
---|
| 125 | assertEquals(data2changed, instance.getData(key2)); |
---|
| 126 | assertEquals(data3changed, instance.getData(key3)); |
---|
| 127 | assertEquals(data4changed, instance.getData(key4)); |
---|
| 128 | assertEquals(data5changed, instance.getData(key5)); |
---|
| 129 | } |
---|
[530] | 130 | |
---|
[555] | 131 | @Test |
---|
| 132 | public void testDatahandling_3() throws Exception { |
---|
| 133 | // test is data can be removed as expected |
---|
| 134 | GlobalDataContainer instance = GlobalDataContainer.getInstance(); |
---|
[530] | 135 | |
---|
[555] | 136 | String key1 = "key1"; |
---|
| 137 | String data1 = "data1"; |
---|
| 138 | String key2 = "key2"; |
---|
| 139 | String data2 = "data2"; |
---|
| 140 | String key3 = "key3"; |
---|
| 141 | Integer data3 = 1; |
---|
| 142 | String key4 = "key4"; |
---|
| 143 | Integer data4 = 2; |
---|
| 144 | String key5 = "key5"; |
---|
| 145 | Integer data5 = 3; |
---|
[530] | 146 | |
---|
[555] | 147 | assertFalse(instance.addData(key1, data1)); |
---|
| 148 | assertFalse(instance.addData(key2, data2)); |
---|
| 149 | assertFalse(instance.addData(key3, data3)); |
---|
| 150 | assertFalse(instance.addData(key4, data4)); |
---|
| 151 | assertFalse(instance.addData(key5, data5)); |
---|
[530] | 152 | |
---|
[555] | 153 | assertFalse(instance.removeData("keyNotFound")); |
---|
| 154 | assertTrue(instance.removeData(key1)); |
---|
[530] | 155 | |
---|
[555] | 156 | assertNull(instance.getData(key1)); |
---|
| 157 | } |
---|
[530] | 158 | |
---|
[555] | 159 | @Test |
---|
| 160 | public void testDatahandling_4() throws Exception { |
---|
| 161 | // test is data can be removed as expected |
---|
| 162 | GlobalDataContainer instance = GlobalDataContainer.getInstance(); |
---|
[530] | 163 | |
---|
[555] | 164 | String key1 = "key1"; |
---|
| 165 | String data1 = "data1"; |
---|
| 166 | String key2 = "key2"; |
---|
| 167 | String data2 = "data2"; |
---|
| 168 | String key3 = "key3"; |
---|
| 169 | Integer data3 = 1; |
---|
| 170 | String key4 = "key4"; |
---|
| 171 | Integer data4 = 2; |
---|
| 172 | String key5 = "key5"; |
---|
| 173 | Integer data5 = 3; |
---|
[530] | 174 | |
---|
[555] | 175 | assertFalse(instance.addData(key1, data1)); |
---|
| 176 | assertFalse(instance.addData(key2, data2)); |
---|
| 177 | assertFalse(instance.addData(key3, data3)); |
---|
| 178 | assertFalse(instance.addData(key4, data4)); |
---|
| 179 | assertFalse(instance.addData(key5, data5)); |
---|
[530] | 180 | |
---|
[555] | 181 | assertNull(instance.getData("keyNotFound")); |
---|
| 182 | } |
---|
[530] | 183 | |
---|
[555] | 184 | @Test |
---|
| 185 | public void testReset_1() throws Exception { |
---|
| 186 | // check if data container is empty after reset |
---|
| 187 | GlobalDataContainer instance = GlobalDataContainer.getInstance(); |
---|
| 188 | String key1 = "key1"; |
---|
| 189 | String data1 = "data1"; |
---|
| 190 | String key2 = "key2"; |
---|
| 191 | String data2 = "data2"; |
---|
| 192 | String key3 = "key3"; |
---|
| 193 | Integer data3 = 1; |
---|
| 194 | String key4 = "key4"; |
---|
| 195 | Integer data4 = 2; |
---|
| 196 | String key5 = "key5"; |
---|
| 197 | Integer data5 = 3; |
---|
[530] | 198 | |
---|
[555] | 199 | assertFalse(instance.addData(key1, data1)); |
---|
| 200 | assertFalse(instance.addData(key2, data2)); |
---|
| 201 | assertFalse(instance.addData(key3, data3)); |
---|
| 202 | assertFalse(instance.addData(key4, data4)); |
---|
| 203 | assertFalse(instance.addData(key5, data5)); |
---|
[530] | 204 | |
---|
[555] | 205 | instance.reset(); |
---|
| 206 | |
---|
| 207 | assertNull(instance.getData(key1)); |
---|
| 208 | assertNull(instance.getData(key2)); |
---|
| 209 | assertNull(instance.getData(key3)); |
---|
| 210 | assertNull(instance.getData(key4)); |
---|
| 211 | assertNull(instance.getData(key5)); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | @Test |
---|
| 215 | public void testGetAllKeys_1() throws Exception { |
---|
| 216 | // check if data container is empty after reset |
---|
| 217 | GlobalDataContainer instance = GlobalDataContainer.getInstance(); |
---|
| 218 | String key1 = "key1"; |
---|
| 219 | String data1 = "data1"; |
---|
| 220 | String key2 = "key2"; |
---|
| 221 | String data2 = "data2"; |
---|
| 222 | String key3 = "key3"; |
---|
| 223 | Integer data3 = 1; |
---|
| 224 | String key4 = "key4"; |
---|
| 225 | Integer data4 = 2; |
---|
| 226 | String key5 = "key5"; |
---|
| 227 | Integer data5 = 3; |
---|
| 228 | |
---|
| 229 | assertFalse(instance.addData(key1, data1)); |
---|
| 230 | assertFalse(instance.addData(key2, data2)); |
---|
| 231 | assertFalse(instance.addData(key3, data3)); |
---|
| 232 | assertFalse(instance.addData(key4, data4)); |
---|
| 233 | assertFalse(instance.addData(key5, data5)); |
---|
| 234 | |
---|
| 235 | Collection<String> expected = new LinkedList<String>(); |
---|
| 236 | expected.add(key1); |
---|
| 237 | expected.add(key2); |
---|
| 238 | expected.add(key3); |
---|
| 239 | expected.add(key4); |
---|
| 240 | expected.add(key5); |
---|
| 241 | |
---|
| 242 | assertCollectionContent(expected, instance.getAllKeys()); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | @Test |
---|
| 246 | public void testSerialization_1() throws Exception { |
---|
| 247 | GlobalDataContainer instance = GlobalDataContainer.getInstance(); |
---|
| 248 | String key1 = "key1"; |
---|
| 249 | String data1 = "data1"; |
---|
| 250 | String key2 = "key2"; |
---|
| 251 | String data2 = "data2"; |
---|
| 252 | String key3 = "key3"; |
---|
| 253 | Integer data3 = 1; |
---|
| 254 | String key4 = "key4"; |
---|
| 255 | Integer data4 = 2; |
---|
| 256 | String key5 = "key5"; |
---|
| 257 | Integer data5 = 3; |
---|
| 258 | |
---|
| 259 | String key6 = "key6"; |
---|
| 260 | Double data6 = 6.0d; |
---|
| 261 | |
---|
| 262 | assertFalse(instance.addData(key1, data1)); |
---|
| 263 | assertFalse(instance.addData(key2, data2)); |
---|
| 264 | assertFalse(instance.addData(key3, data3)); |
---|
| 265 | assertFalse(instance.addData(key4, data4)); |
---|
| 266 | assertFalse(instance.addData(key5, data5)); |
---|
| 267 | |
---|
| 268 | // serialize |
---|
| 269 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
| 270 | ObjectOutputStream oos = new ObjectOutputStream(out); |
---|
| 271 | oos.writeObject(instance); |
---|
| 272 | oos.close(); |
---|
| 273 | |
---|
| 274 | instance.reset(); |
---|
| 275 | assertFalse(instance.addData(key6, data6)); |
---|
| 276 | |
---|
| 277 | // deserialize |
---|
| 278 | byte[] pickled = out.toByteArray(); |
---|
| 279 | InputStream in = new ByteArrayInputStream(pickled); |
---|
| 280 | ObjectInputStream ois = new ObjectInputStream(in); |
---|
| 281 | ois.readObject(); |
---|
| 282 | ois.close(); |
---|
| 283 | |
---|
| 284 | // test the result |
---|
| 285 | assertEquals(data1, instance.getData(key1)); |
---|
| 286 | assertEquals(data2, instance.getData(key2)); |
---|
| 287 | assertEquals(data3, instance.getData(key3)); |
---|
| 288 | assertEquals(data4, instance.getData(key4)); |
---|
| 289 | assertEquals(data5, instance.getData(key5)); |
---|
| 290 | assertNull(instance.getData(key6)); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | @Before |
---|
| 294 | public void setUp() { |
---|
| 295 | GlobalDataContainer.getInstance().reset(); |
---|
| 296 | } |
---|
| 297 | |
---|
[530] | 298 | } |
---|