/*
 * StringConverter03StrParTest 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.FunctionTester;
import java.util.*;

/**
 * {@link ts.util.text.StringConverter StringConverter}クラスの機能試験クラス。
 *
 * @author  佐藤隆之.
 * @version $Revision: 1.1.1.1 $, $Date: 2010-10-16 00:03:53 $
 */
public class StringConverter03StrParTest extends FunctionTester
{
  public static void main(String[] args)
  {
    try {
      run(StringConverter03StrParTest.class, (args.length == 0) ? null : args[0]);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /* -- test case -- */

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

    List<String> spL = Arrays.asList(",", "|", "\"");

    try {
      StringConverter.fromReadString(null, "\\", "(", ")", spL);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("abc", null, "(", ")", spL);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("abc", "\\", null, ")", spL);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("abc", "\\", "(", null, spL);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("abc", "\\", "(", ")", null);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }

  public void fromReadString_str_open_close_esc_spec_0()
  {
    MSG("エスケープ文字列、括弧が空の場合。");

    Collection<String> spL = Arrays.asList(",", "|", "\"");

    try {
      StringConverter.fromReadString("abc", "", "(", ")", spL);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("abc", "\\", "", ")", spL);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("abc", "\\", "(", "", spL);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    spL = Arrays.asList(",", "");
    try {
      StringConverter.fromReadString("abc", "\\", "(", ")", spL);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    spL = Arrays.asList(",", null);
    try {
      StringConverter.fromReadString("abc", "\\", "(", ")", spL);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }

  public void fromReadString_str_open_close_esc_spec_1()
  {
    MSG("読み込んだ文字列が空文字列の場合。");

    Collection<String> spL = Arrays.asList(",", "|", "\"");

    try {
      StringConverter.fromReadString("", "(", ")", "\\", spL);
    } catch (StartWithoutParenthesisException e) {
      OK(e);
      EQUAL(e.getStoppedIndex(), 0);
      EQUAL(e.convertedString(), "");
    } catch (Exception e) {
      NG(e);
    }
  }

  public void fromReadString_str_open_close_esc_spec_2() throws Exception
  {
    MSG("正常に変換できる場合。");

    Collection<String> spL = Arrays.asList(",", "\"");

    EQUAL(StringConverter.fromReadString("(abc)", "(", ")", "#", spL), "abc");
    EQUAL(StringConverter.fromReadString("()", "(", ")", "#", spL), "");
    EQUAL(StringConverter.fromReadString("(a#(bc)","(",")","#",spL),"a(bc");
    EQUAL(StringConverter.fromReadString("(abc#))","(",")","#",spL),"abc)");
    EQUAL(StringConverter.fromReadString("(ab##c)","(",")","#",spL),"ab#c");
    EQUAL(StringConverter.fromReadString("(#\"abc)","(",")","#",spL),"\"abc");
    EQUAL(StringConverter.fromReadString("(  abc)","(",")","#",spL),"  abc");
    EQUAL(StringConverter.fromReadString("(abc  )","(",")","#",spL),"abc  ");
    EQUAL(StringConverter.fromReadString("(abc # )","(",")","#",spL),"abc  ");
  }

  public void fromReadString_str_open_close_esc_spec_3()
  {
    MSG("特殊文字がエスケープされていない場合。");

    Collection<String> spL = Arrays.asList(",", "\"");

    try {
      StringConverter.fromReadString("(a(bc)", "(", ")", "\\", spL);
      NG();
    } catch (SpecialCharNotEscapedException e) {
      OK(e);
      EQUAL(e.getStoppedIndex(), 2);
      EQUAL(e.convertedString(), "a");
      EQUAL(e.getSpecialChar(), "(");
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("(abc))", "(", ")", "\\", spL);
      NG();
    } catch (SpecialCharNotEscapedException e) {
      OK(e);
      EQUAL(e.getStoppedIndex(), 4);
      EQUAL(e.convertedString(), "abc");
      EQUAL(e.getSpecialChar(), ")");
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("(\"abc)", "(", ")", "\\", spL);
      NG();
    } catch (SpecialCharNotEscapedException e) {
      OK(e);
      EQUAL(e.getStoppedIndex(), 1);
      EQUAL(e.convertedString(), "");
      EQUAL(e.getSpecialChar(), "\"");
    } catch (Exception e) {
      NG(e);
    }
  }

  public void fromReadString_str_open_close_esc_spec_4()
  {
    MSG("エスケープ文字で終了している場合。");

    Collection<String> spL = Arrays.asList(",", "\"");

    try {
      StringConverter.fromReadString("(abc\\", "(", ")", "\\", spL);
      NG();
    } catch (EndWithEscapeCharException e) {
      OK(e);
      EQUAL(e.getStoppedIndex(), 4);
      EQUAL(e.convertedString(), "abc");
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("(\\", "(", ")", "\\", spL);
      NG();
    } catch (EndWithEscapeCharException e) {
      OK(e);
      EQUAL(e.getStoppedIndex(), 1);
      EQUAL(e.convertedString(), "");
    } catch (Exception e) {
      NG(e);
    }
  }

  public void fromReadString_str_open_close_esc_spec_5()
  {
    MSG("両端が括弧でない場合。");

    Collection<String> spL = Arrays.asList(",", "\"");

    try {
      StringConverter.fromReadString("abc)", "(", ")", "\\", spL);
      NG();
    } catch (StartWithoutParenthesisException e) {
      OK(e);
      EQUAL(e.getStoppedIndex(), 0);
      EQUAL(e.convertedString(), "");
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("(abc", "(", ")", "\\", spL);
      NG();
    } catch (EndWithoutParenthesisException e) {
      OK(e);
      EQUAL(e.getStoppedIndex(), 4);
      EQUAL(e.convertedString(), "abc");
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("abc", "(", ")", "\\", spL);
      NG();
    } catch (StartWithoutParenthesisException e) {
      OK(e);
      EQUAL(e.getStoppedIndex(), 0);
      EQUAL(e.convertedString(), "");
    } catch (Exception e) {
      NG(e);
    }
  }

  public void fromReadString_str_open_close_esc_spec_6()
  {
    MSG("括弧内の文字列がエスケープ文字で終了している場合。");

    Collection<String> spL = Arrays.asList(",", "\"");

    try {
      StringConverter.fromReadString("(abc\\)", "(", ")", "\\", spL);
      NG();
    } catch (EndWithEscapeCharException e) {
      OK(e);
      EQUAL(e.getStoppedIndex(), 5);
      EQUAL(e.convertedString(), "abc");
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.fromReadString("(\\)", "(", ")", "\\", spL);
      NG();
    } catch (EndWithEscapeCharException e) {
      OK(e);
      EQUAL(e.getStoppedIndex(), 2);
      EQUAL(e.convertedString(), "");
    } catch (Exception e) {
      NG(e);
    }
  }

  public void fromReadString_str_open_close_esc_spec_7()
  {
    MSG("括弧やエスケープ文字列の文字数が複数の場合。");

    Collection<String> spL = Arrays.asList(",", "\"");
    
    try {
      EQUAL(StringConverter.fromReadString("((abc))", "((", "))", "##", spL),
        "abc");
    } catch (StringConvertException e) {
      NG(e);
    }

    try {
      EQUAL(StringConverter.fromReadString("(())", "((", "))", "##", spL),
        "");
    } catch (StringConvertException e) {
      NG(e);
    }

    try {
      EQUAL(StringConverter.fromReadString("((a##(bc))", "((", "))", "##", spL),
        "a(bc");
    } catch (StringConvertException e) {
      NG(e);
    }
  }

  public void fromReadString_str_open_close_esc_spec_8()
  {
    MSG("開き括弧と閉じ括弧が同じ文字の場合。");

    List<String> spL = Arrays.asList(",");

    try {
      EQUAL(StringConverter.fromReadString("\"abc\"", "\"", "\"", "##", spL),
        "abc");
    } catch (StringConvertException e) {
      NG(e);
    }

    try {
      EQUAL(StringConverter.fromReadString("\"a$\"bc\"", "\"", "\"", "$", spL),
        "a\"bc");
    } catch (StringConvertException e) {
      NG(e);
    }

    try {
      EQUAL(StringConverter.fromReadString(
        "\"\"a##\"\"b\"c\"\"", "\"\"", "\"\"", "##", spL), "a\"\"b\"c");
    } catch (StringConvertException e) {
      NG(e);
    }
  }

  public void fromReadString_str_open_close_esc_spec_9()
  {
    MSG("開き括弧と閉じ括弧とエスケープ文字列が同じ文字の場合。");

    List<String> spL = Arrays.asList(",");

    try {
      EQUAL(StringConverter.fromReadString("\"あい\"\"う\"\"え\",お\"",
        "\"", "\"", "\"", spL), "あい\"う\"え,お");;
    } catch (StringConvertException e) {
      NG(e);
    } 

    try {
      EQUAL(StringConverter.fromReadString("\"\"あい\"\"\"\"う\"え\"\",お\"\"",
        "\"\"", "\"\"", "\"\"", spL), "あい\"\"う\"え,お");
    } catch (StringConvertException e) {
      NG(e);
    }
  }

  public void fromReadString_str_open_close_esc_spec_10()
  {
    MSG("エスケープ文字が連続して続く場合。");

    List<String> spL = Arrays.asList("#");

    try {
      EQUAL(StringConverter.fromReadString("#############",
        "##", "##", "##", spL), "###");
    } catch (StringConvertException e) {
      NG(e);
    }
  }

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

    List<String> spL = Arrays.asList(",");

    try {
      StringConverter.toWriteString(null, "(", ")", "\\", spL);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.toWriteString("abc", null, ")", "\\", spL);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.toWriteString("abc", "(", null, "\\", spL);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.toWriteString("abc", "(", ")", null, spL);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.toWriteString("abc", "(", ")", "\\", null);
      NG();
    } catch (AssertionError e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }

  public void toWriteString_str_open_close_esc_spec_0()
  {
    MSG("通常の場合。");

    List<String> spL = Arrays.asList(",", "\"");

    EQUAL(StringConverter.toWriteString("", "(", ")", "\\", spL),
      "()");
    EQUAL(StringConverter.toWriteString("abc", "(", ")", "\\", spL),
      "(abc)");
    EQUAL(StringConverter.toWriteString("a,bc", "(", ")", "\\", spL),
      "(a\\,bc)");
    EQUAL(StringConverter.toWriteString("ab\"c", "(", ")", "\\", spL),
      "(ab\\\"c)");
    EQUAL(StringConverter.toWriteString("abc\\", "(", ")", "\\", spL),
      "(abc\\\\)");
    EQUAL(StringConverter.toWriteString("(abc", "(", ")", "\\", spL),
      "(\\(abc)");
    EQUAL(StringConverter.toWriteString(")abc", "(", ")", "\\", spL),
      "(\\)abc)");
    EQUAL(StringConverter.toWriteString("  abc", "(", ")", "\\", spL),
      "(  abc)");
    EQUAL(StringConverter.toWriteString("a bc  ", "(", ")", "\\", spL),
      "(a bc  )");
    EQUAL(StringConverter.toWriteString(" abc ", "(", ")", "\\", spL),
      "( abc )");
    EQUAL(StringConverter.toWriteString(" ", "(", ")", "\\", spL),
      "( )");
  }

  public void toWriteString_str_open_close_esc_spec_1() throws Exception
  {
    MSG("fromReadStringで元に戻るか。");

    String op = "(";
    String cp = ")";
    String esc = "\\";
    List<String> spec = Arrays.asList(",", "\"");

    String s = "";
    EQUAL(StringConverter.fromReadString(StringConverter.toWriteString(
      s, op, cp, esc, spec), op, cp, esc, spec), s);

    s = "abc";
    EQUAL(StringConverter.fromReadString(StringConverter.toWriteString(
      s, op, cp, esc, spec), op, cp, esc, spec), s);

    s = "ab\"c";
    EQUAL(StringConverter.fromReadString(StringConverter.toWriteString(
      s, op, cp, esc, spec), op, cp, esc, spec), s);

    s = "abc\\";
    EQUAL(StringConverter.fromReadString(StringConverter.toWriteString(
      s, op, cp, esc, spec), op, cp, esc, spec), s);

    s = "(abc";
    EQUAL(StringConverter.fromReadString(StringConverter.toWriteString(
      s, op, cp, esc, spec), op, cp, esc, spec), s);

    s = ")abc";
    EQUAL(StringConverter.fromReadString(StringConverter.toWriteString(
      s, op, cp, esc, spec), op, cp, esc, spec), s);

    s = "  abc";
    EQUAL(StringConverter.fromReadString(StringConverter.toWriteString(
      s, op, cp, esc, spec), op, cp, esc, spec), s);

    s = "a bc  ";
    EQUAL(StringConverter.fromReadString(StringConverter.toWriteString(
      s, op, cp, esc, spec), op, cp, esc, spec), s);

    s = " abc ";
    EQUAL(StringConverter.fromReadString(StringConverter.toWriteString(
      s, op, cp, esc, spec), op, cp, esc, spec), s);

    s = " ";
    EQUAL(StringConverter.fromReadString(StringConverter.toWriteString(
      s, op, cp, esc, spec), op, cp, esc, spec), s);
  }

  public void toWriteString_str_open_close_esc_spec_2()
  {
    MSG("エスケープ文字、特殊文字に空白が含まれている場合。");

    String op = "(";
    String cp = ")";
    String esc = "\\";
    List<String> spec = Arrays.asList(",", "");

    try {
      StringConverter.toWriteString("abc", op, cp, esc, spec);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    spec = Arrays.asList(",", null);
    try {
      StringConverter.toWriteString("abc", op, cp, esc, spec);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    spec = Arrays.asList(",", "\"");
    try {
      StringConverter.toWriteString("abc", op, cp, "", spec);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.toWriteString("abc", "", cp, esc, spec);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }

    try {
      StringConverter.toWriteString("abc", op, "", esc, spec);
      NG();
    } catch (IllegalArgumentException e) {
      OK(e);
    } catch (Exception e) {
      NG(e);
    }
  }
}

