001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) Copyright IBM Corporation, 2005. All rights reserved.
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: */
017: package org.geotools.data.db2;
018:
019: import org.geotools.data.DefaultTransaction;
020: import org.geotools.data.FeatureWriter;
021: import org.geotools.data.Transaction;
022: import org.geotools.feature.Feature;
023:
024: import com.vividsolutions.jts.geom.Geometry;
025: import com.vividsolutions.jts.geom.LineString;
026: import com.vividsolutions.jts.geom.Polygon;
027: import com.vividsolutions.jts.io.WKTReader;
028:
029: import java.io.IOException;
030:
031: /**
032: * Exercise DB2FeatureWriter.
033: *
034: * @author David Adler - IBM Corporation
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/db2/src/test/java/org/geotools/data/db2/DB2FeatureWriterTest.java $
036: */
037: public class DB2FeatureWriterTest extends DB2TestCase {
038: private DB2DataStore dataStore = null;
039:
040: /**
041: * Get a DB2DataStore that we will use for all the tests.
042: *
043: * @throws Exception
044: */
045: public void setUp() throws Exception {
046: super .setUp();
047: dataStore = getDataStore();
048: }
049:
050: public void testRemove() throws IOException {
051: try {
052: DB2FeatureStore fs = (DB2FeatureStore) dataStore
053: .getFeatureSource("Roads");
054: Transaction trans = null;
055: trans = new DefaultTransaction("trans1");
056:
057: // fs.setTransaction(trans);
058: FeatureWriter fw = this .dataStore.getFeatureWriter("Roads",
059: trans);
060:
061: if (fw.hasNext()) {
062: Feature f = fw.next();
063: System.out.println(f);
064: fw.remove();
065: }
066:
067: trans.commit();
068: trans.close();
069: } catch (Exception e) {
070: System.out.println(e);
071: }
072: }
073:
074: public void testUpdateRoads() throws IOException {
075: try {
076: DB2FeatureStore fs = (DB2FeatureStore) dataStore
077: .getFeatureSource("Roads");
078: Transaction trans = null;
079: trans = new DefaultTransaction("trans1");
080:
081: // fs.setTransaction(trans);
082: FeatureWriter fw = this .dataStore.getFeatureWriter("Roads",
083: trans);
084:
085: if (fw.hasNext()) {
086: Feature f = fw.next();
087: System.out.println(f);
088: Object a0 = f.getAttribute(0);
089: String name = (String) f.getAttribute(1);
090: f.setAttribute(1, name.trim() + "1");
091: Object a2 = f.getAttribute(2);
092: f.setAttribute(2, Double.valueOf("1.5"));
093:
094: Geometry a3 = (Geometry) f.getAttribute(3);
095: WKTReader wktReader = new WKTReader();
096: LineString line3 = (LineString) wktReader
097: .read("LINESTRING (599000.0 1162200.0, 599226.0 1162227.0)");
098: f.setAttribute(3, line3);
099: System.out.println(f);
100: fw.write();
101: }
102:
103: trans.commit();
104: trans.close();
105: } catch (Exception e) {
106: System.out.println(e);
107: }
108: }
109:
110: public void testUpdatePlaces() throws IOException {
111: try {
112: DB2FeatureStore fs = (DB2FeatureStore) dataStore
113: .getFeatureSource("Places");
114: Transaction trans = null;
115: trans = new DefaultTransaction("trans1");
116: FeatureWriter fw = this .dataStore.getFeatureWriter(
117: "Places", trans);
118:
119: if (fw.hasNext()) {
120: Feature f = fw.next();
121: System.out.println(f);
122:
123: String name = (String) f.getAttribute(0);
124: f.setAttribute(0, name.trim() + "1");
125:
126: Geometry a1 = (Geometry) f.getAttribute(1);
127: WKTReader wktReader = new WKTReader();
128: Polygon polygon = (Polygon) wktReader
129: .read("POLYGON ((-74.099595 42.019401, -74.100484 42.01992, -74.101161 42.020315, -74.099595 42.019401))");
130: f.setAttribute(1, polygon);
131: System.out.println(f);
132: fw.write();
133: }
134:
135: trans.commit();
136: trans.close();
137: } catch (Exception e) {
138: System.out.println(e);
139: }
140: }
141:
142: public void testAppend() throws IOException {
143: try {
144:
145: FeatureWriter fw = this .dataStore.getFeatureWriterAppend(
146: "Roads", Transaction.AUTO_COMMIT);
147: boolean hasNext = fw.hasNext();
148: Feature f = fw.next();
149: f.setAttribute(0, "100");
150: f.setAttribute(1, "name" + "1");
151: Object a2 = f.getAttribute(2);
152: f.setAttribute(2, Double.valueOf("1.5"));
153:
154: Geometry a3 = (Geometry) f.getAttribute(3);
155: WKTReader wktReader = new WKTReader();
156: LineString line3 = (LineString) wktReader
157: .read("LINESTRING (599000.0 1162200.0, 599226.0 1162227.0)");
158: f.setAttribute(3, line3);
159: f.toString();
160: System.out.println(f);
161: fw.write();
162: } catch (Exception e) {
163: System.out.println(e);
164: }
165: }
166:
167: public void testAppendPlaces() throws IOException {
168: try {
169:
170: FeatureWriter fw = this .dataStore.getFeatureWriterAppend(
171: "Places", Transaction.AUTO_COMMIT);
172: boolean hasNext = fw.hasNext();
173: Feature f = fw.next();
174: System.out.println(f);
175: f.setAttribute(0, "name" + "1");
176: Geometry a1 = (Geometry) f.getAttribute(1);
177: WKTReader wktReader = new WKTReader();
178: Polygon polygon = (Polygon) wktReader
179: .read("POLYGON ((-74.099595 42.019401, -74.100484 42.01992, -74.101161 42.020315, -74.099595 42.019401))");
180: f.setAttribute(1, polygon);
181: f.toString();
182: System.out.println(f);
183: fw.write();
184: } catch (Exception e) {
185: System.out.println(e);
186: }
187: }
188: }
|