GeoJsonWriter { @Nullable public String write(@Nullable final Feature feature) { if (feature == null) { return null; } final StringWriter writer = new StringWriter(); try { write(writer, feature); } catch (IOException ioe) { Log.w(TAG, ioe.getMessage()); return null; } return writer.toString(); }  @Nullable String write(@Nullable final Feature feature); void write(@NonNull final Writer out,
                      @NonNull final Feature feature); @Nullable String write(@Nullable final FeatureCollection featureCollection); void write(@NonNull final Writer out,
                      @NonNull final FeatureCollection featureCollection); void writeFeature(@NonNull final JsonWriter writer,
                             @NonNull final Feature feature);  }
@Test public void testWriteFeatureToJsonString() throws Exception { final Feature feature = mock(Feature.class); doReturn("id1").when(feature) .getId(); doReturn("Feature").when(feature) .getType(); doReturn(createPoint(gf, 47.2256258d, -1.5545135d)).when(feature) .getGeometry(); doReturn(new Bundle()).when(feature) .getProperties(); final String json = geoJsonWriter.write(feature); assertNotNull(json); assertEquals(TestHelper.getFixture("feature_point.json"), json); }
@Test public void testWriteInvalidFeatureToJsonString() throws Exception { final Feature feature = mock(Feature.class); doReturn("id1").when(feature) .getId(); doReturn(mock(Geometry.class)).when(feature) .getGeometry(); final String json = geoJsonWriter.write(feature); assertNull(json); }
@Test public void testWriteFeatureAsPoint() throws Exception { final Feature feature = mock(Feature.class); doReturn("id1").when(feature) .getId(); doReturn("Feature").when(feature) .getType(); doReturn(createPoint(gf, 47.2256258d, -1.5545135d)).when(feature) .getGeometry(); doReturn(new Bundle()).when(feature) .getProperties(); final StringWriter writer = new StringWriter(); geoJsonWriter.write(writer, feature); assertNotNull(writer.toString()); assertEquals(TestHelper.getFixture("feature_point.json"), writer.toString()); }
@Test public void testWriteFeatureAsMultiPoint() throws Exception { final Feature feature = mock(Feature.class); doReturn("id1").when(feature) .getId(); doReturn("Feature").when(feature) .getType(); doReturn(createMultiPoint(gf, createPoint(gf, 47.2256258d, -1.5545135d), createPoint(gf, 47.225136d, -1.553913d))).when(feature) .getGeometry(); doReturn(new Bundle()).when(feature) .getProperties(); final StringWriter writer = new StringWriter(); geoJsonWriter.write(writer, feature); assertNotNull(writer.toString()); assertEquals(TestHelper.getFixture("feature_multipoint.json"), writer.toString()); }
@Test public void testWriteFeatureAsLineString() throws Exception { final Feature feature = mock(Feature.class); doReturn("id1").when(feature) .getId(); doReturn("Feature").when(feature) .getType(); doReturn(createLineString(gf, createCoordinate(gf, 47.2256258d, -1.5545135d), createCoordinate(gf, 47.225136d, -1.553913d))).when(feature) .getGeometry(); doReturn(new Bundle()).when(feature) .getProperties(); final StringWriter writer = new StringWriter(); geoJsonWriter.write(writer, feature); assertNotNull(writer.toString()); assertEquals(TestHelper.getFixture("feature_linestring.json"), writer.toString()); }
@Test public void testWriteFeatureAsMultiLineString() throws Exception { final Feature feature = mock(Feature.class); doReturn("id1").when(feature) .getId(); doReturn("Feature").when(feature) .getType(); doReturn(createMultiLineString(gf, createLineString(gf, createCoordinate(gf, 47.2256258d, -1.5545135d), createCoordinate(gf, 47.225136d, -1.553913d)))).when(feature) .getGeometry(); doReturn(new Bundle()).when(feature) .getProperties(); final StringWriter writer = new StringWriter(); geoJsonWriter.write(writer, feature); assertNotNull(writer.toString()); assertEquals(TestHelper.getFixture("feature_multilinestring.json"), writer.toString()); }
@Test public void testWriteFeatureAsSimplePolygon() throws Exception { final Feature feature = mock(Feature.class); doReturn("id1").when(feature) .getId(); doReturn("Feature").when(feature) .getType(); doReturn(createPolygon(gf, createCoordinate(gf, 47.226219d, -1.554430d), createCoordinate(gf, 47.226237d, -1.554261d), createCoordinate(gf, 47.226122d, -1.554245d), createCoordinate(gf, 47.226106d, -1.554411d), createCoordinate(gf, 47.226219d, -1.554430d))).when(feature) .getGeometry(); doReturn(new Bundle()).when(feature) .getProperties(); final StringWriter writer = new StringWriter(); geoJsonWriter.write(writer, feature); assertNotNull(writer.toString()); assertEquals(TestHelper.getFixture("feature_polygon_simple.json"), writer.toString()); }
@Test public void testWriteFeatureAsPolygonWithHoles() throws Exception { final Feature feature = mock(Feature.class); doReturn("id1").when(feature) .getId(); doReturn("Feature").when(feature) .getType(); doReturn(createPolygon(gf, createLinearRing(gf, createCoordinate(gf, 47.226257d, -1.554564d), createCoordinate(gf, 47.226295d, -1.554202d), createCoordinate(gf, 47.226075d, -1.554169d), createCoordinate(gf, 47.226049d, -1.554496d), createCoordinate(gf, 47.226257d, -1.554564d)), createLinearRing(gf, createCoordinate(gf, 47.226219d, -1.554430d), createCoordinate(gf, 47.226237d, -1.554261d), createCoordinate(gf, 47.226122d, -1.554245d), createCoordinate(gf, 47.226106d, -1.554411d), createCoordinate(gf, 47.226219d, -1.554430d)))).when(feature) .getGeometry(); doReturn(new Bundle()).when(feature) .getProperties(); final StringWriter writer = new StringWriter(); geoJsonWriter.write(writer, feature); assertNotNull(writer.toString()); assertEquals(TestHelper.getFixture("feature_polygon_holes.json"), writer.toString()); }
@Test public void testWriteFeatureAsMultiPolygon() throws Exception { final Feature feature = mock(Feature.class); doReturn("id1").when(feature) .getId(); doReturn("Feature").when(feature) .getType(); doReturn(createMultiPolygon(gf, createPolygon(gf, createCoordinate(gf, 47.226116d, -1.554169d), createCoordinate(gf, 47.226126d, -1.554097d), createCoordinate(gf, 47.225527d, -1.553986d), createCoordinate(gf, 47.225519d, -1.554061d), createCoordinate(gf, 47.226116d, -1.554169d)), createPolygon(gf, createLinearRing(gf, createCoordinate(gf, 47.226257d, -1.554564d), createCoordinate(gf, 47.226295d, -1.554202d), createCoordinate(gf, 47.226075d, -1.554169d), createCoordinate(gf, 47.226049d, -1.554496d), createCoordinate(gf, 47.226257d, -1.554564d)), createLinearRing(gf, createCoordinate(gf, 47.226219d, -1.554430d), createCoordinate(gf, 47.226237d, -1.554261d), createCoordinate(gf, 47.226122d, -1.554245d), createCoordinate(gf, 47.226106d, -1.554411d), createCoordinate(gf, 47.226219d, -1.554430d))))).when(feature) .getGeometry(); doReturn(new Bundle()).when(feature) .getProperties(); final StringWriter writer = new StringWriter(); geoJsonWriter.write(writer, feature); assertNotNull(writer.toString()); assertEquals(TestHelper.getFixture("feature_multipolygon.json"), writer.toString()); }
@Test public void testWriteFeatureAsGeometryCollection() throws Exception { final Feature feature = mock(Feature.class); doReturn("id1").when(feature) .getId(); doReturn("Feature").when(feature) .getType(); doReturn(createGeometryCollection(gf, createPoint(gf, 47.2256258d, -1.5545135d), createMultiPoint(gf, createPoint(gf, 47.2256258d, -1.5545135d), createPoint(gf, 47.225136d, -1.553913d)), createLineString(gf, createCoordinate(gf, 47.2256258d, -1.5545135d), createCoordinate(gf, 47.225136d, -1.553913d)), createMultiLineString(gf, createLineString(gf, createCoordinate(gf, 47.2256258d, -1.5545135d), createCoordinate(gf, 47.225136d, -1.553913d))), createPolygon(gf, createCoordinate(gf, 47.226219d, -1.554430d), createCoordinate(gf, 47.226237d, -1.554261d), createCoordinate(gf, 47.226122d, -1.554245d), createCoordinate(gf, 47.226106d, -1.554411d), createCoordinate(gf, 47.226219d, -1.554430d)))).when(feature) .getGeometry(); doReturn(new Bundle()).when(feature) .getProperties(); final StringWriter writer = new StringWriter(); geoJsonWriter.write(writer, feature); assertNotNull(writer.toString()); assertEquals(TestHelper.getFixture("feature_geometrycollection.json"), writer.toString()); }
@Test public void testWriteFeatureCollectionToJsonString() throws Exception { final Feature feature1 = mock(Feature.class); doReturn("id1").when(feature1) .getId(); doReturn("Feature").when(feature1) .getType(); doReturn(createPoint(gf, 47.2256258d, -1.5545135d)).when(feature1) .getGeometry(); doReturn(new Bundle()).when(feature1) .getProperties(); final Feature feature2 = mock(Feature.class); doReturn("id2").when(feature2) .getId(); doReturn("Feature").when(feature2) .getType(); doReturn(createMultiPoint(gf, createPoint(gf, 47.2256258d, -1.5545135d), createPoint(gf, 47.225136d, -1.553913d))).when(feature2) .getGeometry(); doReturn(new Bundle()).when(feature2) .getProperties(); final Feature feature3 = mock(Feature.class); doReturn("id3").when(feature3) .getId(); doReturn("Feature").when(feature3) .getType(); doReturn(createLineString(gf, createCoordinate(gf, 47.2256258d, -1.5545135d), createCoordinate(gf, 47.225136d, -1.553913d))).when(feature3) .getGeometry(); doReturn(new Bundle()).when(feature3) .getProperties(); final Feature feature4 = mock(Feature.class); doReturn("id4").when(feature4) .getId(); doReturn("Feature").when(feature4) .getType(); doReturn(createMultiLineString(gf, createLineString(gf, createCoordinate(gf, 47.2256258d, -1.5545135d), createCoordinate(gf, 47.225136d, -1.553913d)))).when(feature4) .getGeometry(); doReturn(new Bundle()).when(feature4) .getProperties(); final Feature feature5 = mock(Feature.class); doReturn("id5").when(feature5) .getId(); doReturn("Feature").when(feature5) .getType(); doReturn(createPolygon(gf, createCoordinate(gf, 47.226219d, -1.554430d), createCoordinate(gf, 47.226237d, -1.554261d), createCoordinate(gf, 47.226122d, -1.554245d), createCoordinate(gf, 47.226106d, -1.554411d), createCoordinate(gf, 47.226219d, -1.554430d))).when(feature5) .getGeometry(); doReturn(new Bundle()).when(feature5) .getProperties(); final FeatureCollection featureCollection = mock(FeatureCollection.class); doReturn("FeatureCollection").when(featureCollection) .getType(); doReturn(asList(feature1, feature2, feature3, feature4, feature5)).when(featureCollection) .getFeatures(); final String json = geoJsonWriter.write(featureCollection); assertNotNull(json); assertEquals(TestHelper.getFixture("featurecollection.json"), json); }
@Test public void testWriteEmptyFeatureCollection() throws Exception { final FeatureCollection featureCollection = mock(FeatureCollection.class); doReturn("FeatureCollection").when(featureCollection) .getType(); final StringWriter writer = new StringWriter(); geoJsonWriter.write(writer, featureCollection); assertNotNull(writer.toString()); assertEquals(TestHelper.getFixture("featurecollection_empty.json"), writer.toString()); }