Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.SerializableIteratorTest

LineHitsNoteSource
1  /*
2   * $Id: SerializableIteratorTest.java 1011 2008-06-16 17:57:36Z amandel $
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   */
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   * Tests the Serializable Iterator.
52   * @author Albrecht Messner
53   */
54100 public 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     {
64100(1)      final String[] testData
65              = new String[] {"foo", "bar", "bingo", "bongo", "hallo"};
66100       final SerializableIterator si = SerializableIterator.fromArray(testData);
67100       final byte[] serialized = serialize(si);
68100       final SerializableIterator si2
69              = (SerializableIterator) deserialize(serialized);
70100       int count = 0;
71100       while (si2.hasNext())
72        {
73100          si2.next();
74100          count++;
75        }
76100       assertEquals("wrong # of items in iterator", testData.length, count);
77100    }
78  
79 (2)   public void testContentsAndBoundaries ()
80           throws Exception
81     {
82100(3)      final String[] testData = new String[] {"a", "b", "c"};
83100       final SerializableIterator si = SerializableIterator.fromArray(testData);
84  
85100       final List testDataAsList = new ArrayList(Arrays.asList(testData));
86  
87100       for (int i = 0; i < testData.length; i++)
88        {
89100          final String s = (String) si.next();
90100          assertTrue("Result should be in test data list",
91                 testDataAsList.contains(s));
92100          testDataAsList.remove(s);
93        }
94  
95        try
96        {
970          si.next();
980          fail("Should be at end of iterator");
99        }
100100       catch (NoSuchElementException x)
101        {
102           // expected
1030       }
104100    }
105  
106 (4)   public void testWithCollection ()
107     {
108100       final Set hs = new HashSet();
109100       hs.add("gandalf");
110100       hs.add("frodo");
111100       hs.add("bilbo");
112100       hs.add("aragorn");
113  
114100       final Iterator it = SerializableIterator.fromCollection(hs);
115100       while (it.hasNext())
116        {
117100          final String s = (String) it.next();
118100          assertTrue("String must be in test data", hs.contains(s));
119100       }
120100    }
121  
122     private byte[] serialize (Object o)
123           throws IOException
124     {
125100       final ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
126100       final ObjectOutputStream objOutStream
127              = new ObjectOutputStream(byteOutStream);
128100       objOutStream.writeObject(o);
129100       objOutStream.flush();
130100       objOutStream.close();
131100       return byteOutStream.toByteArray();
132     }
133  
134     private Object deserialize (byte [] b)
135           throws IOException, ClassNotFoundException
136     {
137100       final ByteArrayInputStream byteInStream = new ByteArrayInputStream(b);
138100       final ObjectInputStream objInStream = new ObjectInputStream(byteInStream);
139100       return objInStream.readObject();
140     }
141  }

Findings in this File

i (1) 64 : 0 Method org.jcoderz.commons.util.SerializableIteratorTest.testSerialization() creates array using constants (test code) Decreased severity from 'warning' for testcode.
c (2) 79 : 4 Missing a Javadoc comment.
i (3) 82 : 0 Method org.jcoderz.commons.util.SerializableIteratorTest.testContentsAndBoundaries() creates array using constants (test code) Decreased severity from 'warning' for testcode.
c (4) 106 : 4 Missing a Javadoc comment.