Skip to content

Commit 2e0a647

Browse files
committed
Use JUnit 5
1 parent 9b93ba9 commit 2e0a647

File tree

10 files changed

+195
-114
lines changed

10 files changed

+195
-114
lines changed

build.gradle

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ext {
2525
hadoopVersion = '3.0.3'
2626
jCommanderVersion = '1.72'
2727
almworksVersion = '1.1.1'
28+
junitVersion = '5.4.0-M1'
2829
}
2930

3031
repositories {
@@ -43,7 +44,9 @@ dependencies {
4344

4445
runtimeOnly group: 'org.apache.hadoop', name: 'hadoop-hdfs-client', version: hadoopVersion
4546

46-
testImplementation group: 'junit', name: 'junit', version: '4.12'
47+
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitVersion
48+
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: junitVersion
49+
testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitVersion
4750
}
4851

4952
ext.sharedManifest = manifest {
@@ -80,6 +83,14 @@ startScripts {
8083
}
8184
}
8285

86+
test {
87+
useJUnitPlatform()
88+
testLogging {
89+
events "passed", "skipped", "failed"
90+
}
91+
}
92+
93+
8394
tasks.withType(Tar){
8495
compression = Compression.GZIP
8596
extension = 'tar.gz'

src/test/java/org/radarcns/hdfs/OffsetRangeFileTest.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616

1717
package org.radarcns.hdfs;
1818

19-
import org.junit.Before;
20-
import org.junit.Rule;
21-
import org.junit.Test;
22-
import org.junit.rules.ExpectedException;
23-
import org.junit.rules.TemporaryFolder;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
import java.nio.file.Files;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.extension.ExtendWith;
27+
import org.junit.jupiter.api.support.io.TempDirectory;
28+
import org.junit.jupiter.api.support.io.TempDirectory.TempDir;
2429
import org.radarcns.hdfs.accounting.OffsetRange;
2530
import org.radarcns.hdfs.accounting.OffsetRangeFile;
2631
import org.radarcns.hdfs.accounting.OffsetRangeSet;
@@ -32,22 +37,15 @@
3237
import java.io.IOException;
3338
import java.nio.file.Path;
3439

35-
import static org.junit.Assert.assertEquals;
36-
import static org.junit.Assert.assertFalse;
37-
import static org.junit.Assert.assertTrue;
38-
40+
@ExtendWith(TempDirectory.class)
3941
public class OffsetRangeFileTest {
40-
@Rule
41-
public TemporaryFolder folder = new TemporaryFolder();
42-
43-
@Rule
44-
public ExpectedException exception = ExpectedException.none();
4542
private Path testFile;
4643
private StorageDriver storage;
4744

48-
@Before
49-
public void setUp() throws IOException {
50-
testFile = folder.newFile().toPath();
45+
@BeforeEach
46+
public void setUp(@TempDir Path dir) throws IOException {
47+
testFile = dir.resolve("test");
48+
Files.createFile(testFile);
5149
storage = new LocalStorageDriver();
5250
}
5351

src/test/java/org/radarcns/hdfs/OffsetRangeSetTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.radarcns.hdfs;
22

3-
import org.junit.Test;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
48
import org.radarcns.hdfs.accounting.OffsetRange;
59
import org.radarcns.hdfs.accounting.OffsetRangeSet;
610
import org.radarcns.hdfs.accounting.TopicPartition;
711

8-
import static org.junit.Assert.assertEquals;
9-
import static org.junit.Assert.assertFalse;
10-
import static org.junit.Assert.assertTrue;
11-
1212
public class OffsetRangeSetTest {
1313
@Test
1414
public void containsGap() {
@@ -29,7 +29,7 @@ public void containsGap() {
2929
assertFalse(set.contains(c));
3030
assertFalse(set.contains(ac));
3131
set.add(c);
32-
assertEquals(set.toString(), 2, set.size(topicPartition));
32+
assertEquals(2, set.size(topicPartition), set.toString());
3333
assertTrue(set.contains(c));
3434
assertTrue(set.contains(a));
3535
assertFalse(set.contains(ac));
@@ -60,5 +60,4 @@ public void testGap() {
6060
assertTrue(offsets.contains(3));
6161
assertTrue(offsets.contains(0, 5));
6262
}
63-
6463
}

src/test/java/org/radarcns/hdfs/RadarHdfsRestructureTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
import org.apache.avro.SchemaBuilder;
2121
import org.apache.avro.generic.GenericRecord;
2222
import org.apache.avro.generic.GenericRecordBuilder;
23-
import org.junit.Test;
2423

2524
import java.time.Instant;
25+
import org.junit.jupiter.api.Test;
2626

27-
import static org.junit.Assert.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
2828

2929
public class RadarHdfsRestructureTest {
3030
@Test
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.radarcns.hdfs.data;
2+
3+
import static java.nio.charset.StandardCharsets.US_ASCII;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertSame;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.OutputStream;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
public class CompressionFactoryTest {
16+
17+
private CompressionFactory factory;
18+
19+
@BeforeEach
20+
public void setUp() {
21+
factory = new CompressionFactory();
22+
}
23+
24+
@Test
25+
public void testIdentity() throws IOException {
26+
assertSame(IdentityCompression.class, factory.get("none").getClass());
27+
assertSame(IdentityCompression.class, factory.get("identity").getClass());
28+
29+
Compression compression = factory.get("none");
30+
31+
assertEquals("", compression.getExtension());
32+
33+
ByteArrayOutputStream out = new ByteArrayOutputStream();
34+
OutputStream compressedStream = compression.compress("something", out);
35+
compressedStream.write("abcdef".getBytes(US_ASCII));
36+
compressedStream.close();
37+
38+
assertEquals("abcdef", new String(out.toByteArray(), US_ASCII));
39+
40+
ByteArrayInputStream in = new ByteArrayInputStream("abcdef".getBytes(US_ASCII));
41+
InputStream compressedIn = compression.decompress(in);
42+
byte[] result = new byte[10];
43+
assertEquals(6, compressedIn.read(result));
44+
assertEquals("abcdef", new String(result, 0, 6, US_ASCII));
45+
}
46+
47+
@Test
48+
public void testGzip() throws IOException {
49+
assertSame(GzipCompression.class, factory.get("gzip").getClass());
50+
51+
Compression compression = factory.get("gzip");
52+
53+
assertEquals(".gz", compression.getExtension());
54+
55+
ByteArrayOutputStream out = new ByteArrayOutputStream();
56+
OutputStream compressedStream = compression.compress("something", out);
57+
compressedStream.write("abcdef".getBytes(US_ASCII));
58+
compressedStream.close();
59+
60+
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
61+
InputStream compressedIn = compression.decompress(in);
62+
byte[] result = new byte[10];
63+
assertEquals(6, compressedIn.read(result));
64+
assertEquals("abcdef", new String(result, 0, 6, US_ASCII));
65+
}
66+
67+
68+
@Test
69+
public void testZip() throws IOException {
70+
assertSame(ZipCompression.class, factory.get("zip").getClass());
71+
72+
Compression compression = factory.get("zip");
73+
74+
assertEquals(".zip", compression.getExtension());
75+
76+
ByteArrayOutputStream out = new ByteArrayOutputStream();
77+
OutputStream compressedStream = compression.compress("something", out);
78+
compressedStream.write("abcdef".getBytes(US_ASCII));
79+
compressedStream.close();
80+
81+
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
82+
InputStream compressedIn = compression.decompress(in);
83+
byte[] result = new byte[10];
84+
assertEquals(6, compressedIn.read(result));
85+
assertEquals("abcdef", new String(result, 0, 6, US_ASCII));
86+
}
87+
}

src/test/java/org/radarcns/hdfs/data/CsvAvroConverterTest.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
import org.apache.avro.generic.GenericRecordBuilder;
2626
import org.apache.avro.io.DecoderFactory;
2727
import org.apache.avro.io.JsonDecoder;
28-
import org.junit.Rule;
29-
import org.junit.Test;
30-
import org.junit.rules.ExpectedException;
31-
import org.junit.rules.TemporaryFolder;
3228

3329
import java.io.BufferedReader;
3430
import java.io.BufferedWriter;
@@ -53,22 +49,19 @@
5349
import java.util.Set;
5450
import java.util.zip.GZIPInputStream;
5551
import java.util.zip.GZIPOutputStream;
52+
import org.junit.jupiter.api.Test;
53+
import org.junit.jupiter.api.extension.ExtendWith;
54+
import org.junit.jupiter.api.support.io.TempDirectory;
55+
import org.junit.jupiter.api.support.io.TempDirectory.TempDir;
5656

5757
import static java.nio.charset.StandardCharsets.UTF_8;
58-
import static org.junit.Assert.assertArrayEquals;
59-
import static org.junit.Assert.assertEquals;
60-
import static org.junit.Assert.assertFalse;
61-
import static org.junit.Assert.assertNull;
62-
import static org.junit.Assert.assertTrue;
58+
import static org.junit.jupiter.api.Assertions.assertEquals;
59+
import static org.junit.jupiter.api.Assertions.assertFalse;
60+
import static org.junit.jupiter.api.Assertions.assertNull;
61+
import static org.junit.jupiter.api.Assertions.assertTrue;
6362
import static org.radarcns.hdfs.data.CsvAvroConverter.cleanCsvString;
6463

6564
public class CsvAvroConverterTest {
66-
@Rule
67-
public ExpectedException exception = ExpectedException.none();
68-
69-
@Rule
70-
public TemporaryFolder folder = new TemporaryFolder();
71-
7265
@Test
7366
public void writeRecord() throws IOException {
7467
Parser parser = new Parser();
@@ -94,18 +87,18 @@ public void writeRecord() throws IOException {
9487

9588
int i = 0;
9689
while (actualIterator.hasNext()) {
97-
assertTrue("Actual value has more entries than expected value", expectedIterator.hasNext());
90+
assertTrue(expectedIterator.hasNext(), "Actual value has more entries than expected value");
9891
Object actual = actualIterator.next();
9992
Object expected = expectedIterator.next();
10093

10194
if (expected instanceof byte[]) {
102-
assertEquals("Array for argument " + keys.get(i) + " does not match", Base64.getEncoder().withoutPadding().encodeToString((byte[])expected), actual);
95+
assertEquals(Base64.getEncoder().withoutPadding().encodeToString((byte[])expected), actual, "Array for argument " + keys.get(i) + " does not match");
10396
} else {
104-
assertEquals("Value for argument " + keys.get(i) + " does not match", expected, actual);
97+
assertEquals(expected, actual, "Value for argument " + keys.get(i) + " does not match");
10598
}
10699
i++;
107100
}
108-
assertFalse("Actual value has fewer entries than expected value", expectedIterator.hasNext());
101+
assertFalse(expectedIterator.hasNext(), "Actual value has fewer entries than expected value");
109102

110103
converter.writeRecord(record);
111104

@@ -192,25 +185,27 @@ static void writeTestNumbers(Writer writer) throws IOException {
192185
}
193186

194187
@Test
195-
public void deduplicate() throws IOException {
196-
Path path = folder.newFile().toPath();
188+
@ExtendWith(TempDirectory.class)
189+
public void deduplicate(@TempDir Path dir) throws IOException {
190+
Path path = dir.resolve("test");
197191
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
198192
writeTestNumbers(writer);
199193
}
200-
CsvAvroConverter.getFactory().sortUnique(path, path, new IdentityCompression());
194+
CsvAvroConverter.getFactory().sortUnique("t", path, path, new IdentityCompression());
201195
assertEquals(Arrays.asList("a,b", "1,2", "3,4", "1,3", "a,a"), Files.readAllLines(path));
202196
}
203197

204198

205199
@Test
206-
public void deduplicateGzip() throws IOException {
207-
Path path = folder.newFile("test.csv.gz").toPath();
200+
@ExtendWith(TempDirectory.class)
201+
public void deduplicateGzip(@TempDir Path dir) throws IOException {
202+
Path path = dir.resolve("test.csv.gz");
208203
try (OutputStream out = Files.newOutputStream(path);
209204
GZIPOutputStream gzipOut = new GZIPOutputStream(out);
210205
Writer writer = new OutputStreamWriter(gzipOut)) {
211206
writeTestNumbers(writer);
212207
}
213-
CsvAvroConverter.getFactory().sortUnique(path, path, new GzipCompression());
208+
CsvAvroConverter.getFactory().sortUnique("t", path, path, new GzipCompression());
214209
try (InputStream in = Files.newInputStream(path);
215210
GZIPInputStream gzipIn = new GZIPInputStream(in);
216211
Reader inReader = new InputStreamReader(gzipIn);

src/test/java/org/radarcns/hdfs/data/FileCacheStoreTest.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@
1616

1717
package org.radarcns.hdfs.data;
1818

19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
1925
import org.apache.avro.Schema;
2026
import org.apache.avro.SchemaBuilder;
2127
import org.apache.avro.generic.GenericRecord;
2228
import org.apache.avro.generic.GenericRecordBuilder;
23-
import org.junit.Rule;
24-
import org.junit.Test;
25-
import org.junit.rules.TemporaryFolder;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.extension.ExtendWith;
31+
import org.junit.jupiter.api.support.io.TempDirectory;
32+
import org.junit.jupiter.api.support.io.TempDirectory.TempDir;
2633
import org.radarcns.hdfs.Application;
2734
import org.radarcns.hdfs.FileStoreFactory;
2835
import org.radarcns.hdfs.accounting.Accountant;
@@ -31,29 +38,17 @@
3138
import org.radarcns.hdfs.accounting.TopicPartition;
3239
import org.radarcns.hdfs.config.RestructureSettings;
3340

34-
import java.io.IOException;
35-
import java.nio.file.Files;
36-
import java.nio.file.Path;
37-
38-
import static org.junit.Assert.assertEquals;
39-
import static org.junit.Assert.assertTrue;
40-
4141
public class FileCacheStoreTest {
42-
@Rule
43-
public TemporaryFolder folder = new TemporaryFolder();
44-
4542
@Test
46-
public void appendLine() throws IOException {
47-
Path f1 = folder.newFile().toPath();
48-
Path f2 = folder.newFile().toPath();
49-
Path f3 = folder.newFile().toPath();
50-
Path d4 = folder.newFolder().toPath();
43+
@ExtendWith(TempDirectory.class)
44+
public void appendLine(@TempDir Path root, @TempDir Path tmpDir) throws IOException {
45+
Path f1 = root.resolve("f1");
46+
Path f2 = root.resolve("f2");
47+
Path f3 = root.resolve("f3");
48+
Path d4 = root.resolve("d4");
49+
Files.createDirectories(d4);
5150
Path f4 = d4.resolve("f4.txt");
52-
Path newFile = folder.newFile().toPath();
53-
Path tmpDir = folder.newFolder().toPath();
54-
55-
Files.delete(f1);
56-
Files.delete(d4);
51+
Path newFile = root.resolve("newFile");
5752

5853
Schema simpleSchema = SchemaBuilder.record("simple").fields()
5954
.name("a").type("string").noDefault()
@@ -73,7 +68,7 @@ public void appendLine() throws IOException {
7368
OffsetRange offsetRange1 = new OffsetRange(topicPartition1, 0, 8);
7469
Bin bin = new Bin("t", "c", "00");
7570

76-
RestructureSettings settings = new RestructureSettings.Builder(folder.getRoot().toString())
71+
RestructureSettings settings = new RestructureSettings.Builder(root.toString())
7772
.cacheSize(2)
7873
.tempDir(tmpDir.toString())
7974
.build();

0 commit comments

Comments
 (0)