1 15 package org.apache.tapestry.services.impl; 16 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.List ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.ErrorLog; 23 import org.apache.hivemind.Location; 24 import org.apache.hivemind.test.HiveMindTestCase; 25 26 32 public class TestInfrastructure extends HiveMindTestCase 33 { 34 private static class DeferredObjectFixture implements DeferredObject 35 { 36 private Object _object; 37 38 private Location _location; 39 40 public DeferredObjectFixture(Object object, Location location) 41 { 42 _object = object; 43 _location = location; 44 } 45 46 public Location getLocation() 47 { 48 return _location; 49 } 50 51 public Object getObject() 52 { 53 return _object; 54 } 55 } 56 57 private InfrastructureContribution newContribution(String propertyName, String mode, 58 Object object) 59 { 60 return newContribution(propertyName, mode, object, null); 61 } 62 63 private InfrastructureContribution newContribution(String propertyName, String mode, 64 Object object, Location location) 65 { 66 DeferredObject deferred = new DeferredObjectFixture(object, location); 67 68 InfrastructureContribution ic = new InfrastructureContribution(); 69 ic.setDeferredObject(deferred); 70 ic.setProperty(propertyName); 71 ic.setMode(mode); 72 ic.setLocation(location); 73 74 return ic; 75 } 76 77 public void testGetPropertyUninitialized() 78 { 79 InfrastructureImpl infra = new InfrastructureImpl(); 80 81 try 82 { 83 infra.getProperty("foo"); 84 unreachable(); 85 } 86 catch (IllegalStateException ex) 87 { 88 assertEquals(ImplMessages.infrastructureNotInitialized(), ex.getMessage()); 89 } 90 } 91 92 public void testGetNullProperty() 93 { 94 InfrastructureImpl infra = new InfrastructureImpl(); 95 96 infra.setNormalContributions(Collections.EMPTY_LIST); 97 infra.setOverrideContributions(Collections.EMPTY_LIST); 98 99 infra.initialize("test"); 100 101 try 102 { 103 infra.getProperty("fred"); 104 unreachable(); 105 } 106 catch (ApplicationRuntimeException ex) 107 { 108 assertEquals(ImplMessages.missingInfrastructureProperty("fred"), ex.getMessage()); 109 } 110 } 111 112 public void testReinitalize() 113 { 114 InfrastructureImpl infra = new InfrastructureImpl(); 115 116 infra.setNormalContributions(Collections.EMPTY_LIST); 117 infra.setOverrideContributions(Collections.EMPTY_LIST); 118 119 infra.initialize("ONE"); 120 121 try 122 { 123 infra.initialize("TWO"); 124 unreachable(); 125 } 126 catch (IllegalStateException ex) 127 { 128 assertEquals(ImplMessages.infrastructureAlreadyInitialized("TWO", "ONE"), ex 129 .getMessage()); 130 } 131 } 132 133 137 138 public void testModeOverridesNonMode() 139 { 140 Object fredModal = new Object (); 141 Object plainFred = new Object (); 142 143 InfrastructureImpl infra = new InfrastructureImpl(); 144 145 List l = new ArrayList (); 146 l.add(newContribution("fred", "bedrock", fredModal)); 147 l.add(newContribution("fred", null, plainFred)); 148 149 infra.setNormalContributions(l); 150 infra.setOverrideContributions(Collections.EMPTY_LIST); 151 152 infra.initialize("bedrock"); 153 154 assertSame(fredModal, infra.getProperty("fred")); 155 } 156 157 public void testWrongModeIgnored() 158 { 159 Object fredModal = new Object (); 160 Object wrongFred = new Object (); 161 162 InfrastructureImpl infra = new InfrastructureImpl(); 163 164 List l = new ArrayList (); 165 l.add(newContribution("fred", "bedrock", fredModal)); 166 l.add(newContribution("fred", "shale", wrongFred)); 167 168 infra.setNormalContributions(l); 169 infra.setOverrideContributions(Collections.EMPTY_LIST); 170 171 infra.initialize("bedrock"); 172 173 assertSame(fredModal, infra.getProperty("fred")); 174 } 175 176 179 180 public void testOverrides() 181 { 182 Object normalFred = new Object (); 183 Object overrideFred = new Object (); 184 185 InfrastructureImpl infra = new InfrastructureImpl(); 186 187 infra.setNormalContributions(Collections.singletonList(newContribution( 188 "fred", 189 null, 190 normalFred))); 191 infra.setOverrideContributions(Collections.singletonList(newContribution( 192 "fred", 193 null, 194 overrideFred))); 195 196 infra.initialize("bedrock"); 197 198 assertSame(overrideFred, infra.getProperty("fred")); 199 } 200 201 public void testDuplicate() 202 { 203 ErrorLog log = (ErrorLog) newMock(ErrorLog.class); 204 205 Location l1 = fabricateLocation(99); 206 Location l2 = fabricateLocation(132); 207 208 Object fredModal = new Object (); 209 Object duplicateFred = new Object (); 210 211 List l = new ArrayList (); 212 l.add(newContribution("fred", "bedrock", fredModal, l1)); 213 InfrastructureContribution conflict = newContribution("fred", "bedrock", duplicateFred, l2); 214 l.add(conflict); 215 216 log.error(ImplMessages.duplicateInfrastructureContribution(conflict, l1), l2, null); 217 218 replayControls(); 219 220 InfrastructureImpl infra = new InfrastructureImpl(); 221 infra.setNormalContributions(l); 222 infra.setOverrideContributions(Collections.EMPTY_LIST); 223 infra.setErrorLog(log); 224 225 infra.initialize("bedrock"); 226 227 assertSame(fredModal, infra.getProperty("fred")); 228 229 verifyControls(); 230 } 231 } | Popular Tags |