9f494fb672ec52d8bec3c219e1f2739040ae8612
[utils] / support / general / src / test / java / org / wamblee / conditions / PropertyRegexConditionTest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16
17 package org.wamblee.conditions;
18
19 import junit.framework.TestCase;
20
21 /**
22  * Tests {@link org.wamblee.conditions.PropertyRegexCondition}. 
23  *
24  * @author Erik Brakkee
25  */
26 public class PropertyRegexConditionTest extends TestCase {
27   
28     private boolean match(String aProperty, String aRegex, boolean aToLower, TestBean aBean) { 
29         PropertyRegexCondition<TestBean> condition = new PropertyRegexCondition<TestBean>(aProperty, aRegex, aToLower );
30         return condition.matches(aBean);
31     }
32     
33     private void checkMatch(String aProperty, String aRegex, boolean aToLower, TestBean aBean, boolean aResult) { 
34         assertEquals( aResult, match(aProperty, aRegex, aToLower, aBean)); 
35     }
36   
37     /**
38      * Verifies correct matching behavior for several cases. 
39      *
40      */
41     public void testMatchProperty() {
42         TestBean bean = new TestBean("Hallo"); 
43         checkMatch("value", "Hallo", false, bean, true); 
44         checkMatch("value", "all", false, bean, false);
45         checkMatch("value", ".a.*o", false, bean, true);
46         checkMatch("value", "hallo", false, bean, false); // no match when not converting to lower case.
47         checkMatch("value", "hallo", true, bean, true); // match!
48     }
49     
50     /**
51      * Uses property regex condition for non-existing property. 
52      * Verifies that a runtime exception is thrown. 
53      *
54      */
55     public void testWrongProperty() { 
56         TestBean bean = new TestBean("Hallo");
57         try { 
58            match("bla", ".*", false, bean);       
59         } catch (RuntimeException e) { 
60            return; // ok
61         }
62         fail();
63     }
64     
65     /**
66      * Applies condition to a private property. Verifies that a runtime 
67      * exception is thrown. 
68      *
69      */
70     public void testPrivateProperty() { 
71         TestBean bean = new TestBean("Hallo");
72         try { 
73            match("privateValue", ".*", false, bean);       
74         } catch (RuntimeException e) { 
75            return; // ok
76         }
77         fail();
78     }
79 }