/*
 * StringCutterTest class.
 *
 * Copyright (C) 2007 SATOH Takayuki All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package ts.util.text;

import ts.tester.function.coverage.FunctionTester;
import ts.tester.function.print.*;
import java.util.List;

/**
 * {@link ts.util.text.StringCutter StringCutter}クラスの機能試験クラス。
 *
 * @author  佐藤隆之.
 * @version $Revision: 1.1.1.1 $, $Date: 2010-10-16 00:03:53 $
 */
public class StringCutterTest extends FunctionTester
{
  public static void main(String[] args)
  {
    try {
      PrinterGroup group = new PrinterGroup();
      group.addPrinter(new ConsolePrinter());
      group.addPrinter(new HtmlPrinter("SATOH Takayuki"));
      setPrinter(group);

      run(StringCutterTest.class, (args.length == 0) ? null : args[0]);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  protected void preTesting()
  {
    MSG("StringCutter クラスの機能試験を実行する。");
  }
  
  /* -- test case -- */

  public void constructor()
  {
    MSG("デフォルト・コンストラクタの確認。");

    StringCutter cutter = new StringCutter();
    NOTNULL(cutter);
  }

  public void split_string_chStr()
  {
    MSG("文字列を区切り文字で分割。");

    List<String> lst;

    lst = StringCutter.split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", ","); 
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    lst = StringCutter.split("ABCDEFGHIJKLM,NOPQRSTUVWXYZ", ","); 
    EQUAL(lst.size(), 2);
    EQUAL(lst.get(0), "ABCDEFGHIJKLM");
    EQUAL(lst.get(1), "NOPQRSTUVWXYZ");

    lst = StringCutter.split(",ABCDEFGHIJKLMNOPQRSTUVWXYZ,", ","); 
    EQUAL(lst.size(), 3);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    EQUAL(lst.get(2), "");

    lst = StringCutter.split("AB/CDEFGH//IJKLMNO///PQRSTUVW/XYZ", "/"); 
    EQUAL(lst.size(), 8);
    EQUAL(lst.get(0), "AB");
    EQUAL(lst.get(1), "CDEFGH");
    EQUAL(lst.get(2), "");
    EQUAL(lst.get(3), "IJKLMNO");
    EQUAL(lst.get(4), "");
    EQUAL(lst.get(5), "");
    EQUAL(lst.get(6), "PQRSTUVW");
    EQUAL(lst.get(7), "XYZ");

    lst = StringCutter.split("", "/");
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "");
  }

  public void split_string_chStr_1()
  {
    MSG("引数がヌルの場合。");

    try {
      StringCutter.split(null, ",");
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }

  public void split_string_string()
  {
    MSG("文字列を、指定された区切り文字列で分割。");

    String str;
    List<String> lst;

    str = "あいうえおかきくけこさしすせそ";
    lst = StringCutter.split(str, "<br>");
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "あいうえおかきくけこさしすせそ");

    str = "あいうえお<br>かきくけこ<br>さしすせそ";
    lst = StringCutter.split(str, "<br>");
    EQUAL(lst.size(), 3);
    EQUAL(lst.get(0), "あいうえお");
    EQUAL(lst.get(1), "かきくけこ");
    EQUAL(lst.get(2), "さしすせそ");

    str = "<br>あいうえお<br>かきくけこ<br>さしすせそ<br>";
    lst = StringCutter.split(str, "<br>");
    EQUAL(lst.size(), 5);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "あいうえお");
    EQUAL(lst.get(2), "かきくけこ");
    EQUAL(lst.get(3), "さしすせそ");
    EQUAL(lst.get(4), "");

    str = "b";
    lst = StringCutter.split(str, "<br>");
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "b");

