2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
6
* This program is distributed in the hope that it will be useful,
7
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
* GNU General Public License for more details.
10
* You should have received a copy of the GNU General Public License
11
* along with this program; if not, write to the Free Software
12
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
* PlaceDelegateTest.java
18
* Created on November 8, 2001, 5:00 PM
21
package games.strategy.triplea.delegate;
23
import games.strategy.engine.data.ITestDelegateBridge;
24
import games.strategy.engine.data.PlayerID;
25
import games.strategy.engine.data.Territory;
26
import games.strategy.engine.data.Unit;
27
import games.strategy.engine.data.UnitType;
28
import games.strategy.triplea.Constants;
29
import games.strategy.triplea.delegate.dataObjects.PlaceableUnits;
30
import games.strategy.util.IntegerMap;
32
import java.util.ArrayList;
33
import java.util.Collection;
34
import java.util.HashMap;
35
import java.util.Iterator;
38
import junit.framework.Test;
39
import junit.framework.TestSuite;
42
* @author Sean Bridges
45
@SuppressWarnings("unchecked")
46
public class PlaceDelegateTest extends DelegateTest
49
protected PlaceDelegate m_delegate;
50
protected ITestDelegateBridge m_bridge;
52
/** Creates new PlaceDelegateTest */
53
public PlaceDelegateTest(String name)
58
public static Test suite()
60
TestSuite suite = new TestSuite();
61
suite.addTestSuite(PlaceDelegateTest.class);
66
private Collection getInfantry(int count, PlayerID player)
68
return m_data.getUnitTypeList().getUnitType(Constants.INFANTRY_TYPE).create(count, player);
73
public void setUp() throws Exception
78
m_bridge = super.getDelegateBridge(british);
79
m_delegate = new PlaceDelegate();
80
m_delegate.initialize("place");
81
m_delegate.start(m_bridge, m_data);
84
private Collection getUnits(IntegerMap<UnitType> units, PlayerID from)
86
Iterator<UnitType> iter = units.keySet().iterator();
87
Collection rVal = new ArrayList(units.totalValues());
90
UnitType type = iter.next();
91
rVal.addAll(from.getUnits().getUnits(type, units.getInt(type)));
97
public void testValid()
99
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
100
map.add(infantry ,2);
102
String response = m_delegate.placeUnits(getUnits(map, british), uk);
103
assertValid(response);
106
public void testNotCorrectUnitsValid()
109
String response = m_delegate.placeUnits(infantry.create(3, british), uk);
110
assertError(response);
113
public void testOnlySeaInSeaZone()
115
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
116
map.add(infantry ,2);
118
String response = m_delegate.canUnitsBePlaced(northSea, getUnits(map, british), british);
119
assertError(response);
122
public void testSeaCanGoInSeaZone()
124
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
125
map.add(transport ,2);
127
String response = m_delegate.canUnitsBePlaced(northSea, getUnits(map, british), british);
128
assertValid(response );
131
public void testLandCanGoInLandZone()
133
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
134
map.add(infantry ,2);
136
String response = m_delegate.placeUnits(getUnits(map, british), uk);
137
assertValid(response );
140
public void testSeaCantGoInSeaInLandZone()
142
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
143
map.add(transport ,2);
145
String response = m_delegate.canUnitsBePlaced(uk,getUnits(map, british),british);
146
assertError(response);
149
public void testNoGoIfOpposingTroopsSea()
151
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
152
map.add(transport, 2);
153
String response = m_delegate.canUnitsBePlaced(northSea, getUnits(map, japanese), japanese);
154
assertError(response);
158
public void testNoGoIfOpposingTroopsLand()
160
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
161
map.add(infantry, 2);
162
String response = m_delegate.canUnitsBePlaced(japan, getUnits(map, british), british);
164
assertError(response);
167
public void testOnlyOneFactoryPlaced()
169
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
171
String response = m_delegate.canUnitsBePlaced(uk, getUnits(map, british), british);
173
assertError(response );
176
public void testCantPlaceAAWhenOneAlreadyThere()
178
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
180
String response = m_delegate.canUnitsBePlaced(uk, getUnits(map, british), british);
181
assertError(response );
184
public void testCantPlaceTwoAA()
186
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
188
String response = m_delegate.canUnitsBePlaced(westCanada, getUnits(map, british), british);
189
assertError(response );
192
public void testProduceFactory()
194
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
196
String response = m_delegate.canUnitsBePlaced(egypt, getUnits(map, british), british);
197
assertValid(response );
200
public void testMustOwnToPlace()
202
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
203
map.add(infantry, 2);
204
String response = m_delegate.canUnitsBePlaced(germany, getUnits(map, british), british);
205
assertError(response );
209
public void testCanProduce()
211
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
213
map.add(infantry, 2);
214
PlaceableUnits response = m_delegate.getPlaceableUnits(getUnits(map, british), westCanada);
215
assertFalse(response.isError() );
218
public void testCanProduceInSea()
220
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
221
map.add(transport, 2);
222
PlaceableUnits response = m_delegate.getPlaceableUnits(getUnits(map, british), northSea);
223
assertFalse(response.isError() );
227
public void testCanNotProduceThatManyUnits()
229
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
231
map.add(infantry, 3);
232
PlaceableUnits response = m_delegate.getPlaceableUnits(getUnits(map, british), westCanada);
233
assertTrue(response.getMaxUnits() == 2 );
236
public void testAlreadyProducedUnits()
238
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
239
Map<Territory, Collection<Unit>> alreadyProduced = new HashMap<Territory, Collection<Unit>>();
240
alreadyProduced.put(westCanada, getInfantry(2, british));
241
m_delegate.setProduced(alreadyProduced);
242
map.add(infantry, 1);
243
PlaceableUnits response = m_delegate.getPlaceableUnits(getUnits(map, british), westCanada);
244
assertTrue(response.getMaxUnits() == 0);
247
public void testMultipleFactories()
249
IntegerMap<UnitType> map = new IntegerMap<UnitType>();
251
String response = m_delegate.canUnitsBePlaced(egypt, getUnits(map, british), british);
253
//we can place 1 factory
254
assertValid(response);
257
map = new IntegerMap<UnitType>();
259
response = m_delegate.canUnitsBePlaced(egypt, getUnits(map, british), british);
260
assertError(response);