PropertyUtilsTest.java :  » Web-Framework » calyxo » de » odysseus » calyxo » base » util » Java Open Source

Java Open Source » Web Framework » calyxo 
calyxo » de » odysseus » calyxo » base » util » PropertyUtilsTest.java
/*
 * Copyright 2004, 2005, 2006 Odysseus Software GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package de.odysseus.calyxo.base.util;

import junit.framework.TestCase;

/**
 * 
 * @author Christoph Beck
 */
public class PropertyUtilsTest extends TestCase {

  public static class Bean {
    private String property;
    private Bean bean;
    public String getProperty() {
      return property;
    }
    public void setProperty(String value) {
      property = value;
    }
    public Bean getBean() {
      return bean;
    }
    public void setBean(Bean value) {
      bean = value;
    }
  }

  /**
   * Constructor for PropertyUtilsTest.
   * @param arg0
   */
  public PropertyUtilsTest(String arg0) {
    super(arg0);
  }

  public void testGetProperty() {
    Bean bean = new Bean();
    bean.setProperty("foo");
    try {
      assertSame("foo", PropertyUtils.getProperty(bean, "property"));
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }

  public void testSetProperty() {
    Bean bean = new Bean();
    bean.setProperty("foo");
    try {
      PropertyUtils.setProperty(bean, "property", "foo");
    } catch (Exception e) {
      fail(e.getMessage());
    }
    assertSame("foo", bean.getProperty());
  }

  public void testGetNestedProperty() {
    Bean bean1 = new Bean();
    bean1.setProperty("foo1");
    try {
      assertSame("foo1", PropertyUtils.getNestedProperty(bean1, "property"));
    } catch (Exception e) {
      fail(e.getMessage());
    }
    try {
      assertNull(PropertyUtils.getNestedProperty(bean1, "bean.property"));
    } catch (Exception e) {
      fail(e.getMessage());
    }
    Bean bean2 = new Bean();
    bean2.setProperty("foo2");
    bean1.setBean(bean2);
    try {
      assertSame("foo2", PropertyUtils.getNestedProperty(bean1, "bean.property"));
    } catch (Exception e) {
      fail(e.getMessage());
    }
    try {
      assertNull(PropertyUtils.getNestedProperty(bean1, "bean.bean.property"));
    } catch (Exception e) {
      fail(e.getMessage());
    }
    Bean bean3 = new Bean();
    bean3.setProperty("foo3");
    bean2.setBean(bean3);
    try {
      assertSame("foo3", PropertyUtils.getNestedProperty(bean1, "bean.bean.property"));
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }

  public void testSetNestedProperty() {
    Bean bean1 = new Bean();
    try {
      PropertyUtils.setNestedProperty(bean1, "property", "foo1");
    } catch (Exception e) {
      fail(e.getMessage());
    }
    assertSame("foo1", bean1.getProperty());
    Bean bean2 = new Bean();
    bean1.setBean(bean2);
    try {
      PropertyUtils.setNestedProperty(bean1, "bean.property", "foo2");
    } catch (Exception e) {
      fail(e.getMessage());
    }
    assertSame("foo2", bean2.getProperty());
    Bean bean3 = new Bean();
    bean2.setBean(bean3);
    try {
      PropertyUtils.setNestedProperty(bean1, "bean.bean.property", "foo3");
    } catch (Exception e) {
      fail(e.getMessage());
    }
    assertSame("foo3", bean3.getProperty());
  }

  public void testSetNestedPropertyCreate() {
    Bean bean = new Bean();
    try {
      PropertyUtils.setNestedProperty(bean, "bean.property", "foo1");
    } catch (Exception e) {
      fail(e.getMessage());
    }
    assertSame("foo1", bean.getBean().getProperty());
    try {
      PropertyUtils.setNestedProperty(bean, "bean.bean.bean.property", "foo3");
    } catch (Exception e) {
      fail(e.getMessage());
    }
    assertSame("foo3", bean.getBean().getBean().getBean().getProperty());
  }

  public void testSetNestedPropertyCreateWithException() {
    Bean bean = new Bean();
    try {
      PropertyUtils.setNestedProperty(bean, "bean.bean.foo", "bar");
      fail("Exception expected");
    } catch (Exception e) {
      assertNull(bean.getBean());
    }
  }


  public static void main(String[] args) {
    junit.textui.TestRunner.run(PropertyUtilsTest.class);
  }
}
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.