package com.google.maps.android.geojson;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;

import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class GeoJsonFeatureTest extends TestCase {

    GeoJsonFeature feature;

    public void testGetId() throws Exception {
        feature = new GeoJsonFeature(null, "Pirate", null, null);
        assertNotNull(feature.getId());
        assertTrue(feature.getId().equals("Pirate"));
        feature = new GeoJsonFeature(null, null, null, null);
        assertNull(feature.getId());
    }

    public void testProperty() throws Exception {
        HashMap<String, String> properties = new HashMap<String, String>();
        properties.put("Color", "Yellow");
        properties.put("Width", "5");
        feature = new GeoJsonFeature(null, null, properties, null);
        assertFalse(feature.hasProperty("llama"));
        assertTrue(feature.hasProperty("Color"));
        assertEquals("Yellow", feature.getProperty("Color"));
        assertTrue(feature.hasProperty("Width"));
        assertEquals("5", feature.getProperty("Width"));
        assertNull(feature.removeProperty("banana"));
        assertEquals("5", feature.removeProperty("Width"));
        assertNull(feature.setProperty("Width", "10"));
        assertEquals("10", feature.setProperty("Width", "500"));
    }

    public void testPointStyle() {
        feature = new GeoJsonFeature(null, null, null, null);
        GeoJsonPointStyle pointStyle = new GeoJsonPointStyle();
        feature.setPointStyle(pointStyle);
        assertEquals(pointStyle, feature.getPointStyle());

        try {
            feature.setPointStyle(null);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Point style cannot be null", e.getMessage());
        }
    }

    public void testLineStringStyle() {
        feature = new GeoJsonFeature(null, null, null, null);
        GeoJsonLineStringStyle lineStringStyle = new GeoJsonLineStringStyle();
        feature.setLineStringStyle(lineStringStyle);
        assertEquals(lineStringStyle, feature.getLineStringStyle());

        try {
            feature.setLineStringStyle(null);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Line string style cannot be null", e.getMessage());
        }
    }

    public void testPolygonStyle() {
        feature = new GeoJsonFeature(null, null, null, null);
        GeoJsonPolygonStyle polygonStyle = new GeoJsonPolygonStyle();
        feature.setPolygonStyle(polygonStyle);
        assertEquals(polygonStyle, feature.getPolygonStyle());

        try {
            feature.setPolygonStyle(null);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Polygon style cannot be null", e.getMessage());
        }
    }

    public void testGeometry() {
        feature = new GeoJsonFeature(null, null, null, null);
        assertNull(feature.getGeometry());
        GeoJsonPoint point = new GeoJsonPoint(new LatLng(0, 0));
        feature.setGeometry(point);
        assertEquals(point, feature.getGeometry());
        feature.setGeometry(null);
        assertNull(feature.getGeometry());

        GeoJsonLineString lineString = new GeoJsonLineString(new ArrayList<LatLng>(Arrays.asList(new LatLng(0, 0), new LatLng(50, 50))));
        feature = new GeoJsonFeature(lineString, null, null, null);
        assertEquals(lineString, feature.getGeometry());
        feature.setGeometry(point);
        assertEquals(point, feature.getGeometry());
        feature.setGeometry(null);
        assertNull(feature.getGeometry());
        feature.setGeometry(lineString);
        assertEquals(lineString, feature.getGeometry());
    }

    public void testGetBoundingBox() {
        feature = new GeoJsonFeature(null, null, null, null);
        assertNull(feature.getBoundingBox());

        LatLngBounds boundingBox = new LatLngBounds(new LatLng(-20, -20), new LatLng(50, 50));
        feature = new GeoJsonFeature(null, null, null, boundingBox);
        assertEquals(boundingBox, feature.getBoundingBox());
    }


}
@@ -174,16 +174,45 @@
 tCase;%0A%0A
+import org.json.JSONObject;%0A%0A
 import j
@@ -1456,32 +1456,446 @@
 %22500%22));%0A    %7D%0A%0A
+    public void testNullProperty() throws Exception %7B%0A        GeoJsonLayer layer = new GeoJsonLayer(null, createFeatureCollection());%0A        GeoJsonFeature feature = layer.getFeatures().iterator().next();%0A        assertTrue(feature.hasProperty(%22prop0%22));%0A        assertNull(feature.getProperty(%22prop0%22));%0A        assertFalse(feature.hasProperty(%22prop1%22));%0A        assertNull(feature.getProperty(%22prop1%22));%0A    %7D%0A%0A
     public void 
@@ -4658,10 +4658,710 @@
 %0A    %7D%0A%0A
+    private JSONObject createFeatureCollection() throws Exception %7B%0A        return new JSONObject(%0A                %22%7B %5C%22type%5C%22: %5C%22FeatureCollection%5C%22,%5Cn%22%0A                        + %22%5C%22bbox%5C%22: %5B-150.0, -80.0, 150.0, 80.0%5D,%22%0A                        + %22    %5C%22features%5C%22: %5B%5Cn%22%0A                        + %22      %7B %5C%22type%5C%22: %5C%22Feature%5C%22,%5Cn%22%0A                        + %22        %5C%22id%5C%22: %5C%22point%5C%22, %5Cn%22%0A                        + %22        %5C%22geometry%5C%22: %7B%5C%22type%5C%22: %5C%22Point%5C%22, %5C%22coordinates%5C%22: %5B102.0, 0.5%5D%7D,%5Cn%22%0A                        + %22        %5C%22properties%5C%22: %7B%5C%22prop0%5C%22: null%7D%5Cn%22%0A                        + %22        %7D%5Cn%22%0A                        + %22       %5D%5Cn%22%0A                        + %22     %7D%22%0A        );%0A    %7D%0A
 %0A%7D

Add tests for null GeoJson feature properties.