View Javadoc
1   package org.apache.commons.beanutils2;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static java.util.Arrays.fill;
23  import static org.apache.commons.beanutils2.Assertions.checkArgument;
24  import static org.apache.commons.beanutils2.Assertions.checkNoneIsNull;
25  import static org.apache.commons.beanutils2.Assertions.checkNotNull;
26  import static org.apache.commons.beanutils2.Assertions.checkState;
27  import static org.junit.Assert.assertSame;
28  
29  import org.junit.Rule;
30  import org.junit.Test;
31  import org.junit.rules.ExpectedException;
32  
33  public class AssertionsTest
34  {
35  
36      private static final int INT_VALUE = 15;
37  
38      private static final String STRING_VALUE = "doing something awesome";
39  
40      private static final String PLACEHOLDER = "%s";
41  
42      private static final String ERROR_PART1 = "Something went wrong while ";
43  
44      private static final String ERROR_PART2 = " because we got a value of ";
45  
46      private static final String ERROR_MSG = ERROR_PART1 + PLACEHOLDER + ERROR_PART2 + PLACEHOLDER;
47  
48      @Rule
49      public final ExpectedException thrown = ExpectedException.none();
50  
51      @Test
52      public void checkArgumentTrueString()
53      {
54          checkArgument( true, "True has to be okay!" );
55      }
56  
57      @Test
58      public void checkArgumentFalseString()
59      {
60          thrown.expect( IllegalArgumentException.class );
61          thrown.expectMessage( ERROR_PART1 );
62          checkArgument( false, ERROR_PART1 );
63      }
64  
65      @Test
66      public void checkArgumentFalseStringObjectObject()
67      {
68          thrown.expect( IllegalArgumentException.class );
69          thrown.expectMessage( ERROR_PART1 );
70          thrown.expectMessage( ERROR_PART2 );
71          thrown.expectMessage( String.valueOf( INT_VALUE ) );
72          thrown.expectMessage( STRING_VALUE );
73          checkArgument( false, ERROR_MSG, STRING_VALUE, INT_VALUE );
74      }
75  
76      @Test( expected = NullPointerException.class )
77      public void checkArgumentFalseNull()
78      {
79          checkArgument( false, null );
80      }
81  
82      @Test( expected = NullPointerException.class )
83      public void checkArgumentFalseNullObjects()
84      {
85          checkArgument( false, null, STRING_VALUE, INT_VALUE );
86      }
87  
88      @Test
89      public void checkStateTrueString()
90      {
91          checkState( true, "True has to be okay!" );
92      }
93  
94      @Test
95      public void checkStateFalseString()
96      {
97          thrown.expect( IllegalStateException.class );
98          thrown.expectMessage( ERROR_PART1 );
99          checkState( false, ERROR_PART1 );
100     }
101 
102     @Test
103     public void checkStateFalseStringObjects()
104     {
105         thrown.expect( IllegalStateException.class );
106         thrown.expectMessage( ERROR_PART1 );
107         thrown.expectMessage( ERROR_PART2 );
108         thrown.expectMessage( STRING_VALUE );
109         thrown.expectMessage( String.valueOf( INT_VALUE ) );
110         checkState( false, ERROR_MSG, STRING_VALUE, INT_VALUE );
111     }
112 
113     @Test( expected = NullPointerException.class )
114     public void checkStateFalseNull()
115     {
116         checkState( false, null );
117     }
118 
119     @Test( expected = NullPointerException.class )
120     public void checkStateFalseNullObjects()
121     {
122         checkState( false, null, STRING_VALUE, INT_VALUE );
123     }
124 
125     @Test
126     public void checkNotNullObjectString()
127     {
128         Object obj = new Object();
129         Object checkedObj = checkNotNull( obj, "An object reference has to be okay!" );
130         assertSame( obj, checkedObj );
131     }
132 
133     @Test
134     public void checkNotNullNullString()
135     {
136         thrown.expect( NullPointerException.class );
137         thrown.expectMessage( ERROR_PART1 );
138         checkNotNull( null, ERROR_PART1 );
139     }
140 
141     @Test
142     public void checkNotNullNullStringObjects()
143     {
144         thrown.expect( NullPointerException.class );
145         thrown.expectMessage( ERROR_PART1 );
146         thrown.expectMessage( ERROR_PART2 );
147         thrown.expectMessage( STRING_VALUE );
148         thrown.expectMessage( String.valueOf( INT_VALUE ) );
149         checkNotNull( null, ERROR_MSG, STRING_VALUE, INT_VALUE );
150     }
151 
152     // this is a bit ambiguous, we do not distinguish if NPE is thrown because reference is null or if NPE is
153     // thrown because errorTemplate or errorMessageArgs are null
154 
155     @Test( expected = NullPointerException.class )
156     public void checkNotNullNullNull()
157     {
158         checkNotNull( null, null );
159     }
160 
161     @Test( expected = NullPointerException.class )
162     public void checkNotNullNullObjects()
163     {
164         checkNotNull( null, null, STRING_VALUE, INT_VALUE );
165     }
166 
167     @Test( expected = NullPointerException.class )
168     public void checkNotNullNullStringNull()
169     {
170         checkNotNull( null, ERROR_MSG, (Object[]) null );
171     }
172 
173     @Test( expected = NullPointerException.class )
174     public void checkNotNullNullNullNull()
175     {
176         checkNotNull( null, null, (Object[]) null );
177     }
178 
179     @Test
180     public void checkNoneIsNullEmptyObjectArray()
181     {
182         Object[] refs = new Object[0];
183         Object[] checkedRefs = checkNoneIsNull( refs );
184         assertSame( refs, checkedRefs );
185     }
186 
187     @Test
188     public void checkNoneIsNullObjectArray()
189     {
190         Object[] refs = getObjectArray( 10 );
191         Object[] checkedRefs = checkNoneIsNull( refs );
192         assertSame( refs, checkedRefs );
193         for ( int i = 0; i < refs.length; i++ )
194         {
195             assertSame( refs[i], checkedRefs[i] );
196         }
197     }
198 
199     @Test
200     public void checkNoneIsNullObjectArrayWithNullRefs()
201     {
202 
203         Object[] refs = getObjectArray( 10 );
204         refs[5] = null;
205         thrown.expect( NullPointerException.class );
206         thrown.expectMessage( "5/10" );
207         thrown.expectMessage( "Reference at index" );
208         thrown.expectMessage( "is null!" );
209         checkNoneIsNull( refs );
210     }
211 
212     private Object[] getObjectArray( int size )
213     {
214         Object[] array = new Object[size];
215         fill( array, new Object() );
216         return array;
217     }
218 
219     @Test(expected = NullPointerException.class)
220     public void checkNoneIsNullNull()
221     {
222         checkNoneIsNull( null );
223     }
224 
225 }