File: DecoderTestHelper.java

package info (click to toggle)
mauve 20120103-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 28,504 kB
  • sloc: java: 250,155; sh: 2,834; xml: 208; makefile: 66
file content (195 lines) | stat: -rw-r--r-- 6,554 bytes parent folder | download | duplicates (5)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//Tags: not-a-test

//Uses: EqualityChecker

//Copyright (C) 2004 Robert Schuster <theBohemian@gmx.net>

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA.


package gnu.testlet.java.beans.XMLDecoder;

import gnu.testlet.ResourceNotFoundException;
import gnu.testlet.TestHarness;

import java.beans.ExceptionListener;
import java.beans.XMLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/** Helper class for tests involving the XMLDecoder. It provides a simple environment to set up
 * a sequence of objects which is expected to be decoded and allows the registration of an
 * <code>EqualityChecker</code> instance for objects which do not provide a proper implementation
 * of <code>Object.equals(Object)</code>.
 *
 * Note that the XMLDecoder commonly relies on a SAX parser being available.
 *
 * @author Robert Schuster
 *
 */
class DecoderTestHelper
{
  private List expectedObjects = new ArrayList();
  private Map checkers = new HashMap();
  private String xmlFile;
  private String name;

  /** Creates the environment for a testcase of the XMLDecoder. The argument string must
   * be pointing to a XML file which can be loaded as a resource from the classpath. See
   * <code>TestHarness.getResourceStream()</code> for details.
   *
   * @param name A name that is displayed in case of errors
   * @param xmlResourceFilename A filename pointing to a XML file which can be loaded as a resource.
   */
  DecoderTestHelper(String name, String xmlResourceFilename)
  {
    if (name == null || name.length() == 0)
      throw new IllegalArgumentException("Please provide a name for this test.");
    
    this.name = name;

    if (xmlResourceFilename == null)
      throw new IllegalArgumentException("No filename to a XML resource file provided.");
    xmlFile = xmlResourceFilename;
  }

  /** Adds an object to the sequence of expected objects to be decoded.
   *
   * @param obj Object which is expected to be decoded.
   */
  protected final void addObject(Object obj)
  {
    expectedObjects.add(obj);
  }

  /** Adds an object to the sequence of expectedd objects to be an decoded and provides a specialized
   * <code>EqualityChecker</code> for the instance. Providing an <code>EqualityChecker</code> is useful
   * if the class of the given object does not properly support the <code>Object.equals(Object)</code>
   * method and cannot be changed.
   *
   * Note that only once <code>EqualityChecker</code> per expected object instance is allowed. If you
   * need the same object more than once in the sequence use the one-parameter form of
   * <code>addObject()</code> after the first registration only.
   *
   * @param obj Object which is expected to be decoded.
   * @param eqChecker A specialized <code>EqualityChecker</code> implementation for the given object.
   */
  protected final void addObject(Object obj, EqualityChecker eqChecker)
  {
    // prevents registering an EqualityChecker more than once because such behaviour is not supported 
    if (checkers.containsKey(obj))
      throw new IllegalStateException("Already provided an EqualityChecker instance for object '"
                                      + obj + "'.");

    checkers.put(obj, eqChecker);

    addObject(obj);
  }

  final void doComparison(final TestHarness harness)
  {
    // creates an ExceptionListener implementation that lets the test fail
    // if one occurs
    ExceptionListener exListener = new ExceptionListener()
      {
	public void exceptionThrown(Exception e)
	{
	  harness.fail(name
	               + " - decode.readObject(): unexpected exception occured during decoding: "
	               + e);
	}
      };

    XMLDecoder decoder = null;

    // tries to initialize the XMLDecoder
    try
      {
	decoder = new XMLDecoder(harness.getResourceStream(xmlFile), null,
	                         exListener);
      }
    catch (ResourceNotFoundException e)
      {
	harness.fail(name
	             + " - create XMLDecoder: unable to load resource from classpath: "
	             + xmlFile);
	return;
      }

    /* compares each object of the XMLDecoder with the one that is expected either using
     * Object.equals() or using a specialized EqualityChecker instance
     */
    Iterator ite = expectedObjects.iterator();
    while (ite.hasNext())
      {
	Object expectedObject = ite.next();

	try
	  {
	    Object decodedObject = decoder.readObject();

	    if (checkers.containsKey(expectedObject))
	      {
		EqualityChecker eq = (EqualityChecker) checkers.get(expectedObject);

		harness.check(eq.areEqual(decodedObject, expectedObject),
		              name + " - decoder.readObject()-loop");
	      }
	    else
	      harness.check(decodedObject, expectedObject,
	                    name + " - decoder.readObject()-loop");
	  }
	catch (ArrayIndexOutOfBoundsException aioobe)
	  {
	    decoder.close();

	    // that means that not enough objects where decoded
	    harness.fail(name + " - decoder.close()");

	    harness.verbose(name
	                    + "decoder.close(): no more objects provided by XMLDecoder "
	                    + "although at least one more was expected");

	    return;
	  }
      }

    /* After all expected objects have been compared there should not be any object left in the decoder.
     * Since there is no query method for this we have to invoke readObject() which should throw an
     * ArrayIndexOutOfBoundsException if there is no object left.
     */
    try
      {
	decoder.readObject();
      }
    catch (ArrayIndexOutOfBoundsException aioobe)
      {
	// this exception is expected
	decoder.close();

	return;
      }

    // there was at least one object left if we reach this code
    harness.fail(name
                 + " - readObject()-final: at least one unexpected object was left in the decoder");

    decoder.close();
  }
}