001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.beanutils.bugs; 018 019 import java.util.Comparator; 020 import java.util.HashMap; 021 import java.util.Map; 022 023 import junit.framework.Test; 024 import junit.framework.TestCase; 025 import junit.framework.TestSuite; 026 027 import org.apache.commons.beanutils.BeanUtils; 028 import org.apache.commons.beanutils.PropertyUtils; 029 import org.apache.commons.logging.Log; 030 import org.apache.commons.logging.LogFactory; 031 032 /** 033 * See https://issues.apache.org/jira/browse/BEANUTILS-339 034 * <p /> 035 * 036 * @version $Revision: 800650 $ $Date: 2009-08-03 21:38:16 -0400 (Mon, 03 Aug 2009) $ 037 */ 038 public class Jira339TestCase extends TestCase { 039 040 private Log log = LogFactory.getLog(Jira339TestCase.class); 041 042 /** 043 * Create a test case with the specified name. 044 * 045 * @param name The name of the test 046 */ 047 public Jira339TestCase(String name) { 048 super(name); 049 } 050 051 /** 052 * Run the Test. 053 * 054 * @param args Arguments 055 */ 056 public static void main(String[] args) { 057 junit.textui.TestRunner.run(suite()); 058 } 059 060 /** 061 * Create a test suite for this test. 062 * 063 * @return a test suite 064 */ 065 public static Test suite() { 066 return (new TestSuite(Jira339TestCase.class)); 067 } 068 069 /** 070 * Set up. 071 * 072 * @throws java.lang.Exception 073 */ 074 protected void setUp() throws Exception { 075 super.setUp(); 076 } 077 078 /** 079 * Tear Down. 080 * 081 * @throws java.lang.Exception 082 */ 083 protected void tearDown() throws Exception { 084 super.tearDown(); 085 } 086 087 /** 088 * Test {@link PropertyUtils#setProperty(Object, String, Object)} 089 */ 090 public void testIssue_BEANUTILS_339_BeanUtilsBean_setProperty() { 091 092 TestBean bean = new TestBean(); 093 try { 094 BeanUtils.setProperty(bean, "comparator", null); 095 } catch (Throwable t) { 096 log.error("Failed: " + t.getMessage(), t); 097 fail("Threw exception: " + t); 098 } 099 assertNull("TestBean comparator should be null", bean.getComparator()); 100 } 101 102 /** 103 * Test {@link BeanUtils#populate(Object, Map)} 104 */ 105 public void testIssue_BEANUTILS_331_BeanUtilsBean_populate() { 106 107 TestBean bean = new TestBean(); 108 try { 109 Map properties = new HashMap(); 110 properties.put("comparator", null); 111 BeanUtils.populate(bean, properties); 112 } catch (Throwable t) { 113 log.error("Failed: " + t.getMessage(), t); 114 fail("Threw exception: " + t); 115 } 116 assertNull("TestBean comparator should be null", bean.getComparator()); 117 } 118 119 /** 120 * Test Bean. 121 */ 122 public static class TestBean { 123 private Comparator comparator; 124 125 /** 126 * Return the comparator. 127 * 128 * @return the comparator 129 */ 130 public Comparator getComparator() { 131 return comparator; 132 } 133 134 /** 135 * Set the comparator. 136 * 137 * @param comparator the comparator 138 */ 139 public void setComparator(Comparator comparator) { 140 this.comparator = comparator; 141 } 142 143 } 144 }