package ro.isdc.wro.extensions.processor.support.linter.report;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.output.WriterOutputStream;
import org.junit.Test;

import ro.isdc.wro.extensions.processor.support.linter.LinterError;
import ro.isdc.wro.extensions.processor.support.linter.ResourceLinterErrors;
import ro.isdc.wro.model.resource.Resource;
import ro.isdc.wro.model.resource.processor.ResourcePreProcessor;
import ro.isdc.wro.util.WroTestUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;


/**
 * @author Alex Objelean
 */
public class TestXmlReportBuilder {
  @Test(expected = NullPointerException.class)
  public void cannotCreateBuilderWithNullErrors() {
    XmlReportBuilder.create(null);
  }
  
  @Test
  public void testFromFolder()
      throws Exception {
    final URL url = getClass().getResource("xml");
    
    final File testFolder = new File(url.getFile(), "test");
    final File expectedFolder = new File(url.getFile(), "expected");
    WroTestUtils.compareFromDifferentFoldersByExtension(testFolder, expectedFolder, "*", new ResourcePreProcessor() {
      @Override
      public void process(final Resource resource, final Reader reader, final Writer writer)
          throws IOException {
        final Type type = new TypeToken<List<ResourceLinterErrors<LinterError>>>() {}.getType();
        final List<ResourceLinterErrors<LinterError>> errors = new Gson().fromJson(reader, type);
        XmlReportBuilder.create(errors).write(new WriterOutputStream(writer));
      }
    });
  }
  
  public static void main(String[] args) {
    final List<ResourceLinterErrors<LinterError>> errors = new ArrayList<ResourceLinterErrors<LinterError>>();
    Collection<LinterError> file1Errors = new ArrayList<LinterError>();
    LinterError linterError = new LinterError();
    linterError.setCharacter(2);
    linterError.setEvidence("evidence");
    linterError.setLine(20);
    linterError.setReason("reason");
    file1Errors.add(linterError);
    errors.add(ResourceLinterErrors.<LinterError> create("file1", file1Errors));
    final Type type = new TypeToken<List<ResourceLinterErrors<LinterError>>>() {}.getType();
    String json = new GsonBuilder().setPrettyPrinting().create().toJson(errors, type);
    System.out.println(json);
  }
}

@@ -209,95 +209,8 @@
 RL;%0A
-import java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.Collections;%0A
 impo
@@ -224,24 +224,24 @@
 .util.List;%0A
+
 %0Aimport org.
@@ -636,44 +636,8 @@
 on;%0A
-import com.google.gson.GsonBuilder;%0A
 impo
@@ -908,19 +908,32 @@
 ic void 
-tes
+shouldBuildRepor
 tFromFol
@@ -1689,16 +1689,16 @@
       %7D%0A
+
     %7D);%0A
@@ -1705,753 +1705,6 @@
   %7D%0A
-  %0A  public static void main(String%5B%5D args) %7B%0A    final List%3CResourceLinterErrors%3CLinterError%3E%3E errors = new ArrayList%3CResourceLinterErrors%3CLinterError%3E%3E();%0A    Collection%3CLinterError%3E file1Errors = new ArrayList%3CLinterError%3E();%0A    LinterError linterError = new LinterError();%0A    linterError.setCharacter(2);%0A    linterError.setEvidence(%22evidence%22);%0A    linterError.setLine(20);%0A    linterError.setReason(%22reason%22);%0A    file1Errors.add(linterError);%0A    errors.add(ResourceLinterErrors.%3CLinterError%3E create(%22file1%22, file1Errors));%0A    final Type type = new TypeToken%3CList%3CResourceLinterErrors%3CLinterError%3E%3E%3E() %7B%7D.getType();%0A    String json = new GsonBuilder().setPrettyPrinting().create().toJson(errors, type);%0A    System.out.println(json);%0A  %7D%0A
 %7D%0A

rename test method