~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to test/games/strategy/triplea/delegate/PlaceDelegateTest.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
13
 */
 
14
 
 
15
/*
 
16
 * PlaceDelegateTest.java
 
17
 *
 
18
 * Created on November 8, 2001, 5:00 PM
 
19
 */
 
20
 
 
21
package games.strategy.triplea.delegate;
 
22
 
 
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;
 
31
 
 
32
import java.util.ArrayList;
 
33
import java.util.Collection;
 
34
import java.util.HashMap;
 
35
import java.util.Iterator;
 
36
import java.util.Map;
 
37
 
 
38
import junit.framework.Test;
 
39
import junit.framework.TestSuite;
 
40
/**
 
41
 *
 
42
 * @author  Sean Bridges
 
43
 * @version 1.0
 
44
 */
 
45
@SuppressWarnings("unchecked")
 
46
public class PlaceDelegateTest extends DelegateTest
 
47
{
 
48
                
 
49
        protected PlaceDelegate m_delegate;
 
50
        protected ITestDelegateBridge m_bridge;
 
51
        
 
52
        /** Creates new PlaceDelegateTest */
 
53
    public PlaceDelegateTest(String name) 
 
54
        {
 
55
                super(name);
 
56
    }
 
57
 
 
58
        public static Test suite()
 
59
        {
 
60
                TestSuite suite = new TestSuite();
 
61
                suite.addTestSuite(PlaceDelegateTest.class);
 
62
                
 
63
                return suite;
 
64
        }
 
65
        
 
66
        private Collection getInfantry(int count, PlayerID player)
 
67
        {
 
68
            return m_data.getUnitTypeList().getUnitType(Constants.INFANTRY_TYPE).create(count, player);
 
69
        }
 
70
 
 
71
 
 
72
        
 
73
        public void setUp() throws Exception
 
74
        {
 
75
                super.setUp();
 
76
                
 
77
                
 
78
                m_bridge = super.getDelegateBridge(british);
 
79
                m_delegate = new PlaceDelegate();
 
80
                m_delegate.initialize("place");
 
81
                m_delegate.start(m_bridge, m_data);
 
82
        }
 
83
 
 
84
        private Collection getUnits(IntegerMap<UnitType> units, PlayerID from)
 
85
        {
 
86
                Iterator<UnitType> iter = units.keySet().iterator();
 
87
                Collection rVal = new ArrayList(units.totalValues());
 
88
                while(iter.hasNext())
 
89
                {
 
90
                        UnitType type = iter.next();
 
91
                        rVal.addAll(from.getUnits().getUnits(type, units.getInt(type)));
 
92
                }
 
93
                return rVal;
 
94
        }
 
95
 
 
96
        
 
97
        public void testValid()
 
98
        {
 
99
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
100
                map.add(infantry ,2);
 
101
                
 
102
                String response = m_delegate.placeUnits(getUnits(map, british), uk);
 
103
                assertValid(response);
 
104
        }
 
105
        
 
106
        public void testNotCorrectUnitsValid()
 
107
        {
 
108
                
 
109
                String response = m_delegate.placeUnits(infantry.create(3, british), uk);
 
110
                assertError(response);
 
111
        }
 
112
 
 
113
        public void testOnlySeaInSeaZone()
 
114
        {
 
115
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
116
                map.add(infantry ,2);
 
117
                
 
118
                String response = m_delegate.canUnitsBePlaced(northSea, getUnits(map, british), british);
 
119
                assertError(response);
 
120
        }
 
121
 
 
122
        public void testSeaCanGoInSeaZone()
 
123
        {
 
124
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
125
                map.add(transport ,2);
 
126
                
 
127
                String response = m_delegate.canUnitsBePlaced(northSea, getUnits(map, british), british);
 
128
                assertValid(response );
 
129
        }       
 
130
        
 
131
        public void testLandCanGoInLandZone()
 
132
        {
 
133
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
134
                map.add(infantry ,2);
 
135
                
 
136
                String response = m_delegate.placeUnits(getUnits(map, british), uk);
 
137
                assertValid(response );
 
138
        }
 
139
 
 
140
        public void testSeaCantGoInSeaInLandZone()
 
141
        {
 
142
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
143
                map.add(transport ,2);
 
144
                
 
145
                String response = m_delegate.canUnitsBePlaced(uk,getUnits(map, british),british);
 
146
                assertError(response);
 
147
        }       
 
148
        
 
149
        public void testNoGoIfOpposingTroopsSea()
 
150
        {
 
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);
 
155
                
 
156
        }
 
