1 15 package org.apache.tapestry.services.impl; 16 17 import java.util.Collections ; 18 import java.util.List ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.hivemind.Location; 22 import org.apache.hivemind.ServiceImplementationFactoryParameters; 23 import org.apache.hivemind.lib.DefaultImplementationBuilder; 24 import org.apache.hivemind.test.HiveMindTestCase; 25 import org.apache.tapestry.spec.IApplicationSpecification; 26 import org.easymock.MockControl; 27 28 34 public class TestExtensionLookupFactory extends HiveMindTestCase 35 { 36 private List createParameters(String extensionName) 37 { 38 return createParameters(extensionName, null); 39 } 40 41 private List createParameters(String extensionName, Object defaultValue) 42 { 43 ExtensionLookupParameter p = new ExtensionLookupParameter(); 44 45 p.setExtensionName(extensionName); 46 p.setDefault(defaultValue); 47 48 return Collections.singletonList(p); 49 } 50 51 public void testInSpecification() 52 { 53 MockControl specControl = newControl(IApplicationSpecification.class); 54 IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock(); 55 56 Runnable r = (Runnable ) newMock(Runnable .class); 57 58 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 59 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 60 .getMock(); 61 62 64 fp.getParameters(); 65 fpc.setReturnValue(createParameters("foo.bar")); 66 67 fp.getServiceInterface(); 68 fpc.setReturnValue(Runnable .class); 69 70 spec.checkExtension("foo.bar"); 71 specControl.setReturnValue(true); 72 73 spec.getExtension("foo.bar", Runnable .class); 74 specControl.setReturnValue(r); 75 76 replayControls(); 77 78 ExtensionLookupFactory f = new ExtensionLookupFactory(); 79 f.setSpecification(spec); 80 81 Object actual = f.createCoreServiceImplementation(fp); 82 83 assertSame(r, actual); 84 85 verifyControls(); 86 } 87 88 public void testSyntheticDefault() 89 { 90 MockControl specControl = newControl(IApplicationSpecification.class); 91 IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock(); 92 93 MockControl dibControl = newControl(DefaultImplementationBuilder.class); 94 DefaultImplementationBuilder dib = (DefaultImplementationBuilder) dibControl.getMock(); 95 96 Runnable r = (Runnable ) newMock(Runnable .class); 97 98 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 99 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 100 .getMock(); 101 102 104 fp.getParameters(); 105 fpc.setReturnValue(createParameters("foo.bar")); 106 107 fp.getServiceInterface(); 108 fpc.setReturnValue(Runnable .class); 109 110 spec.checkExtension("foo.bar"); 111 specControl.setReturnValue(false); 112 113 dib.buildDefaultImplementation(Runnable .class); 114 dibControl.setReturnValue(r); 115 116 replayControls(); 117 118 ExtensionLookupFactory f = new ExtensionLookupFactory(); 119 f.setSpecification(spec); 120 f.setDefaultBuilder(dib); 121 122 Object actual = f.createCoreServiceImplementation(fp); 123 124 assertSame(r, actual); 125 126 verifyControls(); 127 } 128 129 public void testConfigurationDefault() 130 { 131 MockControl specControl = newControl(IApplicationSpecification.class); 132 IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock(); 133 134 Runnable r = (Runnable ) newMock(Runnable .class); 135 136 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 137 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 138 .getMock(); 139 140 142 fp.getParameters(); 143 fpc.setReturnValue(createParameters("foo.bar", r)); 144 145 fp.getServiceInterface(); 146 fpc.setReturnValue(Runnable .class); 147 148 spec.checkExtension("foo.bar"); 149 specControl.setReturnValue(false); 150 151 replayControls(); 152 153 ExtensionLookupFactory f = new ExtensionLookupFactory(); 154 f.setSpecification(spec); 155 156 Object actual = f.createCoreServiceImplementation(fp); 157 158 assertSame(r, actual); 159 160 verifyControls(); 161 } 162 163 public void testFailure() 164 { 165 Location l = fabricateLocation(264); 166 ExtensionLookupParameter p = new ExtensionLookupParameter(); 167 168 p.setLocation(l); 169 p.setExtensionName("gnip.gnop"); 170 171 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 172 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 173 .getMock(); 174 175 fp.getParameters(); 176 fpc.setReturnValue(Collections.singletonList(p)); 177 178 fp.getServiceInterface(); 179 fpc.setReturnValue(null); 180 181 ExtensionLookupFactory f = new ExtensionLookupFactory(); 182 183 replayControls(); 184 185 try 186 { 187 f.createCoreServiceImplementation(fp); 188 189 unreachable(); 190 } 191 catch (ApplicationRuntimeException ex) 192 { 193 assertSame(l, ex.getLocation()); 194 } 195 196 verifyControls(); 197 } 198 } | Popular Tags |