1 | package com.hammurapi.extract.java.test; |
2 | |
3 | import static org.junit.Assert.assertEquals; |
4 | import static org.junit.Assert.assertTrue; |
5 | |
6 | import java.math.BigDecimal; |
7 | |
8 | import org.junit.Test; |
9 | |
10 | import com.hammurapi.common.extract.tests.Account; |
11 | import com.hammurapi.extract.And; |
12 | import com.hammurapi.extract.CommutativeAnd; |
13 | import com.hammurapi.extract.CommutativeOr; |
14 | import com.hammurapi.extract.ComparisonResult; |
15 | import com.hammurapi.extract.Extractor; |
16 | import com.hammurapi.extract.ExtractorFactory; |
17 | import com.hammurapi.extract.GreaterThan; |
18 | import com.hammurapi.extract.LessEqual; |
19 | import com.hammurapi.extract.LessThan; |
20 | import com.hammurapi.extract.java.JavaExtractor; |
21 | import com.hammurapi.store.local.LocalStore; |
22 | |
23 | public class JavaExtractorTests { |
24 | |
25 | @Test |
26 | public void testEquality() { |
27 | Extractor<Account,BigDecimal, LocalStore<Account,Integer>> extractor1 = ExtractorFactory.INSTANCE.createExtractor( |
28 | "java", |
29 | "x + y", |
30 | new String[] {"x", "y"}, |
31 | new Class[] {Integer.class, Integer.class}, |
32 | Integer.class, |
33 | Object.class, |
34 | this.getClass().getClassLoader()); |
35 | |
36 | Extractor<Account,BigDecimal, LocalStore<Account,Integer>> extractor2 = ExtractorFactory.INSTANCE.createExtractor( |
37 | "java", |
38 | "x+y /* addition */", |
39 | new String[] {"x", "y"}, |
40 | new Class[] {Integer.class, Integer.class}, |
41 | Integer.class, |
42 | Object.class, |
43 | this.getClass().getClassLoader()); |
44 | |
45 | assertEquals(extractor1, extractor2); |
46 | |
47 | ComparisonResult cr = extractor1.compareTo(extractor2); |
48 | assertEquals(ComparisonResult.Type.EQUAL, cr.getType()); |
49 | assertTrue(cr.isOneToOneMapping()); |
50 | } |
51 | |
52 | @Test |
53 | public void testParameterNormalization() { |
54 | Extractor<Account,BigDecimal, LocalStore<Account,Integer>> extractor1 = ExtractorFactory.INSTANCE.createExtractor( |
55 | "java", |
56 | "a + b", |
57 | new String[] {"a", "b"}, |
58 | new Class[] {Integer.class, Integer.class}, |
59 | Integer.class, |
60 | Object.class, |
61 | this.getClass().getClassLoader()); |
62 | |
63 | Extractor<Account,BigDecimal, LocalStore<Account,Integer>> extractor2 = ExtractorFactory.INSTANCE.createExtractor( |
64 | "java", |
65 | "x+y", |
66 | new String[] {"x", "y"}, |
67 | new Class[] {Integer.class, Integer.class}, |
68 | Integer.class, |
69 | Object.class, |
70 | this.getClass().getClassLoader()); |
71 | |
72 | assertEquals(extractor1, extractor2); |
73 | |
74 | ComparisonResult cr = extractor1.compareTo(extractor2); |
75 | assertEquals(ComparisonResult.Type.EQUAL, cr.getType()); |
76 | assertTrue(cr.isOneToOneMapping()); |
77 | } |
78 | |
79 | @Test |
80 | public void testMacros() { |
81 | Extractor<Account,BigDecimal, LocalStore<Account,Integer>> extractor1 = ExtractorFactory.INSTANCE.createExtractor( |
82 | "java", |
83 | "OR(a>0, b<33)", |
84 | new String[] {"a", "b"}, |
85 | new Class[] {Integer.class, Integer.class}, |
86 | Integer.class, |
87 | Object.class, |
88 | this.getClass().getClassLoader()); |
89 | |
90 | Extractor<Account,BigDecimal, LocalStore<Account,Integer>> extractor2 = ExtractorFactory.INSTANCE.createExtractor( |
91 | "java", |
92 | "AND(x<=88, y>10, z<20)", |
93 | new String[] {"x", "y", "z"}, |
94 | new Class[] {Integer.class, Integer.class, Integer.class}, |
95 | Integer.class, |
96 | Object.class, |
97 | this.getClass().getClassLoader()); |
98 | |
99 | assertTrue(extractor1 instanceof CommutativeOr); |
100 | assertTrue(extractor2 instanceof CommutativeAnd); |
101 | } |
102 | |
103 | @Test |
104 | public void testCostMacro() { |
105 | Extractor<Account,BigDecimal, LocalStore<Account,Integer>> extractor1 = ExtractorFactory.INSTANCE.createExtractor( |
106 | "java", |
107 | "COST(2.5*33, OR(a>0, b<33))", |
108 | new String[] {"a", "b"}, |
109 | new Class[] {Integer.class, Integer.class}, |
110 | Integer.class, |
111 | Object.class, |
112 | this.getClass().getClassLoader()); |
113 | |
114 | assertTrue(extractor1 instanceof CommutativeOr); |
115 | assertEquals(2.5*33, extractor1.getCost(), 0.001); |
116 | |
117 | Extractor<Account,BigDecimal, LocalStore<Account,Integer>> extractor2 = ExtractorFactory.INSTANCE.createExtractor( |
118 | "java", |
119 | "COST(MILLISECONDS, AND(x<=88, y>10, z<20))", |
120 | new String[] {"x", "y", "z"}, |
121 | new Class[] {Integer.class, Integer.class, Integer.class}, |
122 | Integer.class, |
123 | Object.class, |
124 | this.getClass().getClassLoader()); |
125 | |
126 | |
127 | assertTrue(extractor2 instanceof CommutativeAnd); |
128 | assertEquals(0, extractor2.getCost(), 0.001); |
129 | } |
130 | |
131 | @SuppressWarnings("rawtypes") |
132 | @Test |
133 | public void testComparisonPredicates() { |
134 | |
135 | @SuppressWarnings("unchecked") |
136 | Extractor<Account,BigDecimal, LocalStore<Account,Integer>> extractor = ExtractorFactory.INSTANCE.createExtractor( |
137 | "java", |
138 | "x<=88 && y>10 && z<20", |
139 | new String[] {"x", "y", "z"}, |
140 | new Class[] {Integer.class, Integer.class, Integer.class}, |
141 | Integer.class, |
142 | Object.class, |
143 | this.getClass().getClassLoader()); |
144 | |
145 | assertTrue(((And) extractor).getParts().get(0) instanceof LessEqual); |
146 | assertTrue(((And) extractor).getParts().get(1) instanceof GreaterThan); |
147 | assertTrue(((And) extractor).getParts().get(2) instanceof LessThan); |
148 | } |
149 | |
150 | @Test |
151 | public void testCompilation() { |
152 | |
153 | @SuppressWarnings("unchecked") |
154 | Extractor<Account,BigDecimal, LocalStore<Account,Integer>> extractor = ExtractorFactory.INSTANCE.createExtractor( |
155 | "java", |
156 | "x.getBalance(y+z)", |
157 | new String[] {"x", "y", "z"}, |
158 | new Class[] {Integer.class, Integer.class, Integer.class}, |
159 | Integer.class, |
160 | Object.class, |
161 | this.getClass().getClassLoader()); |
162 | |
163 | @SuppressWarnings("rawtypes") |
164 | Object state = ((JavaExtractor) extractor).getIdentity(); |
165 | System.out.println(state); |
166 | |
167 | @SuppressWarnings("unchecked") |
168 | Extractor<Account,BigDecimal, LocalStore<Account,Integer>> extractor2 = ExtractorFactory.INSTANCE.createExtractor( |
169 | "java", |
170 | "a.getBalance(b+c)", |
171 | new String[] {"a", "b", "c"}, |
172 | new Class[] {Integer.class, Integer.class, Integer.class}, |
173 | Integer.class, |
174 | Object.class, |
175 | this.getClass().getClassLoader()); |
176 | |
177 | assertEquals(state, ((JavaExtractor) extractor2).getIdentity()); |
178 | } |
179 | |
180 | } |