001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2010 The Gridarta Developers.
004 *
005 * This program is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 2 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License along
016 * with this program; if not, write to the Free Software Foundation, Inc.,
017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018 */
019
020package net.sf.gridarta.model.archetypetype;
021
022import java.util.Iterator;
023import net.sf.gridarta.model.anim.AnimationObjects;
024import net.sf.gridarta.model.anim.TestAnimationObjects;
025import net.sf.gridarta.model.archetype.TestDefaultArchetype;
026import net.sf.gridarta.model.baseobject.Attributes;
027import net.sf.gridarta.model.baseobject.BaseObject;
028import net.sf.gridarta.model.face.FaceObjectProviders;
029import net.sf.gridarta.model.face.FaceObjects;
030import net.sf.gridarta.model.face.TestFaceObjects;
031import net.sf.gridarta.utils.ResourceIcons;
032import org.junit.Assert;
033import org.junit.Test;
034
035/**
036 * Regression tests for {@link ArchetypeType}.
037 * @author Andreas Kirschbaum
038 */
039public class ArchetypeTypeTest {
040
041    /**
042     * Checks that empty expansions work correctly.
043     */
044    @Test
045    public void testAttributes1() {
046        final ResourceIcons resourceIcons = new ResourceIcons();
047        final FaceObjects faceObjects = new TestFaceObjects();
048        final FaceObjectProviders faceObjectProviders = new FaceObjectProviders(0, faceObjects, resourceIcons);
049        final AnimationObjects animationObjects = new TestAnimationObjects();
050        final ArchetypeAttributesDefinition typeAttributes = new ArchetypeAttributesDefinition();
051        final Attributes archetype = new TestDefaultArchetype("base", faceObjectProviders, animationObjects);
052
053        final ArchetypeType archetypeType1 = new ArchetypeType("name1", 1, "", true, null, false, "description", "use", typeAttributes);
054        Assert.assertEquals("name1 (1)", archetypeType1.getDisplayName(archetype));
055    }
056
057    /**
058     * Checks that ${xxx} expansions work correctly.
059     */
060    @Test
061    public void testAttributes2() {
062        final ResourceIcons resourceIcons = new ResourceIcons();
063        final FaceObjects faceObjects = new TestFaceObjects();
064        final FaceObjectProviders faceObjectProviders = new FaceObjectProviders(0, faceObjects, resourceIcons);
065        final AnimationObjects animationObjects = new TestAnimationObjects();
066        final ArchetypeAttributesDefinition typeAttributes = new ArchetypeAttributesDefinition();
067        final BaseObject<?, ?, ?, ?> archetype = new TestDefaultArchetype("base", faceObjectProviders, animationObjects);
068
069        final ArchetypeType archetypeType2 = new ArchetypeType("name2", 1, "a=${a}${a}", true, null, false, "description", "use", typeAttributes);
070        Assert.assertEquals("name2 (1) [a=]", archetypeType2.getDisplayName(archetype));
071
072        archetype.setObjectText("a xyz\n");
073        Assert.assertEquals("name2 (1) [a=xyzxyz]", archetypeType2.getDisplayName(archetype));
074
075        archetype.setObjectText("a ${a}\n");
076        Assert.assertEquals("name2 (1) [a=${a}${a}]", archetypeType2.getDisplayName(archetype));
077
078        // syntax errors are not fatal
079        final ArchetypeType archetypeType3 = new ArchetypeType("name", 1, "${}", true, null, false, "description", "use", typeAttributes);
080        Assert.assertEquals("name (1) []", archetypeType3.getDisplayName(archetype));
081
082        final ArchetypeType archetypeType4 = new ArchetypeType("name", 1, "${a", true, null, false, "description", "use", typeAttributes);
083        Assert.assertEquals("name (1) [${a]", archetypeType4.getDisplayName(archetype));
084    }
085
086    /**
087     * Checks that ${xxx?true:false} expansions work correctly.
088     */
089    @Test
090    public void testAttributes3() {
091        final ResourceIcons resourceIcons = new ResourceIcons();
092        final FaceObjects faceObjects = new TestFaceObjects();
093        final FaceObjectProviders faceObjectProviders = new FaceObjectProviders(0, faceObjects, resourceIcons);
094        final AnimationObjects animationObjects = new TestAnimationObjects();
095        final ArchetypeAttributesDefinition typeAttributes = new ArchetypeAttributesDefinition();
096        final BaseObject<?, ?, ?, ?> archetype = new TestDefaultArchetype("base", faceObjectProviders, animationObjects);
097
098        final ArchetypeType archetypeType2 = new ArchetypeType("name2", 1, "a=${a?True:False}", true, null, false, "description", "use", typeAttributes);
099        Assert.assertEquals("name2 (1) [a=False]", archetypeType2.getDisplayName(archetype));
100
101        archetype.setObjectText("a xyz\n");
102        Assert.assertEquals("name2 (1) [a=True]", archetypeType2.getDisplayName(archetype));
103
104        archetype.setObjectText("a 0\n");
105        Assert.assertEquals("name2 (1) [a=False]", archetypeType2.getDisplayName(archetype));
106
107        // empty replacements are allowed
108        final ArchetypeType archetypeType3 = new ArchetypeType("name", 1, "${a?:}", true, null, false, "description", "use", typeAttributes);
109        archetype.setObjectText("a 0\n");
110        Assert.assertEquals("name (1) []", archetypeType3.getDisplayName(archetype));
111        archetype.setObjectText("a 1\n");
112        Assert.assertEquals("name (1) []", archetypeType3.getDisplayName(archetype));
113
114        // replacements with multiple colons
115        final ArchetypeType archetypeType4 = new ArchetypeType("name", 1, "${a?b:c:d}", true, null, false, "description", "use", typeAttributes);
116        archetype.setObjectText("a 0\n");
117        Assert.assertEquals("name (1) [c:d]", archetypeType4.getDisplayName(archetype));
118        archetype.setObjectText("a 1\n");
119        Assert.assertEquals("name (1) [b]", archetypeType4.getDisplayName(archetype));
120
121        // syntax errors are not fatal
122        final ArchetypeType archetypeType5 = new ArchetypeType("name", 1, "${?", true, null, false, "description", "use", typeAttributes);
123        archetype.setObjectText("a 0\n");
124        Assert.assertEquals("name (1) [${?]", archetypeType5.getDisplayName(archetype));
125        archetype.setObjectText("a 1\n");
126        Assert.assertEquals("name (1) [${?]", archetypeType5.getDisplayName(archetype));
127
128        final ArchetypeType archetypeType6 = new ArchetypeType("name", 1, "${a?", true, null, false, "description", "use", typeAttributes);
129        archetype.setObjectText("a 0\n");
130        Assert.assertEquals("name (1) [${a?]", archetypeType6.getDisplayName(archetype));
131        archetype.setObjectText("a 1\n");
132        Assert.assertEquals("name (1) [${a?]", archetypeType6.getDisplayName(archetype));
133
134        final ArchetypeType archetypeType7 = new ArchetypeType("name", 1, "${a?:", true, null, false, "description", "use", typeAttributes);
135        archetype.setObjectText("a 0\n");
136        Assert.assertEquals("name (1) [${a?:]", archetypeType7.getDisplayName(archetype));
137        archetype.setObjectText("a 1\n");
138        Assert.assertEquals("name (1) [${a?:]", archetypeType7.getDisplayName(archetype));
139    }
140
141    /**
142     * Checks that {@link ArchetypeType#iterator()} returns a read-only
143     * iterator.
144     */
145    @Test(expected = UnsupportedOperationException.class)
146    public void testIteratorReadOnly() {
147        final ArchetypeAttributeSection archetypeAttributeSection = new ArchetypeAttributeSection("section");
148        archetypeAttributeSection.add(new ArchetypeAttributeInt("name", "name", "", 1, 0, 0, 0, 0));
149        final Iterable<?> archetypeType = new ArchetypeType("name", 0, "", false, null, false, null, null, new ArchetypeAttributesDefinition());
150        final Iterator<?> iterator = archetypeType.iterator();
151        Assert.assertTrue(iterator.hasNext());
152        iterator.next();
153        iterator.remove();
154    }
155
156}