    str = "";
    lst = StringCutter.split(str, "<br>");
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "");
  }

  public void split_string_string_1()
  {
    MSG("引数がヌルの場合。");

    try {
      StringCutter.split(null, ",");
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringCutter.split("", (String) null);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    } 
  }

  public void split_string_string_2()
  {
    MSG("区切り文字が空文字列の場合。");

    try {
      StringCutter.split("あいうえお", "");
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }

  public void splitIgnoreCase_string_string()
  {
    MSG("文字列を、指定した区切り文字列で大文字小文字を考慮せずに分割。");

    String str;
    List<String> lst;

    str = "あいうえおKaKiKuKeKoさしすせそ";
    lst = StringCutter.splitIgnoreCase(str, "<br>");
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "あいうえおKaKiKuKeKoさしすせそ");

    str = "あいうえお<BR>KaKiKuKeKo<Br>さしすせそ";
    lst = StringCutter.splitIgnoreCase(str, "<br>");
    EQUAL(lst.size(), 3);
    EQUAL(lst.get(0), "あいうえお");
    EQUAL(lst.get(1), "KaKiKuKeKo");
    EQUAL(lst.get(2), "さしすせそ");

    str = "<bR>あいうえお<br>KaKiKuKeKo<BR>さしすせそ<BR>";
    lst = StringCutter.splitIgnoreCase(str, "<br>");
    EQUAL(lst.size(), 5);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "あいうえお");
    EQUAL(lst.get(2), "KaKiKuKeKo");
    EQUAL(lst.get(3), "さしすせそ");
    EQUAL(lst.get(4), "");

    str = "b";
    lst = StringCutter.splitIgnoreCase(str, "<br>");
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "b");

    str = "";
    lst = StringCutter.splitIgnoreCase(str, "<br>");
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "");
  }

  public void splitIgnoreCase_string_string_1()
  {
    MSG("引数がヌルの場合。");

    try {
      StringCutter.splitIgnoreCase(null, ",");
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringCutter.splitIgnoreCase("", (String) null);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    } 
  }

  public void splitIgnoreCase_string_string_2()
  {
    MSG("区切り文字が空文字列の場合。");

    try {
      StringCutter.splitIgnoreCase("あいうえお", "");
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }

  public void split_string_chars()
  {
    MSG("文字列を複数の区切り文字で分割。");

    List lst;
    String[] delims = { ",", "/" };

    lst = StringCutter.split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", delims); 
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    lst = StringCutter.split("ABCDEFGHIJKLM,NOPQRSTUVWXYZ", delims);
    EQUAL(lst.size(), 2);
    EQUAL(lst.get(0), "ABCDEFGHIJKLM");
    EQUAL(lst.get(1), "NOPQRSTUVWXYZ");

    lst = StringCutter.split("ABC/DEFGHIJKLMNOP/QRSTUVWXYZ", delims);
    EQUAL(lst.size(), 3);
    EQUAL(lst.get(0), "ABC");
    EQUAL(lst.get(1), "DEFGHIJKLMNOP");
    EQUAL(lst.get(2), "QRSTUVWXYZ");

    lst = StringCutter.split("/ABCDEFGHIJKLMNOPQRSTUVWXYZ,", delims); 
    EQUAL(lst.size(), 3);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    EQUAL(lst.get(2), "");

    lst = StringCutter.split("/AB/C,DEFG,/HIJKL,MNO/,P/QRSTU/VWXYZ,", delims); 
    EQUAL(lst.size(), 12);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "AB");
    EQUAL(lst.get(2), "C");
    EQUAL(lst.get(3), "DEFG");
    EQUAL(lst.get(4), "");
    EQUAL(lst.get(5), "HIJKL");
    EQUAL(lst.get(6), "MNO");
    EQUAL(lst.get(7), "");
    EQUAL(lst.get(8), "P");
    EQUAL(lst.get(9), "QRSTU");
    EQUAL(lst.get(10), "VWXYZ");
    EQUAL(lst.get(11), "");

    lst = StringCutter.split("", delims);
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "");
  }

  public void split_string_chars_1()
  {
    MSG("引数がヌルの場合。");

    String[] delims = { ",", "/" };
    try {
      StringCutter.split(null, delims);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringCutter.split("", (String[]) null);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }

  public void split_string_strings()
  {
    MSG("文字列を、複数の区切り文字列で分割。");

    String str;
    String[] delims = { "<p>", "<br>", "</p>" };
    List<String> lst;

    str = "あいうえおかきくけこさしすせそ";
    lst = StringCutter.split(str, delims);
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "あいうえおかきくけこさしすせそ");

    str = "あいうえお<br>かきくけこ<br>さしすせそ";
    lst = StringCutter.split(str, delims);
    EQUAL(lst.size(), 3);
    EQUAL(lst.get(0), "あいうえお");
    EQUAL(lst.get(1), "かきくけこ");
    EQUAL(lst.get(2), "さしすせそ");

    str = "<p>あいうえお<br>かきくけこ<br>さしすせそ</p>";
    lst = StringCutter.split(str, delims);
    EQUAL(lst.size(), 5);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "あいうえお");
    EQUAL(lst.get(2), "かきくけこ");
    EQUAL(lst.get(3), "さしすせそ");
    EQUAL(lst.get(4), "");

    str = "<p>あいうえお<bq>かきくけこ<br>さしすせそ</p>";
    lst = StringCutter.split(str, delims);
    EQUAL(lst.size(), 4);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "あいうえお<bq>かきくけこ");
    EQUAL(lst.get(2), "さしすせそ");
    EQUAL(lst.get(3), "");

    str = "b";
    lst = StringCutter.split(str, delims);
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "b");

    str = "";
    lst = StringCutter.split(str, delims);
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "");
  }

  public void split_string_strings_1()
  {
    MSG("引数がヌルの場合。");

    String str;
    String delims[] = { "<p>", "<br>", "</p>" };
    List<String> lst;

    try {
      StringCutter.split(null, delims);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringCutter.split("", (String[]) null);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    } 
  }

  public void split_string_strings_2()
  {
    MSG("区切り文字の配列の中にヌルの要素が入っている場合。");

    String str;
    String[] delims = { "<p>", null, "</p>" };

    str = "あいうえおかきくけこさしすせそ";
    try {
      StringCutter.split(str, delims);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }

  public void split_string_strings_3()
  {
    MSG("区切り文字の配列の中に空文字列の要素が入っている場合。");

    String str;
    String[] delims = { "<p>", "", "</p>" };

    str = "あいうえおかきくけこさしすせそ";
    try {
      StringCutter.split(str, delims);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }

  public void splitIgnoreCase_string_strings()
  {
    MSG("文字列を、複数の区切り文字列（大文字小文字は無視）で分割。");

    String str;
    String[] delims = { "<p>", "<br>", "</p>" };
    List<String> lst;

    str = "あいうえおKaKiKuKeKoさしすせそ";
    lst = StringCutter.splitIgnoreCase(str, delims);
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "あいうえおKaKiKuKeKoさしすせそ");

    str = "あいうえお<Br>KaKiKuKeKo<bR>さしすせそ";
    lst = StringCutter.splitIgnoreCase(str, delims);
    EQUAL(lst.size(), 3);
    EQUAL(lst.get(0), "あいうえお");
    EQUAL(lst.get(1), "KaKiKuKeKo");
    EQUAL(lst.get(2), "さしすせそ");

    str = "<P>あいうえお<bq>KaKiKuKeKo<BR>さしすせそ</p>";
    lst = StringCutter.splitIgnoreCase(str, delims);
    EQUAL(lst.size(), 4);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "あいうえお<bq>KaKiKuKeKo");
    EQUAL(lst.get(2), "さしすせそ");
    EQUAL(lst.get(3), "");

    str = "<P>あいうえお<br>KaKiKuKeKo<BR>さしすせそ</p>";
    lst = StringCutter.splitIgnoreCase(str, delims);
    EQUAL(lst.size(), 5);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "あいうえお");
    EQUAL(lst.get(2), "KaKiKuKeKo");
    EQUAL(lst.get(3), "さしすせそ");
    EQUAL(lst.get(4), "");

    str = "b";
    lst = StringCutter.splitIgnoreCase(str, delims);
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "b");

    str = "";
    lst = StringCutter.splitIgnoreCase(str, delims);
    EQUAL(lst.size(), 1);
    EQUAL(lst.get(0), "");
  }

  public void splitIgnoreCase_string_strings_1()
  {
    MSG("引数がヌルの場合。");

    String str;
    String delims[] = { "<p>", "<br>", "</p>" };
    List<String> lst;

    try {
      StringCutter.splitIgnoreCase(null, delims);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringCutter.splitIgnoreCase("", (String[]) null);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    } 
  }

  public void splitIgnoreCase_string_strings_2()
  {
    MSG("区切り文字の配列の中にヌルの要素が入っている場合。");

    String str;
    String[] delims = { "<p>", null, "</p>" };

    str = "あいうえおかきくけこさしすせそ";
    try {
      StringCutter.splitIgnoreCase(str, delims);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }

  public void splitIgnoreCase_string_strings_3()
  {
    MSG("区切り文字の配列の中に空文字列の要素が入っている場合。");

    String str;
    String[] delims = { "<p>", "", "</p>" };

    str = "あいうえおかきくけこさしすせそ";
    try {
      StringCutter.splitIgnoreCase(str, delims);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }

  public void split_string_string_9()
  {
    MSG("区切り文字が連続して続く場合。");

    List lst;

    lst = StringCutter.split("/////", "/"); 
    EQUAL(lst.size(), 6);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "");
    EQUAL(lst.get(2), "");
    EQUAL(lst.get(3), "");
    EQUAL(lst.get(4), "");
    EQUAL(lst.get(5), "");

    lst = StringCutter.split("/////", "//"); 
    EQUAL(lst.size(), 3);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "");
    EQUAL(lst.get(2), "/");
  }

  public void splitIgnoreCase_string_string_9()
  {
    MSG("区切り文字が連続して続く場合。");

    List lst;

    lst = StringCutter.splitIgnoreCase("/////", "/"); 
    EQUAL(lst.size(), 6);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "");
    EQUAL(lst.get(2), "");
    EQUAL(lst.get(3), "");
    EQUAL(lst.get(4), "");
    EQUAL(lst.get(5), "");

    lst = StringCutter.splitIgnoreCase("/////", "//"); 
    EQUAL(lst.size(), 3);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "");
    EQUAL(lst.get(2), "/");
  }

  public void split_string_strings_9()
  {
    MSG("区切り文字が連続して続く場合。");

    List lst;

    String[] delims = { "\\", ".", "/" };
    lst = StringCutter.split("/////", delims); 
    EQUAL(lst.size(), 6);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "");
    EQUAL(lst.get(2), "");
    EQUAL(lst.get(3), "");
    EQUAL(lst.get(4), "");
    EQUAL(lst.get(5), "");

    String[] delims2 = { "\\", ".", "//" };
    lst = StringCutter.split("/////", delims2); 
    EQUAL(lst.size(), 3);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "");
    EQUAL(lst.get(2), "/");
  }

  public void splitIgnoreCase_string_strings_9()
  {
    MSG("区切り文字が連続して続く場合。");

    List lst;

    String[] delims = { "\\", ".", "/" };
    lst = StringCutter.splitIgnoreCase("/////", delims); 
    EQUAL(lst.size(), 6);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "");
    EQUAL(lst.get(2), "");
    EQUAL(lst.get(3), "");
    EQUAL(lst.get(4), "");
    EQUAL(lst.get(5), "");

    String[] delims2 = { "\\", ".", "//" };
    lst = StringCutter.splitIgnoreCase("/////", delims2); 
    EQUAL(lst.size(), 3);
    EQUAL(lst.get(0), "");
    EQUAL(lst.get(1), "");
    EQUAL(lst.get(2), "/");
  }
}

