001: package org.geotools.gml.producer;
002:
003: import javax.sound.midi.Receiver;
004:
005: import org.xml.sax.Attributes;
006: import org.xml.sax.ContentHandler;
007: import org.xml.sax.Locator;
008: import org.xml.sax.SAXException;
009:
010: import com.vividsolutions.jts.geom.Coordinate;
011:
012: import junit.framework.TestCase;
013:
014: /**
015: * We need to ensure that the CoordianteWriter can output Z ordinates
016: * (if they are actually around).
017: * <p>
018: * This test case added as part of fixing
019: *
020: * @author Jody
021: *
022: */
023: public class CoordinateWriterTest extends TestCase {
024: /**
025: * Test normal 2D output
026: * @throws Exception
027: */
028: public void test2D() throws Exception {
029: Coordinate[] coords = coords2D(new int[] { 1, 1, 4, 4, 0, 4, 1,
030: 1 });
031: assertNotNull(coords);
032: assertEquals(4, coords.length);
033:
034: CoordinateWriter writer = new CoordinateWriter(4);
035: CoordinateHandler output = new CoordinateHandler();
036:
037: output.startDocument();
038: writer.writeCoordinates(coords, output);
039: output.endDocument();
040:
041: assertEquals("<coordinates>1,1 4,4 0,4 1,1</coordinates>",
042: output.received);
043: System.out.println(output.received);
044: }
045:
046: public void test2DWithDummyZ() throws Exception {
047: Coordinate[] coords = coords2D(new int[] { 1, 1, 4, 4, 0, 4, 1,
048: 1 });
049: assertNotNull(coords);
050: assertEquals(4, coords.length);
051:
052: CoordinateWriter writer = new CoordinateWriter(4, " ", ",",
053: true, 0.0);
054: CoordinateHandler output = new CoordinateHandler();
055:
056: output.startDocument();
057: writer.writeCoordinates(coords, output);
058: output.endDocument();
059:
060: assertEquals(
061: "<coordinates>1,1,0 4,4,0 0,4,0 1,1,0</coordinates>",
062: output.received);
063: System.out.println(output.received);
064: }
065:
066: public void test3D() throws Exception {
067: Coordinate[] coords = coords3D(new int[] { 1, 1, 3, 4, 4, 2, 0,
068: 4, 2, 1, 1, 3 });
069: assertNotNull(coords);
070: assertEquals(4, coords.length);
071:
072: CoordinateWriter writer = new CoordinateWriter(4, " ", ",",
073: true, 0.0, 3);
074: CoordinateHandler output = new CoordinateHandler();
075:
076: output.startDocument();
077: writer.writeCoordinates(coords, output);
078: output.endDocument();
079:
080: assertEquals(
081: "<coordinates>1,1,3 4,4,2 0,4,2 1,1,3</coordinates>",
082: output.received);
083: System.out.println(output.received);
084: }
085:
086: class CoordinateHandler implements ContentHandler {
087: StringBuffer buffer;
088: String received;
089:
090: public void characters(char[] ch, int start, int length)
091: throws SAXException {
092: buffer.append(new String(ch, start, length));
093: }
094:
095: public void endElement(String uri, String localName, String name)
096: throws SAXException {
097: buffer.append("</");
098: buffer.append(localName);
099: buffer.append(">");
100: }
101:
102: public void endDocument() throws SAXException {
103: received = buffer.toString();
104: }
105:
106: public void endPrefixMapping(String prefix) throws SAXException {
107: }
108:
109: public void ignorableWhitespace(char[] ch, int start, int length)
110: throws SAXException {
111: }
112:
113: public void processingInstruction(String target, String data)
114: throws SAXException {
115: }
116:
117: public void setDocumentLocator(Locator locator) {
118: }
119:
120: public void skippedEntity(String name) throws SAXException {
121: }
122:
123: public void startDocument() throws SAXException {
124: buffer = new StringBuffer();
125: }
126:
127: public void startElement(String uri, String localName,
128: String name, Attributes atts) throws SAXException {
129: buffer.append("<");
130: buffer.append(localName);
131: buffer.append(">");
132: }
133:
134: public void startPrefixMapping(String prefix, String uri)
135: throws SAXException {
136: }
137: };
138:
139: Coordinate[] coords2D(int array[]) {
140: Coordinate coords[] = new Coordinate[array.length / 2];
141: for (int i = 0; i < coords.length; i++) {
142: int offset = i * 2;
143: coords[i] = new Coordinate(array[offset + 0],
144: array[offset + 1]);
145: }
146: return coords;
147: }
148:
149: Coordinate[] coords3D(int array[]) {
150: Coordinate coords[] = new Coordinate[array.length / 3];
151: for (int i = 0; i < coords.length; i++) {
152: int offset = i * 3;
153: coords[i] = new Coordinate(array[offset + 0],
154: array[offset + 1], array[offset + 2]);
155: }
156: return coords;
157: }
158: }
|