|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 grootstebozewolf |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * and Eclipse Distribution License v. 1.0 which accompanies this distribution. |
| 7 | + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html |
| 8 | + * and the Eclipse Distribution License is available at |
| 9 | + * |
| 10 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 11 | + */ |
| 12 | +package org.locationtech.jts.io; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.StreamTokenizer; |
| 16 | +import java.io.Writer; |
| 17 | +import java.util.EnumSet; |
| 18 | + |
| 19 | +import org.locationtech.jts.geom.Geometry; |
| 20 | + |
| 21 | +import junit.framework.Test; |
| 22 | +import junit.framework.TestSuite; |
| 23 | +import junit.textui.TestRunner; |
| 24 | +import test.jts.GeometryTestCase; |
| 25 | + |
| 26 | +/** |
| 27 | + * Verifies the {@link WKTReader#readOtherGeometryText} and |
| 28 | + * {@link WKTWriter#appendOtherGeometryTaggedText} extension hooks added |
| 29 | + * for SFA / ISO 19125-2 extended geometry support, without taking any |
| 30 | + * dependency on jts-curved. A dummy subclass of {@link WKTReader} |
| 31 | + * recognises a made-up keyword and a dummy subclass of |
| 32 | + * {@link WKTWriter} emits it; this confirms the seam is wired and the |
| 33 | + * promoted helpers are accessible across packages. |
| 34 | + */ |
| 35 | +public class WKTReaderExtensionHookTest extends GeometryTestCase { |
| 36 | + |
| 37 | + public static void main(String args[]) { |
| 38 | + TestRunner.run(suite()); |
| 39 | + } |
| 40 | + |
| 41 | + public static Test suite() { return new TestSuite(WKTReaderExtensionHookTest.class); } |
| 42 | + |
| 43 | + public WKTReaderExtensionHookTest(String name) { super(name); } |
| 44 | + |
| 45 | + /** A reader that recognises a made-up {@code DUMMYTYPE} keyword and |
| 46 | + * returns an empty Point. Exercises the protected helpers. */ |
| 47 | + private static class DummyReader extends WKTReader { |
| 48 | + boolean hookCalled = false; |
| 49 | + |
| 50 | + @Override |
| 51 | + protected Geometry readOtherGeometryText(StreamTokenizer t, String type, EnumSet<Ordinate> ord) |
| 52 | + throws IOException, ParseException { |
| 53 | + if ("DUMMYTYPE".equals(type)) { |
| 54 | + hookCalled = true; |
| 55 | + // Use the promoted-protected helpers from core. |
| 56 | + String tok = getNextEmptyOrOpener(t); |
| 57 | + if (!WKTConstants.EMPTY.equals(tok)) { |
| 58 | + // burn through the body to a balanced ')' |
| 59 | + int depth = 1; |
| 60 | + while (depth > 0) { |
| 61 | + int c = t.nextToken(); |
| 62 | + if (c == '(') depth++; |
| 63 | + else if (c == ')') depth--; |
| 64 | + else if (c == StreamTokenizer.TT_EOF) |
| 65 | + throw parseErrorWithLine(t, "Unexpected EOF in DUMMYTYPE body"); |
| 66 | + } |
| 67 | + } |
| 68 | + return geometryFactory.createPoint(); |
| 69 | + } |
| 70 | + return super.readOtherGeometryText(t, type, ord); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** A writer that intercepts a dummy custom geometry type. */ |
| 75 | + private static class DummyWriter extends WKTWriter { |
| 76 | + boolean hookCalled = false; |
| 77 | + |
| 78 | + @Override |
| 79 | + protected boolean appendOtherGeometryTaggedText(Geometry geometry, EnumSet<Ordinate> outputOrdinates, |
| 80 | + boolean useFormatting, int level, Writer writer, OrdinateFormat formatter) throws IOException { |
| 81 | + // Pretend we have a custom type: any Geometry whose toString starts with "POINT" |
| 82 | + // (i.e. all Points) should be emitted with our marker. This confirms the hook |
| 83 | + // runs before the instanceof ladder. |
| 84 | + if ("Point".equals(geometry.getGeometryType()) && !geometry.isEmpty()) { |
| 85 | + writer.write("DUMMYTYPE EMPTY"); |
| 86 | + return true; |
| 87 | + } |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public void testReaderHookIsInvokedForUnknownType() throws Exception { |
| 93 | + DummyReader reader = new DummyReader(); |
| 94 | + Geometry g = reader.read("DUMMYTYPE EMPTY"); |
| 95 | + assertTrue("readOtherGeometryText should have been called", reader.hookCalled); |
| 96 | + assertEquals("Point", g.getGeometryType()); |
| 97 | + } |
| 98 | + |
| 99 | + public void testReaderHookHandlesParenthesisedBody() throws Exception { |
| 100 | + DummyReader reader = new DummyReader(); |
| 101 | + Geometry g = reader.read("DUMMYTYPE (1 2, 3 4)"); |
| 102 | + assertTrue(reader.hookCalled); |
| 103 | + assertNotNull(g); |
| 104 | + } |
| 105 | + |
| 106 | + public void testReaderHookFallsThroughToCoreError() { |
| 107 | + try { |
| 108 | + new DummyReader().read("UNKNOWNTYPE EMPTY"); |
| 109 | + fail("Expected ParseException for unknown type"); |
| 110 | + } catch (Throwable e) { |
| 111 | + assertTrue("Expected ParseException, got: " + e, e instanceof ParseException); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public void testCoreReaderStillThrowsForUnknownType() { |
| 116 | + try { |
| 117 | + new WKTReader().read("DUMMYTYPE EMPTY"); |
| 118 | + fail("Expected ParseException from default WKTReader"); |
| 119 | + } catch (Throwable e) { |
| 120 | + assertTrue("Expected ParseException, got: " + e, e instanceof ParseException); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public void testWriterHookFiresBeforeInstanceofLadder() throws Exception { |
| 125 | + DummyWriter writer = new DummyWriter(); |
| 126 | + Geometry pt = read("POINT (1 2)"); |
| 127 | + String wkt = writer.write(pt); |
| 128 | + assertEquals("Hook should have intercepted before the Point branch", |
| 129 | + "DUMMYTYPE EMPTY", wkt); |
| 130 | + } |
| 131 | + |
| 132 | + public void testWriterDefaultHookReturnsFalse() throws Exception { |
| 133 | + // The default WKTWriter must keep writing "POINT (...)" — confirms the hook |
| 134 | + // returning false does not skip the standard path. |
| 135 | + Geometry pt = read("POINT (1 2)"); |
| 136 | + String wkt = new WKTWriter().write(pt); |
| 137 | + assertTrue("Default writer should emit POINT, got: " + wkt, wkt.toUpperCase().startsWith("POINT")); |
| 138 | + } |
| 139 | +} |
0 commit comments