Skip to content

Commit 1849d78

Browse files
Migrate tests to JUnit5
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent e725b51 commit 1849d78

File tree

3 files changed

+115
-129
lines changed

3 files changed

+115
-129
lines changed

src/test/java/org/jenkinsci/plugins/vsphere/tools/CloudProvisioningAlgorithmTest.java

+30-35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.jenkinsci.plugins.vsphere.tools;
2-
3-
import static org.junit.Assert.*;
2+
import static org.hamcrest.MatcherAssert.assertThat;
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
44
import static org.hamcrest.CoreMatchers.*;
55
import hudson.slaves.JNLPLauncher;
66
import hudson.slaves.RetentionStrategy;
@@ -13,21 +13,21 @@
1313
import java.util.List;
1414

1515
import org.jenkinsci.plugins.vSphereCloudSlaveTemplate;
16-
import org.junit.Before;
17-
import org.junit.Test;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.Test;
1818

19-
public class CloudProvisioningAlgorithmTest {
19+
class CloudProvisioningAlgorithmTest {
2020

2121
/** Used when faking up test data */
2222
private int instanceNumber;
2323

24-
@Before
25-
public void setup() {
24+
@BeforeEach
25+
void setup() {
2626
instanceNumber = 0;
2727
}
2828

2929
@Test
30-
public void findTemplateWithMostFreeCapacityGivenNoOptionsThenReturnsNull() {
30+
void findTemplateWithMostFreeCapacityGivenNoOptionsThenReturnsNull() {
3131
// Given
3232
final List<CloudProvisioningRecord> emptyList = Collections.emptyList();
3333

@@ -39,7 +39,7 @@ public void findTemplateWithMostFreeCapacityGivenNoOptionsThenReturnsNull() {
3939
}
4040

4141
@Test
42-
public void findTemplateWithMostFreeCapacityGivenSomeActiveAndOneUnusedThenPrefersUnusedTemplate() {
42+
void findTemplateWithMostFreeCapacityGivenSomeActiveAndOneUnusedThenPrefersUnusedTemplate() {
4343
// Given
4444
final CloudProvisioningRecord zeroOfTwo = createInstance(2, 0, 0);
4545
final CloudProvisioningRecord onePlannedOfTwo = createInstance(2, 0, 1);
@@ -57,7 +57,7 @@ public void findTemplateWithMostFreeCapacityGivenSomeActiveAndOneUnusedThenPrefe
5757
}
5858

5959
@Test
60-
public void findTemplateWithMostFreeCapacityGivenEqualCapsThenDistributesTheLoadEvenly() {
60+
void findTemplateWithMostFreeCapacityGivenEqualCapsThenDistributesTheLoadEvenly() {
6161
// Given
6262
final CloudProvisioningRecord a = createInstance(2, 0, 0);
6363
final CloudProvisioningRecord b = createInstance(2, 0, 0);
@@ -69,7 +69,7 @@ public void findTemplateWithMostFreeCapacityGivenEqualCapsThenDistributesTheLoad
6969
}
7070

7171
@Test
72-
public void findTemplateWithMostFreeCapacityGivenEqualCapsButExistingUsageThenDistributesTheLoadEvenly() {
72+
void findTemplateWithMostFreeCapacityGivenEqualCapsButExistingUsageThenDistributesTheLoadEvenly() {
7373
// Given
7474
final CloudProvisioningRecord a = createInstance(2, 2, 0);
7575
final CloudProvisioningRecord b = createInstance(2, 1, 0);
@@ -81,7 +81,7 @@ public void findTemplateWithMostFreeCapacityGivenEqualCapsButExistingUsageThenDi
8181
}
8282

8383
@Test
84-
public void findTemplateWithMostFreeCapacityGivenNoCapsThenDistributesTheLoadEvenly() {
84+
void findTemplateWithMostFreeCapacityGivenNoCapsThenDistributesTheLoadEvenly() {
8585
// Given
8686
final CloudProvisioningRecord a = createInstance(0, 0, 0);
8787
final CloudProvisioningRecord b = createInstance(0, 0, 0);
@@ -93,12 +93,12 @@ public void findTemplateWithMostFreeCapacityGivenNoCapsThenDistributesTheLoadEve
9393
}
9494

9595
@Test
96-
public void findTemplateWithMostFreeCapacityGivenUnequalCapsThenDistributesTheLoadFairly() {
96+
void findTemplateWithMostFreeCapacityGivenUnequalCapsThenDistributesTheLoadFairly() {
9797
findTemplateWithMostFreeCapacityGivenUnequalCapsThenDistributesTheLoadFairly(true);
9898
}
9999

100100
@Test
101-
public void findTemplateWithMostFreeCapacityGivenUnequalCapsThenDistributesTheLoadFairly2() {
101+
void findTemplateWithMostFreeCapacityGivenUnequalCapsThenDistributesTheLoadFairly2() {
102102
findTemplateWithMostFreeCapacityGivenUnequalCapsThenDistributesTheLoadFairly(false);
103103
}
104104

@@ -114,12 +114,12 @@ private void findTemplateWithMostFreeCapacityGivenUnequalCapsThenDistributesTheL
114114
}
115115

116116
@Test
117-
public void findTemplateWithMostFreeCapacityGivenOneCappedAndOneUncappedThenDistributesTheLoadEvenlyUntilCapReached() {
117+
void findTemplateWithMostFreeCapacityGivenOneCappedAndOneUncappedThenDistributesTheLoadEvenlyUntilCapReached() {
118118
findTemplateWithMostFreeCapacityGivenDifferentCapnessThenDistributesTheLoadEvenlyUntilCapReached(true);
119119
}
120120

121121
@Test
122-
public void findTemplateWithMostFreeCapacityGivenOneUncappedAndOneCappedThenDistributesTheLoadEvenlyUntilCapReached() {
122+
void findTemplateWithMostFreeCapacityGivenOneUncappedAndOneCappedThenDistributesTheLoadEvenlyUntilCapReached() {
123123
findTemplateWithMostFreeCapacityGivenDifferentCapnessThenDistributesTheLoadEvenlyUntilCapReached(false);
124124
}
125125

@@ -150,22 +150,22 @@ private static void testScenario(List<CloudProvisioningRecord> records, CloudPro
150150
}
151151

152152
@Test
153-
public void toBigIntegerGivenTwoPow128MinusOneThenReturnsTwoPow128MinusOne() {
153+
void toBigIntegerGivenTwoPow128MinusOneThenReturnsTwoPow128MinusOne() {
154154
testToBigInteger(-1, -1, "340282366920938463463374607431768211455");
155155
}
156156

157157
@Test
158-
public void toBigIntegerGivenTwoPow64PlusOneThenReturnsTwoPow64PlusOne() {
158+
void toBigIntegerGivenTwoPow64PlusOneThenReturnsTwoPow64PlusOne() {
159159
testToBigInteger(1, 1, "18446744073709551617");
160160
}
161161

162162
@Test
163-
public void toBigIntegerGivenZeroThenReturnsZero() {
163+
void toBigIntegerGivenZeroThenReturnsZero() {
164164
testToBigInteger(0, 0, "0");
165165
}
166166

167167
@Test
168-
public void toBigIntegerGivenPowersOfTwoThenReturnsPowersOfTwo() {
168+
void toBigIntegerGivenPowersOfTwoThenReturnsPowersOfTwo() {
169169
long lsb = 1;
170170
long msb = 0;
171171
BigInteger big = new BigInteger("1");
@@ -182,7 +182,7 @@ public void toBigIntegerGivenPowersOfTwoThenReturnsPowersOfTwo() {
182182
}
183183
}
184184

185-
private void testToBigInteger(long msb, long lsb, String expected) {
185+
private static void testToBigInteger(long msb, long lsb, String expected) {
186186
// Given
187187
final BigInteger expectedValue = new BigInteger(expected);
188188
final byte[] expectedBytes = expectedValue.toByteArray();
@@ -196,7 +196,7 @@ private void testToBigInteger(long msb, long lsb, String expected) {
196196
}
197197

198198
@Test
199-
public void findUnusedNameGivenZeroOfTwoExistsThenReturnsOneThenTwo() {
199+
void findUnusedNameGivenZeroOfTwoExistsThenReturnsOneThenTwo() {
200200
// Given
201201
final CloudProvisioningRecord record = createInstance(2, 0, 0);
202202
final String prefix = record.getTemplate().getCloneNamePrefix();
@@ -215,7 +215,7 @@ public void findUnusedNameGivenZeroOfTwoExistsThenReturnsOneThenTwo() {
215215
}
216216

217217
@Test
218-
public void findUnusedNameGivenMiddleOfThreeStillExistsThenReturnsOneThenThree() {
218+
void findUnusedNameGivenMiddleOfThreeStillExistsThenReturnsOneThenThree() {
219219
// Given
220220
final CloudProvisioningRecord record = createInstance(3, 0, 0);
221221
final String prefix = record.getTemplate().getCloneNamePrefix();
@@ -236,7 +236,7 @@ public void findUnusedNameGivenMiddleOfThreeStillExistsThenReturnsOneThenThree()
236236
}
237237

238238
@Test
239-
public void findUnusedNameGivenNoSpaceThenThrowsIllegalStateException() {
239+
void findUnusedNameGivenNoSpaceThenThrowsIllegalStateException() {
240240
// Given
241241
final CloudProvisioningRecord record = createInstance(3, 0, 0);
242242
final String prefix = record.getTemplate().getCloneNamePrefix();
@@ -246,21 +246,16 @@ public void findUnusedNameGivenNoSpaceThenThrowsIllegalStateException() {
246246
record.setCurrentlyUnwanted(unwanted, false);
247247
record.addCurrentlyActive(active);
248248
record.addCurrentlyPlanned(planned);
249-
final List<CloudProvisioningRecord> records = Arrays.asList(record);
249+
final List<CloudProvisioningRecord> records = List.of(record);
250250
final CloudProvisioningRecord shouldBeNull = CloudProvisioningAlgorithm.findTemplateWithMostFreeCapacity(records);
251251
assertThat(shouldBeNull, nullValue());
252252

253253
// When
254-
try {
255-
final String unexpected = CloudProvisioningAlgorithm.findUnusedName(record);
256-
fail("Expected IllegalStateException, got '" + unexpected + "'.");
257-
} catch (IllegalStateException expected) {
258-
// Then passed.
259-
}
254+
assertThrows(IllegalStateException.class, () -> CloudProvisioningAlgorithm.findUnusedName(record));
260255
}
261256

262257
@Test
263-
public void findUnusedNameGivenOneOfTwoHasEndedThenReturnsOne() {
258+
void findUnusedNameGivenOneOfTwoHasEndedThenReturnsOne() {
264259
// Given
265260
final CloudProvisioningRecord record = createInstance(2, 0, 0);
266261
final String prefix = record.getTemplate().getCloneNamePrefix();
@@ -280,11 +275,11 @@ public void findUnusedNameGivenOneOfTwoHasEndedThenReturnsOne() {
280275
}
281276

282277
@Test
283-
public void findUnusedNameGivenUncappedInstancesThenReturnsUniqueNames() {
278+
void findUnusedNameGivenUncappedInstancesThenReturnsUniqueNames() {
284279
// Given
285280
final CloudProvisioningRecord record = createInstance(0, 5, 6);
286281
final String prefix = record.getTemplate().getCloneNamePrefix();
287-
final List<String> actuals = new ArrayList<String>();
282+
final List<String> actuals = new ArrayList<>();
288283

289284
// When
290285
for (int i = 0; i < 100; i++) {
@@ -294,7 +289,7 @@ public void findUnusedNameGivenUncappedInstancesThenReturnsUniqueNames() {
294289
}
295290

296291
// Then
297-
final List<String> uniques = new ArrayList<String>(new LinkedHashSet<String>(actuals));
292+
final List<String> uniques = new ArrayList<>(new LinkedHashSet<>(actuals));
298293
assertThat(actuals, equalTo(uniques));
299294
assertThat(actuals, everyItem(startsWith(prefix)));
300295
}

0 commit comments

Comments
 (0)