CompositeDateConstructorTest.java :  » Testing » springunit-0.6 » org » springunit » examples » constructor » junit » v4 » Java Open Source

Java Open Source » Testing » springunit 0.6 
springunit 0.6 » org » springunit » examples » constructor » junit » v4 » CompositeDateConstructorTest.java
package org.springunit.examples.constructor.junit.v4;

import java.util.HashMap;
import java.util.Map;

import org.springunit.examples.CompositeDate;
import org.springunit.examples.InvalidDateException;

import junit.framework.TestCase;


public class CompositeDateConstructorTest extends TestCase {
  
  protected void setUp() throws Exception {
    this.data = new HashMap<String, Map<String, Object>>();
    
    Map<String, Object> testData = new HashMap<String, Object>();
    testData.put("day", 1);
    testData.put("month", 1);
    testData.put("year", 2006);
    testData.put("expectedDay", 1);
    testData.put("expectedMonth", 1);
    testData.put("expectedYear", 2006);
    this.data.put("testJan01", testData);
    
    testData = new HashMap<String, Object>();
    testData.put("day", 31);
    testData.put("month", 12);
    testData.put("year", 2006);
    testData.put("expectedDay", 31);
    testData.put("expectedMonth", 12);
    testData.put("expectedYear", 2006);
    this.data.put("testDec31", testData);
    
    testData = new HashMap<String, Object>();
    testData.put("day", 29);
    testData.put("month", 2);
    testData.put("year", 2000);
    testData.put("expectedDay", 29);
    testData.put("expectedMonth", 2);
    testData.put("expectedYear", 2000);
    this.data.put("testFeb29Leap2000", testData);
    
    testData = new HashMap<String, Object>();
    testData.put("day", 29);
    testData.put("month", 2);
    testData.put("year", 2004);
    testData.put("expectedDay", 29);
    testData.put("expectedMonth", 2);
    testData.put("expectedYear", 2004);
    this.data.put("testFeb29Leap2004", testData);
    
    testData = new HashMap<String, Object>();
    testData.put("day", 31);
    testData.put("month", 4);
    testData.put("year", 2006);
    this.data.put("testApr31", testData);
    
    testData = new HashMap<String, Object>();
    testData.put("day", 31);
    testData.put("month", 6);
    testData.put("year", 2006);
    this.data.put("testJun31", testData);
    
    testData = new HashMap<String, Object>();
    testData.put("day", 31);
    testData.put("month", 9);
    testData.put("year", 2006);
    this.data.put("testSep31", testData);
    
    testData = new HashMap<String, Object>();
    testData.put("day", 31);
    testData.put("month", 11);
    testData.put("year", 2006);
    this.data.put("testNov31", testData);
    
    testData = new HashMap<String, Object>();
    testData.put("day", 31);
    testData.put("month", 2);
    testData.put("year", 2006);
    this.data.put("testFeb31", testData);
    
    testData = new HashMap<String, Object>();
    testData.put("day", 30);
    testData.put("month", 2);
    testData.put("year", 2006);
    this.data.put("testFeb30", testData);
    
    testData = new HashMap<String, Object>();
    testData.put("day", 29);
    testData.put("month", 2);
    testData.put("year", 2003);
    this.data.put("testFeb29NoLeap", testData);
    
    testData = new HashMap<String, Object>();
    testData.put("day", 29);
    testData.put("month", 2);
    testData.put("year", 1900);
    this.data.put("testFeb29NoLeap1900", testData);
    
  }

  protected void runValid(int year, int month, int day,
      int expectedYear, int expectedMonth, int expectedDay) throws Exception {
    CompositeDate subject = new CompositeDate(year, month, day);
    assertTrue(expectedMonth == subject.getMonth());
    assertTrue(expectedDay == subject.getDay());
    assertTrue(expectedYear == subject.getYear());
  }

  public void testJan01() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    Integer expectedDay = (Integer)this.data.get(getName()).get("expectedDay");
    Integer expectedMonth = (Integer)this.data.get(getName()).get("expectedMonth");
    Integer expectedYear = (Integer)this.data.get(getName()).get("expectedYear");
    runValid(year, month, day, expectedYear, expectedMonth, expectedDay);
  }
  
  public void testDec31() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    Integer expectedDay = (Integer)this.data.get(getName()).get("expectedDay");
    Integer expectedMonth = (Integer)this.data.get(getName()).get("expectedMonth");
    Integer expectedYear = (Integer)this.data.get(getName()).get("expectedYear");
    runValid(year, month, day, expectedYear, expectedMonth, expectedDay);
  }
  
  public void testFeb29Leap2000() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    Integer expectedDay = (Integer)this.data.get(getName()).get("expectedDay");
    Integer expectedMonth = (Integer)this.data.get(getName()).get("expectedMonth");
    Integer expectedYear = (Integer)this.data.get(getName()).get("expectedYear");
    runValid(year, month, day, expectedYear, expectedMonth, expectedDay);
  }
  
  public void testFeb29Leap2004() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    Integer expectedDay = (Integer)this.data.get(getName()).get("expectedDay");
    Integer expectedMonth = (Integer)this.data.get(getName()).get("expectedMonth");
    Integer expectedYear = (Integer)this.data.get(getName()).get("expectedYear");
    runValid(year, month, day, expectedYear, expectedMonth, expectedDay);
  }
  
  protected void runInvalid(int year, int month, int day) throws Exception {
    try {
      new CompositeDate(year, month, day);
      fail("Exception not thrown");
    }
    catch (InvalidDateException ex) {
    }
  }

  public void testApr31() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    runInvalid(year, month, day);
  }
  
  public void testJun31() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    runInvalid(year, month, day);
  }
  
  public void testSep31() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    runInvalid(year, month, day);
  }
  
  public void testNov31() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    runInvalid(year, month, day);
  }
  
  public void testFeb31() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    runInvalid(year, month, day);
  }
  
  public void testFeb30() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    runInvalid(year, month, day);
  }
  
  public void testFeb29NoLeap() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    runInvalid(year, month, day);
  }
  
  public void testFeb29NoLeap1900() throws Exception {
    Integer day = (Integer)this.data.get(getName()).get("day");
    Integer month = (Integer)this.data.get(getName()).get("month");
    Integer year = (Integer)this.data.get(getName()).get("year");
    runInvalid(year, month, day);
  }
  
  private Map<String, Map<String, Object>> data;
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.