001package org.andromda.core.mappings; 002 003import java.net.URL; 004import java.util.Collection; 005import java.util.Iterator; 006import junit.framework.TestCase; 007import org.andromda.core.mapping.Mapping; 008import org.andromda.core.mapping.Mappings; 009import org.andromda.core.mapping.MappingsException; 010 011/** 012 * Tests {@link org.andromda.core.mapping.Mappings} 013 * 014 * @author Chad Brandon 015 * @author Wouter Zoons 016 */ 017public class MappingsTest 018 extends TestCase 019{ 020 /** 021 * Constructor for MappingsTest. 022 * 023 * @param name the name for this test case 024 */ 025 public MappingsTest(String name) 026 { 027 super(name); 028 } 029 030 private static final String TO_1 = "base64Binary"; 031 private static final String FROM_1 = "datatype.Blob"; 032 private static final String FROM_2 = "datatype.byte[]"; 033 private static final String FROM_3 = "datatype.Byte[]"; 034 private static final String FROM_4 = "datatype.Clob"; 035 // Assumes mappings without 'java.lang' in mapped type 036 private static final String TO_2 = "String"; 037 private static final String FROM_5 = "datatype.String"; 038 private static final String NOT_MAPPED_1 = "datatype.byte"; 039 private static final String NOT_MAPPED_2 = "datatype.String[]"; 040 private static final String TO_3 = "Class<? extends ToType>"; 041 private static final String FROM_6 = "Class<? extends FromType>"; 042 043 /** 044 * 045 */ 046 public void testGetTo() 047 { 048 Mappings mappings1 = new Mappings(); 049 Mapping mapping1 = new Mapping(); 050 mapping1.setTo(TO_1); 051 mapping1.addFrom(FROM_1); 052 mapping1.addFrom(FROM_2); 053 mapping1.addFrom(FROM_3); 054 mapping1.addFrom(FROM_4); 055 mappings1.addMapping(mapping1); 056 057 // make sure the to == from when passing in a mappings1 that don't exist 058 assertNotNull(mappings1.getTo(NOT_MAPPED_1)); 059 assertEquals( 060 NOT_MAPPED_1, 061 mappings1.getTo(NOT_MAPPED_1)); 062 assertEquals( 063 mappings1.getTo(NOT_MAPPED_2), 064 mappings1.getTo(NOT_MAPPED_2)); 065 066 // make sure we can retrieve the to using a from array. 067 assertNotNull(mappings1.getTo(FROM_1)); 068 assertEquals( 069 TO_1, 070 mappings1.getTo(FROM_1)); 071 072 // make sure we can retrieve the to using a from non array. 073 assertEquals( 074 TO_1, 075 mappings1.getTo(FROM_4)); 076 077 Mappings mappings2 = new Mappings(); 078 Mapping mapping2 = new Mapping(); 079 mapping2.setTo(TO_2); 080 mapping2.addFrom(FROM_5); 081 mappings2.addMapping(mapping2); 082 assertEquals( 083 TO_2, 084 mappings2.getTo(FROM_5)); 085 086 Mappings mappings3 = new Mappings(); 087 Mapping mapping3 = new Mapping(); 088 mapping3.setTo(TO_3); 089 mapping3.addFrom(FROM_6); 090 mappings3.addMapping(mapping3); 091 // make sure whitespace isn't deleted, only trimmed (Java generics would fail compilation otherwise for example) 092 assertEquals( 093 TO_3, 094 mappings3.getTo(FROM_6)); 095 } 096 097 /** 098 * 099 */ 100 public void testMappingsInheritance() 101 { 102 final URL testMappingsParentUri = MappingsTest.class.getResource("TestMappingsParent.xml"); 103 assertNotNull(testMappingsParentUri); 104 final Mappings testMappingsParent = Mappings.getInstance(testMappingsParentUri); 105 assertNotNull(testMappingsParent); 106 final Collection<Mapping> mappings1 = testMappingsParent.getMappings(); 107 assertEquals( 108 3, 109 mappings1.size()); 110 final Iterator mappings1Iterator = mappings1.iterator(); 111 Mapping mapping1 = (Mapping)mappings1Iterator.next(); 112 assertEquals( 113 "datatype::typeOne", 114 mapping1.getFroms().iterator().next()); 115 assertEquals( 116 "Type_One", 117 mapping1.getTo()); 118 Mapping mapping2 = (Mapping)mappings1Iterator.next(); 119 assertEquals( 120 "datatype::typeTwo", 121 mapping2.getFroms().iterator().next()); 122 assertEquals( 123 "Type_Two", 124 mapping2.getTo()); 125 Mapping mapping3 = (Mapping)mappings1Iterator.next(); 126 assertEquals( 127 "datatype::typeThree", 128 mapping3.getFroms().iterator().next()); 129 assertEquals( 130 "Type_Three", 131 mapping3.getTo()); 132 133 final URL testMappingsUri = MappingsTest.class.getResource("TestMappings.xml"); 134 assertNotNull(testMappingsUri); 135 Mappings testMappings = Mappings.getInstance(testMappingsUri); 136 assertNotNull(testMappings); 137 final Collection<Mapping> mappings2 = testMappings.getMappings(); 138 assertEquals( 139 4, 140 mappings2.size()); 141 final Iterator mappings2Iterator = mappings2.iterator(); 142 mapping1 = (Mapping)mappings2Iterator.next(); 143 assertEquals( 144 "datatype::typeOne", 145 mapping1.getFroms().iterator().next()); 146 assertEquals( 147 "Type_One", 148 mapping1.getTo()); 149 mapping2 = (Mapping)mappings2Iterator.next(); 150 assertEquals( 151 "datatype::typeTwo", 152 mapping2.getFroms().iterator().next()); 153 assertEquals( 154 "Overridden", 155 mapping2.getTo()); 156 mapping3 = (Mapping)mappings2Iterator.next(); 157 assertEquals( 158 "datatype::typeThree", 159 mapping3.getFroms().iterator().next()); 160 assertEquals( 161 "Type_Three", 162 mapping3.getTo()); 163 Mapping mapping4 = (Mapping)mappings2Iterator.next(); 164 assertEquals( 165 "datatype::typeFour", 166 mapping4.getFroms().iterator().next()); 167 assertEquals( 168 "Type_Four", 169 mapping4.getTo()); 170 } 171 172 /** 173 * 174 */ 175 public void testEmptyMappings() 176 { 177 final URL testEmptyMappingsUri = MappingsTest.class.getResource("TestMappingsEmpty.xml"); 178 assertNotNull(testEmptyMappingsUri); 179 180 final Mappings mappings = Mappings.getInstance(testEmptyMappingsUri); 181 assertNotNull(mappings); 182 183 final Collection<Mapping> mappingCollection = mappings.getMappings(); 184 assertEquals(0, mappingCollection.size()); 185 } 186 187 /** 188 * 189 */ 190 public void testTransitivelyExtendingLogicalMappings() 191 { 192 // the order has been mixed up on purpose 193 Mappings.addLogicalMappings(MappingsTest.class.getResource("TestMappingsExtendsLevelA.xml")); 194 Mappings.addLogicalMappings(MappingsTest.class.getResource("TestMappingsExtendsLevelD.xml")); 195 Mappings.addLogicalMappings(MappingsTest.class.getResource("TestMappingsExtendsLevelC.xml")); 196 Mappings.addLogicalMappings(MappingsTest.class.getResource("TestMappingsExtendsLevelB.xml")); 197 198 Mappings.initializeLogicalMappings(); 199 200 final Mappings mappings = Mappings.getInstance("TestMappingsExtendsLevelD"); 201 assertNotNull(mappings); 202 203 final Mapping aaa = mappings.getMapping("datatype::aaa"); 204 assertNotNull(aaa); 205 assertEquals("AAA", aaa.getTo()); 206 207 final Mapping bbb = mappings.getMapping("datatype::bbb"); 208 assertNotNull(bbb); 209 assertEquals("BBB", bbb.getTo()); 210 211 final Mapping ccc = mappings.getMapping("datatype::ccc"); 212 assertNotNull(ccc); 213 assertEquals("CCC", ccc.getTo()); 214 215 final Mapping ddd = mappings.getMapping("datatype::ddd"); 216 assertNotNull(ddd); 217 assertEquals("DDD", ddd.getTo()); 218 } 219 220 /** 221 * 222 */ 223 public void testCyclicInheritanceLogicalMappingsException() 224 { 225 Mappings.addLogicalMappings(MappingsTest.class.getResource("TestMappingsCyclicA.xml")); 226 Mappings.addLogicalMappings(MappingsTest.class.getResource("TestMappingsCyclicB.xml")); 227 228 try 229 { 230 Mappings.initializeLogicalMappings(); 231 fail("Expected exception"); 232 } 233 catch (MappingsException mappingsException) 234 { 235 final String message = mappingsException.getMessage(); 236 assertTrue(message.startsWith("Logical mappings cannot be initialized due to invalid inheritance")); 237 assertTrue(message.contains("TestMappingsCyclicA")); 238 assertTrue(message.contains("TestMappingsCyclicB")); 239 } 240 finally 241 { 242 // clear out the cached entries so that the other tests won't fail because if the invalid 243 // ones we have entered here 244 Mappings.clearLogicalMappings(); 245 } 246 } 247}