1 15 package org.apache.hivemind.lib.util; 16 17 import java.io.Serializable ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.apache.hivemind.lib.util.StrategyRegistry; 22 import org.apache.hivemind.lib.util.StrategyRegistryImpl; 23 import org.apache.hivemind.test.HiveMindTestCase; 24 25 31 32 public class TestStrategyRegistry extends HiveMindTestCase 33 { 34 35 private StrategyRegistry build() 36 { 37 StrategyRegistry result = new StrategyRegistryImpl(); 38 39 result.register(Object .class, "OBJECT"); 40 result.register(Object [].class, "OBJECT[]"); 41 result.register(String .class, "STRING"); 42 result.register(List .class, "LIST"); 43 result.register(Map .class, "MAP"); 44 result.register(Serializable .class, "SERIALIZABLE"); 45 result.register(int[].class, "INT[]"); 46 result.register(double.class, "DOUBLE"); 47 result.register(Number [].class, "NUMBER[]"); 48 49 return result; 50 } 51 52 private void expect(String expected, Class subjectClass) 53 { 54 Object actual = build().getStrategy(subjectClass); 55 56 assertEquals(expected, actual); 57 } 58 59 public void testDefaultMatch() 60 { 61 expect("OBJECT", TestStrategyRegistry.class); 62 } 63 64 public void testClassBeforeInterface() 65 { 66 expect("STRING", String .class); 67 } 68 69 public void testInterfaceMatch() 70 { 71 expect("SERIALIZABLE", Boolean .class); 72 } 73 74 public void testObjectArrayMatch() 75 { 76 expect("OBJECT[]", Object [].class); 77 } 78 79 public void testObjectSubclassArray() 80 { 81 expect("OBJECT[]", String [].class); 82 } 83 84 public void testRegisteredSubclassArray() 85 { 86 expect("NUMBER[]", Number [].class); 87 } 88 89 public void testScalarArrayMatch() 90 { 91 expect("INT[]", int[].class); 92 } 93 94 public void testScalarArrayDefault() 95 { 96 98 expect("SERIALIZABLE", short[].class); 99 } 100 101 public void testScalar() 102 { 103 expect("DOUBLE", double.class); 104 } 105 106 public void testScalarDefault() 107 { 108 expect("OBJECT", float.class); 109 } 110 111 public void testSearchNoInterfaces() 112 { 113 expect("OBJECT", Object .class); 114 } 115 116 public void testNoMatch() 117 { 118 StrategyRegistry r = new StrategyRegistryImpl(); 119 120 r.register(String .class, "STRING"); 121 122 try 123 { 124 r.getStrategy(Boolean .class); 125 126 unreachable(); 127 } 128 catch (IllegalArgumentException ex) 129 { 130 assertEquals(UtilMessages.strategyNotFound(Boolean .class), ex.getMessage()); 131 } 132 } 133 134 public void testToString() 135 { 136 StrategyRegistry r = new StrategyRegistryImpl(); 137 138 r.register(String .class, "STRING"); 139 140 assertEquals("AdaptorRegistry[java.lang.String=STRING]", r.toString()); 141 } 142 143 public void testDuplicateRegistration() 144 { 145 StrategyRegistry r = new StrategyRegistryImpl(); 146 147 r.register(String .class, "STRING"); 148 149 try 150 { 151 r.register(String .class, "STRING2"); 152 153 unreachable(); 154 } 155 catch (IllegalArgumentException ex) 156 { 157 assertEquals(UtilMessages.duplicateRegistration(String .class), ex.getMessage()); 158 } 159 } 160 } | Popular Tags |