001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.services.impl; 016 017 import java.util.ArrayList; 018 import java.util.Collections; 019 import java.util.List; 020 021 import org.apache.hivemind.ApplicationRuntimeException; 022 import org.apache.hivemind.ErrorLog; 023 import org.apache.hivemind.Location; 024 import org.apache.tapestry.BaseComponentTestCase; 025 import org.testng.annotations.Test; 026 027 /** 028 * Tests for {@link org.apache.tapestry.services.impl.InfrastructureImpl}. 029 * 030 * @author Howard M. Lewis Ship 031 * @since 4.0 032 */ 033 @Test 034 public class TestInfrastructure extends BaseComponentTestCase 035 { 036 private static class DeferredObjectFixture implements DeferredObject 037 { 038 private Object _object; 039 040 private Location _location; 041 042 public DeferredObjectFixture(Object object, Location location) 043 { 044 _object = object; 045 _location = location; 046 } 047 048 public Location getLocation() 049 { 050 return _location; 051 } 052 053 public Object getObject() 054 { 055 return _object; 056 } 057 } 058 059 private InfrastructureContribution newContribution(String propertyName, String mode, 060 Object object) 061 { 062 return newContribution(propertyName, mode, object, null); 063 } 064 065 private InfrastructureContribution newContribution(String propertyName, String mode, 066 Object object, Location location) 067 { 068 DeferredObject deferred = new DeferredObjectFixture(object, location); 069 070 InfrastructureContribution ic = new InfrastructureContribution(); 071 ic.setDeferredObject(deferred); 072 ic.setProperty(propertyName); 073 ic.setMode(mode); 074 ic.setLocation(location); 075 076 return ic; 077 } 078 079 public void testGetPropertyUninitialized() 080 { 081 InfrastructureImpl infra = new InfrastructureImpl(); 082 083 try 084 { 085 infra.getProperty("foo"); 086 unreachable(); 087 } 088 catch (IllegalStateException ex) 089 { 090 assertEquals(ImplMessages.infrastructureNotInitialized(), ex.getMessage()); 091 } 092 } 093 094 public void testGetNullProperty() 095 { 096 InfrastructureImpl infra = new InfrastructureImpl(); 097 098 infra.setNormalContributions(Collections.EMPTY_LIST); 099 infra.setOverrideContributions(Collections.EMPTY_LIST); 100 101 infra.initialize("test"); 102 103 try 104 { 105 infra.getProperty("fred"); 106 unreachable(); 107 } 108 catch (ApplicationRuntimeException ex) 109 { 110 assertEquals(ImplMessages.missingInfrastructureProperty("fred"), ex.getMessage()); 111 } 112 } 113 114 public void testReinitalize() 115 { 116 InfrastructureImpl infra = new InfrastructureImpl(); 117 118 infra.setNormalContributions(Collections.EMPTY_LIST); 119 infra.setOverrideContributions(Collections.EMPTY_LIST); 120 121 infra.initialize("ONE"); 122 123 try 124 { 125 infra.initialize("TWO"); 126 unreachable(); 127 } 128 catch (IllegalStateException ex) 129 { 130 assertEquals(ImplMessages.infrastructureAlreadyInitialized("TWO", "ONE"), ex 131 .getMessage()); 132 } 133 } 134 135 /** 136 * Test that a contribution for a mode quietly overrides a contribution for the same property 137 * that does not specify a mode. 138 */ 139 140 public void testModeOverridesNonMode() 141 { 142 Object fredModal = new Object(); 143 Object plainFred = new Object(); 144 145 InfrastructureImpl infra = new InfrastructureImpl(); 146 147 List l = new ArrayList(); 148 l.add(newContribution("fred", "bedrock", fredModal)); 149 l.add(newContribution("fred", null, plainFred)); 150 151 infra.setNormalContributions(l); 152 infra.setOverrideContributions(Collections.EMPTY_LIST); 153 154 infra.initialize("bedrock"); 155 156 assertSame(fredModal, infra.getProperty("fred")); 157 } 158 159 public void testWrongModeIgnored() 160 { 161 Object fredModal = new Object(); 162 Object wrongFred = new Object(); 163 164 InfrastructureImpl infra = new InfrastructureImpl(); 165 166 List l = new ArrayList(); 167 l.add(newContribution("fred", "bedrock", fredModal)); 168 l.add(newContribution("fred", "shale", wrongFred)); 169 170 infra.setNormalContributions(l); 171 infra.setOverrideContributions(Collections.EMPTY_LIST); 172 173 infra.initialize("bedrock"); 174 175 assertSame(fredModal, infra.getProperty("fred")); 176 } 177 178 /** 179 * Test that override contributions trump contributions from the normal path. 180 */ 181 182 public void testOverrides() 183 { 184 Object normalFred = new Object(); 185 Object overrideFred = new Object(); 186 187 InfrastructureImpl infra = new InfrastructureImpl(); 188 189 infra.setNormalContributions(Collections.singletonList(newContribution( 190 "fred", 191 null, 192 normalFred))); 193 infra.setOverrideContributions(Collections.singletonList(newContribution( 194 "fred", 195 null, 196 overrideFred))); 197 198 infra.initialize("bedrock"); 199 200 assertSame(overrideFred, infra.getProperty("fred")); 201 } 202 203 public void testDuplicate() 204 { 205 ErrorLog log = newMock(ErrorLog.class); 206 207 Location l1 = fabricateLocation(99); 208 Location l2 = fabricateLocation(132); 209 210 Object fredModal = new Object(); 211 Object duplicateFred = new Object(); 212 213 List l = new ArrayList(); 214 l.add(newContribution("fred", "bedrock", fredModal, l1)); 215 InfrastructureContribution conflict = newContribution("fred", "bedrock", duplicateFred, l2); 216 l.add(conflict); 217 218 log.error(ImplMessages.duplicateInfrastructureContribution(conflict, l1), l2, null); 219 220 replay(); 221 222 InfrastructureImpl infra = new InfrastructureImpl(); 223 infra.setNormalContributions(l); 224 infra.setOverrideContributions(Collections.EMPTY_LIST); 225 infra.setErrorLog(log); 226 227 infra.initialize("bedrock"); 228 229 assertSame(fredModal, infra.getProperty("fred")); 230 231 verify(); 232 } 233 }