33df8e97b9cdd9649a99a60d509482973941db0e
[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 package org.wamblee.conditions;
17
18 import junit.framework.TestCase;
19
20
21 /**
22  * Tests {@link org.wamblee.conditions.PropertyRegexCondition}.
23  *
24  * @author Erik Brakkee
25  */
26 public class PropertyRegexConditionTest extends TestCase {
27     private boolean match(String aProperty, String aRegex, boolean aToLower,
28         TestBean aBean) {
29         PropertyRegexCondition<TestBean> condition = new PropertyRegexCondition<TestBean>(aProperty,
30                 aRegex, aToLower);
31
32         return condition.matches(aBean);
33     }
34
35     private void checkMatch(String aProperty, String aRegex, boolean aToLower,
36         TestBean aBean, boolean aResult) {
37         assertEquals(aResult, match(aProperty, aRegex, aToLower, aBean));
38     }
39
40     /**
41      * Verifies correct matching behavior for several cases.
42      *
43      */
44     public void testMatchProperty() {
45         TestBean bean = new TestBean("Hallo");
46         checkMatch("value", "Hallo", false, bean, true);
47         checkMatch("value", "all", false, bean, false);
48         checkMatch("value", ".a.*o", false, bean, true);
49         checkMatch("value", "hallo", false, bean, false); // no match when not converting to lower case.
50         checkMatch("value", "hallo", true, bean, true); // match!
51     }
52
53     /**
54      * Uses property regex condition for non-existing property.
55      * Verifies that a runtime exception is thrown.
56      *
57      */
58     public void testWrongProperty() {
59         TestBean bean = new TestBean("Hallo");
60
61         try {
62             match("bla", ".*", false, bean);
63         } catch (RuntimeException e) {
64             return; // ok
65         }
66
67         fail();
68     }
69
70     /**
71      * Applies condition to a private property. Verifies that a runtime
72      * exception is thrown.
73      *
74      */
75     public void testPrivateProperty() {
76         TestBean bean = new TestBean("Hallo");
77
78         try {
79             match("privateValue", ".*", false, bean);
80         } catch (RuntimeException e) {
81             return; // ok
82         }
83
84         fail();
85     }
86 }