-
Notifications
You must be signed in to change notification settings - Fork 1
/
CityGeneratorTest.java
245 lines (188 loc) · 7.47 KB
/
CityGeneratorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (c) 2020 Stefan Kuehnel - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Stefan Kuehnel <stefan.kuehnel@hm.edu>, May 2020
*
* DISCLAIMER. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OR CONDITION,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. THE AUTHOR HEREBY DISCLAIMS
* ALL LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE.
*/
package edu.hm.kuehnel.powergrid.datastore;
import edu.hm.cs.rs.powergrid.datastore.City;
import edu.hm.cs.rs.powergrid.datastore.Factory;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Die Testklasse fuer den CityGenerator
* @author Stefan Kuehnel, stefan.kuehnel@hm.edu
* @version last-modified 2020-05-16
*/
public class CityGeneratorTest {
/** Verhindert unendliche Schleifen. */
@Rule
public final Timeout globalTimeout = Timeout.seconds(1); // Maximale Anzahl an Sekunden pro Testfall.
/** FQCN der konkreten Factory Klasse */
private final String factoryFQCN;
/** Factory. */
private final Factory factory;
/** Initialisiert die Factory. */
public CityGeneratorTest() throws IOException {
factoryFQCN = "edu.hm.kuehnel.powergrid.datastore.FactoryProvider";
factory = Factory.newFactory(factoryFQCN);
}
/** Instanziiert eine neue Stadt mit fallbezogenen oder falschen Parametern. */
public City getSUT(String name, int region) {
return factory.newCity(name, region);
}
/** Instanziiert eine neue Stadt mit korrekten Parametern. */
public City getSUT() {
final String cityName = "Entenhausen";
final int region = 1;
return factory.newCity(cityName, region);
}
@Test (expected = IllegalArgumentException.class)
public void testNewCity_RequireArgument_NonNullName() {
final String name = null;
final int region = 1;
getSUT(name, region);
}
@Test (expected = IllegalArgumentException.class)
public void testNewCity_RequireArgument_NonEmptyName() {
final String name = "";
final int region = 1;
getSUT(name, region);
}
@Test
public void testGetName() {
final City sut = getSUT();
final String want = "Entenhausen";
final String have = sut.getName();
assertEquals(want, have);
}
@Test (expected = IllegalArgumentException.class)
public void testNewCity_RequireArgument_RegionNumberGreaterOrEqualOne() {
final String name = "Entenhausen";
final int region = -1;
getSUT(name, region);
}
@Test
public void testGetRegion_VerifyFunctionality_SuppliedAndReturnedRegionNumbersAreEqual() {
final City sut = getSUT();
final int want = 1;
final int have = sut.getRegion();
assertEquals(want, have);
}
@Test (expected = IllegalStateException.class)
public void testConnect_RequireArgument_NonClosedCity() {
final City sut = getSUT();
// Initialisieren der zu verbindenden Stadt.
final City city = getSUT("Entenburg", 2);
final int costs = 1;
// Die Stadt wird geschlossen.
sut.close();
// Die Verbindung kann aufgrund der geschlossenen Stadt jetzt nicht mehr gelegt werden.
sut.connect(city, costs);
}
@Test (expected = IllegalArgumentException.class)
public void testConnect_RequireArgument_NotThisCity() {
final City sut = getSUT();
sut.connect(sut, 1);
}
@Test (expected = IllegalArgumentException.class)
public void testConnect_RequireArgument_NonIdenticalCity() {
final City sut = getSUT();
final City city = getSUT("Entenhausen", 1);
final int costs = 1;
sut.connect(city, costs);
}
@Test (expected = IllegalArgumentException.class)
public void testConnect_RequireArgument_NonConnectedCity() {
final City sut = getSUT();
final City city = getSUT("Entenhausen", 1);
final int costs = 1;
// Verbindet die Stadt ein erstes Mal.
sut.connect(city, costs); // Hier funktioniert es.
// Verbindet die Stadt ein zweites Mal.
sut.connect(city, costs); // Hier funktioniert es nicht mehr.
}
@Test (expected = IllegalArgumentException.class)
public void testConnect_RequireArgument_NonNullCity() {
final City sut = getSUT();
final City city = null;
final int costs = 1;
sut.connect(city, costs);
}
@Test (expected = IllegalArgumentException.class)
public void testConnect_RequireArgument_NonNegativeCosts() {
final City sut = getSUT();
final City city = getSUT("Entenburg", 2);
final int costs = -1;
sut.connect(city, costs);
}
@Test
public void testConnect_VerifyFunctionality_CheckIfCityIsIndeedConnected() {
final City sut = getSUT();
// Die zu verbindende Stadt wird erstellt.
final City city = getSUT("Entenburg", 2);
final int costs = 1;
// Die Staedte werden verbunden.
sut.connect(city, costs);
// Prueft, ob die Stadt tatsächlich verbunden wurde.
final boolean cityHasBeenConnected = sut.getConnections().containsKey(city);
assertTrue(cityHasBeenConnected);
}
@Test (expected = IllegalStateException.class)
public void testGetConnections_RequireReturn_NonEmptyMap() {
final City sut = getSUT();
// Das Auslesen der leeren Verbindungsliste wird einen Fehler verursachen.
sut.getConnections();
}
@Test (expected = UnsupportedOperationException.class)
public void testGetConnections_RequireReturn_ImmutableMapWhenCityIsClosed() {
final City sut = getSUT();
// Die zu verbindende Stadt wird erstellt.
final City city = getSUT("Entenburg", 2);
final int costs = 1;
// Die Staedte werden verbunden.
sut.connect(city, costs);
// Die Stadt wird geschlossen.
sut.close();
// Pruefung der Map auf Unveraenderlichkeit.
sut.getConnections().put(city, costs);
}
@Test
public void testGetConnections_VerifyFunctionality_CityIsProperlyConnected() {
final City sut = getSUT();
// Die zu verbindende Stadt wird erstellt.
final City city = getSUT("Entenburg", 2);
final int costs = 1;
// Die Staedte werden verbunden.
sut.connect(city, costs);
// Pruefung, ob Stadt tatsaechlich verbunden wurde.
final boolean cityIsConnected = sut.getConnections().containsKey(city);
assertTrue(cityIsConnected);
}
@Test (expected = IllegalStateException.class)
public void testClose_DenyAction_MultipleCityCloseRequests() {
final City sut = getSUT();
// Die Stadt wird zum ersten Mal geschlossen.
sut.close();
// Die Stadt wird zum zweiten Mal geschlossen.
sut.close(); // Wird verweigert.
}
@Test
public void testCompareTo_VerifyFunctionality_SuppliedAndReturnedNaturalOrdersAreEqual() {
final City sut = getSUT();
final City city = getSUT("Entenburg", 1);
final int want = 6;
final int have = sut.compareTo(city);
assertEquals(want, have);
}
}