/*
 * OutOfRangeExceptionTest 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;

import ts.tester.function.coverage.FunctionTester;
import ts.tester.function.print.*;
import ts.tester.util.ObjectSerializer;
import java.math.BigDecimal;

/**
 * <code>OutOfRangeException</code>クラスの機能試験クラス。
 *
 * @author  佐藤隆之. 
 * @version $Revision: 1.1.1.1 $, $Date: 2010-10-16 00:03:44 $
 */
public class OutOfRangeExceptionTest 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(OutOfRangeExceptionTest.class, (args.length == 0) ? null : args[0]);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  protected void preTesting()
  {
    MSG("OutOfRangeExceptionクラスの機能試験を行う。");
  }

  /* -- text case -- */

  public void constructor_int_int_int()
  {
    MSG("int型の引数をとるコンストラクタの確認。");

    OutOfRangeException exc = new OutOfRangeException(1, 2, 3);
    EQUAL(exc.getValue(), new Integer(1));
    EQUAL(exc.getStart(), new Integer(2));
    EQUAL(exc.getEnd(), new Integer(3));
    EQUAL(exc.getMessage(), "1 is out of range (2,3).");

    exc = new OutOfRangeException(1, 3, 2);
    EQUAL(exc.getValue(), new Integer(1));
    EQUAL(exc.getStart(), new Integer(2));
    EQUAL(exc.getEnd(), new Integer(3));
    EQUAL(exc.getMessage(), "1 is out of range (2,3).");
  }

  public void constructor_long_long_long()
  {
    MSG("long型の引数をとるコンストラクタの確認。");

    OutOfRangeException exc = new OutOfRangeException(1L, 2L, 3L);
    EQUAL(exc.getValue(), new Long(1));
    EQUAL(exc.getStart(), new Long(2));
    EQUAL(exc.getEnd(), new Long(3));
    EQUAL(exc.getMessage(), "1 is out of range (2,3).");

    exc = new OutOfRangeException(1L, 3L, 2L);
    EQUAL(exc.getValue(), new Long(1));
    EQUAL(exc.getStart(), new Long(2));
    EQUAL(exc.getEnd(), new Long(3));
    EQUAL(exc.getMessage(), "1 is out of range (2,3).");
  }

  public void constructor_float_float_float()
  {
    MSG("float型の引数をとるコンストラクタの確認。");

    OutOfRangeException exc = new OutOfRangeException(0.1f, 0.2f, 0.3f);
    EQUAL(exc.getValue(), new Float(0.1f));
    EQUAL(exc.getStart(), new Float(0.2f));
    EQUAL(exc.getEnd(), new Float(0.3f));
    EQUAL(exc.getMessage(), "0.1 is out of range (0.2,0.3).");

    exc = new OutOfRangeException(0.1f, 0.3f, 0.2f);
    EQUAL(exc.getValue(), new Float(0.1f));
    EQUAL(exc.getStart(), new Float(0.2f));
    EQUAL(exc.getEnd(), new Float(0.3f));
    EQUAL(exc.getMessage(), "0.1 is out of range (0.2,0.3).");
  }

  public void constructor_double_double_double()
  {
    MSG("double型の引数をとるコンストラクタの確認。");

    OutOfRangeException exc = new OutOfRangeException(0.1, 0.2, 0.3);
    EQUAL(exc.getValue(), new Double(0.1));
    EQUAL(exc.getStart(), new Double(0.2));
    EQUAL(exc.getEnd(), new Double(0.3));
    EQUAL(exc.getMessage(), "0.1 is out of range (0.2,0.3).");

    exc = new OutOfRangeException(0.1, 0.3, 0.2);
    EQUAL(exc.getValue(), new Double(0.1));
    EQUAL(exc.getStart(), new Double(0.2));
    EQUAL(exc.getEnd(), new Double(0.3));
    EQUAL(exc.getMessage(), "0.1 is out of range (0.2,0.3).");
  }

  public void constructor_BigDecimal_BigDecimal_BigDecimal()
  {
    MSG("引数にBigDecimalオブジェクトを指定したコンストラクタの確認。");

    MSG("- 通常の場合");
    OutOfRangeException exc = new OutOfRangeException(
      new BigDecimal("-0.1"),
      new BigDecimal("-0.2"),
      new BigDecimal("-0.3")
    );
    EQUAL(exc.getValue(), new BigDecimal("-0.1"));
    EQUAL(exc.getStart(), new BigDecimal("-0.3"));
    EQUAL(exc.getEnd()  , new BigDecimal("-0.2"));
    EQUAL(exc.getMessage(), "-0.1 is out of range (-0.3,-0.2).");

    exc = new OutOfRangeException(
      new BigDecimal("-0.1"),
      new BigDecimal("-0.3"),
      new BigDecimal("-0.2")
    );
    EQUAL(exc.getValue(), new BigDecimal("-0.1"));
    EQUAL(exc.getStart(), new BigDecimal("-0.3"));
    EQUAL(exc.getEnd()  , new BigDecimal("-0.2"));
    EQUAL(exc.getMessage(), "-0.1 is out of range (-0.3,-0.2).");

    MSG("- 引数の全てにヌルを指定した場合");
    exc = new OutOfRangeException(
      (BigDecimal)null, (BigDecimal)null, (BigDecimal)null);
    EQUAL(exc.getValue(), null);
    EQUAL(exc.getStart(), null);
    EQUAL(exc.getEnd()  , null);
    EQUAL(exc.getMessage(), null);

    MSG("- 範囲を越えた値に通常のBigDecimal、それ以外にヌルを指定した場合");
    exc = new OutOfRangeException(new BigDecimal(1.0), null, null);
    EQUAL(exc.getValue(), new BigDecimal(1.0));
    EQUAL(exc.getStart(), null);
    EQUAL(exc.getEnd()  , null);
    EQUAL(exc.getMessage(), "1 is out of range (-,-).");

    MSG("- 範囲の開始値に通常のBigDecimal、それ以外にヌルを指定した場合");
    exc = new OutOfRangeException(null, new BigDecimal(1.0), null);
    EQUAL(exc.getValue(), null);
    EQUAL(exc.getStart(), new BigDecimal(1.0));
    EQUAL(exc.getEnd()  , null);
    EQUAL(exc.getMessage(), "? is out of range (1,-).");

    MSG("- 範囲の終了値に通常のBigDecimal、それ以外にヌルを指定した場合");
    exc = new OutOfRangeException(null, null, new BigDecimal(1.0));
    EQUAL(exc.getValue(), null);
    EQUAL(exc.getStart(), null);
    EQUAL(exc.getEnd()  , new BigDecimal(1.0));
    EQUAL(exc.getMessage(), "? is out of range (-,1).");
  }

  public void constructor_msg()
  {
    MSG("メッセージを引数にとるコンストラクタの確認。");

    OutOfRangeException exc = new OutOfRangeException("Hello!");
    EQUAL(exc.getValue(), null);
    EQUAL(exc.getStart(), null);
    EQUAL(exc.getEnd()  , null);
    EQUAL(exc.getMessage(), "Hello!");
  }

  public void _toString()
  {
    MSG("toString()メソッドの確認。");

    OutOfRangeException exc = new OutOfRangeException("Hello!");
    OK(exc.toString());

    exc = new OutOfRangeException(1, 2, 3);
    OK(exc.toString());
  }

  public void serialize()
  {
    MSG("シリアライズの確認。");

    ObjectSerializer os = new ObjectSerializer();

    OutOfRangeException exc = new OutOfRangeException("あああ");
    try {
      byte[] bytes = os.serializeToByteArray(exc);
      OutOfRangeException newExc = (OutOfRangeException)os.deserialize(bytes);
      EQUAL(newExc.getMessage(), exc.getMessage());
    } catch (Exception e) {
      NG(e);
    }
  }
}
