View Javadoc
1   package org.apache.commons.beanutils2;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import static org.apache.commons.beanutils2.BeanUtils.on;
21  import static org.junit.Assert.assertTrue;
22  
23  import org.apache.commons.beanutils2.testbeans.TestBean;
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  public class SetIndexedPropertyTestCase
29  {
30  
31      private TestBean testBean;
32  
33      @Before
34      public void setUp()
35      {
36          testBean = new TestBean();
37      }
38  
39      @After
40      public void tearDown()
41      {
42          testBean = null;
43      }
44  
45      @Test( expected = IllegalArgumentException.class )
46      public void setIndexedNegative()
47          throws Exception
48      {
49          on( testBean ).setIndexed( "intIndexed" ).at( -1 );
50      }
51  
52      @Test( expected = NoSuchPropertyException.class )
53      public void setIndexedUnknown()
54          throws Exception
55      {
56          on( testBean ).setIndexed( "unkown" );
57      }
58  
59      @Test
60      public void setIntIndexed()
61          throws Exception
62      {
63          on( testBean ).setIndexed( "intIndexed" ).at( 1 ).with( 4711 );
64          assertTrue( testBean.getIntIndexed( 1 ) == 4711 );
65      }
66  
67      @Test( expected = IllegalArgumentException.class )
68      public void setIntIndexedNull()
69          throws Exception
70      {
71          on( testBean ).setIndexed( "intIndexed" ).at( 1 ).with( null );
72      }
73  
74      @Test
75      public void setStringIndexed()
76          throws Exception
77      {
78          on( testBean ).setIndexed( "stringIndexed" ).at( 1 ).with( "Hello World!" );
79          assertTrue( testBean.getStringIndexed( 1 ).equals( "Hello World!" ) );
80      }
81  
82      @Test
83      public void setStringIndexedNull()
84          throws Exception
85      {
86          on( testBean ).setIndexed( "stringIndexed" ).at( 1 ).with( null );
87          assertTrue( testBean.getStringIndexed( 1 ) == null );
88      }
89  
90      @Test( expected = IllegalArgumentException.class )
91      public void setStringIndexedInt()
92          throws Exception
93      {
94          on( testBean ).setIndexed( "stringIndexed" ).at( 1 ).with( 4711 );
95      }
96  
97  }