157
        
 
158
        public void testNoGoIfOpposingTroopsLand()
 
159
        {
 
160
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
161
                map.add(infantry, 2);
 
162
                String response = m_delegate.canUnitsBePlaced(japan, getUnits(map, british), british);
 
163
                
 
164
                assertError(response);
 
165
        }
 
166
        
 
167
        public void testOnlyOneFactoryPlaced()
 
168
        {
 
169
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
170
                map.add(factory, 1);
 
171
                String response = m_delegate.canUnitsBePlaced(uk, getUnits(map, british), british);
 
172
                
 
173
                assertError(response );
 
174
        }
 
175
 
 
176
        public void testCantPlaceAAWhenOneAlreadyThere()
 
177
        {
 
178
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
179
                map.add(aaGun, 1);
 
180
                String response = m_delegate.canUnitsBePlaced(uk, getUnits(map, british), british);
 
181
                assertError(response );
 
182
        }
 
183
 
 
184
        public void testCantPlaceTwoAA()
 
185
        {
 
186
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
187
                map.add(aaGun, 2);
 
188
                String response = m_delegate.canUnitsBePlaced(westCanada, getUnits(map, british), british);
 
189
                assertError(response );
 
190
        }
 
191
        
 
192
        public void testProduceFactory()
 
193
        {
 
194
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
195
                map.add(factory, 1);
 
196
                String response = m_delegate.canUnitsBePlaced(egypt, getUnits(map, british), british);
 
197
                assertValid(response );
 
198
        }
 
199
 
 
200
                public void testMustOwnToPlace()
 
201
        {
 
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 );
 
206
        }
 
207
 
 
208
        
 
209
        public void testCanProduce()
 
210
        {
 
211
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
212
                
 
213
                map.add(infantry, 2);
 
214
                PlaceableUnits response = m_delegate.getPlaceableUnits(getUnits(map, british), westCanada);
 
215
                assertFalse(response.isError() );
 
216
        }
 
217
        
 
218
        public void testCanProduceInSea()
 
219
        {
 
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() );
 
224
        }
 
225
        
 
226
        
 
227
        public void testCanNotProduceThatManyUnits()
 
228
        {
 
229
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
230
                
 
231
                map.add(infantry, 3);
 
232
                PlaceableUnits response = m_delegate.getPlaceableUnits(getUnits(map, british), westCanada);
 
233
                assertTrue(response.getMaxUnits() == 2 );
 
234
        }
 
235
        
 
236
        public void testAlreadyProducedUnits()
 
237
        {
 
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);
 
245
        }
 
246
        
 
247
        public void testMultipleFactories()
 
248
        {
 
249
                IntegerMap<UnitType> map = new IntegerMap<UnitType>();
 
250
                map.add(factory, 1);
 
251
                String response = m_delegate.canUnitsBePlaced(egypt, getUnits(map, british), british);
 
252
                
 
253
                //we can place 1 factory
 
254
                assertValid(response);
 
255
                
 
256
                //we cant place 2
 
257
                map = new IntegerMap<UnitType>();
 
258
                map.add(factory, 2);
 
259
                response = m_delegate.canUnitsBePlaced(egypt, getUnits(map, british), british);
 
260
                assertError(response);
 
261
        }
 
262
}