package cc.redpen.config;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.util.LinkedHashSet;

import static cc.redpen.config.SymbolType.*;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

public class ConfigurationExporterTest {
  ConfigurationExporter exporter = new ConfigurationExporter();
  ByteArrayOutputStream out = new ByteArrayOutputStream();

  @Test
  public void emptyConfig() throws Exception {
    Configuration config = new Configuration.ConfigurationBuilder().build();
    exporter.export(config, out);
    assertEquals(
      "<redpen-conf lang=\"en\"></redpen-conf>", new String(out.toByteArray()));
  }

  @Test
  public void emptyConfigForJapaneseLanguage() throws Exception {
    Configuration config = new Configuration.ConfigurationBuilder().setLanguage("ja").build();
    exporter.export(config, out);
    assertEquals(
      "<redpen-conf lang=\"ja\" variant=\"zenkaku\"></redpen-conf>", new String(out.toByteArray()));
  }

  @Test
  public void validators() throws Exception {
    Configuration config = new Configuration.ConfigurationBuilder()
      .addValidatorConfig(new ValidatorConfiguration("Mega"))
      .addValidatorConfig(new ValidatorConfiguration("Super").addAttribute("hello", "world")).build();
    exporter.export(config, out);
    assertEquals(
      "<redpen-conf lang=\"en\">\n" +
      "  <validators>\n" +
      "    <validator name=\"Mega\"/>\n" +
      "    <validator name=\"Super\">\n" +
      "      <property name=\"hello\" value=\"world\"/>\n" +
      "    </validator>\n" +
      "  </validators>\n" +
      "</redpen-conf>", new String(out.toByteArray()));
  }

  @Test
  public void symbols() throws Exception {
    Configuration config = mock(Configuration.class, RETURNS_DEEP_STUBS);

    when(config.getLang()).thenReturn("en");
    when(config.getVariant()).thenReturn("");
    when(config.getValidatorConfigs()).thenReturn(emptyList());

    when(config.getSymbolTable().getSymbol(ASTERISK)).thenReturn(new Symbol(ASTERISK, 'X'));
    when(config.getSymbolTable().getSymbol(COLON)).thenReturn(new Symbol(COLON, ';', ":", false, true));
    when(config.getSymbolTable().getSymbol(SEMICOLON)).thenReturn(new Symbol(SEMICOLON, ':', ";&", true, false));
    when(config.getSymbolTable().getNames()).thenReturn(new LinkedHashSet<>(asList(ASTERISK, COLON, SEMICOLON)));

    exporter.export(config, out);

    assertEquals(
      "<redpen-conf lang=\"en\">\n" +
      "  <symbols>\n" +
      "    <symbol name=\"ASTERISK\" value=\"X\"/>\n" +
      "    <symbol name=\"COLON\" value=\";\" invalid-chars=\":\" after-space=\"true\"/>\n" +
      "    <symbol name=\"SEMICOLON\" value=\":\" invalid-chars=\";&amp;\" before-space=\"true\"/>\n" +
      "  </symbols>\n" +
      "</redpen-conf>", new String(out.toByteArray()));
  }
}
@@ -20,16 +20,40 @@
 onfig;%0A%0A
+import org.junit.After;%0A
 import o
@@ -67,16 +67,16 @@
 t.Test;%0A
-
 %0Aimport 
@@ -315,16 +315,62 @@
 Equals;%0A
+import static org.junit.Assert.assertNotNull;%0A
 import s
@@ -564,16 +564,165 @@
 eam();%0A%0A
+  @After%0A  public void assertConfigIsLoadable() throws Exception %7B%0A    assertNotNull(new ConfigurationLoader().loadFromString(out.toString()));%0A  %7D%0A%0A
   @Test%0A
@@ -888,39 +888,32 @@
    assertEquals(
-%0A      
 %22%3Credpen-conf la
@@ -943,36 +943,21 @@
 %3E%22, 
-new String(out.toByteArray()
+out.toString(
 ));%0A
@@ -1173,39 +1173,32 @@
    assertEquals(
-%0A      
 %22%3Credpen-conf la
@@ -1248,36 +1248,21 @@
 %3E%22, 
-new String(out.toByteArray()
+out.toString(
 ));%0A
@@ -1902,36 +1902,21 @@
 %3E%22, 
-new String(out.toByteArray()
+out.toString(
 ));%0A
@@ -3051,36 +3051,743 @@
 %3E%22, 
-new String(out.toByteArray()
+out.toString());%0A  %7D%0A%0A  @Test%0A  public void generatedConfigIsLoadable() throws Exception %7B%0A    String config = %22%3Credpen-conf lang=%5C%22en%5C%22%3E%5Cn%22 +%0A      %22  %3Cvalidators%3E%5Cn%22 +%0A      %22    %3Cvalidator name=%5C%22SentenceLength%5C%22%3E%5Cn%22 +%0A      %22      %3Cproperty name=%5C%22max_len%5C%22 value=%5C%22100%5C%22/%3E%5Cn%22 +%0A      %22    %3C/validator%3E%5Cn%22 +%0A      %22    %3Cvalidator name=%5C%22InvalidSymbol%5C%22/%3E%5Cn%22 +%0A      %22  %3C/validators%3E%5Cn%22 +%0A      %22  %3Csymbols%3E%5Cn%22 +%0A      %22    %3Csymbol name=%5C%22EXCLAMATION_MARK%5C%22 value=%5C%22%EF%BC%81%5C%22 invalid-chars=%5C%22!%5C%22 after-space=%5C%22true%5C%22/%3E%5Cn%22 +%0A      %22  %3C/symbols%3E%5Cn%22 +%0A      %22%3C/redpen-conf%3E%22;%0A    Configuration configuration = new ConfigurationLoader().loadFromString(config);%0A%0A    exporter.export(configuration, out);%0A%0A    assertEquals(config, out.toString(
 ));%0A

test that generated configurations are loadable using ConfigurationLoader