root/trunk/test/java/org/jcoderz/commons/util/SerializableIteratorTest.java

Revision 1011, 4.6 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.util;
34
35import java.io.ByteArrayInputStream;
36import java.io.ByteArrayOutputStream;
37import java.io.IOException;
38import java.io.ObjectInputStream;
39import java.io.ObjectOutputStream;
40import java.util.ArrayList;
41import java.util.Arrays;
42import java.util.HashSet;
43import java.util.Iterator;
44import java.util.List;
45import java.util.NoSuchElementException;
46import java.util.Set;
47
48import junit.framework.TestCase;
49
50/**
51 * Tests the Serializable Iterator.
52 * @author Albrecht Messner
53 */
54public class SerializableIteratorTest
55      extends TestCase
56{
57   /**
58    * Tests serialization of the SerializableIterator.
59    * @throws Exception if the testcase fails
60    */
61   public void testSerialization ()
62         throws Exception
63   {
64      final String[] testData
65            = new String[] {"foo", "bar", "bingo", "bongo", "hallo"};
66      final SerializableIterator si = SerializableIterator.fromArray(testData);
67      final byte[] serialized = serialize(si);
68      final SerializableIterator si2
69            = (SerializableIterator) deserialize(serialized);
70      int count = 0;
71      while (si2.hasNext())
72      {
73         si2.next();
74         count++;
75      }
76      assertEquals("wrong # of items in iterator", testData.length, count);
77   }
78
79   public void testContentsAndBoundaries ()
80         throws Exception
81   {
82      final String[] testData = new String[] {"a", "b", "c"};
83      final SerializableIterator si = SerializableIterator.fromArray(testData);
84
85      final List testDataAsList = new ArrayList(Arrays.asList(testData));
86
87      for (int i = 0; i < testData.length; i++)
88      {
89         final String s = (String) si.next();
90         assertTrue("Result should be in test data list",
91               testDataAsList.contains(s));
92         testDataAsList.remove(s);
93      }
94
95      try
96      {
97         si.next();
98         fail("Should be at end of iterator");
99      }
100      catch (NoSuchElementException x)
101      {
102         // expected
103      }
104   }
105
106   public void testWithCollection ()
107   {
108      final Set hs = new HashSet();
109      hs.add("gandalf");
110      hs.add("frodo");
111      hs.add("bilbo");
112      hs.add("aragorn");
113
114      final Iterator it = SerializableIterator.fromCollection(hs);
115      while (it.hasNext())
116      {
117         final String s = (String) it.next();
118         assertTrue("String must be in test data", hs.contains(s));
119      }
120   }
121
122   private byte[] serialize (Object o)
123         throws IOException
124   {
125      final ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
126      final ObjectOutputStream objOutStream
127            = new ObjectOutputStream(byteOutStream);
128      objOutStream.writeObject(o);
129      objOutStream.flush();
130      objOutStream.close();
131      return byteOutStream.toByteArray();
132   }
133
134   private Object deserialize (byte [] b)
135         throws IOException, ClassNotFoundException
136   {
137      final ByteArrayInputStream byteInStream = new ByteArrayInputStream(b);
138      final ObjectInputStream objInStream = new ObjectInputStream(byteInStream);
139      return objInStream.readObject();
140   }
141}
Note: See TracBrowser for help on using the browser.