001package org.apache.commons.beanutils2; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import static java.util.Arrays.fill; 023import static org.apache.commons.beanutils2.Assertions.checkArgument; 024import static org.apache.commons.beanutils2.Assertions.checkNoneIsNull; 025import static org.apache.commons.beanutils2.Assertions.checkNotNull; 026import static org.apache.commons.beanutils2.Assertions.checkState; 027import static org.junit.Assert.assertSame; 028 029import org.junit.Rule; 030import org.junit.Test; 031import org.junit.rules.ExpectedException; 032 033public class AssertionsTest 034{ 035 036 private static final int INT_VALUE = 15; 037 038 private static final String STRING_VALUE = "doing something awesome"; 039 040 private static final String PLACEHOLDER = "%s"; 041 042 private static final String ERROR_PART1 = "Something went wrong while "; 043 044 private static final String ERROR_PART2 = " because we got a value of "; 045 046 private static final String ERROR_MSG = ERROR_PART1 + PLACEHOLDER + ERROR_PART2 + PLACEHOLDER; 047 048 @Rule 049 public final ExpectedException thrown = ExpectedException.none(); 050 051 @Test 052 public void checkArgumentTrueString() 053 { 054 checkArgument( true, "True has to be okay!" ); 055 } 056 057 @Test 058 public void checkArgumentFalseString() 059 { 060 thrown.expect( IllegalArgumentException.class ); 061 thrown.expectMessage( ERROR_PART1 ); 062 checkArgument( false, ERROR_PART1 ); 063 } 064 065 @Test 066 public void checkArgumentFalseStringObjectObject() 067 { 068 thrown.expect( IllegalArgumentException.class ); 069 thrown.expectMessage( ERROR_PART1 ); 070 thrown.expectMessage( ERROR_PART2 ); 071 thrown.expectMessage( String.valueOf( INT_VALUE ) ); 072 thrown.expectMessage( STRING_VALUE ); 073 checkArgument( false, ERROR_MSG, STRING_VALUE, INT_VALUE ); 074 } 075 076 @Test( expected = NullPointerException.class ) 077 public void checkArgumentFalseNull() 078 { 079 checkArgument( false, null ); 080 } 081 082 @Test( expected = NullPointerException.class ) 083 public void checkArgumentFalseNullObjects() 084 { 085 checkArgument( false, null, STRING_VALUE, INT_VALUE ); 086 } 087 088 @Test 089 public void checkStateTrueString() 090 { 091 checkState( true, "True has to be okay!" ); 092 } 093 094 @Test 095 public void checkStateFalseString() 096 { 097 thrown.expect( IllegalStateException.class ); 098 thrown.expectMessage( ERROR_PART1 ); 099 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}