001package org.andromda.core.cartridge;
002
003import java.util.Collection;
004import java.util.Iterator;
005import java.util.Map;
006import junit.framework.TestCase;
007import org.andromda.core.cartridge.template.ModelElement;
008import org.andromda.core.cartridge.template.Template;
009import org.andromda.core.cartridge.template.Type;
010import org.andromda.core.cartridge.template.Type.Property;
011import org.andromda.core.common.ComponentContainer;
012import org.andromda.core.common.TemplateObject;
013import org.andromda.core.namespace.NamespaceComponents;
014import org.andromda.core.profile.Profile;
015
016/**
017 * Implements the JUnit test suit for
018 * {@link org.andromda.core.cartridge.Cartridge}
019 *
020 * @see org.andromda.core.cartridge.Cartridge
021 * @since 01.04.2003
022 * @author <a href="http://www.mbohlen.de">Matthias Bohlen </a>
023 * @author Chad Brandon
024 */
025public class CartridgeTest
026    extends TestCase
027{
028    private Cartridge cartridge;
029
030    /**
031     * Constructor for AndroMDATestCartridgeTest.
032     *
033     * @param name
034     */
035    public CartridgeTest(String name)
036    {
037        super(name);
038    }
039
040    /**
041     * @see TestCase#setUp()
042     */
043    protected void setUp()
044        throws Exception
045    {
046        NamespaceComponents.instance().discover();
047        Collection<Cartridge> cartridges = ComponentContainer.instance().findComponentsOfType(Cartridge.class);
048        assertNotNull(cartridges);
049        this.cartridge = cartridges.iterator().next();
050        Profile.instance().setNamespace(this.cartridge.getNamespace());
051        this.cartridge.initialize();
052    }
053
054    /**
055     * @see TestCase#tearDown()
056     */
057    protected void tearDown()
058        throws Exception
059    {
060        this.cartridge = null;
061    }
062
063    /**
064     *
065     */
066    public void testGetNamespace()
067    {
068        assertEquals(
069            "test",
070            this.cartridge.getNamespace());
071    }
072
073    /**
074     *
075     */
076    public void testGetResources()
077    {
078        Collection<Resource> resources = this.cartridge.getResources();
079        assertNotNull(resources);
080        assertEquals(
081            3,
082            resources.size());
083
084        // first template
085        final Iterator<Resource> templateIterator = resources.iterator();
086        Resource resource = templateIterator.next();
087        assertTrue(resource.isLastModifiedCheck());
088
089        Template template = (Template)templateIterator.next();
090        assertEquals(
091            "EntityBean.vsl",
092            template.getPath());
093        assertEquals(
094            "{0}/{1}Bean.java",
095            template.getOutputPattern());
096        assertEquals(
097            "beans",
098            template.getOutlet());
099        assertTrue(template.isOverwrite());
100        assertNotNull(template.getSupportedModeElements());
101        assertEquals(
102            "entity",
103            template.getSupportedModeElements().getVariable());
104        Collection<ModelElement> modelElements = template.getSupportedModeElements().getModelElements();
105        assertNotNull(modelElements);
106        assertEquals(
107            1,
108            modelElements.size());
109        ModelElement element = modelElements.iterator().next();
110        assertEquals(
111            "Entity",
112            element.getStereotype());
113
114        // second template
115        template = (Template)templateIterator.next();
116        assertEquals(
117            "templates/webservice/axis/server-config.wsdd.vsl",
118            template.getPath());
119        assertEquals(
120            "WEB-INF/server-config.wsdd",
121            template.getOutputPattern());
122        assertEquals(
123            "axis-configuration",
124            template.getOutlet());
125        assertEquals("$viewType.equals('jsp')", template.getOutputCondition());
126        assertTrue(template.isOverwrite());
127        assertTrue(template.isOutputToSingleFile());
128        assertFalse(template.isOutputOnEmptyElements());
129        assertNotNull(template.getSupportedModeElements());
130        assertEquals(
131            "services",
132            template.getSupportedModeElements().getVariable());
133        modelElements = template.getSupportedModeElements().getModelElements();
134        assertNotNull(modelElements);
135        assertEquals(
136            1,
137            modelElements.size());
138        element = modelElements.iterator().next();
139        assertNull(element.getVariable());
140        assertNull(element.getStereotype());
141
142        final Collection<Type> types = element.getTypes();
143        assertNotNull(types);
144        assertEquals(1, types.size());
145        final Type type = types.iterator().next();
146        final Collection<Property> properties = type.getProperties();
147        assertEquals(2, properties.size());
148        final Iterator propertyIterator = properties.iterator();
149        Type.Property property1 = (Type.Property)propertyIterator.next();
150        assertEquals("propertyOne", property1.getName());
151        assertEquals("", property1.getValue());
152        Type.Property property2 = (Type.Property)propertyIterator.next();
153        assertEquals("propertyThree", property2.getName());
154        assertEquals("Contents", property2.getValue());
155
156        final Map<String, String> conditions = cartridge.getConditions();
157        assertEquals(1, conditions.size());
158        final String expressionName = conditions.keySet().iterator().next();
159        assertEquals("viewTypeIsJsp", expressionName);
160        final String expressionValue = conditions.get(expressionName);
161        assertEquals("$viewType.equalsIgnoreCase('jsp')", expressionValue);
162    }
163
164    /**
165     *
166     */
167    public void testGetPropertyReferences()
168    {
169        String[] propertyRefs = this.cartridge.getPropertyReferences();
170        assertNotNull(propertyRefs);
171        assertEquals(
172            2,
173            propertyRefs.length);
174
175        String propertyReferenceOne = "propertyReferenceOne";
176        String propertyReferenceTwo = "propertyReferenceTwo";
177
178        assertEquals(propertyReferenceOne, propertyRefs[0]);
179        assertEquals(propertyReferenceTwo, propertyRefs[1]);
180    }
181
182    /**
183     *
184     */
185    public void testGetTemplateObjects()
186    {
187        final Collection<TemplateObject> templateObjects = this.cartridge.getTemplateObjects();
188        assertNotNull(templateObjects);
189        assertEquals(
190            1,
191            templateObjects.size());
192        TemplateObject templateObject = templateObjects.iterator().next();
193        assertEquals("utils", templateObject.getName());
194        assertEquals("test",templateObject.getNamespace());
195        CartridgeTemplateObject object = (CartridgeTemplateObject)templateObject.getObject();
196        assertNotNull(object);
197        assertEquals("3", object.getDefinitionOne());
198
199    }
200
201    /**
202     *
203     */
204    public void testGetContents()
205    {
206        Collection<String> contents = this.cartridge.getContents();
207        assertNotNull(contents);
208
209        //    TODO wrong Test, new Integer() are always not same!
210        // make sure there's more than 0 contents listed
211//        assertNotSame(
212//            new Integer(0),
213//            new Integer(contents.size()));
214    }
215}