package org.multibit.hd.ui.utils;

import org.junit.After;
import org.junit.Test;
import org.multibit.hd.core.config.Configuration;
import org.multibit.hd.core.config.Configurations;

import java.util.Locale;

import static org.fest.assertions.Assertions.assertThat;

public class HtmlUtilsTest {

  private Locale originalLocale = Locale.getDefault();

  @After
  public void tearDown() throws Exception {

    Locale.setDefault(originalLocale);

  }

  @Test
  public void testLocaliseWithLineBreaks_LTR() throws Exception {

    Locale.setDefault(Locale.UK);

    // New Configuration relies on default locale
    Configurations.currentConfiguration = new Configuration();

    String[] lines = new String[] {

      "Line 1",
      "Line 2",
      "Line 3",

    };

    String expected = "<html><body style='width: 100%'><div align=left><p>Line 1</p><br><p>Line 2</p><br><p>Line 3</p><br></div></body></html>";
    assertThat(HtmlUtils.localiseWithLineBreaks(lines)).isEqualTo(expected);

  }

  @Test
  public void testLocaliseWithLineBreaks_RTL() throws Exception {

    Locale.setDefault(new Locale("ar","SA"));

    // New Configuration relies on default locale
    Configurations.currentConfiguration = new Configuration();

    String[] lines = new String[] {

      "Line 1",
      "Line 2",
      "Line 3",

    };

    String expected = "<html><body style='width: 100%'><div align=right><p>Line 1</p><br><p>Line 2</p><br><p>Line 3</p><br></div></body></html>";
    assertThat(HtmlUtils.localiseWithLineBreaks(lines)).isEqualTo(expected);

  }

  @Test
  public void testApplyBoldFragments_NoFragment() throws Exception {

    // New Configuration relies on default locale
    Configurations.currentConfiguration = new Configuration();

    String expected = "<html>hello world</html>";
    assertThat(HtmlUtils.applyBoldFragments("","hello world")).isEqualTo(expected);

  }

  @Test
  public void testApplyBoldFragments_VariableCase() throws Exception {

    // New Configuration relies on default locale
    Configurations.currentConfiguration = new Configuration();

    String expected = "<html><b>abc</b> <b>ABC</b> <b>Abc</b> <b>AbC</b> <b>abC</b> hello<b>abc</b>world</html>";
    assertThat(HtmlUtils.applyBoldFragments("abc","abc ABC Abc AbC abC helloabcworld")).isEqualTo(expected);

  }

  @Test
  public void testApplyBoldFragments_NoMatch() throws Exception {

    // New Configuration relies on default locale
    Configurations.currentConfiguration = new Configuration();

    String expected = "<html>hello world</html>";
    assertThat(HtmlUtils.applyBoldFragments("abc","hello world")).isEqualTo(expected);

  }

  @Test
  public void testApplyBoldFragments_MatchAtEnd() throws Exception {

    // New Configuration relies on default locale
    Configurations.currentConfiguration = new Configuration();

    String expected = "<html>hello world<b>abc</b></html>";
    assertThat(HtmlUtils.applyBoldFragments("abc","hello worldabc")).isEqualTo(expected);

  }

  @Test
  public void testApplyBoldFragments_PartialMatches() throws Exception {

    // New Configuration relies on default locale
    Configurations.currentConfiguration = new Configuration();

    String expected = "<html>ab ab <b>abc</b><b>abc</b><b>abc</b><b>abc</b></html>";
    assertThat(HtmlUtils.applyBoldFragments("abc","ab ab abcabcabcabc")).isEqualTo(expected);

  }

}

@@ -438,32 +438,589 @@
 lLocale);%0A%0A  %7D%0A%0A
+  @Test%0A  public void testLocaliseWithCenteredLineBreaks() throws Exception %7B%0A%0A    Locale.setDefault(Locale.UK);%0A%0A    // New Configuration relies on default locale%0A    Configurations.currentConfiguration = new Configuration();%0A%0A    String%5B%5D lines = new String%5B%5D %7B%0A%0A      %22Line 1%22,%0A      %22Line 2%22,%0A      %22Line 3%22,%0A%0A    %7D;%0A%0A    String expected = %22%3Chtml%3E%3Cbody style='width: 100%25'%3E%3Cdiv align=center%3E%3Cp%3ELine 1%3C/p%3E%3Cbr%3E%3Cp%3ELine 2%3C/p%3E%3Cbr%3E%3Cp%3ELine 3%3C/p%3E%3C/div%3E%3C/body%3E%3C/html%3E%22;%0A    assertThat(HtmlUtils.localiseWithCenteredLinedBreaks(lines)).isEqualTo(expected);%0A%0A  %7D%0A%0A
   @Test%0A  public
@@ -1069,32 +1069,32 @@
 ows Exception %7B%0A
-
 %0A    Locale.setD
@@ -1432,36 +1432,32 @@
 r%3E%3Cp%3ELine 3%3C/p%3E%3C
-br%3E%3C
 /div%3E%3C/body%3E%3C/ht
@@ -1867,32 +1867,32 @@
 ne 3%22,%0A%0A    %7D;%0A%0A
+
     String expec
@@ -1995,20 +1995,16 @@
 e 3%3C/p%3E%3C
-br%3E%3C
 /div%3E%3C/b

Fix the HtmlUtils test