package com.maxmind.geoip2;

import java.lang.*;
import java.util.*;
import java.io.*;
import org.json.*;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;


public class CountryTest 
    extends TestCase
{

    public CountryTest( String testName )
    {
        super( testName );
    }
    public static Test suite()
    {
        return new TestSuite( CountryTest.class );
    }
    private JSONObject createJSONCountry() {

      StringBuilder sb = new StringBuilder();
      sb.append("{\"continent\":{");
      sb.append("\"continent_code\":\"NA\",");
      sb.append("\"geoname_id\":42,");
      sb.append("\"names\":{\"en\":\"North America\"}");
      sb.append("},");
      sb.append("\"country\":{");
      sb.append("\"geoname_id\":1,");
      sb.append("\"iso_code\":\"US\",");
      sb.append("\"confidence\":56,");
      sb.append("\"names\":{\"en\":\"United States\"}");
      sb.append("},");
      sb.append("\"registered_country\":{");
      sb.append("\"geoname_id\":2,");
      sb.append("\"iso_code\":\"CA\",");
      sb.append("\"names\":{\"en\":\"Canada\"}");
      sb.append("},");
      sb.append("\"traits\":{");
      sb.append("\"ip_address\":\"1.2.3.4\",");
      sb.append("}}");
      String str = sb.toString();
      try {
        return new JSONObject(str);
      } catch (JSONException e) {
        fail(e.getMessage());
        return null;
      }

    }
    
    public void testCountry() {
      JSONObject jcountry = createJSONCountry();
      Country country = new Country(jcountry);
      assertEquals(
        "country.getContinent().getCode() does not return NA",
         country.getContinent().getCode(),
         "NA");
      assertEquals(
        "country.getContinent().getGeoNameId() does not return 42",
         country.getContinent().getGeoNameId(),
         42);
      assertEquals(
"country.getContinent().getName(\"en\") does not return North America",
         country.getContinent().getName("en"),
         "North America");

      assertEquals(
        "country.getCountry().getCode() does not return US",
         country.getCountry().getCode(),
         "US");
      assertEquals(
        "country.getCountry().getGeoNameId() does not return 1",
         country.getCountry().getGeoNameId(),1);
      assertEquals(
        "country.getCountry().getConfidence() does not return 56",
         country.getCountry().getConfidence(),new Integer(56));
      assertEquals(
"country.getCountry().getName(\"en\") does not return United States",
         country.getCountry().getName("en"),
         "United States");

      assertEquals(
        "country.getRegisteredCountry().getCode() does not return CA",
         country.getRegisteredCountry().getCode(),
         "CA");
      assertEquals(
        "country.getRegisteredCountry().getGeoNameId() does not return 2",
         country.getRegisteredCountry().getGeoNameId(),
         2);
      assertEquals(
"country.getRegisteredCountry().getName(\"en\") does not return United States",
         country.getRegisteredCountry().getName("en"),
         "Canada");
      assertEquals(
        "country.getTraits().getIpAddress does not return 1.2.3.4",
         country.getTraits().getIpAddress(),
         "1.2.3.4");

    }

}


@@ -468,17 +468,16 @@
 try() %7B%0A
-%0A
       St
@@ -1644,24 +1644,39 @@
 return NA%22,%0A
+         %22NA%22,%0A
          cou
@@ -1704,31 +1704,23 @@
 etCode()
-,
 %0A      
-   %22NA%22
 );%0A     
@@ -1798,24 +1798,37 @@
 return 42%22,%0A
+         42,%0A
          cou
@@ -1861,29 +1861,23 @@
 NameId()
-,
 %0A      
-   42
 );%0A     
@@ -1963,16 +1963,42 @@
 erica%22,%0A
+         %22North America%22,%0A
         
@@ -2038,38 +2038,18 @@
 en%22)
-,
 %0A      
-   %22North America%22
 );%0A
-%0A
     
@@ -2125,16 +2125,31 @@
 rn US%22,%0A
+         %22US%22,%0A
         
@@ -2179,31 +2179,23 @@
 etCode()
-,
 %0A      
-   %22US%22
 );%0A     
@@ -2274,16 +2274,28 @@
 urn 1%22,%0A
+         1,%0A
         
@@ -2334,10 +2334,15 @@
 Id()
-,1
+%0A      
 );%0A 
@@ -2424,24 +2424,48 @@
 eturn 56%22,%0A 
+       new Integer(56),%0A
         coun
@@ -2500,24 +2500,15 @@
 ce()
-,new Integer(56)
+%0A      
 );%0A 
@@ -2588,32 +2588,58 @@
 United States%22,%0A
+         %22United States%22,%0A
          country
@@ -2665,25 +2665,24 @@
 me(%22en%22)
-,
 %0A       
   %22Unite
@@ -2673,33 +2673,16 @@
 %0A       
-  %22United States%22
 );%0A%0A    
@@ -2764,24 +2764,38 @@
 return CA%22,%0A
+        %22CA%22,%0A
          cou
@@ -2835,23 +2835,15 @@
 de()
-,
 %0A      
-   %22CA%22
 );%0A 
@@ -2932,24 +2932,36 @@
  return 2%22,%0A
+         2,%0A
          cou
@@ -3002,28 +3002,23 @@
 NameId()
-,
 %0A      
-   2
 );%0A     
@@ -3104,32 +3104,51 @@
 United States%22,%0A
+         %22Canada%22,%0A
          country
@@ -3184,35 +3184,23 @@
 me(%22en%22)
-,
 %0A      
-   %22Canada%22
 );%0A     
@@ -3282,16 +3282,36 @@
 2.3.4%22,%0A
+        %221.2.3.4%22, %0A
         
@@ -3349,28 +3349,15 @@
 ss()
-,
 %0A      
-   %221.2.3.4%22
 );%0A%0A

swap parameters around