1   package org.andromda.core.metafacade;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Iterator;
6   import java.util.List;
7   
8   import junit.framework.TestCase;
9   
10  import org.andromda.core.metafacade.MetafacadeMapping.PropertyGroup;
11  import org.andromda.core.namespace.NamespaceComponents;
12  import org.apache.commons.collections.CollectionUtils;
13  import org.apache.commons.collections.Predicate;
14  
15  /***
16   * Tests {@link org.andromda.core.mappings.MetafacadeMappings)
17   * 
18   * @author Chad Brandon
19   */
20  public class MetafacadeMappingsTest
21      extends TestCase
22  {
23      /***
24       * Constructor for MappingsTest.
25       * 
26       * @param name
27       */
28      public MetafacadeMappingsTest(
29          String name)
30      {
31          super(name);
32      }
33      
34      /***
35       * @see TestCase#setUp()
36       */
37      protected void setUp()
38          throws Exception
39      {
40          NamespaceComponents.instance().discover();
41      }
42  
43  
44      private static final String METAFACADE_1 = "org.andromda.core.metafacade.Metafacade1";
45      private static final String METAFACADE_IMPL_1 = "org.andromda.core.metafacade.Metafacade1Impl";
46      private static final Object MAPPING_OBJECT_1 = new MappingObject1();
47      private static final List STEREOTYPES_1;
48  
49      private static final String METAFACADE_2 = "org.andromda.core.metafacade.Metafacade2";
50      private static final String METAFACADE_IMPL_2 = "org.andromda.core.metafacade.Metafacade2Impl";
51      private static final Object MAPPING_OBJECT_2 = new MappingObject2();
52      private static final List STEREOTYPES_2;
53  
54      private static final String METAFACADE_3 = "org.andromda.core.metafacade.Metafacade3";
55      private static final String METAFACADE_IMPL_3 = "org.andromda.core.metafacade.Metafacade3Impl";
56      private static final Object MAPPING_OBJECT_3 = new MappingObject3();
57      private static final List STEREOTYPES_3;
58  
59      private static final String METAFACADE_IMPL_4 = "org.andromda.core.metafacade.Metafacade4Impl";
60      private static final Object MAPPING_OBJECT_4 = new MappingObject4();
61  
62      private static final String METAFACADE_5 = "org.andromda.core.metafacade.Metafacade5";
63      private static final String METAFACADE_IMPL_5 = "org.andromda.core.metafacade.Metafacade5Impl";
64      private static final Object MAPPING_OBJECT_5 = new MappingObject5();
65  
66      private static final String METAFACADE_IMPL_6 = "org.andromda.core.metafacade.Metafacade6Impl";
67      private static final Object MAPPING_OBJECT_6 = new MappingObject6();
68  
69      private static final Object MAPPING_OBJECT_7 = new MappingObject7();
70  
71      private static final String METAFACADE_IMPL_8 = "org.andromda.core.metafacade.Metafacade8Impl";
72      private static final Object MAPPING_OBJECT_8 = new MappingObject8();
73  
74      private static final String METAFACADE_IMPL_9 = "org.andromda.core.metafacade.Metafacade9Impl";
75      private static final Object MAPPING_OBJECT_9 = new MappingObject9();
76  
77      private static final String METAFACADE_IMPL_10 = "org.andromda.core.metafacade.Metafacade10Impl";
78      private static final Object MAPPING_OBJECT_10 = new MappingObject10();
79      
80      private static final Object MAPPING_OBJECT_11 = new MappingObject11();
81  
82      private static final String MAPPING_PROPERTY = "mappingProperty";
83      private static final String PROPERTY = "property";
84      private static final String PROPERTY_ONE = "propertyOne";
85      private static final String PROPERTY_TWO = "propertyTwo";
86  
87      private static final String STEREOTYPE_FINDER_METHOD = "FINDER_METHOD";
88      private static final String STEREOTYPE_ENUMERATION = "ENUMERATION";
89      private static final String STEREOTYPE_APPLICATION_EXCEPTION = "APPLICATION_EXCEPTION";
90      private static final String STEREOTYPE_UNEXPECTED_EXCEPTION = "UNEXPECTED_EXCEPTION";
91      private static final String STEREOTYPE_QUERY_METHOD = "QUERY_METHOD";
92  
93      static
94      {
95          STEREOTYPES_1 = new ArrayList();
96          STEREOTYPES_1.add(STEREOTYPE_FINDER_METHOD);
97          STEREOTYPES_2 = new ArrayList();
98          STEREOTYPES_2.add(STEREOTYPE_ENUMERATION);
99          STEREOTYPES_3 = new ArrayList();
100         STEREOTYPES_3.add(STEREOTYPE_APPLICATION_EXCEPTION);
101         STEREOTYPES_3.add(STEREOTYPE_UNEXPECTED_EXCEPTION);
102     }
103 
104     public void testGetMetafacadeMapping()
105     {
106 
107         MetafacadeMappings mappings = MetafacadeMappings.newInstance();
108         mappings.initialize();
109         final String modelTypeNamespace = "test";
110         mappings = mappings.getModelMetafacadeMappings(modelTypeNamespace);
111         final String namespace = mappings.getNamespace();
112         final MetafacadeFactory factory = MetafacadeFactory.getInstance();
113         factory.setModel(new Model(), modelTypeNamespace);
114         factory.setNamespace(mappings.getNamespace());
115 
116         // verify the property references
117         Collection propertyReferences = mappings.getPropertyReferences();
118         // test retrieval of the namespace properties
119         assertEquals(2, propertyReferences.size());
120         Iterator referenceIterator = propertyReferences.iterator();
121         assertEquals("definitionOne", referenceIterator.next());
122         assertEquals("definitionTwo", referenceIterator.next());
123 
124         // test the default metafacade mapping
125         assertNotNull(mappings.getDefaultMetafacadeClass(namespace));
126         assertEquals(METAFACADE_IMPL_1, mappings.getDefaultMetafacadeClass(
127             namespace).getName());
128 
129         // test a mapping having a single stereotype with property references
130         MetafacadeMapping mapping = mappings.getMetafacadeMapping(
131             MAPPING_OBJECT_1,
132             namespace,
133             null,
134             STEREOTYPES_1);
135         assertNotNull(mapping);
136         // assertNull(mapping.getContext());
137         assertTrue(mapping.getMetafacadeClass().getName().equals(
138             METAFACADE_IMPL_1));
139         propertyReferences = mapping.getPropertyReferences();
140         assertNotNull(propertyReferences);
141         assertEquals(2, propertyReferences.size());
142         assertNotNull(CollectionUtils.find(propertyReferences, 
143             new Predicate()
144             {
145                 public boolean evaluate(Object object)
146                 {
147                     return ((String)object).equals("metafacadeProperteryOne");
148                 }
149             }));
150         assertNotNull(CollectionUtils.find(propertyReferences, 
151             new Predicate()
152             {
153                 public boolean evaluate(Object object)
154                 {
155                     return ((String)object).equals("metafacadeProperteryTwo");
156                 }
157             }));
158 
159         // test that we can get a mapping to the same metafacade with a
160         // different stereotype
161         // (an 'OR' scenario)
162         List stereotypes = new ArrayList();
163         stereotypes.add(STEREOTYPE_QUERY_METHOD);
164         mapping = mappings.getMetafacadeMapping(
165             MAPPING_OBJECT_1,
166             namespace,
167             null,
168             STEREOTYPES_1);
169         assertNotNull(mapping);
170         // assertNull(mapping.getContext());
171         assertTrue(mapping.getMetafacadeClass().getName().equals(
172             METAFACADE_IMPL_1));
173 
174         // test a mapping having a context
175         mapping = mappings.getMetafacadeMapping(
176             MAPPING_OBJECT_2,
177             namespace,
178             METAFACADE_1,
179             null);
180         assertNotNull(mapping);
181         assertEquals(METAFACADE_IMPL_4, mapping.getMetafacadeClass().getName());
182         assertTrue(mapping.getPropertyReferences().isEmpty());
183 
184         // test a mapping having a context (with using an inherited context)
185         mapping = mappings.getMetafacadeMapping(
186             MAPPING_OBJECT_2,
187             namespace,
188             METAFACADE_2,
189             null);
190         assertNotNull(mapping);
191         assertEquals(METAFACADE_IMPL_4, mapping.getMetafacadeClass().getName());
192         assertTrue(mapping.getPropertyReferences().isEmpty());
193 
194         // test a mapping having 2 required stereotypes
195         mapping = mappings.getMetafacadeMapping(
196             MAPPING_OBJECT_2,
197             namespace,
198             null,
199             STEREOTYPES_3);
200         assertNotNull(mapping);
201         assertFalse(mapping.hasContext());
202         assertEquals(METAFACADE_IMPL_3, mapping.getMetafacadeClass().getName());
203         assertTrue(mapping.getPropertyReferences().isEmpty());
204 
205         // make sure we can't get the mapping that requires 2 stereotypes with
206         // ONLY one of the stereotypes (an 'AND' scenario)
207         stereotypes = new ArrayList();
208         stereotypes.add(STEREOTYPE_UNEXPECTED_EXCEPTION);
209         mapping = mappings.getMetafacadeMapping(
210             MAPPING_OBJECT_2,
211             namespace,
212             null,
213             stereotypes);
214         assertNull(mapping);
215         stereotypes = new ArrayList();
216         stereotypes.add(STEREOTYPE_APPLICATION_EXCEPTION);
217         mapping = mappings.getMetafacadeMapping(
218             MAPPING_OBJECT_2,
219             namespace,
220             null,
221             stereotypes);
222         assertNull(mapping);
223 
224         // test a mapping having a context AND a stereotype
225         mapping = mappings.getMetafacadeMapping(
226             MAPPING_OBJECT_2,
227             namespace,
228             METAFACADE_3,
229             STEREOTYPES_2);
230         assertNotNull(mapping);
231         assertEquals(METAFACADE_IMPL_2, mapping.getMetafacadeClass().getName());
232 
233         // test a mapping having a context and multiple stereotypes
234         mapping = mappings.getMetafacadeMapping(
235             MAPPING_OBJECT_2,
236             namespace,
237             METAFACADE_3,
238             STEREOTYPES_3);
239         assertNotNull(mapping);
240         assertEquals(METAFACADE_IMPL_4, mapping.getMetafacadeClass().getName());
241 
242         // make sure we can't get the mapping that requires 2 stereotypes and
243         // one context
244         // with only one of the stereotypes.
245         stereotypes = new ArrayList();
246         stereotypes.add(STEREOTYPE_APPLICATION_EXCEPTION);
247         mapping = mappings.getMetafacadeMapping(
248             MAPPING_OBJECT_2,
249             namespace,
250             METAFACADE_3,
251             stereotypes);
252         assertNull(mapping);
253 
254         // try a plain mapping (no contexts or stereotypes)
255         mapping = mappings.getMetafacadeMapping(
256             MAPPING_OBJECT_7,
257             namespace,
258             null,
259             null);
260         assertNotNull(mapping);
261         assertEquals(METAFACADE_IMPL_3, mapping.getMetafacadeClass().getName());
262 
263         // make sure we CAN'T get the mapping having the single property
264         // since the mapping object doesn't contain the property
265         mapping = mappings.getMetafacadeMapping(
266             MAPPING_OBJECT_2,
267             namespace,
268             null,
269             null);
270         assertNull(mapping);
271 
272         // make sure we CAN get the mapping having the single property
273         // since the mapping has the property and it's set to true.
274         mapping = mappings.getMetafacadeMapping(
275             MAPPING_OBJECT_5,
276             namespace,
277             null,
278             null);
279         assertNotNull(mapping);
280         assertEquals(METAFACADE_IMPL_5, mapping.getMetafacadeClass().getName());
281         PropertyGroup group = mapping.getMappingProperties();
282         Collection mappingProperties = group.getProperties();
283         assertNotNull(mappingProperties);
284         assertEquals(1, mappingProperties.size());
285         assertEquals(
286             PROPERTY,
287             ((MetafacadeMapping.Property)mappingProperties.iterator().next())
288                 .getName());
289         assertEquals("false", ((MetafacadeMapping.Property)mappingProperties
290             .iterator().next()).getValue());
291 
292         // get a property that has a value defined
293         mapping = mappings.getMetafacadeMapping(
294             MAPPING_OBJECT_4,
295             namespace,
296             null,
297             null);
298         assertNotNull(mapping);
299         assertEquals(METAFACADE_IMPL_6, mapping.getMetafacadeClass().getName());
300         group = mapping.getMappingProperties();
301         mappingProperties = group.getProperties();
302         assertNotNull(mappingProperties);
303         assertEquals(1, mappingProperties.size());
304         assertEquals(
305             MAPPING_PROPERTY,
306             ((MetafacadeMapping.Property)mappingProperties.iterator().next())
307                 .getName());
308         assertEquals("true", ((MetafacadeMapping.Property)mappingProperties
309             .iterator().next()).getValue());
310 
311         // get a metafacade that has no ancestors
312         mapping = mappings.getMetafacadeMapping(
313             MAPPING_OBJECT_3,
314             namespace,
315             null,
316             null);
317         assertNotNull(mapping);
318         assertEquals(METAFACADE_IMPL_5, mapping.getMetafacadeClass().getName());
319         assertEquals(1, mappingProperties.size());
320         assertEquals(
321             MAPPING_PROPERTY,
322             ((MetafacadeMapping.Property)mappingProperties.iterator().next())
323                 .getName());
324         assertEquals("true", ((MetafacadeMapping.Property)mappingProperties
325             .iterator().next()).getValue());
326 
327         // get a mapping by context and property
328         mapping = mappings.getMetafacadeMapping(
329             MAPPING_OBJECT_6,
330             namespace,
331             METAFACADE_5,
332             null);
333         assertNotNull(mapping);
334         assertEquals(MAPPING_OBJECT_6.getClass().getName(), mapping
335             .getMappingClassName());
336         assertEquals(METAFACADE_5, mapping.getContext());
337         group = mapping.getMappingProperties();
338         mappingProperties = group.getProperties();
339         assertNotNull(mappingProperties);
340         assertEquals(1, mappingProperties.size());
341         assertEquals(
342             MAPPING_PROPERTY,
343             ((MetafacadeMapping.Property)mappingProperties.iterator().next())
344                 .getName());
345         assertEquals("", ((MetafacadeMapping.Property)mappingProperties
346             .iterator().next()).getValue());
347 
348         // attempt to get a mapping that has 2 properties with one being invalid
349         mapping = mappings.getMetafacadeMapping(
350             MAPPING_OBJECT_8,
351             namespace,
352             null,
353             null);
354         assertNull(mapping);
355 
356         // attempt to get a mapping that has 2 properties with both being valid
357         mapping = mappings.getMetafacadeMapping(
358             MAPPING_OBJECT_9,
359             namespace,
360             null,
361             null);
362         assertNotNull(mapping);
363         assertEquals(METAFACADE_IMPL_8, mapping.getMetafacadeClass().getName());
364         group = mapping.getMappingProperties();
365         mappingProperties = group.getProperties();
366         assertNotNull(mappingProperties);
367         assertEquals(2, mappingProperties.size());
368         Iterator propertyIterator = mappingProperties.iterator();
369         MetafacadeMapping.Property propertyOne = (MetafacadeMapping.Property)propertyIterator
370             .next();
371         assertEquals(PROPERTY_ONE, propertyOne.getName());
372         assertEquals("", propertyOne.getValue());
373         MetafacadeMapping.Property propertyTwo = (MetafacadeMapping.Property)propertyIterator
374             .next();
375         assertEquals(PROPERTY_TWO, propertyTwo.getName());
376         assertEquals("SomeValue", propertyTwo.getValue());
377 
378         // attempt to get a mapping that is mapped twice 
379         // each one using a different property of the 
380         // metafacade
381         mapping = mappings.getMetafacadeMapping(
382             MAPPING_OBJECT_10,
383             namespace,
384             null,
385             null);
386         assertNotNull(mapping);
387         assertEquals(METAFACADE_IMPL_9, mapping.getMetafacadeClass().getName());
388         group = mapping.getMappingProperties();
389         mappingProperties = group.getProperties();
390         assertNotNull(mappingProperties);
391         assertEquals(1, mappingProperties.size());
392         propertyIterator = mappingProperties.iterator();
393         propertyOne = (MetafacadeMapping.Property)propertyIterator.next();
394         assertEquals(PROPERTY_ONE, propertyOne.getName());
395         assertEquals("", propertyOne.getValue());
396         
397         // attempt with two mappings pointing to the metafacade with
398         // mutually exclusive properties (the first one listed should
399         // be retrieved).
400         mapping = mappings.getMetafacadeMapping(
401             MAPPING_OBJECT_11,
402             namespace,
403             null,
404             null);
405         assertNotNull(mapping);
406         assertEquals(METAFACADE_IMPL_10, mapping.getMetafacadeClass().getName());
407         group = mapping.getMappingProperties();
408         mappingProperties = group.getProperties();
409         assertNotNull(mappingProperties);
410         assertEquals(1, mappingProperties.size());
411         propertyIterator = mappingProperties.iterator();
412         propertyOne = (MetafacadeMapping.Property)propertyIterator.next();
413         assertEquals(PROPERTY_TWO, propertyOne.getName());
414         assertEquals("", propertyOne.getValue());
415     }
416 }