| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.io.ByteArrayInputStream; |
| 36 | | | import java.io.ByteArrayOutputStream; |
| 37 | | | import java.io.IOException; |
| 38 | | | import java.io.ObjectInputStream; |
| 39 | | | import java.io.ObjectOutputStream; |
| 40 | | | import java.util.ArrayList; |
| 41 | | | import java.util.Arrays; |
| 42 | | | import java.util.HashSet; |
| 43 | | | import java.util.Iterator; |
| 44 | | | import java.util.List; |
| 45 | | | import java.util.NoSuchElementException; |
| 46 | | | import java.util.Set; |
| 47 | | | |
| 48 | | | import junit.framework.TestCase; |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | @author |
| 53 | | | |
| 54 | 100 | | public class SerializableIteratorTest |
| 55 | | | extends TestCase |
| 56 | | | { |
| 57 | | | |
| 58 | | | |
| 59 | | | @throws |
| 60 | | | |
| 61 | | | public void testSerialization () |
| 62 | | | throws Exception |
| 63 | | | { |
| 64 | 100 | (1) | final String[] testData |
| 65 | | | = new String[] {"foo", "bar", "bingo", "bongo", "hallo"}; |
| 66 | 100 | | final SerializableIterator si = SerializableIterator.fromArray(testData); |
| 67 | 100 | | final byte[] serialized = serialize(si); |
| 68 | 100 | | final SerializableIterator si2 |
| 69 | | | = (SerializableIterator) deserialize(serialized); |
| 70 | 100 | | int count = 0; |
| 71 | 100 | | while (si2.hasNext()) |
| 72 | | | { |
| 73 | 100 | | si2.next(); |
| 74 | 100 | | count++; |
| 75 | | | } |
| 76 | 100 | | assertEquals("wrong # of items in iterator", testData.length, count); |
| 77 | 100 | | } |
| 78 | | | |
| 79 | | (2) | public void testContentsAndBoundaries () |
| 80 | | | throws Exception |
| 81 | | | { |
| 82 | 100 | (3) | final String[] testData = new String[] {"a", "b", "c"}; |
| 83 | 100 | | final SerializableIterator si = SerializableIterator.fromArray(testData); |
| 84 | | | |
| 85 | 100 | | final List testDataAsList = new ArrayList(Arrays.asList(testData)); |
| 86 | | | |
| 87 | 100 | | for (int i = 0; i < testData.length; i++) |
| 88 | | | { |
| 89 | 100 | | final String s = (String) si.next(); |
| 90 | 100 | | assertTrue("Result should be in test data list", |
| 91 | | | testDataAsList.contains(s)); |
| 92 | 100 | | testDataAsList.remove(s); |
| 93 | | | } |
| 94 | | | |
| 95 | | | try |
| 96 | | | { |
| 97 | 0 | | si.next(); |
| 98 | 0 | | fail("Should be at end of iterator"); |
| 99 | | | } |
| 100 | 100 | | catch (NoSuchElementException x) |
| 101 | | | { |
| 102 | | | |
| 103 | 0 | | } |
| 104 | 100 | | } |
| 105 | | | |
| 106 | | (4) | public void testWithCollection () |
| 107 | | | { |
| 108 | 100 | | final Set hs = new HashSet(); |
| 109 | 100 | | hs.add("gandalf"); |
| 110 | 100 | | hs.add("frodo"); |
| 111 | 100 | | hs.add("bilbo"); |
| 112 | 100 | | hs.add("aragorn"); |
| 113 | | | |
| 114 | 100 | | final Iterator it = SerializableIterator.fromCollection(hs); |
| 115 | 100 | | while (it.hasNext()) |
| 116 | | | { |
| 117 | 100 | | final String s = (String) it.next(); |
| 118 | 100 | | assertTrue("String must be in test data", hs.contains(s)); |
| 119 | 100 | | } |
| 120 | 100 | | } |
| 121 | | | |
| 122 | | | private byte[] serialize (Object o) |
| 123 | | | throws IOException |
| 124 | | | { |
| 125 | 100 | | final ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(); |
| 126 | 100 | | final ObjectOutputStream objOutStream |
| 127 | | | = new ObjectOutputStream(byteOutStream); |
| 128 | 100 | | objOutStream.writeObject(o); |
| 129 | 100 | | objOutStream.flush(); |
| 130 | 100 | | objOutStream.close(); |
| 131 | 100 | | return byteOutStream.toByteArray(); |
| 132 | | | } |
| 133 | | | |
| 134 | | | private Object deserialize (byte [] b) |
| 135 | | | throws IOException, ClassNotFoundException |
| 136 | | | { |
| 137 | 100 | | final ByteArrayInputStream byteInStream = new ByteArrayInputStream(b); |
| 138 | 100 | | final ObjectInputStream objInStream = new ObjectInputStream(byteInStream); |
| 139 | 100 | | return objInStream.readObject(); |
| 140 | | | } |
| 141 | | | } |