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.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.util.ArrayList;
025: import java.util.Arrays;
026: import java.util.List;
027:
028: /**
029: * @version $Rev$ $Date$
030: */
031: public class PojoSerializationTest 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 static final String skipStatic = "Don't serialize this";
081: private transient final String skipTransient = "Don't serialize this";
082:
083: private final double mydouble;
084: private final double[] adouble = new double[] { 10, 10 };
085: private float myfloat = 10;
086: private final float[] afloat = new float[] { 10, 10 };
087: private long mylong = 10;
088: private final long[] along = new long[] { 10, 10 };
089: private int myint = 10;
090: private final int[] aint = new int[] { 10, 10 };
091: private short myshort = 10;
092: private final short[] ashort = new short[] { 10, 10 };
093: private byte mybyte = 10;
094: private final byte[] abyte = new byte[] { 10, 10 };
095: private char mychar = 10;
096: private final char[] achar = new char[] { 10, 10 };
097: private boolean myboolean = false;
098: private final boolean[] aboolean = new boolean[] { false, false };
099: private String myString = "";
100: private final String[] aString = new String[] { "one", "two" };
101: private final List myList = new ArrayList();
102: private final List[] aList = new List[] { new ArrayList(),
103: new ArrayList() };
104:
105: public Color() {
106: mydouble = 10;
107: }
108:
109: public Color(int i) {
110: mydouble = 20;
111: }
112:
113: protected void init() {
114: adouble[0] = 20;
115: adouble[1] = 22;
116: myfloat = 20;
117: afloat[0] = 20;
118: afloat[1] = 22;
119: mylong = 20;
120: along[0] = 20;
121: along[1] = 22;
122: myint = 20;
123: aint[0] = 20;
124: aint[1] = 22;
125: myshort = 20;
126: ashort[0] = 20;
127: ashort[1] = 22;
128: mybyte = 20;
129: abyte[0] = 20;
130: abyte[1] = 22;
131: mychar = 20;
132: achar[0] = 20;
133: achar[1] = 22;
134: myboolean = true;
135: aboolean[0] = true;
136: aboolean[1] = false;
137: myString = "hello";
138: aString[0] = "three";
139: aString[1] = "four";
140: aList[0].add("Color.list[0].entryOne");
141: aList[0].add("Color.list[0].entryTwo");
142: aList[1].add("Color.list[1].entryOne");
143: aList[1].add("Color.list[1].entryTwo");
144: }
145:
146: public boolean equals(Object o) {
147: if (this == o)
148: return true;
149: if (o == null || getClass() != o.getClass())
150: return false;
151:
152: final Color color = (Color) o;
153:
154: if (myboolean != color.myboolean)
155: return false;
156: if (mybyte != color.mybyte)
157: return false;
158: if (mychar != color.mychar)
159: return false;
160: if (Double.compare(color.mydouble, mydouble) != 0)
161: return false;
162: if (Float.compare(color.myfloat, myfloat) != 0)
163: return false;
164: if (myint != color.myint)
165: return false;
166: if (mylong != color.mylong)
167: return false;
168: if (myshort != color.myshort)
169: return false;
170: if (!Arrays.equals(aList, color.aList))
171: return false;
172: if (!Arrays.equals(aString, color.aString))
173: return false;
174: if (!Arrays.equals(aboolean, color.aboolean))
175: return false;
176: if (!Arrays.equals(abyte, color.abyte))
177: return false;
178: if (!Arrays.equals(achar, color.achar))
179: return false;
180: if (!Arrays.equals(adouble, color.adouble))
181: return false;
182: if (!Arrays.equals(afloat, color.afloat))
183: return false;
184: if (!Arrays.equals(aint, color.aint))
185: return false;
186: if (!Arrays.equals(along, color.along))
187: return false;
188: if (!Arrays.equals(ashort, color.ashort))
189: return false;
190: if (!myList.equals(color.myList))
191: return false;
192: if (!myString.equals(color.myString))
193: return false;
194:
195: return true;
196: }
197:
198: public int hashCode() {
199: int result;
200: long temp;
201: temp = mydouble != +0.0d ? Double
202: .doubleToLongBits(mydouble) : 0L;
203: result = (int) (temp ^ (temp >>> 32));
204: result = 29 * result + myfloat != +0.0f ? Float
205: .floatToIntBits(myfloat) : 0;
206: result = 29 * result + (int) (mylong ^ (mylong >>> 32));
207: result = 29 * result + myint;
208: result = 29 * result + (int) myshort;
209: result = 29 * result + (int) mybyte;
210: result = 29 * result + (int) mychar;
211: result = 29 * result + (myboolean ? 1 : 0);
212: result = 29 * result + myString.hashCode();
213: result = 29 * result + myList.hashCode();
214: return result;
215: }
216: }
217:
218: public static class Green extends Color {
219: private double mydouble = 10;
220: private final double[] adouble = new double[] { 10, 10 };
221: private float myfloat = 10;
222: private final float[] afloat = new float[] { 10, 10 };
223: private long mylong = 10;
224: private final long[] along = new long[] { 10, 10 };
225: private int myint = 10;
226: private final int[] aint = new int[] { 10, 10 };
227: private short myshort = 10;
228: private final short[] ashort = new short[] { 10, 10 };
229: private byte mybyte = 10;
230: private final byte[] abyte = new byte[] { 10, 10 };
231: private char mychar = 10;
232: private final char[] achar = new char[] { 10, 10 };
233: private boolean myboolean = false;
234: private final boolean[] aboolean = new boolean[] { false, false };
235: private String myString = "";
236: private final String[] aString = new String[] { "one", "two" };
237: private final List myList = new ArrayList();
238: private final List[] aList = new List[] { new ArrayList(),
239: new ArrayList() };
240:
241: public Green() {
242: }
243:
244: public Green(int i) {
245: super (i);
246: }
247:
248: protected void init() {
249: super .init();
250: mydouble = 30;
251: adouble[0] = 30;
252: adouble[1] = 33;
253: myfloat = 30;
254: afloat[0] = 30;
255: afloat[1] = 33;
256: mylong = 30;
257: along[0] = 30;
258: along[1] = 33;
259: myint = 30;
260: aint[0] = 30;
261: aint[1] = 33;
262: myshort = 30;
263: ashort[0] = 30;
264: ashort[1] = 33;
265: mybyte = 30;
266: abyte[0] = 30;
267: abyte[1] = 33;
268: mychar = 30;
269: achar[0] = 30;
270: achar[1] = 33;
271: myboolean = true;
272: aboolean[0] = true;
273: aboolean[1] = false;
274: myString = "hello";
275: aString[0] = "three";
276: aString[1] = "four";
277: aList[0].add("Green.list[0].entryOne");
278: aList[0].add("Green.list[0].entryTwo");
279: aList[1].add("Green.list[1].entryOne");
280: aList[1].add("Green.list[1].entryTwo");
281: }
282:
283: public boolean equals(Object o) {
284: if (this == o)
285: return true;
286: if (o == null || getClass() != o.getClass())
287: return false;
288: if (!super .equals(o))
289: return false;
290:
291: final Green green = (Green) o;
292:
293: if (myboolean != green.myboolean)
294: return false;
295: if (mybyte != green.mybyte)
296: return false;
297: if (mychar != green.mychar)
298: return false;
299: if (Double.compare(green.mydouble, mydouble) != 0)
300: return false;
301: if (Float.compare(green.myfloat, myfloat) != 0)
302: return false;
303: if (myint != green.myint)
304: return false;
305: if (mylong != green.mylong)
306: return false;
307: if (myshort != green.myshort)
308: return false;
309: if (!Arrays.equals(aList, green.aList))
310: return false;
311: if (!Arrays.equals(aString, green.aString))
312: return false;
313: if (!Arrays.equals(aboolean, green.aboolean))
314: return false;
315: if (!Arrays.equals(abyte, green.abyte))
316: return false;
317: if (!Arrays.equals(achar, green.achar))
318: return false;
319: if (!Arrays.equals(adouble, green.adouble))
320: return false;
321: if (!Arrays.equals(afloat, green.afloat))
322: return false;
323: if (!Arrays.equals(aint, green.aint))
324: return false;
325: if (!Arrays.equals(along, green.along))
326: return false;
327: if (!Arrays.equals(ashort, green.ashort))
328: return false;
329: if (!myList.equals(green.myList))
330: return false;
331: if (!myString.equals(green.myString))
332: return false;
333:
334: return true;
335: }
336:
337: public int hashCode() {
338: int result = super .hashCode();
339: long temp;
340: temp = mydouble != +0.0d ? Double
341: .doubleToLongBits(mydouble) : 0L;
342: result = 29 * result + (int) (temp ^ (temp >>> 32));
343: result = 29 * result + myfloat != +0.0f ? Float
344: .floatToIntBits(myfloat) : 0;
345: result = 29 * result + (int) (mylong ^ (mylong >>> 32));
346: result = 29 * result + myint;
347: result = 29 * result + (int) myshort;
348: result = 29 * result + (int) mybyte;
349: result = 29 * result + (int) mychar;
350: result = 29 * result + (myboolean ? 1 : 0);
351: result = 29 * result + myString.hashCode();
352: result = 29 * result + myList.hashCode();
353: return result;
354: }
355: }
356:
357: }
|