package org.zanata.adapter.po;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
import org.xml.sax.InputSource;
import org.zanata.common.LocaleId;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TextFlow;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;

@Test(groups = { "unit-tests" })
public class PoReader2Test
{

   private static final Logger log = LoggerFactory.getLogger(PoReader2Test.class);

   LocaleId ja = new LocaleId("ja-JP");

   @Test
   public void extractTemplateThenAdd2Targets() throws IOException, JAXBException
   {
      String testDir = "src/test/resources/";


      InputSource inputSource = new InputSource(new File(testDir, "pot/RPM.pot").toURI().toString());
      inputSource.setEncoding("utf8");

      PoReader2 poReader = new PoReader2();

      System.out.println("parsing template");
      Resource doc = poReader.extractTemplate(inputSource, LocaleId.EN_US, "doc1");
      assertThat(doc.getTextFlows().size(), is(137));
      String locale = "ja-JP";
      inputSource = new InputSource(new File(testDir, locale + "/RPM.po").toURI().toString());
      inputSource.setEncoding("utf8");
      System.out.println("extracting target: " + locale);
      TranslationsResource targetDoc = poReader.extractTarget(inputSource, doc, false);
      List<TextFlowTarget> textFlowTargets = targetDoc.getTextFlowTargets();
      assertThat(textFlowTargets.size(), is(137));
      TextFlowTarget target = textFlowTargets.iterator().next();
      assertThat(target, notNullValue());

      JAXBContext jaxbContext = JAXBContext.newInstance(Resource.class, TranslationsResource.class);
      Marshaller m = jaxbContext.createMarshaller();
      m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

      System.out.println("marshalling source doc");
      {
         StringWriter writer = new StringWriter();
         m.marshal(doc, writer);
         log.debug("{}", writer);
      }

      System.out.println("marshalling target doc");
      {
         StringWriter writer = new StringWriter();
         m.marshal(targetDoc, writer);
         log.debug("{}", writer);
      }

      List<TextFlow> resources = doc.getTextFlows();

      TextFlow tf1 = resources.get(3);
      assertThat(tf1.getContent(), equalTo("Important"));
      TextFlowTarget tfTarget = textFlowTargets.get(3);
      assertThat(tfTarget.getContent(), equalTo("キーのインポート"));

      // TODO test PO headers and attributes
   }
}

@@ -848,17 +848,16 @@
 2Test%0A%7B%0A
-%0A
    priva
@@ -976,152 +976,131 @@
 %22);%0A
-%0A
    
-@Test%0A   public void extractTemplateThenAdd2Targets() throws IOException, JAXBException%0A   %7B%0A      String testDir = %22src/test/resources/%22;%0A%0A
+String testDir = %22src/test/resources/%22;%0A   PoReader2 poReader = new PoReader2();%0A%0A   private Resource getTemplate()%0A   %7B
 %0A   
@@ -1242,53 +1242,8 @@
 );%0A%0A
-      PoReader2 poReader = new PoReader2();%0A%0A
     
@@ -1418,24 +1418,205 @@
 , is(137));%0A
+      return doc;%0A   %7D%0A%0A   private void extractTarget(boolean useSrcOrder) throws IOException, JAXBException%0A   %7B%0A      InputSource inputSource;%0A      Resource doc = getTemplate();%0A
       String
@@ -1910,13 +1910,19 @@
 oc, 
-false
+useSrcOrder
 );%0A 
@@ -3072,10 +3072,1299 @@
 es%0A   %7D%0A
+%0A   @Test%0A   public void extractTemplate()%0A   %7B%0A      getTemplate();%0A   %7D%0A%0A   @Test(expectedExceptions = %7B RuntimeException.class %7D, expectedExceptionsMessageRegExp = %22.*unsupported charset.*%22)%0A   public void extractInvalidTemplate() throws IOException, JAXBException%0A   %7B%0A      InputSource inputSource = new InputSource(new File(testDir, %22pot/invalid.pot%22).toURI().toString());%0A      inputSource.setEncoding(%22utf8%22);%0A%0A      poReader.extractTemplate(inputSource, LocaleId.EN_US, %22doc1%22);%0A   %7D%0A%0A   @Test%0A   public void extractTargetWithSourceOrder() throws IOException, JAXBException%0A   %7B%0A      extractTarget(true);%0A   %7D%0A%0A   @Test%0A   public void extractTargetWithTargetOrder() throws IOException, JAXBException%0A   %7B%0A      extractTarget(false);%0A   %7D%0A%0A   @Test(expectedExceptions = %7B RuntimeException.class %7D, expectedExceptionsMessageRegExp = %22.*unsupported charset.*%22)%0A   public void extractInvalidTarget() throws IOException, JAXBException%0A   %7B%0A      Resource srcDoc = getTemplate();%0A%0A      String locale = %22ja-JP%22;%0A      InputSource inputSource = new InputSource(new File(testDir, locale + %22/invalid.po%22).toURI().toString());%0A      inputSource.setEncoding(%22utf8%22);%0A      System.out.println(%22extracting target: %22 + locale);%0A%0A      poReader.extractTarget(inputSource, srcDoc, false);%0A   %7D%0A%0A
 %7D%0A

Add charset test to PoReader2Test; remove obsolete test