/
TestMatchingRule.java
132 lines (115 loc) · 5.07 KB
/
TestMatchingRule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (c) 2010-2013 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.prism.match;
import static org.testng.AssertJUnit.assertEquals;
import static com.evolveum.midpoint.prism.PrismInternalTestUtil.DEFAULT_NAMESPACE_PREFIX;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;
import java.io.IOException;
import javax.xml.namespace.QName;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;
import com.evolveum.midpoint.prism.PrismInternalTestUtil;
import com.evolveum.midpoint.prism.match.MatchingRule;
import com.evolveum.midpoint.prism.match.MatchingRuleRegistry;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
/**
* @author semancik
*
*/
public class TestMatchingRule {
private static MatchingRuleRegistry matchingRuleRegistry;
@BeforeSuite
public void setup() throws SchemaException, SAXException, IOException {
PrettyPrinter.setDefaultNamespacePrefix(DEFAULT_NAMESPACE_PREFIX);
PrismTestUtil.resetPrismContext(new PrismInternalTestUtil());
matchingRuleRegistry = MatchingRuleRegistryFactory.createRegistry();
}
@Test
public void testStringDefault() throws Exception {
// GIVEN
MatchingRule<String> rule = matchingRuleRegistry.getMatchingRule(null, DOMUtil.XSD_STRING);
// WHEN, THEN
assertMatch(rule, "foo", "foo");
assertNoMatch(rule, "foo", "bar");
assertNoMatch(rule, "foo", "Foo");
assertNoMatch(rule, "FOO", "Foo");
assertNormalized(rule, "Foo", "Foo");
assertNormalized(rule, "baR", "baR");
}
@Test
public void testStringCaseInsensitive() throws Exception {
// GIVEN
MatchingRule<String> rule = matchingRuleRegistry.getMatchingRule(StringIgnoreCaseMatchingRule.NAME,
DOMUtil.XSD_STRING);
// WHEN, THEN
assertMatch(rule, "foo", "foo");
assertNoMatch(rule, "foo", "bar");
assertMatch(rule, "foo", "Foo");
assertMatch(rule, "FOO", "Foo");
assertNormalized(rule, "bar", "baR");
assertNormalized(rule, "foo", "FoO");
assertNormalized(rule, "foobar", "foobar");
}
@Test
public void testPolyStringStrict() throws Exception {
// GIVEN
MatchingRule<PolyString> rule = matchingRuleRegistry.getMatchingRule(PolyStringStrictMatchingRule.NAME,
PolyStringType.COMPLEX_TYPE);
// WHEN, THEN
assertMatch(rule, new PolyString("Bar", "bar"), new PolyString("Bar", "bar"));
assertNoMatch(rule, new PolyString("BAR", "bar"), new PolyString("Foo", "bar"));
assertNoMatch(rule, new PolyString("Bar", "bar"), new PolyString("bAR", "bar"));
assertNoMatch(rule, new PolyString("Bar", "bar"), new PolyString("Bar", "barbar"));
}
@Test
public void testPolyStringOrig() throws Exception {
// GIVEN
MatchingRule<PolyString> rule = matchingRuleRegistry.getMatchingRule(PolyStringOrigMatchingRule.NAME,
PolyStringType.COMPLEX_TYPE);
// WHEN, THEN
assertMatch(rule, new PolyString("Bar", "bar"), new PolyString("Bar", "bar"));
assertNoMatch(rule, new PolyString("BAR", "bar"), new PolyString("Foo", "bar"));
assertNoMatch(rule, new PolyString("Bar", "bar"), new PolyString("bAR", "bar"));
assertMatch(rule, new PolyString("Bar", "bar"), new PolyString("Bar", "barbar"));
}
@Test
public void testPolyStringNorm() throws Exception {
// GIVEN
MatchingRule<PolyString> rule = matchingRuleRegistry.getMatchingRule(PolyStringNormMatchingRule.NAME,
PolyStringType.COMPLEX_TYPE);
// WHEN, THEN
assertMatch(rule, new PolyString("Bar", "bar"), new PolyString("Bar", "bar"));
assertMatch(rule, new PolyString("BAR", "bar"), new PolyString("Foo", "bar"));
assertMatch(rule, new PolyString("Bar", "bar"), new PolyString("bAR", "bar"));
assertNoMatch(rule, new PolyString("Bar", "bar"), new PolyString("Bar", "barbar"));
}
private <T> void assertMatch(MatchingRule<T> rule, T a, T b) throws SchemaException {
assertTrue("Values '"+a+"' and '"+b+"' does not match; rule: "+rule, rule.match(a, b));
}
private <T> void assertNoMatch(MatchingRule<T> rule, T a, T b) throws SchemaException {
assertFalse("Values '"+a+"' and '"+b+"' DOES match but they should not; rule: "+rule, rule.match(a, b));
}
private void assertNormalized(MatchingRule<String> rule, String expected, String original) throws SchemaException {
assertEquals("Normalized value does not match", expected, rule.normalize(original));
}
}