001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.util;
017:
018: import junit.framework.TestCase;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.ObjectOutputStream;
022: import java.io.ByteArrayInputStream;
023: import java.io.ObjectInputStream;
024: import java.util.List;
025: import java.util.ArrayList;
026: import java.util.Arrays;
027:
028: /**
029: * @version $Rev$ $Date$
030: */
031: public class PojoExternalizationTest extends TestCase {
032:
033: public void _testSpeed() throws Exception {
034: long start = System.currentTimeMillis();
035: ByteArrayOutputStream baos = new ByteArrayOutputStream();
036: ObjectOutputStream out = new ObjectOutputStream(baos);
037:
038: Green green = new Green(1);
039: green.init();
040:
041: int count = 20000;
042:
043: for (int i = count; i > 0; i--) {
044: out.writeObject(new PojoSerialization(green));
045: }
046: out.close();
047:
048: ByteArrayInputStream bais = new ByteArrayInputStream(baos
049: .toByteArray());
050: ObjectInputStream in = new ObjectInputStream(bais);
051:
052: for (int i = count; i > 0; i--) {
053: Green actual = (Green) in.readObject();
054: assertEquals(green, actual);
055: }
056: long finish = System.currentTimeMillis();
057: fail("" + (finish - start));
058: }
059:
060: public void test() throws Exception {
061:
062: ByteArrayOutputStream baos = new ByteArrayOutputStream();
063: ObjectOutputStream out = new ObjectOutputStream(baos);
064:
065: Green green = new Green(1);
066: green.init();
067:
068: out.writeObject(new PojoSerialization(green));
069: out.close();
070:
071: ByteArrayInputStream bais = new ByteArrayInputStream(baos
072: .toByteArray());
073: ObjectInputStream in = new ObjectInputStream(bais);
074: Green actual = (Green) in.readObject();
075:
076: assertEquals(green, actual);
077: }
078:
079: public static class Color {
080: private final double mydouble;
081: private final double[] adouble = new double[] { 10, 10 };
082: private float myfloat = 10;
083: private final float[] afloat = new float[] { 10, 10 };
084: private long mylong = 10;
085: private final long[] along = new long[] { 10, 10 };
086: private int myint = 10;
087: private final int[] aint = new int[] { 10, 10 };
088: private short myshort = 10;
089: private final short[] ashort = new short[] { 10, 10 };
090: private byte mybyte = 10;
091: private final byte[] abyte = new byte[] { 10, 10 };
092: private char mychar = 10;
093: private final char[] achar = new char[] { 10, 10 };
094: private boolean myboolean = false;
095: private final boolean[] aboolean = new boolean[] { false, false };
096: private String myString = "";
097: private final String[] aString = new String[] { "one", "two" };
098: private final List myList = new ArrayList();
099: private final List[] aList = new List[] { new ArrayList(),
100: new ArrayList() };
101:
102: public Color() {
103: mydouble = 10;
104: }
105:
106: public Color(int i) {
107: mydouble = 20;
108: }
109:
110: protected void init() {
111: adouble[0] = 20;
112: adouble[1] = 22;
113: myfloat = 20;
114: afloat[0] = 20;
115: afloat[1] = 22;
116: mylong = 20;
117: along[0] = 20;
118: along[1] = 22;
119: myint = 20;
120: aint[0] = 20;
121: aint[1] = 22;
122: myshort = 20;
123: ashort[0] = 20;
124: ashort[1] = 22;
125: mybyte = 20;
126: abyte[0] = 20;
127: abyte[1] = 22;
128: mychar = 20;
129: achar[0] = 20;
130: achar[1] = 22;
131: myboolean = true;
132: aboolean[0] = true;
133: aboolean[1] = false;
134: myString = "hello";
135: aString[0] = "three";
136: aString[1] = "four";
137: aList[0].add("Color.list[0].entryOne");
138: aList[0].add("Color.list[0].entryTwo");
139: aList[1].add("Color.list[1].entryOne");
140: aList[1].add("Color.list[1].entryTwo");
141: }
142:
143: public boolean equals(Object o) {
144: if (this == o)
145: return true;
146: if (o == null || getClass() != o.getClass())
147: return false;
148:
149: final PojoExternalizationTest.Color color = (PojoExternalizationTest.Color) o;
150:
151: if (myboolean != color.myboolean)
152: return false;
153: if (mybyte != color.mybyte)
154: return false;
155: if (mychar != color.mychar)
156: return false;
157: if (Double.compare(color.mydouble, mydouble) != 0)
158: return false;
159: if (Float.compare(color.myfloat, myfloat) != 0)
160: return false;
161: if (myint != color.myint)
162: return false;
163: if (mylong != color.mylong)
164: return false;
165: if (myshort != color.myshort)
166: return false;
167: if (!Arrays.equals(aList, color.aList))
168: return false;
169: if (!Arrays.equals(aString, color.aString))
170: return false;
171: if (!Arrays.equals(aboolean, color.aboolean))
172: return false;
173: if (!Arrays.equals(abyte, color.abyte))
174: return false;
175: if (!Arrays.equals(achar, color.achar))
176: return false;
177: if (!Arrays.equals(adouble, color.adouble))
178: return false;
179: if (!Arrays.equals(afloat, color.afloat))
180: return false;
181: if (!Arrays.equals(aint, color.aint))
182: return false;
183: if (!Arrays.equals(along, color.along))
184: return false;
185: if (!Arrays.equals(ashort, color.ashort))
186: return false;
187: if (!myList.equals(color.myList))
188: return false;
189: if (!myString.equals(color.myString))
190: return false;
191:
192: return true;
193: }
194:
195: public int hashCode() {
196: int result;
197: long temp;
198: temp = mydouble != +0.0d ? Double
199: .doubleToLongBits(mydouble) : 0L;
200: result = (int) (temp ^ (temp >>> 32));
201: result = 29 * result + myfloat != +0.0f ? Float
202: .floatToIntBits(myfloat) : 0;
203: result = 29 * result + (int) (mylong ^ (mylong >>> 32));
204: result = 29 * result + myint;
205: result = 29 * result + (int) myshort;
206: result = 29 * result + (int) mybyte;
207: result = 29 * result + (int) mychar;
208: result = 29 * result + (myboolean ? 1 : 0);
209: result = 29 * result + myString.hashCode();
210: result = 29 * result + myList.hashCode();
211: return result;
212: }
213: }
214:
215: public static class Green extends PojoExternalizationTest.Color {
216: private double mydouble = 10;
217: private final double[] adouble = new double[] { 10, 10 };
218: private float myfloat = 10;
219: private final float[] afloat = new float[] { 10, 10 };
220: private long mylong = 10;
221: private final long[] along = new long[] { 10, 10 };
222: private int myint = 10;
223: private final int[] aint = new int[] { 10, 10 };
224: private short myshort = 10;
225: private final short[] ashort = new short[] { 10, 10 };
226: private byte mybyte = 10;
227: private final byte[] abyte = new byte[] { 10, 10 };
228: private char mychar = 10;
229: private final char[] achar = new char[] { 10, 10 };
230: private boolean myboolean = false;
231: private final boolean[] aboolean = new boolean[] { false, false };
232: private String myString = "";
233: private final String[] aString = new String[] { "one", "two" };
234: private final List myList = new ArrayList();
235: private final List[] aList = new List[] { new ArrayList(),
236: new ArrayList() };
237:
238: public Green() {
239: }
240:
241: public Green(int i) {
242: super (i);
243: }
244:
245: protected void init() {
246: super .init();
247: mydouble = 30;
248: adouble[0] = 30;
249: adouble[1] = 33;
250: myfloat = 30;
251: afloat[0] = 30;
252: afloat[1] = 33;
253: mylong = 30;
254: along[0] = 30;
255: along[1] = 33;
256: myint = 30;
257: aint[0] = 30;
258: aint[1] = 33;
259: myshort = 30;
260: ashort[0] = 30;
261: ashort[1] = 33;
262: mybyte = 30;
263: abyte[0] = 30;
264: abyte[1] = 33;
265: mychar = 30;
266: achar[0] = 30;
267: achar[1] = 33;
268: myboolean = true;
269: aboolean[0] = true;
270: aboolean[1] = false;
271: myString = "hello";
272: aString[0] = "three";
273: aString[1] = "four";
274: aList[0].add("Green.list[0].entryOne");
275: aList[0].add("Green.list[0].entryTwo");
276: aList[1].add("Green.list[1].entryOne");
277: aList[1].add("Green.list[1].entryTwo");
278: }
279:
280: public boolean equals(Object o) {
281: if (this == o)
282: return true;
283: if (o == null || getClass() != o.getClass())
284: return false;
285: if (!super .equals(o))
286: return false;
287:
288: final Green green = (Green) o;
289:
290: if (myboolean != green.myboolean)
291: return false;
292: if (mybyte != green.mybyte)
293: return false;
294: if (mychar != green.mychar)
295: return false;
296: if (Double.compare(green.mydouble, mydouble) != 0)
297: return false;
298: if (Float.compare(green.myfloat, myfloat) != 0)
299: return false;
300: if (myint != green.myint)
301: return false;
302: if (mylong != green.mylong)
303: return false;
304: if (myshort != green.myshort)
305: return false;
306: if (!Arrays.equals(aList, green.aList))
307: return false;
308: if (!Arrays.equals(aString, green.aString))
309: return false;
310: if (!Arrays.equals(aboolean, green.aboolean))
311: return false;
312: if (!Arrays.equals(abyte, green.abyte))
313: return false;
314: if (!Arrays.equals(achar, green.achar))
315: return false;
316: if (!Arrays.equals(adouble, green.adouble))
317: return false;
318: if (!Arrays.equals(afloat, green.afloat))
319: return false;
320: if (!Arrays.equals(aint, green.aint))
321: return false;
322: if (!Arrays.equals(along, green.along))
323: return false;
324: if (!Arrays.equals(ashort, green.ashort))
325: return false;
326: if (!myList.equals(green.myList))
327: return false;
328: if (!myString.equals(green.myString))
329: return false;
330:
331: return true;
332: }
333:
334: public int hashCode() {
335: int result = super .hashCode();
336: long temp;
337: temp = mydouble != +0.0d ? Double
338: .doubleToLongBits(mydouble) : 0L;
339: result = 29 * result + (int) (temp ^ (temp >>> 32));
340: result = 29 * result + myfloat != +0.0f ? Float
341: .floatToIntBits(myfloat) : 0;
342: result = 29 * result + (int) (mylong ^ (mylong >>> 32));
343: result = 29 * result + myint;
344: result = 29 * result + (int) myshort;
345: result = 29 * result + (int) mybyte;
346: result = 29 * result + (int) mychar;
347: result = 29 * result + (myboolean ? 1 : 0);
348: result = 29 * result + myString.hashCode();
349: result = 29 * result + myList.hashCode();
350: return result;
351: }
352: }
353:
354: }
|