001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.caching;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021:
022: import org.geotools.caching.util.FeatureMarshaller;
023:
024: import org.geotools.feature.DefaultFeature;
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.IllegalAttributeException;
027:
028: import java.io.ByteArrayInputStream;
029: import java.io.ByteArrayOutputStream;
030: import java.io.IOException;
031: import java.io.ObjectInputStream;
032: import java.io.ObjectOutputStream;
033:
034: import java.util.ArrayList;
035: import java.util.List;
036:
037: public class MarshallerTest extends TestCase {
038: public static Test suite() {
039: return new TestSuite(MarshallerTest.class);
040: }
041:
042: /** Marshall and unmarshall a DefaultFeature, and test for equality with the result.
043: *
044: * @throws IOException
045: * @throws ClassNotFoundException
046: * @throws IllegalAttributeException
047: */
048: public void testMarshall() throws IOException,
049: ClassNotFoundException, IllegalAttributeException {
050: Generator gen = new Generator(1000, 1000);
051: Feature f = gen.createFeature(0);
052: FeatureMarshaller m = new FeatureMarshaller(gen
053: .getFeatureType());
054: ByteArrayOutputStream baos = new ByteArrayOutputStream();
055: ObjectOutputStream oos = new ObjectOutputStream(baos);
056: m.marshall(f, oos);
057:
058: byte[] ba = baos.toByteArray();
059: baos.close();
060:
061: ByteArrayInputStream bais = new ByteArrayInputStream(ba);
062: ObjectInputStream ois = new ObjectInputStream(bais);
063: Feature newf = m.unmarshall(ois);
064: bais.close();
065: assertTrue(f.equals(newf));
066: }
067:
068: /** Same as testMarshall, but uses complex representation of DefaultFeature.
069: * Marshall and unmarshall, and test for equality with the result.
070: *
071: * @task seems to be a bug in DefaultFeature.equals() or JTS.Geometry.equals()
072: * test is disabled.
073: *
074: * @throws IOException
075: * @throws ClassNotFoundException
076: * @throws IllegalAttributeException
077: */
078: public void ztestComplexMarshall() throws IOException,
079: ClassNotFoundException, IllegalAttributeException {
080: Generator gen = new Generator(1000, 1000);
081: Feature f = ((DefaultFeature) gen.createFeature(0)).toComplex();
082: FeatureMarshaller m = new FeatureMarshaller(gen
083: .getFeatureType());
084: ByteArrayOutputStream baos = new ByteArrayOutputStream();
085: ObjectOutputStream oos = new ObjectOutputStream(baos);
086: m.marshall(f, oos);
087:
088: byte[] ba = baos.toByteArray();
089: baos.close();
090:
091: ByteArrayInputStream bais = new ByteArrayInputStream(ba);
092: ObjectInputStream ois = new ObjectInputStream(bais);
093: Feature newf = m.unmarshall(ois);
094: bais.close();
095: //newf = ((DefaultFeature) newf).toComplex() ;
096: assertTrue(f.equals(newf));
097: }
098:
099: /** Disabled test to mesure time to marshall/unmarshall features.
100: * Test results on my PC :
101: * <ul><li>0.8 ms per feature for a marshall/unmarshall cycle
102: * <li>0.2 ms per feature for marshalling only
103: * </ul>
104: */
105: public void ztestMarshallTime() {
106: Generator gen = new Generator(1000, 1000);
107: List features = new ArrayList();
108:
109: for (int i = 0; i < 10000; i++) {
110: Feature f = gen.createFeature(i);
111: features.add(f);
112: }
113:
114: long start = System.currentTimeMillis();
115:
116: for (int i = 0; i < 10000; i++) {
117: Feature f = (Feature) features.get(i);
118: FeatureMarshaller marsh = new FeatureMarshaller(gen
119: .getFeatureType());
120: ByteArrayOutputStream baos = new ByteArrayOutputStream();
121:
122: try {
123: ObjectOutputStream oos = new ObjectOutputStream(baos);
124: marsh.marshall(f, oos);
125:
126: byte[] ba = baos.toByteArray();
127: ByteArrayInputStream bais = new ByteArrayInputStream(ba);
128: ObjectInputStream ois = new ObjectInputStream(bais);
129: Feature newf = marsh.unmarshall(ois);
130:
131: if (!newf.equals(f)) {
132: throw new RuntimeException("Error at unmarshall");
133: }
134:
135: if (i == (1000 * (i / 1000))) {
136: System.out.println(i);
137: }
138: } catch (IOException e) {
139: // TODO Auto-generated catch block
140: e.printStackTrace();
141: } catch (ClassNotFoundException e) {
142: // TODO Auto-generated catch block
143: e.printStackTrace();
144: } catch (IllegalAttributeException e) {
145: // TODO Auto-generated catch block
146: e.printStackTrace();
147: }
148: }
149:
150: long stop = System.currentTimeMillis();
151: System.out.println("Elapsed time for 10000 features : "
152: + (stop - start) + " ms.");
153: }
154: }
|