1 15 package org.apache.hivemind.impl; 16 17 import hivemind.test.FrameworkTestCase; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.hivemind.Element; 22 import org.apache.hivemind.ErrorHandler; 23 import org.apache.hivemind.Location; 24 import org.apache.hivemind.definition.ConfigurationParserDefinition; 25 import org.apache.hivemind.definition.ConfigurationPointDefinition; 26 import org.apache.hivemind.definition.RegistryDefinition; 27 import org.apache.hivemind.definition.Visibility; 28 import org.apache.hivemind.definition.impl.RegistryDefinitionImpl; 29 import org.apache.hivemind.parse.ConfigurationPointDescriptor; 30 import org.apache.hivemind.parse.ContributionDescriptor; 31 import org.apache.hivemind.parse.ModuleDescriptor; 32 import org.apache.hivemind.schema.impl.SchemaImpl; 33 import org.apache.hivemind.test.AggregateArgumentsMatcher; 34 import org.apache.hivemind.test.ArgumentMatcher; 35 import org.apache.hivemind.test.TypeMatcher; 36 import org.apache.hivemind.xml.definition.impl.HiveMindSchemaParser; 37 import org.apache.hivemind.xml.definition.impl.HiveMindSchemaParserConstructor; 38 import org.easymock.MockControl; 39 40 46 public class TestXmlModuleDescriptorProcessor extends FrameworkTestCase 47 { 48 52 public void testSchemaResolving() 53 { 54 SchemaImpl schema = new SchemaImpl("module"); 55 schema.setId("Baz"); 56 57 DefaultErrorHandler errorHandler = new DefaultErrorHandler(); 58 RegistryDefinition definition = new RegistryDefinitionImpl(); 59 60 ModuleDescriptor fooBar = new ModuleDescriptor(null, errorHandler); 61 fooBar.setModuleId("foo.bar"); 62 63 fooBar.addSchema(schema); 64 65 ModuleDescriptor zipZoop = new ModuleDescriptor(null, errorHandler); 66 zipZoop.setModuleId("zip.zoop"); 67 68 ConfigurationPointDescriptor cpd = new ConfigurationPointDescriptor(); 69 cpd.setId("Zap"); 70 cpd.setContributionsSchemaId("foo.bar.Baz"); 71 72 zipZoop.addConfigurationPoint(cpd); 73 74 XmlModuleDescriptorProcessor processor = new XmlModuleDescriptorProcessor(definition, 75 errorHandler); 76 processor.processModuleDescriptor(fooBar); 77 processor.processModuleDescriptor(zipZoop); 78 79 XmlExtensionResolver extensionResolver = new XmlExtensionResolver(definition, errorHandler); 80 extensionResolver.resolveSchemas(); 81 82 ConfigurationPointDefinition point = definition.getConfigurationPoint("zip.zoop.Zap"); 83 84 ConfigurationParserDefinition parserDef = point.getParser(HiveMindSchemaParser.INPUT_FORMAT_NAME); 85 assertNotNull(parserDef); 86 87 assertEquals(parserDef.getParserConstructor().getClass(), HiveMindSchemaParserConstructor.class); 88 89 HiveMindSchemaParserConstructor constructor = (HiveMindSchemaParserConstructor) parserDef.getParserConstructor(); 90 assertEquals(schema, constructor.getSchema()); 91 } 92 93 97 public void testSchemaNotVisible() 98 { 99 MockControl ehControl = newControl(ErrorHandler.class); 100 ErrorHandler errorHandler = (ErrorHandler) ehControl.getMock(); 101 102 Log log = LogFactory.getLog(XmlExtensionResolver.class); 103 104 SchemaImpl schema = new SchemaImpl("foo.bar"); 105 schema.setId("Baz"); 106 schema.setVisibility(Visibility.PRIVATE); 107 108 RegistryDefinition definition = new RegistryDefinitionImpl(); 109 110 Location l = newLocation(); 111 112 errorHandler.error( 113 log, 114 XmlImplMessages.schemaNotVisible("foo.bar.Baz", "zip.zoop"), 115 l, 116 null); 117 118 replayControls(); 119 120 ModuleDescriptor fooBar = new ModuleDescriptor(null, errorHandler); 121 fooBar.setModuleId("foo.bar"); 122 123 fooBar.addSchema(schema); 124 125 ModuleDescriptor zipZoop = new ModuleDescriptor(null, errorHandler); 126 zipZoop.setModuleId("zip.zoop"); 127 128 ConfigurationPointDescriptor cpd = new ConfigurationPointDescriptor(); 129 cpd.setId("Zap"); 130 cpd.setContributionsSchemaId("foo.bar.Baz"); 131 cpd.setLocation(l); 132 133 zipZoop.addConfigurationPoint(cpd); 134 135 XmlModuleDescriptorProcessor processor = new XmlModuleDescriptorProcessor(definition, 136 errorHandler); 137 processor.processModuleDescriptor(fooBar); 138 processor.processModuleDescriptor(zipZoop); 139 140 XmlExtensionResolver extensionResolver = new XmlExtensionResolver(definition, errorHandler); 141 extensionResolver.resolveSchemas(); 142 143 verifyControls(); 144 } 145 146 public void testSchemaNotFound() 147 { 148 ErrorHandler errorHandler = new DefaultErrorHandler(); 149 150 Log log = LogFactory.getLog(XmlModuleDescriptorProcessor.class); 151 152 RegistryDefinition definition = new RegistryDefinitionImpl(); 153 154 Location l = newLocation(); 155 156 errorHandler.error(log, XmlImplMessages.unableToResolveSchema("foo.bar.Baz"), l, null); 157 158 replayControls(); 159 160 ModuleDescriptor zipZoop = new ModuleDescriptor(null, errorHandler); 161 zipZoop.setModuleId("zip.zoop"); 162 163 ConfigurationPointDescriptor cpd = new ConfigurationPointDescriptor(); 164 cpd.setId("Zap"); 165 cpd.setContributionsSchemaId("foo.bar.Baz"); 166 cpd.setLocation(l); 167 168 zipZoop.addConfigurationPoint(cpd); 169 170 XmlModuleDescriptorProcessor processor = new XmlModuleDescriptorProcessor(definition, 171 errorHandler); 172 processor.processModuleDescriptor(zipZoop); 173 174 XmlExtensionResolver extensionResolver = new XmlExtensionResolver(definition, errorHandler); 175 extensionResolver.resolveSchemas(); 176 177 verifyControls(); 178 } 179 180 private Element newElement(String name) 181 { 182 ElementImpl e = new ElementImpl(); 183 184 e.setElementName(name); 185 186 return e; 187 } 188 189 public void testContributionConditionalExpressionTrue() 190 { 191 ErrorHandler errorHandler = new DefaultErrorHandler(); 192 193 RegistryDefinition definition = new RegistryDefinitionImpl(); 194 195 replayControls(); 196 197 ModuleDescriptor md = new ModuleDescriptor(getClassResolver(), errorHandler); 198 md.setModuleId("zip.zoop"); 199 200 ConfigurationPointDescriptor cpd = new ConfigurationPointDescriptor(); 201 202 cpd.setId("Fred"); 203 204 md.addConfigurationPoint(cpd); 205 206 ContributionDescriptor cd = new ContributionDescriptor(); 207 cd.setConfigurationId("Fred"); 208 cd.setConditionalExpression("class " + Location.class.getName()); 209 210 cd.addElement(newElement("foo")); 211 212 md.addContribution(cd); 213 214 XmlModuleDescriptorProcessor processor = new XmlModuleDescriptorProcessor(definition, 215 errorHandler); 216 processor.processModuleDescriptor(md); 217 218 ExtensionResolver extensionResolver = new ExtensionResolver(definition, new StrictErrorHandler()); 219 extensionResolver.resolveExtensions(); 220 221 ConfigurationPointDefinition configurationPoint = definition 222 .getConfigurationPoint("zip.zoop.Fred"); 223 224 assertTrue(configurationPoint.getContributions().size() > 0); 225 226 verifyControls(); 227 } 228 229 public void testConditionalExpressionFalse() 230 { 231 ErrorHandler errorHandler = new DefaultErrorHandler(); 232 233 RegistryDefinition definition = new RegistryDefinitionImpl(); 234 235 replayControls(); 236 237 ModuleDescriptor md = new ModuleDescriptor(getClassResolver(), errorHandler); 238 md.setModuleId("zip.zoop"); 239 240 ConfigurationPointDescriptor cpd = new ConfigurationPointDescriptor(); 241 242 cpd.setId("Fred"); 243 244 md.addConfigurationPoint(cpd); 245 246 ContributionDescriptor cd = new ContributionDescriptor(); 247 cd.setConfigurationId("Fred"); 248 cd.setConditionalExpression("class foo.bar.Baz"); 249 250 cd.addElement(newElement("foo")); 251 252 md.addContribution(cd); 253 254 XmlModuleDescriptorProcessor processor = new XmlModuleDescriptorProcessor(definition, 255 errorHandler); 256 processor.processModuleDescriptor(md); 257 258 ExtensionResolver extensionResolver = new ExtensionResolver(definition, new StrictErrorHandler()); 259 extensionResolver.resolveExtensions(); 260 261 ConfigurationPointDefinition configurationPoint = definition 262 .getConfigurationPoint("zip.zoop.Fred"); 263 264 assertTrue(configurationPoint.getContributions().isEmpty()); 265 266 verifyControls(); 267 } 268 269 public void testConditionalExpressionError() 270 { 271 MockControl ehControl = newControl(ErrorHandler.class); 272 ErrorHandler eh = (ErrorHandler) ehControl.getMock(); 273 274 RegistryDefinition definition = new RegistryDefinitionImpl(); 275 276 Log log = LogFactory.getLog(XmlModuleDescriptorProcessor.class); 277 278 Location location = newLocation(); 279 280 eh.error( 281 log, 282 "Unexpected token <AND> in expression 'and class foo'.", 283 location, 284 new RuntimeException ()); 285 ehControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[] 286 { null, null, null, new TypeMatcher() })); 287 288 replayControls(); 289 290 ModuleDescriptor md = new ModuleDescriptor(getClassResolver(), eh); 291 md.setModuleId("zip.zoop"); 292 293 ConfigurationPointDescriptor cpd = new ConfigurationPointDescriptor(); 294 295 cpd.setId("Fred"); 296 297 md.addConfigurationPoint(cpd); 298 299 ContributionDescriptor cd = new ContributionDescriptor(); 300 cd.setConfigurationId("Fred"); 301 cd.setConditionalExpression("and class foo"); 302 cd.setLocation(location); 303 304 cd.addElement(newElement("bar")); 305 306 md.addContribution(cd); 307 308 XmlModuleDescriptorProcessor processor = new XmlModuleDescriptorProcessor(definition, eh); 309 processor.processModuleDescriptor(md); 310 311 ExtensionResolver extensionResolver = new ExtensionResolver(definition, new StrictErrorHandler()); 312 extensionResolver.resolveExtensions(); 313 314 ConfigurationPointDefinition configurationPoint = definition 315 .getConfigurationPoint("zip.zoop.Fred"); 316 317 assertTrue(configurationPoint.getContributions().isEmpty()); 318 319 verifyControls(); 320 } 321 } | Popular Tags |