Gridarta Editor
GameObjectFactoryTest.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.gameobject;
21 
22 import java.awt.Point;
23 import javax.swing.ImageIcon;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35 import org.junit.Assert;
36 import org.junit.Test;
37 
42 public class GameObjectFactoryTest {
43 
49  @Test
51  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
52  final TestMapModelHelper mapModelHelper = mapModelCreator.newTestMapModelHelper();
53  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(1, 1);
54 
55  final Point point = new Point(0, 0);
56  mapModel.beginTransaction("TEST");
57  final TestGameObject ob1 = mapModelHelper.insertFloor(mapModel, point);
58  final TestGameObject ob2 = mapModelHelper.insertExit(ob1);
59 
60  final TestGameObject ob1Clone = mapModelCreator.getGameObjectFactory().cloneGameObject(ob1);
61 
62  Assert.assertNotSame(ob1, ob1Clone);
63  Assert.assertEquals(1, ob1Clone.countInvObjects());
64  final TestGameObject ob2Clone = ob1Clone.getFirst();
65  Assert.assertNotNull(ob2Clone);
66  Assert.assertNotSame(ob2, ob2Clone);
67 
68  Assert.assertSame(ob1, ob2.getContainer());
69  Assert.assertSame(ob1Clone, ob2Clone.getContainer());
70  }
71 
77  @Test
79  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
80  final TestMapModelHelper mapModelHelper = mapModelCreator.newTestMapModelHelper();
81  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(1, 1);
82 
85 
86  final TestArchetype invArchetype = gameObjectFactory.newArchetype("inv");
87  invArchetype.setAttributeString(BaseObject.FACE, "face");
88  archetypeSet.addArchetype(invArchetype);
89 
90  final TestArchetype envArchetype = gameObjectFactory.newArchetype("env");
91  envArchetype.setAttributeString(BaseObject.FACE, "face");
92  envArchetype.addLast(gameObjectFactory.createGameObject(invArchetype));
93  archetypeSet.addArchetype(envArchetype);
94 
95  final FaceProvider faceProvider = new FaceProvider() {
96 
97  @Nullable
98  @Override
99  public ImageIcon getImageIconForFacename(@NotNull final String faceName, final long stretch) {
100  if (faceName.equals("face")) {
101  return new ImageIcon();
102  }
103  return null;
104  }
105 
106  @Override
107  public void reload() {
108  // do nothing
109  }
110 
111  };
112  mapModelCreator.getFaceObjectProviders().setNormal(faceProvider);
113 
114  final Point point = new Point(0, 0);
115  mapModel.beginTransaction("TEST");
116  final TestGameObject env = mapModelHelper.insertArchetype(mapModel, point, envArchetype, false);
117 
118  Assert.assertNotNull(env);
119  Assert.assertEquals(FaceSource.ARCHETYPE_FACE, env.getFaceObjSource());
120 
121  final TestGameObject inv = env.getFirst();
122  Assert.assertNotNull(inv);
123  Assert.assertEquals(FaceSource.ARCHETYPE_FACE, inv.getFaceObjSource());
124  }
125 
126 }
G createGameObject(@NotNull R archetype)
Creates a new GameObject from an Archetype.
void addLast(@NotNull G gameObject)
Adds the given GameObject at the end of this Container.
MapModel< TestGameObject, TestMapArchObject, TestArchetype > newMapModel(final int w, final int h)
Creates a new MapModel instance.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
ArchetypeSet< TestGameObject, TestMapArchObject, TestArchetype > getArchetypeSet()
Returns the ArchetypeSet.
Helper class for regression tests to create MapModel instances.
Enumeration describing the state of the face.
Definition: FaceSource.java:28
Helper class for creating MapModel instances for regression tests.
String FACE
The attribute name of the object&#39;s face.
Definition: BaseObject.java:54
This interface represents a lazy loader that provides images on demand.
FaceObjectProviders getFaceObjectProviders()
Returns the FaceObjectProviders instance.
A MapArchObject implementation for testing purposes.
TestGameObject insertFloor(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point)
Inserts a floorArchetype game object into a map model.
Base package of all Gridarta classes.
ARCHETYPE_FACE
This GameObject has no face defined and thus inherited the face from its archetype.
Definition: FaceSource.java:44
Abstract factory for creating GameObject instances.
void testDoMoveSquareTopSingle()
Checks that net.sf.gridarta.model.gameobject.GameObjectFactory#cloneGameObject(GameObject) correctly ...
TestGameObject insertArchetype(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, final int x, final int y, @NotNull final BaseObject< TestGameObject, TestMapArchObject, TestArchetype, ?> archetype, final boolean join)
Inserts an archetype game object into a map model.
An Exception indicating that an Archetype name is not unique.
void testUpdateFaceInformation()
Checks that face information is updated for inventory objects cloned from an archetype.
void setNormal(@NotNull final FaceProvider normalFaceProvider)
Sets the normal FaceProvider.
R newArchetype(@NotNull String archetypeName)
Creates a new Archetype instance.
void addArchetype(@NotNull R archetype)
Adds an Archetype to this Set.
TestGameObject insertExit(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point)
Inserts an exitArchetype game object into a map model.
TestMapModelHelper newTestMapModelHelper()
Creates a new TestMapModelHelper instance.
The face is the appearance of an object.
An Archetype implementation for testing purposes.
A GameObject implementation for testing purposes.
Interface that captures similarities between different ArchetypeSet implementations.
void beginTransaction(@NotNull String name)
Starts a new transaction.
GameObjectFactory< TestGameObject, TestMapArchObject, TestArchetype > getGameObjectFactory()
Returns the GameObjectFactory instance.