001package org.apache.commons.beanutils2;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020import static org.apache.commons.beanutils2.BeanUtils.on;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.commons.beanutils2.testbeans.TestBean;
024import org.junit.After;
025import org.junit.Before;
026import org.junit.Test;
027
028public class SetIndexedPropertyTestCase
029{
030
031    private TestBean testBean;
032
033    @Before
034    public void setUp()
035    {
036        testBean = new TestBean();
037    }
038
039    @After
040    public void tearDown()
041    {
042        testBean = null;
043    }
044
045    @Test( expected = IllegalArgumentException.class )
046    public void setIndexedNegative()
047        throws Exception
048    {
049        on( testBean ).setIndexed( "intIndexed" ).at( -1 );
050    }
051
052    @Test( expected = NoSuchPropertyException.class )
053    public void setIndexedUnknown()
054        throws Exception
055    {
056        on( testBean ).setIndexed( "unkown" );
057    }
058
059    @Test
060    public void setIntIndexed()
061        throws Exception
062    {
063        on( testBean ).setIndexed( "intIndexed" ).at( 1 ).with( 4711 );
064        assertTrue( testBean.getIntIndexed( 1 ) == 4711 );
065    }
066
067    @Test( expected = IllegalArgumentException.class )
068    public void setIntIndexedNull()
069        throws Exception
070    {
071        on( testBean ).setIndexed( "intIndexed" ).at( 1 ).with( null );
072    }
073
074    @Test
075    public void setStringIndexed()
076        throws Exception
077    {
078        on( testBean ).setIndexed( "stringIndexed" ).at( 1 ).with( "Hello World!" );
079        assertTrue( testBean.getStringIndexed( 1 ).equals( "Hello World!" ) );
080    }
081
082    @Test
083    public void setStringIndexedNull()
084        throws Exception
085    {
086        on( testBean ).setIndexed( "stringIndexed" ).at( 1 ).with( null );
087        assertTrue( testBean.getStringIndexed( 1 ) == null );
088    }
089
090    @Test( expected = IllegalArgumentException.class )
091    public void setStringIndexedInt()
092        throws Exception
093    {
094        on( testBean ).setIndexed( "stringIndexed" ).at( 1 ).with( 4711 );
095    }
096
097}