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