package fgis.server.support.geojson;

import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.Arrays;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.stream.JsonGenerator;
import org.geolatte.geom.Geometry;
import org.geolatte.geom.codec.Wkt;
import org.geolatte.geom.codec.Wkt.Dialect;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

public class GeoJsonWriterTest
{
  @Test
  public void emitPointGeometry()
  {
    final Geometry geometry = fromWkT( "POINT (1 1)" );
    final GjElement e = new GjGeometry( geometry, null, null, null );

    final JsonStructure result = writeAndRead( e );

    final String expectedJson = "{\"type\":\"Point\",\"coordinates\":[1.0,1.0]}";
    final JsonObject expected = (JsonObject) parse( expectedJson );
    assertEquals( result, expected );
  }

  private JsonStructure writeAndRead( final GjElement e )
  {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final JsonGenerator g = Json.createGenerator( baos );

    new GeoJsonWriter().emit( g, e );

    g.flush();

    return parse( baos.toByteArray() );
  }

  private JsonStructure parse( final byte[] bytes )
  {
    return parse( new String( bytes, Charset.forName( "US-ASCII" ) ) );
  }

  private JsonStructure parse( final String json )
  {
    return new JsonReader( new StringReader( json ) ).read();
  }


  @SuppressWarnings( "unchecked" )
  public final <T extends Geometry> T fromWkT( final String wkt )
  {
    return (T) Wkt.newDecoder( Dialect.POSTGIS_EWKT_1 ).decode( wkt );
  }
}

@@ -944,32 +944,604 @@
 xpected );%0A  %7D%0A%0A
+  @Test%0A  public void emitGeometryCollection()%0A  %7B%0A    final Geometry geometry = fromWkT( %22POINT (1 1)%22 );%0A    final GjGeometry g = new GjGeometry( geometry, null, null, null );%0A    final GjElement e = new GjGeometryCollection( Arrays.asList( g ), null, null, null );%0A%0A    final JsonStructure result = writeAndRead( e );%0A%0A    final String expectedJson =%0A      %22%7B%5C%22type%5C%22:%5C%22GeometryCollection%5C%22,%5C%22geometries%5C%22:%5B%7B%5C%22type%5C%22:%5C%22Point%5C%22,%5C%22coordinates%5C%22:%5B1.0,1.0%5D%7D%5D%7D%22;%0A    final JsonObject expected = (JsonObject) parse( expectedJson );%0A    assertEquals( result, expected );%0A  %7D%0A%0A
   private JsonSt

Add a test GeometryCollection