001package org.andromda.core.metafacade; 002 003import java.util.ArrayList; 004import java.util.Collection; 005import java.util.Iterator; 006import java.util.List; 007import junit.framework.TestCase; 008import org.andromda.core.metafacade.MetafacadeMapping.Property; 009import org.andromda.core.metafacade.MetafacadeMapping.PropertyGroup; 010import org.andromda.core.namespace.NamespaceComponents; 011import org.apache.commons.collections.CollectionUtils; 012import org.apache.commons.collections.Predicate; 013 014/** 015 * Tests {@link org.andromda.core.metafacade.MetafacadeMappings} 016 * 017 * @author Chad Brandon 018 */ 019public class MetafacadeMappingsTest 020 extends TestCase 021{ 022 /** 023 * Constructor for MappingsTest. 024 * 025 * @param name 026 */ 027 public MetafacadeMappingsTest( 028 String name) 029 { 030 super(name); 031 } 032 033 /** 034 * @see TestCase#setUp() 035 */ 036 protected void setUp() 037 throws Exception 038 { 039 NamespaceComponents.instance().discover(); 040 } 041 042 private static final String METAFACADE_1 = "org.andromda.core.metafacade.Metafacade1"; 043 private static final String METAFACADE_IMPL_1 = "org.andromda.core.metafacade.Metafacade1Impl"; 044 private static final Object MAPPING_OBJECT_1 = new MappingObject1(); 045 private static final List<String> STEREOTYPES_1; 046 047 private static final String METAFACADE_2 = "org.andromda.core.metafacade.Metafacade2"; 048 private static final String METAFACADE_IMPL_2 = "org.andromda.core.metafacade.Metafacade2Impl"; 049 private static final Object MAPPING_OBJECT_2 = new MappingObject2(); 050 private static final List<String> STEREOTYPES_2; 051 052 private static final String METAFACADE_3 = "org.andromda.core.metafacade.Metafacade3"; 053 private static final String METAFACADE_IMPL_3 = "org.andromda.core.metafacade.Metafacade3Impl"; 054 private static final Object MAPPING_OBJECT_3 = new MappingObject3(); 055 private static final List<String> STEREOTYPES_3; 056 057 private static final String METAFACADE_IMPL_4 = "org.andromda.core.metafacade.Metafacade4Impl"; 058 private static final Object MAPPING_OBJECT_4 = new MappingObject4(); 059 060 private static final String METAFACADE_5 = "org.andromda.core.metafacade.Metafacade5"; 061 private static final String METAFACADE_IMPL_5 = "org.andromda.core.metafacade.Metafacade5Impl"; 062 private static final Object MAPPING_OBJECT_5 = new MappingObject5(); 063 064 private static final String METAFACADE_IMPL_6 = "org.andromda.core.metafacade.Metafacade6Impl"; 065 private static final Object MAPPING_OBJECT_6 = new MappingObject6(); 066 067 private static final Object MAPPING_OBJECT_7 = new MappingObject7(); 068 069 private static final String METAFACADE_IMPL_8 = "org.andromda.core.metafacade.Metafacade8Impl"; 070 private static final Object MAPPING_OBJECT_8 = new MappingObject8(); 071 072 private static final String METAFACADE_IMPL_9 = "org.andromda.core.metafacade.Metafacade9Impl"; 073 private static final Object MAPPING_OBJECT_9 = new MappingObject9(); 074 075 private static final String METAFACADE_IMPL_10 = "org.andromda.core.metafacade.Metafacade10Impl"; 076 private static final Object MAPPING_OBJECT_10 = new MappingObject10(); 077 078 private static final Object MAPPING_OBJECT_11 = new MappingObject11(); 079 080 private static final String MAPPING_PROPERTY = "mappingProperty"; 081 private static final String PROPERTY = "property"; 082 private static final String PROPERTY_ONE = "propertyOne"; 083 private static final String PROPERTY_TWO = "propertyTwo"; 084 085 private static final String STEREOTYPE_FINDER_METHOD = "FINDER_METHOD"; 086 private static final String STEREOTYPE_ENUMERATION = "ENUMERATION"; 087 private static final String STEREOTYPE_APPLICATION_EXCEPTION = "APPLICATION_EXCEPTION"; 088 private static final String STEREOTYPE_UNEXPECTED_EXCEPTION = "UNEXPECTED_EXCEPTION"; 089 private static final String STEREOTYPE_QUERY_METHOD = "QUERY_METHOD"; 090 091 static 092 { 093 STEREOTYPES_1 = new ArrayList<String>(); 094 STEREOTYPES_1.add(STEREOTYPE_FINDER_METHOD); 095 STEREOTYPES_2 = new ArrayList<String>(); 096 STEREOTYPES_2.add(STEREOTYPE_ENUMERATION); 097 STEREOTYPES_3 = new ArrayList<String>(); 098 STEREOTYPES_3.add(STEREOTYPE_APPLICATION_EXCEPTION); 099 STEREOTYPES_3.add(STEREOTYPE_UNEXPECTED_EXCEPTION); 100 } 101 102 /** 103 * 104 */ 105 public void testGetMetafacadeMapping() 106 { 107 108 MetafacadeMappings mappings = MetafacadeMappings.newInstance(); 109 mappings.initialize(); 110 final String modelTypeNamespace = "test"; 111 mappings = mappings.getModelMetafacadeMappings(modelTypeNamespace); 112 final String namespace = mappings.getNamespace(); 113 final MetafacadeFactory factory = MetafacadeFactory.getInstance(); 114 factory.setModel(new Model(), modelTypeNamespace); 115 factory.setNamespace(mappings.getNamespace()); 116 117 // verify the property references 118 Collection<String> propertyReferences = mappings.getPropertyReferences(); 119 // test retrieval of the namespace properties 120 assertEquals(2, propertyReferences.size()); 121 Iterator referenceIterator = propertyReferences.iterator(); 122 assertEquals("definitionOne", referenceIterator.next()); 123 assertEquals("definitionTwo", referenceIterator.next()); 124 125 // test the default metafacade mapping 126 assertNotNull(mappings.getDefaultMetafacadeClass(namespace)); 127 assertEquals(METAFACADE_IMPL_1, mappings.getDefaultMetafacadeClass( 128 namespace).getName()); 129 130 // test a mapping having a single stereotype with property references 131 MetafacadeMapping mapping = mappings.getMetafacadeMapping( 132 MAPPING_OBJECT_1, 133 namespace, 134 null, 135 STEREOTYPES_1); 136 assertNotNull(mapping); 137 // assertNull(mapping.getContext()); 138 assertTrue(mapping.getMetafacadeClass().getName().equals( 139 METAFACADE_IMPL_1)); 140 propertyReferences = mapping.getPropertyReferences(); 141 assertNotNull(propertyReferences); 142 assertEquals(2, propertyReferences.size()); 143 assertNotNull(CollectionUtils.find(propertyReferences, 144 new Predicate() 145 { 146 public boolean evaluate(Object object) 147 { 148 return "metafacadeProperteryOne".equals(object); 149 } 150 })); 151 assertNotNull(CollectionUtils.find(propertyReferences, 152 new Predicate() 153 { 154 public boolean evaluate(Object object) 155 { 156 return "metafacadeProperteryTwo".equals(object); 157 } 158 })); 159 160 // test that we can get a mapping to the same metafacade with a 161 // different stereotype 162 // (an 'OR' scenario) 163 List<String> stereotypes = new ArrayList<String>(); 164 stereotypes.add(STEREOTYPE_QUERY_METHOD); 165 mapping = mappings.getMetafacadeMapping( 166 MAPPING_OBJECT_1, 167 namespace, 168 null, 169 STEREOTYPES_1); 170 assertNotNull(mapping); 171 // assertNull(mapping.getContext()); 172 assertTrue(mapping.getMetafacadeClass().getName().equals( 173 METAFACADE_IMPL_1)); 174 175 // test a mapping having a context 176 mapping = mappings.getMetafacadeMapping( 177 MAPPING_OBJECT_2, 178 namespace, 179 METAFACADE_1, 180 null); 181 assertNotNull(mapping); 182 assertEquals(METAFACADE_IMPL_4, mapping.getMetafacadeClass().getName()); 183 assertTrue(mapping.getPropertyReferences().isEmpty()); 184 185 // test a mapping having a context (with using an inherited context) 186 mapping = mappings.getMetafacadeMapping( 187 MAPPING_OBJECT_2, 188 namespace, 189 METAFACADE_2, 190 null); 191 assertNotNull(mapping); 192 assertEquals(METAFACADE_IMPL_4, mapping.getMetafacadeClass().getName()); 193 assertTrue(mapping.getPropertyReferences().isEmpty()); 194 195 // test a mapping having 2 required stereotypes 196 mapping = mappings.getMetafacadeMapping( 197 MAPPING_OBJECT_2, 198 namespace, 199 null, 200 STEREOTYPES_3); 201 assertNotNull(mapping); 202 assertFalse(mapping.hasContext()); 203 assertEquals(METAFACADE_IMPL_3, mapping.getMetafacadeClass().getName()); 204 assertTrue(mapping.getPropertyReferences().isEmpty()); 205 206 // make sure we can't get the mapping that requires 2 stereotypes with 207 // ONLY one of the stereotypes (an 'AND' scenario) 208 stereotypes = new ArrayList<String>(); 209 stereotypes.add(STEREOTYPE_UNEXPECTED_EXCEPTION); 210 mapping = mappings.getMetafacadeMapping( 211 MAPPING_OBJECT_2, 212 namespace, 213 null, 214 stereotypes); 215 assertNull(mapping); 216 stereotypes = new ArrayList<String>(); 217 stereotypes.add(STEREOTYPE_APPLICATION_EXCEPTION); 218 mapping = mappings.getMetafacadeMapping( 219 MAPPING_OBJECT_2, 220 namespace, 221 null, 222 stereotypes); 223 assertNull(mapping); 224 225 // test a mapping having a context AND a stereotype 226 mapping = mappings.getMetafacadeMapping( 227 MAPPING_OBJECT_2, 228 namespace, 229 METAFACADE_3, 230 STEREOTYPES_2); 231 assertNotNull(mapping); 232 assertEquals(METAFACADE_IMPL_2, mapping.getMetafacadeClass().getName()); 233 234 // test a mapping having a context and multiple stereotypes 235 mapping = mappings.getMetafacadeMapping( 236 MAPPING_OBJECT_2, 237 namespace, 238 METAFACADE_3, 239 STEREOTYPES_3); 240 assertNotNull(mapping); 241 assertEquals(METAFACADE_IMPL_4, mapping.getMetafacadeClass().getName()); 242 243 // make sure we can't get the mapping that requires 2 stereotypes and 244 // one context 245 // with only one of the stereotypes. 246 stereotypes = new ArrayList<String>(); 247 stereotypes.add(STEREOTYPE_APPLICATION_EXCEPTION); 248 mapping = mappings.getMetafacadeMapping( 249 MAPPING_OBJECT_2, 250 namespace, 251 METAFACADE_3, 252 stereotypes); 253 assertNull(mapping); 254 255 // try a plain mapping (no contexts or stereotypes) 256 mapping = mappings.getMetafacadeMapping( 257 MAPPING_OBJECT_7, 258 namespace, 259 null, 260 null); 261 assertNotNull(mapping); 262 assertEquals(METAFACADE_IMPL_3, mapping.getMetafacadeClass().getName()); 263 264 // make sure we CAN'T get the mapping having the single property 265 // since the mapping object doesn't contain the property 266 mapping = mappings.getMetafacadeMapping( 267 MAPPING_OBJECT_2, 268 namespace, 269 null, 270 null); 271 assertNull(mapping); 272 273 // make sure we CAN get the mapping having the single property 274 // since the mapping has the property and it's set to true. 275 mapping = mappings.getMetafacadeMapping( 276 MAPPING_OBJECT_5, 277 namespace, 278 null, 279 null); 280 assertNotNull(mapping); 281 assertEquals(METAFACADE_IMPL_5, mapping.getMetafacadeClass().getName()); 282 PropertyGroup group = mapping.getMappingProperties(); 283 Collection<Property> mappingProperties = group.getProperties(); 284 assertNotNull(mappingProperties); 285 assertEquals(1, mappingProperties.size()); 286 assertEquals( 287 PROPERTY, 288 (mappingProperties.iterator().next()) 289 .getName()); 290 assertEquals("false", (mappingProperties 291 .iterator().next()).getValue()); 292 293 // get a property that has a value defined 294 mapping = mappings.getMetafacadeMapping( 295 MAPPING_OBJECT_4, 296 namespace, 297 null, 298 null); 299 assertNotNull(mapping); 300 assertEquals(METAFACADE_IMPL_6, mapping.getMetafacadeClass().getName()); 301 group = mapping.getMappingProperties(); 302 mappingProperties = group.getProperties(); 303 assertNotNull(mappingProperties); 304 assertEquals(1, mappingProperties.size()); 305 assertEquals( 306 MAPPING_PROPERTY, 307 (mappingProperties.iterator().next()) 308 .getName()); 309 assertEquals("true", (mappingProperties 310 .iterator().next()).getValue()); 311 312 // get a metafacade that has no ancestors 313 mapping = mappings.getMetafacadeMapping( 314 MAPPING_OBJECT_3, 315 namespace, 316 null, 317 null); 318 assertNotNull(mapping); 319 assertEquals(METAFACADE_IMPL_5, mapping.getMetafacadeClass().getName()); 320 assertEquals(1, mappingProperties.size()); 321 assertEquals( 322 MAPPING_PROPERTY, 323 (mappingProperties.iterator().next()) 324 .getName()); 325 assertEquals("true", (mappingProperties 326 .iterator().next()).getValue()); 327 328 // get a mapping by context and property 329 mapping = mappings.getMetafacadeMapping( 330 MAPPING_OBJECT_6, 331 namespace, 332 METAFACADE_5, 333 null); 334 assertNotNull(mapping); 335 assertTrue(mapping.getMappingClassNames() 336 .contains(MAPPING_OBJECT_6.getClass().getName())); 337 assertEquals(METAFACADE_5, mapping.getContext()); 338 group = mapping.getMappingProperties(); 339 mappingProperties = group.getProperties(); 340 assertNotNull(mappingProperties); 341 assertEquals(1, mappingProperties.size()); 342 assertEquals( 343 MAPPING_PROPERTY, 344 (mappingProperties.iterator().next()) 345 .getName()); 346 assertEquals("", (mappingProperties 347 .iterator().next()).getValue()); 348 349 // attempt to get a mapping that has 2 properties with one being invalid 350 mapping = mappings.getMetafacadeMapping( 351 MAPPING_OBJECT_8, 352 namespace, 353 null, 354 null); 355 assertNull(mapping); 356 357 // attempt to get a mapping that has 2 properties with both being valid 358 mapping = mappings.getMetafacadeMapping( 359 MAPPING_OBJECT_9, 360 namespace, 361 null, 362 null); 363 assertNotNull(mapping); 364 assertEquals(METAFACADE_IMPL_8, mapping.getMetafacadeClass().getName()); 365 group = mapping.getMappingProperties(); 366 mappingProperties = group.getProperties(); 367 assertNotNull(mappingProperties); 368 assertEquals(2, mappingProperties.size()); 369 Iterator propertyIterator = mappingProperties.iterator(); 370 MetafacadeMapping.Property propertyOne = (MetafacadeMapping.Property)propertyIterator 371 .next(); 372 assertEquals(PROPERTY_ONE, propertyOne.getName()); 373 assertEquals("", propertyOne.getValue()); 374 MetafacadeMapping.Property propertyTwo = (MetafacadeMapping.Property)propertyIterator 375 .next(); 376 assertEquals(PROPERTY_TWO, propertyTwo.getName()); 377 assertEquals("SomeValue", propertyTwo.getValue()); 378 379 // attempt to get a mapping that is mapped twice 380 // each one using a different property of the 381 // metafacade 382 mapping = mappings.getMetafacadeMapping( 383 MAPPING_OBJECT_10, 384 namespace, 385 null, 386 null); 387 assertNotNull(mapping); 388 assertEquals(METAFACADE_IMPL_9, mapping.getMetafacadeClass().getName()); 389 group = mapping.getMappingProperties(); 390 mappingProperties = group.getProperties(); 391 assertNotNull(mappingProperties); 392 assertEquals(1, mappingProperties.size()); 393 propertyIterator = mappingProperties.iterator(); 394 propertyOne = (MetafacadeMapping.Property)propertyIterator.next(); 395 assertEquals(PROPERTY_ONE, propertyOne.getName()); 396 assertEquals("", propertyOne.getValue()); 397 398 // attempt with two mappings pointing to the metafacade with 399 // mutually exclusive properties (the first one listed should 400 // be retrieved). 401 mapping = mappings.getMetafacadeMapping( 402 MAPPING_OBJECT_11, 403 namespace, 404 null, 405 null); 406 assertNotNull(mapping); 407 assertEquals(METAFACADE_IMPL_10, mapping.getMetafacadeClass().getName()); 408 group = mapping.getMappingProperties(); 409 mappingProperties = group.getProperties(); 410 assertNotNull(mappingProperties); 411 assertEquals(1, mappingProperties.size()); 412 propertyIterator = mappingProperties.iterator(); 413 propertyOne = (MetafacadeMapping.Property)propertyIterator.next(); 414 assertEquals(PROPERTY_TWO, propertyOne.getName()); 415 assertEquals("", propertyOne.getValue()); 416 } 417}