webwork/src/test/webwork/util BeanUtilTestCase.java,NONE,1.1

[email protected] Sat, 01 Nov 2003 17:17:40 -0800
Newsgroups gmane.comp.java.open-symphony.cvs
Message-ID <[email protected]>
Update of /cvsroot/opensymphony/webwork/src/test/webwork/util
In directory sc8-pr-cvs1:/tmp/cvs-serv6764/webwork/util

Added Files:
	BeanUtilTestCase.java 
Log Message:
Got rid of complex and unintelligible mock testcase, it's just too badly written
Added BeanUtilTestCase
Renamed all testcases that require a server to *WebTestCase.java


--- NEW FILE: BeanUtilTestCase.java ---
package webwork.util;

import java.util.*;
import java.text.SimpleDateFormat;
import java.text.DateFormat;

import webwork.TestCaseSupport;

/**
 * @author Hani Suleiman ([email protected])
 *         Date: Nov 1
 * @author 2003
 *         Time: 7:18:32 PM
 */
public class BeanUtilTestCase extends TestCaseSupport
{
  public void testCopyBean()
  {
    TestBean bean1 = new TestBean();
    bean1.setBool(true);
    TestBean inner = new TestBean();
    inner.setLongValue(20);
    bean1.setTestBean(inner);
    TestBean copy = new TestBean();
    BeanUtil.copy(bean1, copy);
    assertEquals("bean copy failed", bean1, copy);
  }

  public void testProperties()
  {
    TestBean bean = new TestBean();

    BeanUtil.setProperty("longValue", "12345", bean);
    assertEquals("setting long property failed", 12345, bean.getLongValue());

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT);
    BeanUtil.setProperty("date", formatter.format(cal.getTime()), bean);
    assertEquals("setting date property failed", cal.getTime(), bean.getDate());

    BeanUtil.setProperty("bool", "true", bean);
    assertTrue("setting boolean property failed", bean.isBool());

    bean.getList().add(new TestBean());
    BeanUtil.setProperty("list[0]/stringValue", "test", bean);
    assertEquals("setting list index failed", "test", ((TestBean)bean.getList().get(0)).getStringValue());

    Map props = new HashMap();
    props.put("longValue", new Integer(20));
    props.put("stringValue", "blah");
    props.put("dateFormat", "ddMMMyy");
    props.put("longObj", "12345");
    BeanUtil.setProperties(props, bean);
    assertEquals("setting long property failed", 20, bean.getLongValue());
    assertEquals("setting long object property failed", new Long(12345), bean.getLongObj());
    assertEquals("setting String property failed", "blah", bean.getStringValue());
    assertEquals("setting dateformat property failed", "ddMMMyy", ((SimpleDateFormat)bean.getDateFormat()).toPattern());
    assertEquals(formatter.format(cal.getTime()), BeanUtil.toStringValue(cal.getTime()));
  }

  class TestBean
  {
    private String stringValue;
    private Date date;
    private long longValue;
    private Long longObj;
    private DateFormat dateFormat;
    private boolean bool;
    private TestBean testBean;
    private Object myReadOnlyBean;
    private List list = new ArrayList();

    public Object getMyReadOnlyBean()
    {
      if(myReadOnlyBean==null) myReadOnlyBean = new TestBean();
      return myReadOnlyBean;
    }

    public Long getLongObj()
    {
      return longObj;
    }

    public void setLongObj(Long longObj)
    {
      this.longObj = longObj;
    }

    public DateFormat getDateFormat()
    {
      return dateFormat;
    }

    public void setDateFormat(DateFormat dateFormat)
    {
      this.dateFormat = dateFormat;
    }

    public List getList()
    {
      return list;
    }

    public void setList(List list)
    {
      this.list = list;
    }

    public String getStringValue()
    {
      return stringValue;
    }

    public void setStringValue(String stringValue)
    {
      this.stringValue = stringValue;
    }

    public Date getDate()
    {
      return date;
    }

    public void setDate(Date date)
    {
      this.date = date;
    }

    public long getLongValue()
    {
      return longValue;
    }

    public void setLongValue(long longValue)
    {
      this.longValue = longValue;
    }

    public boolean isBool()
    {
      return bool;
    }

    public void setBool(boolean bool)
    {
      this.bool = bool;
    }

    public TestBean getTestBean()
    {
      return testBean;
    }

    public void setTestBean(TestBean testBean)
    {
      this.testBean = testBean;
    }

    public boolean equals(Object o)
    {
      if(this == o) return true;
      if(!(o instanceof TestBean)) return false;

      final TestBean testBean1 = (TestBean)o;

      if(bool != testBean1.bool) return false;
      if(longValue != testBean1.longValue) return false;
      if(date != null ? !date.equals(testBean1.date) : testBean1.date != null) return false;
      if(!list.equals(testBean1.list)) return false;
      if(myReadOnlyBean != null ? !myReadOnlyBean.equals(testBean1.myReadOnlyBean) : testBean1.myReadOnlyBean != null) return false;
      if(stringValue != null ? !stringValue.equals(testBean1.stringValue) : testBean1.stringValue != null) return false;
      if(testBean != null ? !testBean.equals(testBean1.testBean) : testBean1.testBean != null) return false;

      return true;
    }
  }
}




-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?   SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/