View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  package ch.elca.el4j.tests.util.codingsupport;
19  
20  import static org.junit.Assert.fail;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  
26  import org.junit.Test;
27  
28  import ch.elca.el4j.util.codingsupport.Reject;
29  import ch.elca.el4j.util.codingsupport.annotations.FindBugsSuppressWarnings;
30  
31  // Checkstyle: EmptyBlock off
32  
33  /**
34   * This tests check the behavior of the {@link
35   * ch.elca.el4j.util.codingsupport.Reject} class.
36   *
37   * @svnLink $Revision: 4010 $;$Date: 2009-12-01 10:59:54 +0100 (Di, 01. Dez 2009) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/util/codingsupport/RejectTest.java $
38   *
39   * @author Andreas Bur (ABU)
40   */
41  public class RejectTest {
42  
43  	/**
44  	 * Checks {@link Reject#ifNull(Object)}.
45  	 */
46  	@Test
47  	public void testIfNull() {
48  		try {
49  			Reject.ifNull(null);
50  			fail("Bad implementatino of Reject.isNull(null)");
51  		} catch (Exception e) { }
52  		try {
53  			Reject.ifNull(new Object());
54  		} catch (Exception e) {
55  			fail("Bad implementatino of Reject.isNull(new Object())");
56  		}
57  	}
58  	
59  	/**
60  	 *  Check with a subclass of RuntimeException
61  	 * Checks {@link Reject#ifNull(Object)}.
62  	 */
63  	@Test
64  	public void testIfNull2() {
65  		try {
66  			Reject.ifNull(null, IllegalArgumentException.class, "My String");
67  			fail("Bad implementation of Reject.isNull()");
68  		} catch (IllegalArgumentException e) { }
69  		try {
70  			Reject.ifEmpty("", IllegalArgumentException.class, "My String");
71  			fail("Bad implementation of Reject.isEmpty()");
72  		} catch (IllegalArgumentException e) { }
73  		try {
74  			Reject.ifCondition(true, IllegalArgumentException.class,
75  				"My String");
76  			fail("Bad implementation of Reject.ifCondition()");
77  		} catch (IllegalArgumentException e) { }
78  		
79  		try {
80  			Reject.ifNull(new Object(), IllegalArgumentException.class, "");
81  		} catch (IllegalArgumentException e) {
82  			fail("Bad implementation of Reject.isNull(new Object())");
83  		}
84  	}
85  	
86  	/**
87  	 * Checks {@link Reject#ifEmpty(Collection)}.
88  	 */
89  	@Test
90  	@SuppressWarnings("unchecked")
91  	@FindBugsSuppressWarnings(value = "DE_MIGHT_IGNORE",
92  						justification = "Exception handling not important as test is supposed to fail.")
93  	public void testIfEmptyCollection() {
94  		try {
95  			Reject.ifEmpty((List) null);
96  			fail("Bad implementatino of Reject.isEmpty((List) null)");
97  		} catch (Exception e) { }
98  		ArrayList list = new ArrayList();
99  		try {
100 			Reject.ifEmpty(list);
101 			fail("Bad implementatino of Reject.isEmpty(new ArrayList())");
102 		} catch (Exception e) { }
103 		try {
104 			list.add(new Object());
105 			Reject.ifEmpty(list);
106 		} catch (Exception e) {
107 			fail("Bad implementatino of Reject.isEmpty('nonempty list')");
108 		}
109 	}
110 	
111 	/**
112 	 * Checks {@link Reject#ifEmpty(String)}.
113 	 */
114 	@Test
115 	public void testIfEmptyString() {
116 		try {
117 			Reject.ifEmpty((String) null);
118 			fail("Bad implementatino of Reject.isEmpty((String) null)");
119 		} catch (Exception e) { }
120 		try {
121 			Reject.ifEmpty(" ");
122 			fail("Bad implementatino of Reject.isEmpty(\" \")");
123 		} catch (Exception e) { }
124 		try {
125 			Reject.ifEmpty(" test");
126 		} catch (Exception e) {
127 			fail("Bad implementatino of Reject.isEmpty(\" test\")");
128 		}
129 	}
130 }
131 //Checkstyle: EmptyBlock on