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.autojoin;
021
022import net.sf.gridarta.model.archetype.DuplicateArchetypeException;
023import net.sf.gridarta.model.archetype.TestArchetype;
024import net.sf.gridarta.model.archetype.UndefinedArchetypeException;
025import net.sf.gridarta.model.gameobject.TestGameObject;
026import net.sf.gridarta.model.maparchobject.TestMapArchObject;
027import net.sf.gridarta.model.mapmodel.CannotInsertGameObjectException;
028import net.sf.gridarta.model.mapmodel.MapModel;
029import net.sf.gridarta.model.mapmodel.TestMapModelCreator;
030import net.sf.gridarta.model.mapmodel.TestMapModelHelper;
031import org.junit.Assert;
032import org.junit.Test;
033
034/**
035 * Regression tests for {@link AutojoinLists}.
036 * @author Andreas Kirschbaum
037 */
038public class AutojoinListsTest {
039
040    /**
041     * Checks that a wrong number of archetypes is detected.
042     * @throws IllegalAutojoinListException if the test fails
043     */
044    @Test
045    public void testWrongCount() throws IllegalAutojoinListException {
046        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper();
047        autojoinListsHelper.newAutojoinListsFail("autojoin list with less than 16 valid entries");
048        autojoinListsHelper.newAutojoinListsFail("autojoin list with less than 16 valid entries", "a0");
049        autojoinListsHelper.newAutojoinListsFail("autojoin list with less than 16 valid entries", "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14");
050        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15");
051        autojoinListsHelper.newAutojoinListsFail("autojoin list with more than 16 valid entries", "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15", "a16");
052    }
053
054    /**
055     * Checks that duplicate archetypes are detected.
056     * @throws IllegalAutojoinListException if the test fails
057     */
058    @Test
059    public void testDuplicateArchetype1() throws IllegalAutojoinListException {
060        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper();
061        //Check disabled for now as Daimonin uses this feature:
062        //autojoinListsHelper.newAutojoinListsFail("autojoin list contains duplicate archetype 'a0'", "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a0");
063        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a0");
064    }
065
066    /**
067     * Checks that duplicate archetypes are detected.
068     * @throws IllegalAutojoinListException if the test fails
069     */
070    @Test
071    public void testDuplicateArchetype2() throws IllegalAutojoinListException {
072        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper();
073        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15");
074        autojoinListsHelper.newAutojoinListsFail("archetype 'a4' contained in more than one autojoin list", "b0", "b1", "b2", "b3", "a4", "b5", "b6", "b7", "b8", "b9", "b10", "b11", "b12", "b13", "b14", "b15");
075        autojoinListsHelper.newAutojoinListsFail("autojoin list contains duplicate archetype 'c0'", "c0|c1|c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11", "c12", "c13", "c14", "c15");
076        autojoinListsHelper.newAutojoinListsFail("autojoin list is empty", "d0", "d1", "d2", "", "d4", "d5", "d6", "d7", "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15");
077    }
078
079    /**
080     * Checks that inserting with autojoin works as expected.
081     * @throws IllegalAutojoinListException if the test fails
082     * @throws DuplicateArchetypeException if the test fails
083     * @throws UndefinedArchetypeException if the test fails
084     * @throws CannotInsertGameObjectException if the test fails
085     */
086    @Test
087    public void testInsert1() throws CannotInsertGameObjectException, DuplicateArchetypeException, IllegalAutojoinListException, UndefinedArchetypeException {
088        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
089        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper(mapModelCreator);
090        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15");
091        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
092        final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
093        final TestArchetype a0 = mapModelCreator.getArchetype("a0");
094        final TestArchetype a1 = mapModelCreator.getArchetype("a1");
095        final TestArchetype a6 = mapModelCreator.getArchetype("a6");
096        final TestArchetype a8 = mapModelCreator.getArchetype("a8");
097        mapModel.beginTransaction("TEST");
098        testMapModelHelper.insertArchetype(mapModel, 1, 0, a1, false);
099        testMapModelHelper.insertArchetype(mapModel, 0, 1, a8, false);
100
101        // insert using autojoin
102        testMapModelHelper.insertArchetype(mapModel, 1, 1, a0, true);
103        TestMapModelHelper.checkMapContents(mapModel, "|a5|", "a10|a9|");
104
105        // insert duplicate archetype is ignored
106        try {
107            testMapModelHelper.insertArchetype(mapModel, 1, 1, a0, true);
108            Assert.fail();
109        } catch (final CannotInsertGameObjectException ignored) {
110            // ignore
111        }
112        TestMapModelHelper.checkMapContents(mapModel, "|a5|", "a10|a9|");
113
114        // insert using autojoin
115        testMapModelHelper.insertArchetype(mapModel, 2, 1, a6, true);
116        TestMapModelHelper.checkMapContents(mapModel, "|a5|", "a10|a11|a8");
117
118        // insert without autojoin
119        testMapModelHelper.insertArchetype(mapModel, 2, 0, a6, false);
120        TestMapModelHelper.checkMapContents(mapModel, "|a5|a6", "a10|a11|a8");
121    }
122
123    /**
124     * Checks that removing with autojoin works as expected.
125     * @throws IllegalAutojoinListException if the test fails
126     * @throws DuplicateArchetypeException if the test fails
127     * @throws UndefinedArchetypeException if the test fails
128     * @throws CannotInsertGameObjectException if the test fails
129     */
130    @Test
131    public void testRemove1() throws CannotInsertGameObjectException, DuplicateArchetypeException, IllegalAutojoinListException, UndefinedArchetypeException {
132        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
133        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper(mapModelCreator);
134        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15");
135        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
136        final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
137        final TestArchetype a4 = mapModelCreator.getArchetype("a4");
138        final TestArchetype a8 = mapModelCreator.getArchetype("a8");
139        final TestArchetype a10 = mapModelCreator.getArchetype("a10");
140        final TestArchetype a11 = mapModelCreator.getArchetype("a11");
141        mapModel.beginTransaction("TEST");
142        final TestGameObject g10 = testMapModelHelper.insertArchetype(mapModel, 1, 0, a4, false);
143        final TestGameObject g01 = testMapModelHelper.insertArchetype(mapModel, 0, 1, a10, false);
144        testMapModelHelper.insertArchetype(mapModel, 1, 1, a11, false);
145        final TestGameObject g21 = testMapModelHelper.insertArchetype(mapModel, 2, 1, a8, false);
146
147        // remove using autojoin
148        mapModel.removeGameObject(g21, true);
149        TestMapModelHelper.checkMapContents(mapModel, "|a4|", "a10|a9|");
150
151        // remove without autojoin
152        mapModel.removeGameObject(g10, false);
153        TestMapModelHelper.checkMapContents(mapModel, "||", "a10|a9|");
154
155        // remove using autojoin
156        mapModel.removeGameObject(g01, true);
157        TestMapModelHelper.checkMapContents(mapModel, "||", "|a1|");
158    }
159
160    /**
161     * Checks that inserting alternative archetypes with autojoin works as
162     * expected.
163     * @throws IllegalAutojoinListException if the test fails
164     * @throws DuplicateArchetypeException if the test fails
165     * @throws UndefinedArchetypeException if the test fails
166     * @throws CannotInsertGameObjectException if the test fails
167     */
168    @Test
169    public void testInsertAlt1() throws CannotInsertGameObjectException, DuplicateArchetypeException, IllegalAutojoinListException, UndefinedArchetypeException {
170        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
171        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper(mapModelCreator);
172        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5|b5", "a6", "a7", "a8", "a9", "a10|b10", "a11", "a12", "a13", "a14", "a15");
173        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
174        final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
175        final TestArchetype a0 = mapModelCreator.getArchetype("a0");
176        final TestArchetype a3 = mapModelCreator.getArchetype("a3");
177        final TestArchetype a8 = mapModelCreator.getArchetype("a8");
178        final TestArchetype b5 = mapModelCreator.getArchetype("b5");
179        final TestArchetype b10 = mapModelCreator.getArchetype("b10");
180        mapModel.beginTransaction("TEST");
181        testMapModelHelper.insertArchetype(mapModel, 0, 0, b5, false);
182        testMapModelHelper.insertArchetype(mapModel, 2, 0, b10, false);
183        testMapModelHelper.insertArchetype(mapModel, 0, 1, a3, false);
184        testMapModelHelper.insertArchetype(mapModel, 1, 1, a8, false);
185
186        // insert using autojoin
187        testMapModelHelper.insertArchetype(mapModel, 1, 0, a0, true);
188        TestMapModelHelper.checkMapContents(mapModel, "b5|a6|b10", "a3|a9|");
189    }
190
191    /**
192     * Checks that inserting alternative archetypes with autojoin works as
193     * expected.
194     * @throws IllegalAutojoinListException if the test fails
195     * @throws DuplicateArchetypeException if the test fails
196     * @throws UndefinedArchetypeException if the test fails
197     * @throws CannotInsertGameObjectException if the test fails
198     */
199    @Test
200    public void testInsertAlt2() throws CannotInsertGameObjectException, DuplicateArchetypeException, IllegalAutojoinListException, UndefinedArchetypeException {
201        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
202        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper(mapModelCreator);
203        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5|b5", "a6", "a7", "a8", "a9", "a10|b10", "a11", "a12", "a13", "a14", "a15");
204        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
205        final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
206        final TestArchetype a3 = mapModelCreator.getArchetype("a3");
207        final TestArchetype a8 = mapModelCreator.getArchetype("a8");
208        final TestArchetype b5 = mapModelCreator.getArchetype("b5");
209        final TestArchetype b10 = mapModelCreator.getArchetype("b10");
210        mapModel.beginTransaction("TEST");
211        testMapModelHelper.insertArchetype(mapModel, 0, 0, b5, false);
212        testMapModelHelper.insertArchetype(mapModel, 2, 0, b10, false);
213        testMapModelHelper.insertArchetype(mapModel, 0, 1, a3, false);
214        testMapModelHelper.insertArchetype(mapModel, 1, 1, a8, false);
215
216        // insert using autojoin
217        testMapModelHelper.insertArchetype(mapModel, 1, 0, b10, true);
218        TestMapModelHelper.checkMapContents(mapModel, "b5|b10|b10", "a3|a8|");
219    }
220
221    /**
222     * Checks that inserting alternative archetypes with autojoin works as
223     * expected.
224     * @throws IllegalAutojoinListException if the test fails
225     * @throws DuplicateArchetypeException if the test fails
226     * @throws UndefinedArchetypeException if the test fails
227     * @throws CannotInsertGameObjectException if the test fails
228     */
229    @Test
230    public void testInsertAlt3() throws CannotInsertGameObjectException, DuplicateArchetypeException, IllegalAutojoinListException, UndefinedArchetypeException {
231        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
232        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper(mapModelCreator);
233        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5|b5", "a6", "a7", "a8", "a9", "a10|b10", "a11", "a12", "a13", "a14", "a15");
234        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
235        final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
236        final TestArchetype a3 = mapModelCreator.getArchetype("a3");
237        final TestArchetype a8 = mapModelCreator.getArchetype("a8");
238        final TestArchetype b5 = mapModelCreator.getArchetype("b5");
239        final TestArchetype b10 = mapModelCreator.getArchetype("b10");
240        mapModel.beginTransaction("TEST");
241        testMapModelHelper.insertArchetype(mapModel, 0, 0, b5, false);
242        testMapModelHelper.insertArchetype(mapModel, 2, 0, b10, false);
243        testMapModelHelper.insertArchetype(mapModel, 0, 1, a3, false);
244        testMapModelHelper.insertArchetype(mapModel, 1, 1, a8, false);
245
246        // insert using autojoin
247        testMapModelHelper.insertArchetype(mapModel, 1, 0, b5, true);
248        TestMapModelHelper.checkMapContents(mapModel, "b5|b5|b10", "a3|a9|");
249    }
250
251    /**
252     * Checks that removing alternative archetypes with autojoin works as
253     * expected.
254     * @throws IllegalAutojoinListException if the test fails
255     * @throws DuplicateArchetypeException if the test fails
256     * @throws UndefinedArchetypeException if the test fails
257     * @throws CannotInsertGameObjectException if the test fails
258     */
259    @Test
260    public void testRemoveAlt1() throws CannotInsertGameObjectException, DuplicateArchetypeException, IllegalAutojoinListException, UndefinedArchetypeException {
261        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
262        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper(mapModelCreator);
263        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5|b5", "a6", "a7", "a8", "a9", "a10|b10", "a11", "a12", "a13", "a14", "a15");
264        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
265        final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
266        final TestArchetype a10 = mapModelCreator.getArchetype("a10");
267        final TestArchetype b10 = mapModelCreator.getArchetype("b10");
268        final TestArchetype a11 = mapModelCreator.getArchetype("a11");
269        mapModel.beginTransaction("TEST");
270        testMapModelHelper.insertArchetype(mapModel, 0, 0, a10, false);
271        final TestGameObject g10 = testMapModelHelper.insertArchetype(mapModel, 1, 0, b10, false);
272        testMapModelHelper.insertArchetype(mapModel, 2, 0, a10, false);
273        testMapModelHelper.insertArchetype(mapModel, 1, 1, a11, false);
274
275        // remove using autojoin
276        mapModel.removeGameObject(g10, true);
277        TestMapModelHelper.checkMapContents(mapModel, "a8||a2", "|a11|");
278    }
279
280    /**
281     * Checks that removing alternative archetypes with autojoin works as
282     * expected.
283     * @throws IllegalAutojoinListException if the test fails
284     * @throws DuplicateArchetypeException if the test fails
285     * @throws UndefinedArchetypeException if the test fails
286     * @throws CannotInsertGameObjectException if the test fails
287     */
288    @Test
289    public void testRemoveAlt2() throws CannotInsertGameObjectException, DuplicateArchetypeException, IllegalAutojoinListException, UndefinedArchetypeException {
290        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
291        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper(mapModelCreator);
292        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5|b5", "a6", "a7", "a8", "a9", "a10|b10", "a11", "a12", "a13", "a14", "a15");
293        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
294        final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
295        final TestArchetype b5 = mapModelCreator.getArchetype("b5");
296        final TestArchetype b10 = mapModelCreator.getArchetype("b10");
297        final TestArchetype a11 = mapModelCreator.getArchetype("a11");
298        final TestArchetype a12 = mapModelCreator.getArchetype("a12");
299        mapModel.beginTransaction("TEST");
300        testMapModelHelper.insertArchetype(mapModel, 0, 0, b10, false);
301        final TestGameObject g10 = testMapModelHelper.insertArchetype(mapModel, 1, 0, a12, false);
302        testMapModelHelper.insertArchetype(mapModel, 2, 0, b5, false);
303        testMapModelHelper.insertArchetype(mapModel, 1, 1, a11, false);
304
305        // remove using autojoin
306        mapModel.removeGameObject(g10, true);
307        TestMapModelHelper.checkMapContents(mapModel, "b10||b5", "|a10|");
308    }
309
310    /**
311     * Checks that removing alternative archetypes with autojoin works as
312     * expected.
313     * @throws IllegalAutojoinListException if the test fails
314     * @throws DuplicateArchetypeException if the test fails
315     * @throws UndefinedArchetypeException if the test fails
316     * @throws CannotInsertGameObjectException if the test fails
317     */
318    @Test
319    public void testRemoveAlt3() throws CannotInsertGameObjectException, DuplicateArchetypeException, IllegalAutojoinListException, UndefinedArchetypeException {
320        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
321        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper(mapModelCreator);
322        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5|b5", "a6", "a7", "a8", "a9", "a10|b10", "a11", "a12", "a13", "a14", "a15");
323        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
324        final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
325        final TestArchetype b10 = mapModelCreator.getArchetype("b10");
326        final TestArchetype a11 = mapModelCreator.getArchetype("a11");
327        mapModel.beginTransaction("TEST");
328        testMapModelHelper.insertArchetype(mapModel, 0, 0, b10, false);
329        final TestGameObject g10 = testMapModelHelper.insertArchetype(mapModel, 1, 0, b10, false);
330        testMapModelHelper.insertArchetype(mapModel, 2, 0, b10, false);
331        testMapModelHelper.insertArchetype(mapModel, 1, 1, a11, false);
332
333        // remove using autojoin
334        mapModel.removeGameObject(g10, true);
335        TestMapModelHelper.checkMapContents(mapModel, "b10||b10", "|a11|");
336    }
337
338    /**
339     * Checks that inserting duplicate archetypes with autojoin works as
340     * expected.
341     * @throws IllegalAutojoinListException if the test fails
342     * @throws DuplicateArchetypeException if the test fails
343     * @throws UndefinedArchetypeException if the test fails
344     * @throws CannotInsertGameObjectException if the test fails
345     */
346    @Test
347    public void testInsertDuplicate1() throws CannotInsertGameObjectException, DuplicateArchetypeException, IllegalAutojoinListException, UndefinedArchetypeException {
348        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
349        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper(mapModelCreator);
350        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5|b5", "a6", "a7", "a8", "a9", "a10|b10", "a11", "a12", "a13", "a14", "a15");
351        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 3);
352        final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
353        final TestArchetype a5 = mapModelCreator.getArchetype("a5");
354        final TestArchetype b5 = mapModelCreator.getArchetype("b5");
355        final TestArchetype a10 = mapModelCreator.getArchetype("a10");
356        final TestArchetype b10 = mapModelCreator.getArchetype("b10");
357        final TestArchetype a15 = mapModelCreator.getArchetype("a15");
358        mapModel.beginTransaction("TEST");
359        testMapModelHelper.insertArchetype(mapModel, 1, 0, a10, false);
360        testMapModelHelper.insertArchetype(mapModel, 0, 1, b10, false);
361        testMapModelHelper.insertArchetype(mapModel, 1, 1, a15, false);
362        testMapModelHelper.insertArchetype(mapModel, 2, 1, a5, false);
363        testMapModelHelper.insertArchetype(mapModel, 1, 2, b5, false);
364
365        try {
366            testMapModelHelper.insertArchetype(mapModel, 1, 1, a10, true);
367            Assert.fail();
368        } catch (final CannotInsertGameObjectException ignored) {
369            // ignore
370        }
371        try {
372            testMapModelHelper.insertArchetype(mapModel, 0, 1, b10, true);
373            Assert.fail();
374        } catch (final CannotInsertGameObjectException ignored) {
375            // ignore
376        }
377        TestMapModelHelper.checkMapContents(mapModel, "|a10|", "b10|a15|a5", "|b5|");
378    }
379
380    /**
381     * Checks that replacing archetypes works as expected.
382     * @throws IllegalAutojoinListException if the test fails
383     * @throws DuplicateArchetypeException if the test fails
384     * @throws UndefinedArchetypeException if the test fails
385     * @throws CannotInsertGameObjectException if the test fails
386     */
387    @Test
388    public void testInsertReplace1() throws CannotInsertGameObjectException, DuplicateArchetypeException, IllegalAutojoinListException, UndefinedArchetypeException {
389        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
390        final AutojoinListsHelper autojoinListsHelper = new AutojoinListsHelper(mapModelCreator);
391        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5|b5", "a6", "a7", "a8", "a9", "a10|b10", "a11", "a12", "a13", "a14", "a15");
392        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 3);
393        final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
394        final TestArchetype a0 = mapModelCreator.getArchetype("a0");
395        final TestArchetype a5 = mapModelCreator.getArchetype("a5");
396        final TestArchetype b5 = mapModelCreator.getArchetype("b5");
397        final TestArchetype a10 = mapModelCreator.getArchetype("a10");
398        final TestArchetype b10 = mapModelCreator.getArchetype("b10");
399        mapModel.beginTransaction("TEST");
400        for (int y = 0; y < 3; y++) {
401            for (int x = 0; x < 3; x++) {
402                testMapModelHelper.insertArchetype(mapModel, x, y, a10, false);
403            }
404        }
405
406        TestMapModelHelper.checkMapContents(mapModel, "a10|a10|a10", "a10|a10|a10", "a10|a10|a10");
407        testMapModelHelper.insertArchetype(mapModel, 1, 1, b5, true); // replace main->alt
408        TestMapModelHelper.checkMapContents(mapModel, "a10|a14|a10", "a8|b5|a2", "a10|a11|a10");
409        testMapModelHelper.insertArchetype(mapModel, 1, 1, b10, true); // replace alt->alt, different archetype
410        TestMapModelHelper.checkMapContents(mapModel, "a10|a10|a10", "a10|b10|a10", "a10|a10|a10");
411        testMapModelHelper.insertArchetype(mapModel, 1, 1, a5, true); // replace alt->main
412        TestMapModelHelper.checkMapContents(mapModel, "a10|a14|a10", "a10|a15|a10", "a10|a11|a10");
413        testMapModelHelper.insertArchetype(mapModel, 1, 1, b10, true); // replace main->alt
414        TestMapModelHelper.checkMapContents(mapModel, "a10|a10|a10", "a10|b10|a10", "a10|a10|a10");
415        try {
416            testMapModelHelper.insertArchetype(mapModel, 1, 1, b10, true); // replace alt->alt, same archetype
417            Assert.fail();
418        } catch (final CannotInsertGameObjectException ignored) {
419            // ignore
420        }
421        TestMapModelHelper.checkMapContents(mapModel, "a10|a10|a10", "a10|b10|a10", "a10|a10|a10");
422        try {
423            testMapModelHelper.insertArchetype(mapModel, 1, 0, a0, true); // replace main->main
424            Assert.fail();
425        } catch (final CannotInsertGameObjectException ignored) {
426            // ignore
427        }
428        TestMapModelHelper.checkMapContents(mapModel, "a10|a10|a10", "a10|b10|a10", "a10|a10|a10");
429    }
430
431}