package com.stanfy.gsonxml.test;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;

import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.stanfy.gsonxml.GsonXmlBuilder;


/**
 * Test for {@link com.stanfy.gsonxml.XmlReader}.
 * @author Roman Mazur (Stanfy - http://stanfy.com)
 *
 */
public class ListsTest extends AbstractXmlTest {

  /** Test XML. */
  public static final String TEST_XML =
      "<places>"
    + "  <place id=\"1\" lat=\"0.25\" long=\"0.26\">"
    + "    <name>&lt;Place&gt;</name>"
    + "  </place>"
    + "  <place id=\"2\" lat=\"0.27\" long=\"0.28\">"
    + "    <name>Place 2</name>"
    + "  </place>"
    + "</places>";

  /** Place object. */
  public static class Place {
    @SerializedName("@id")
    long id;
    @SerializedName("@lat")
    double lat;
    @SerializedName("@long")
    double lon;

    String name;
  }

  /** Container. */
  public static class PlacesContainer {
    List<Place> places;
  }

  @Test
  public void listsTest() {
    final List<Place> places = gsonXml.fromXml(TEST_XML, new TypeToken<List<Place>>() {}.getType());
    assertPlaces(places);
  }

  private void assertPlaces(final List<Place> places) {
    assertEquals(2, places.size());
    assertEquals(1, places.get(0).id);
    assertEquals(2, places.get(1).id);

    assertEquals(0.26, places.get(0).lon, 0.00001);
  }

  @Test
  public void skipTest() {
    final PlacesContainer placesC = new GsonXmlBuilder()
        .setXmlParserCreator(SimpleXmlReaderTest.PARSER_CREATOR)
        .setSkipRoot(false)
        .create()
        .fromXml(TEST_XML, PlacesContainer.class);
    assertPlaces(placesC.places);
  }

}

@@ -89,43 +89,165 @@
 ava.
-util.List;%0A%0Aimport org.junit.Test;%0A
+io.FileNotFoundException;%0Aimport java.util.List;%0A%0Aimport org.junit.Test;%0A%0Aimport com.google.gson.JsonIOException;%0Aimport com.google.gson.JsonSyntaxException;
 %0Aimp
@@ -857,16 +857,558 @@
 ces%3E%22;%0A%0A
+  public static final String TEST_XML_WITH_HEADER =%0A      %22%3Cplaces%3E%22%0A    + %22  %3Cerror%3E0%3C/error%3E%22%0A    + %22  %3Cplace id=%5C%221%5C%22 lat=%5C%220.25%5C%22 long=%5C%220.26%5C%22%3E%22%0A    + %22    %3Cname%3E&lt;Place&gt;%3C/name%3E%22%0A    + %22  %3C/place%3E%22%0A    + %22  %3Cplace id=%5C%222%5C%22 lat=%5C%220.27%5C%22 long=%5C%220.28%5C%22%3E%22%0A    + %22    %3Cname%3EPlace 2%3C/name%3E%22%0A    + %22  %3C/place%3E%22%0A    + %22%3C/places%3E%22;%0A%0A  public static final String TEST_XML_WITH_HEADER_AND_PRIMITIVES_LIST =%0A        %22%3Cresponce%3E%22%0A      + %22  %3Cerror%3E0%3C/error%3E%22%0A      + %22  %3Clist%3Eitem0%3C/list%3E%22%0A      + %22  %3Clist%3Eitem1%3C/list%3E%22%0A      + %22%3C/responce%3E%22;%0A%0A
   /** Pl
@@ -1664,16 +1664,34 @@
 ainer %7B%0A
+    String error;%0A
     List
@@ -1707,24 +1707,109 @@
 laces;%0A  %7D%0A%0A
+  public static class ListWithHeader %7B%0A    String error;%0A    List%3CObject%3E list;%0A  %7D%0A%0A
   @Test%0A  pu
@@ -1964,16 +1964,580 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void mixedListTest() throws JsonSyntaxException, JsonIOException, FileNotFoundException %7B%0A    final ListWithHeader listWithHeader =%0A        new GsonXmlBuilder()%0A        .setXmlParserCreator(SimpleXmlReaderTest.PARSER_CREATOR)%0A        .setSameNameLists(true)%0A        .setSkipRoot(false)%0A        .create()%0A        .fromXml(TEST_XML_WITH_HEADER_AND_PRIMITIVES_LIST, ListWithHeader.class);%0A%0A    assertEquals(%220%22, listWithHeader.error);%0A    assertEquals(%22item0%22, listWithHeader.list.get(0));%0A    assertEquals(%22item1%22, listWithHeader.list.get(1));%0A  %7D%0A%0A
   privat
@@ -2984,16 +2984,28 @@
 TEST_XML
+_WITH_HEADER
 , Places

Test for list with header