| 1 | package com.hammurapi.common.extract.tests; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertNotNull; |
| 6 | import static org.junit.Assert.assertTrue; |
| 7 | |
| 8 | import java.math.BigDecimal; |
| 9 | import java.util.HashMap; |
| 10 | import java.util.Map; |
| 11 | import java.util.concurrent.TimeUnit; |
| 12 | |
| 13 | import org.junit.Test; |
| 14 | |
| 15 | import com.hammurapi.extract.AbstractExtractor; |
| 16 | import com.hammurapi.extract.ComparisonResult; |
| 17 | import com.hammurapi.extract.ComparisonResult.Type; |
| 18 | import com.hammurapi.extract.Constant; |
| 19 | import com.hammurapi.extract.Equal; |
| 20 | import com.hammurapi.extract.Extractor; |
| 21 | import com.hammurapi.extract.ExtractorFactory; |
| 22 | import com.hammurapi.extract.IndexedExtractor; |
| 23 | import com.hammurapi.extract.InstanceOfPredicate; |
| 24 | import com.hammurapi.extract.MappedPredicate; |
| 25 | import com.hammurapi.extract.Predicate; |
| 26 | import com.hammurapi.store.local.LocalStore; |
| 27 | |
| 28 | public class ExtractorTests { |
| 29 | |
| 30 | @Test |
| 31 | public void testExtract() { |
| 32 | Account account = new Account(); |
| 33 | account.setNumber(123); |
| 34 | AccountNumberExtractor accountNumberExtractor = new AccountNumberExtractor(); |
| 35 | Map<LocalStore<Account, Integer>, Map<Extractor<Account, ? super Integer, LocalStore<Account, Integer>>, ? super Integer>> cache = new HashMap<LocalStore<Account,Integer>, Map<Extractor<Account,? super Integer,LocalStore<Account,Integer>>,? super Integer>>(); |
| 36 | Integer accountNumber = accountNumberExtractor.extract(null, cache, account); |
| 37 | assertEquals(accountNumber.intValue(), 123); |
| 38 | } |
| 39 | |
| 40 | public static class Calculator { |
| 41 | |
| 42 | int addCounter; |
| 43 | int multiplyCounter; |
| 44 | |
| 45 | int add(int a, int b) { |
| 46 | ++addCounter; |
| 47 | return a+b; |
| 48 | } |
| 49 | |
| 50 | int multiply(int a, int b) { |
| 51 | ++multiplyCounter; |
| 52 | return a*b; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public static class AddExtractor extends AbstractExtractor<Calculator, Integer, Object> { |
| 57 | |
| 58 | private int a; |
| 59 | private int b; |
| 60 | |
| 61 | public AddExtractor(int a, int b) { |
| 62 | super(0, null, false, 0); |
| 63 | this.a = a; |
| 64 | this.b = b; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected Integer extractInternal( |
| 69 | Object context, |
| 70 | Map<Object, Map<Extractor<Calculator, ? super Integer, Object>, ? super Integer>> cache, |
| 71 | Calculator... obj) { |
| 72 | return obj[0].add(a,b); |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | |
| 77 | public static class MultiplyExtractor extends AbstractExtractor<Calculator, Integer, Object> { |
| 78 | |
| 79 | private int a; |
| 80 | private int b; |
| 81 | |
| 82 | public MultiplyExtractor(int a, int b) { |
| 83 | super(0, null, false, 0); |
| 84 | this.a = a; |
| 85 | this.b = b; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected Integer extractInternal( |
| 90 | Object context, |
| 91 | Map<Object, Map<Extractor<Calculator, ? super Integer, Object>, ? super Integer>> cache, |
| 92 | Calculator... obj) { |
| 93 | return obj[0].multiply(a,b); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testCaching() { |
| 100 | Calculator calculator = new Calculator(); |
| 101 | Map<Object, Map<Extractor<Calculator, ? super Integer, Object>, ? super Integer>> cache = new HashMap<Object, Map<Extractor<Calculator,? super Integer,Object>,? super Integer>>(); |
| 102 | |
| 103 | // Extracting 5+6 |
| 104 | AddExtractor addExtractor = new AddExtractor(5, 6); |
| 105 | Integer addResult = addExtractor.extract(null, cache, calculator); |
| 106 | assertEquals(5+6, addResult.intValue()); |
| 107 | assertEquals(1, calculator.addCounter); |
| 108 | |
| 109 | // Extracting 33+88 |
| 110 | AddExtractor addExtractor2 = new AddExtractor(33, 88); |
| 111 | Integer addResult2 = addExtractor2.extract(null, cache, calculator); |
| 112 | assertEquals(33+88, addResult2.intValue()); |
| 113 | assertEquals(2, calculator.addCounter); |
| 114 | |
| 115 | // Extracting 5+6, should come from cache. |
| 116 | addResult = addExtractor.extract(null, cache, calculator); |
| 117 | assertEquals(5+6, addResult.intValue()); |
| 118 | assertEquals(2, calculator.addCounter); |
| 119 | |
| 120 | // Extracting 33+88 |
| 121 | MultiplyExtractor multiplyExtractor = new MultiplyExtractor(33, 88); |
| 122 | Integer multiplyResult = multiplyExtractor.extract(null, cache, calculator); |
| 123 | assertEquals(33*88, multiplyResult.intValue()); |
| 124 | assertEquals(1, calculator.multiplyCounter); |
| 125 | |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testEquality() { |
| 130 | Predicate<Account, LocalStore<Account,Integer>> predicate = ExtractorFactory.INSTANCE.createPredicate( |
| 131 | "javascript", |
| 132 | "account.getNumber()!=123", |
| 133 | new String[] {"account"}, |
| 134 | new Class[] {Account.class}, |
| 135 | Object.class, |
| 136 | this.getClass().getClassLoader()); |
| 137 | |
| 138 | Predicate<Account, LocalStore<Account,Integer>> predicate2 = ExtractorFactory.INSTANCE.createPredicate( |
| 139 | "javascript", |
| 140 | "account.getNumber()!=123", |
| 141 | new String[] {"account"}, |
| 142 | new Class[] {Account.class}, |
| 143 | Object.class, |
| 144 | this.getClass().getClassLoader()); |
| 145 | |
| 146 | assertEquals(predicate, predicate2); |
| 147 | |
| 148 | Predicate<Account, LocalStore<Account,Integer>> predicate3 = ExtractorFactory.INSTANCE.createPredicate( |
| 149 | "javascript", |
| 150 | "account.getNumber() != 123", |
| 151 | new String[] {"account"}, |
| 152 | new Class[] {Account.class}, |
| 153 | Object.class, |
| 154 | this.getClass().getClassLoader()); |
| 155 | |
| 156 | assertFalse(predicate3.equals(predicate)); |
| 157 | |
| 158 | Predicate<Account, LocalStore<Account,Integer>> predicate4 = ExtractorFactory.INSTANCE.createPredicate( |
| 159 | "javascript", |
| 160 | "account.getNumber()!=123", |
| 161 | new String[] {"balance"}, |
| 162 | new Class[] {Account.class}, |
| 163 | Object.class, |
| 164 | this.getClass().getClassLoader()); |
| 165 | |
| 166 | assertFalse(predicate4.equals(predicate)); |
| 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void testCost() { |
| 171 | Account account = new Account(); |
| 172 | account.setNumber(123); |
| 173 | account.setBalance(new BigDecimal(3.14159)); |
| 174 | Extractor<Account, BigDecimal, LocalStore<Account, Integer>> balanceExtractor = new AbstractExtractor<Account, BigDecimal, LocalStore<Account, Integer>>(0, TimeUnit.MICROSECONDS, false, 0) { |
| 175 | |
| 176 | @Override |
| 177 | protected BigDecimal extractInternal( |
| 178 | LocalStore<Account, Integer> context, |
| 179 | Map<LocalStore<Account, Integer>, Map<Extractor<Account, ? super BigDecimal, LocalStore<Account, Integer>>, ? super BigDecimal>> cache, |
| 180 | Account... obj) { |
| 181 | return obj[0].getBalance(); |
| 182 | } |
| 183 | |
| 184 | }; |
| 185 | BigDecimal balance = balanceExtractor.extract(null, null, account); |
| 186 | // Verify that extractor cost is 100000 (100 milliseconds) +- 10 millisecond. |
| 187 | assertEquals(100000, balanceExtractor.getCost(), 20000); |
| 188 | assertEquals(new BigDecimal(3.14159), balance); |
| 189 | } |
| 190 | |
| 191 | @Test |
| 192 | public void testMappedPredicate() { |
| 193 | Predicate<Object, Object> p1 = MappedPredicate.mapPredicate(new InstanceOfPredicate<Object, Object>(new IndexedExtractor<Object, Object>(3), String.class), new int[] {1, 10, 3}); |
| 194 | Predicate<Object, Object> p2 = MappedPredicate.mapPredicate(new InstanceOfPredicate<Object, Object>(new IndexedExtractor<Object, Object>(3), String.class), new int[] {1, 10, 3}); |
| 195 | ComparisonResult cr = p1.compareTo(p2); |
| 196 | |
| 197 | assertNotNull(cr); |
| 198 | assertEquals(Type.EQUAL, cr.getType()); |
| 199 | assertTrue(cr.isOneToOneMapping()); |
| 200 | } |
| 201 | |
| 202 | enum TestEnum { |
| 203 | ONE, TWO |
| 204 | } |
| 205 | |
| 206 | @Test |
| 207 | public void testEnumEquals() { |
| 208 | Constant c1 = new Constant(TestEnum.ONE); |
| 209 | Constant c2 = new Constant(TestEnum.TWO); |
| 210 | Constant c3 = new Constant(null); |
| 211 | |
| 212 | Equal equal = new Equal(0, null, c1, c2, true); |
| 213 | equal.extract(null, null, null); |
| 214 | |
| 215 | equal = new Equal(0, null, c1, c2, false); |
| 216 | equal.extract(null, null, null); |
| 217 | |
| 218 | equal = new Equal(0, null, c1, c1, true); |
| 219 | equal.extract(null, null, null); |
| 220 | |
| 221 | equal = new Equal(0, null, c1, c2, false); |
| 222 | equal.extract(null, null, null); |
| 223 | |
| 224 | equal = new Equal(0, null, c3, c2, true); |
| 225 | equal.extract(null, null, null); |
| 226 | |
| 227 | equal = new Equal(0, null, c3, c2, false); |
| 228 | equal.extract(null, null, null); |
| 229 | |
| 230 | equal = new Equal(0, null, c3, c3, true); |
| 231 | equal.extract(null, null, null); |
| 232 | |
| 233 | equal = new Equal(0, null, c3, c3, false); |
| 234 | equal.extract(null, null, null); |
| 235 | |
| 236 | } |
| 237 | |
| 238 | } |