Skip to content

Commit a109682

Browse files
committed
Do more tests and less repetitions
1 parent 3ae7c7f commit a109682

File tree

7 files changed

+206
-278
lines changed

7 files changed

+206
-278
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ hs_err_pid*
2525
# IntelliJ
2626
*.iml
2727
.idea
28+
29+
target/
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package de.sfuhrm.maven.assemblyplugin;
2+
3+
import mockit.Expectations;
4+
import mockit.Mocked;
5+
import mockit.Verifications;
6+
import org.codehaus.plexus.archiver.ArchiveEntry;
7+
import org.codehaus.plexus.archiver.Archiver;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.io.ByteArrayInputStream;
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.nio.charset.StandardCharsets;
14+
import java.nio.file.Files;
15+
import java.util.Arrays;
16+
import java.util.Collections;
17+
import java.util.List;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
public abstract class AbstractMessageDigestTest {
22+
23+
protected abstract String getAbcSum();
24+
protected abstract String getTestSum();
25+
protected abstract String getChecksumFile();
26+
protected abstract MessageDigestContainerDescriptorHandler newInstance();
27+
28+
@Test
29+
public void getVirtualFiles() {
30+
MessageDigestContainerDescriptorHandler instance = newInstance();
31+
List<String> list = instance.getVirtualFiles();
32+
assertEquals(Collections.singletonList(getChecksumFile()), list);
33+
}
34+
35+
@Test
36+
public void getVirtualFilesWithDifferentChecksum() {
37+
MessageDigestContainerDescriptorHandler instance = newInstance();
38+
instance.setChecksumFile("FOO");
39+
List<String> list = instance.getVirtualFiles();
40+
assertEquals(Collections.singletonList("FOO"), list);
41+
}
42+
43+
@Test
44+
public void finalizeArchiveCreationWithNoFiles(@Mocked Archiver archiver) throws IOException {
45+
List<ArchiveEntry> archiveEntries = Collections.emptyList();
46+
47+
new Expectations() {{
48+
archiver.getResources(); result = Utility.iterator(archiveEntries.iterator());
49+
}};
50+
51+
MessageDigestContainerDescriptorHandler instance = newInstance();
52+
instance.finalizeArchiveCreation(archiver);
53+
54+
new Verifications() {{
55+
File file;
56+
archiver.addFile(file = withCapture(), getChecksumFile());
57+
assertEquals(Collections.emptyList(),
58+
Files.readAllLines(file.toPath()));
59+
}};
60+
}
61+
62+
@Test
63+
public void finalizeArchiveCreationWithAlphabeticInput(@Mocked Archiver archiver, @Mocked ArchiveEntry archiveEntryA, @Mocked ArchiveEntry archiveEntryB) throws IOException {
64+
List<ArchiveEntry> archiveEntries = Arrays.asList(archiveEntryB, archiveEntryA);
65+
66+
new Expectations() {{
67+
archiver.getResources(); result = Utility.iterator(archiveEntries.iterator());
68+
archiveEntryB.getName(); result = "abc";
69+
archiveEntryB.getType(); result = ArchiveEntry.FILE;
70+
archiveEntryB.getInputStream(); result = new ByteArrayInputStream("abc".getBytes(StandardCharsets.US_ASCII));
71+
archiveEntryA.getName(); result = "test";
72+
archiveEntryA.getType(); result = ArchiveEntry.FILE;
73+
archiveEntryA.getInputStream(); result = new ByteArrayInputStream(new byte[0]);
74+
}};
75+
76+
MessageDigestContainerDescriptorHandler instance = newInstance();
77+
instance.finalizeArchiveCreation(archiver);
78+
79+
new Verifications() {{
80+
File file;
81+
archiver.addFile(file = withCapture(), getChecksumFile());
82+
assertEquals(Arrays.asList(
83+
getAbcSum()+" *abc",
84+
getTestSum()+" *test"),
85+
Files.readAllLines(file.toPath()));
86+
}};
87+
}
88+
89+
@Test
90+
public void finalizeArchiveCreationWithInverseAlphabeticInput(@Mocked Archiver archiver, @Mocked ArchiveEntry archiveEntryA, @Mocked ArchiveEntry archiveEntryB) throws IOException {
91+
List<ArchiveEntry> archiveEntries = Arrays.asList(archiveEntryA, archiveEntryB);
92+
93+
new Expectations() {{
94+
archiver.getResources(); result = Utility.iterator(archiveEntries.iterator());
95+
archiveEntryA.getName(); result = "test";
96+
archiveEntryA.getType(); result = ArchiveEntry.FILE;
97+
archiveEntryA.getInputStream(); result = new ByteArrayInputStream(new byte[0]);
98+
archiveEntryB.getName(); result = "abc";
99+
archiveEntryB.getType(); result = ArchiveEntry.FILE;
100+
archiveEntryB.getInputStream(); result = new ByteArrayInputStream("abc".getBytes(StandardCharsets.US_ASCII));
101+
}};
102+
103+
MessageDigestContainerDescriptorHandler instance = newInstance();
104+
instance.finalizeArchiveCreation(archiver);
105+
106+
new Verifications() {{
107+
File file;
108+
archiver.addFile(file = withCapture(), getChecksumFile());
109+
assertEquals(Arrays.asList(
110+
getAbcSum()+" *abc",
111+
getTestSum()+" *test"),
112+
Files.readAllLines(file.toPath()));
113+
}};
114+
}
115+
116+
@Test
117+
public void finalizeArchiveCreationWithExclusion(@Mocked Archiver archiver, @Mocked ArchiveEntry archiveEntry) {
118+
List<ArchiveEntry> archiveEntries = Collections.singletonList(archiveEntry);
119+
120+
new Expectations() {{
121+
archiver.getResources(); result = Utility.iterator(archiveEntries.iterator());
122+
archiveEntry.getName(); result = "test";
123+
archiveEntry.getType(); result = ArchiveEntry.FILE;
124+
}};
125+
126+
MessageDigestContainerDescriptorHandler instance = newInstance();
127+
instance.setExclusionRegex("test");
128+
instance.finalizeArchiveCreation(archiver);
129+
130+
new Verifications() {{
131+
File file;
132+
archiver.addFile(file = withCapture(), getChecksumFile());
133+
assertEquals(0, file.length()); // nothing in file, no matches
134+
}};
135+
}
136+
}
Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,23 @@
11
package de.sfuhrm.maven.assemblyplugin;
22

3-
import mockit.Expectations;
4-
import mockit.Mocked;
5-
import mockit.Verifications;
6-
import org.codehaus.plexus.archiver.ArchiveEntry;
7-
import org.codehaus.plexus.archiver.Archiver;
8-
import org.junit.jupiter.api.Test;
9-
10-
import java.io.ByteArrayInputStream;
11-
import java.io.File;
12-
import java.io.IOException;
13-
import java.nio.charset.StandardCharsets;
14-
import java.nio.file.Files;
15-
import java.util.Arrays;
16-
import java.util.List;
17-
18-
import static org.junit.jupiter.api.Assertions.assertEquals;
19-
20-
public class MD5Test {
21-
22-
@Test
23-
public void finalizeArchiveCreation(@Mocked Archiver archiver, @Mocked ArchiveEntry archiveEntryA, @Mocked ArchiveEntry archiveEntryB) throws IOException {
24-
25-
List<ArchiveEntry> archiveEntries = Arrays.asList(archiveEntryA, archiveEntryB);
3+
public class MD5Test extends AbstractMessageDigestTest {
4+
@Override
5+
protected String getAbcSum() {
6+
return "900150983cd24fb0d6963f7d28e17f72";
7+
}
268

27-
new Expectations() {{
28-
archiver.getResources(); result = Utility.iterator(archiveEntries.iterator());
29-
archiveEntryA.getName(); result = "test";
30-
archiveEntryA.getType(); result = ArchiveEntry.FILE;
31-
archiveEntryA.getInputStream(); result = new ByteArrayInputStream(new byte[0]);
32-
archiveEntryB.getName(); result = "abc";
33-
archiveEntryB.getType(); result = ArchiveEntry.FILE;
34-
archiveEntryB.getInputStream(); result = new ByteArrayInputStream("abc".getBytes(StandardCharsets.US_ASCII));
35-
}};
9+
@Override
10+
protected String getTestSum() {
11+
return "d41d8cd98f00b204e9800998ecf8427e";
12+
}
3613

37-
MessageDigestContainerDescriptorHandler instance = new MD5();
38-
instance.finalizeArchiveCreation(archiver);
14+
@Override
15+
protected String getChecksumFile() {
16+
return "MD5SUMS";
17+
}
3918

40-
new Verifications() {{
41-
File file;
42-
archiver.addFile(file = withCapture(), "MD5SUMS");
43-
// see https://tools.ietf.org/html/rfc1321#appendix-A.5
44-
assertEquals(Arrays.asList(
45-
"900150983cd24fb0d6963f7d28e17f72 *abc",
46-
"d41d8cd98f00b204e9800998ecf8427e *test"),
47-
Files.readAllLines(file.toPath()));
48-
}};
19+
@Override
20+
protected MessageDigestContainerDescriptorHandler newInstance() {
21+
return new MD5();
4922
}
5023
}
Lines changed: 4 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,9 @@
11
package de.sfuhrm.maven.assemblyplugin;
22

3-
import org.codehaus.plexus.archiver.ArchiveEntry;
4-
import org.codehaus.plexus.archiver.Archiver;
5-
import org.junit.jupiter.api.Test;
6-
import static org.junit.jupiter.api.Assertions.assertEquals;
3+
public class MessageDigestContainerDescriptorHandlerTest extends MD5Test {
74

8-
import java.io.ByteArrayInputStream;
9-
import java.io.File;
10-
import java.io.IOException;
11-
import java.nio.charset.StandardCharsets;
12-
import java.nio.file.Files;
13-
import java.util.Arrays;
14-
import java.util.Collections;
15-
import java.util.List;
16-
import mockit.*;
17-
18-
public class MessageDigestContainerDescriptorHandlerTest {
19-
20-
@Test
21-
public void getVirtualFiles() {
22-
MessageDigestContainerDescriptorHandler instance = new MessageDigestContainerDescriptorHandler();
23-
List<String> list = instance.getVirtualFiles();
24-
assertEquals(Collections.singletonList("MD5SUMS"), list);
25-
}
26-
27-
@Test
28-
public void getVirtualFilesWithDifferentChecksum() {
29-
MessageDigestContainerDescriptorHandler instance = new MessageDigestContainerDescriptorHandler();
30-
instance.setChecksumFile("FOO");
31-
List<String> list = instance.getVirtualFiles();
32-
assertEquals(Collections.singletonList("FOO"), list);
33-
}
34-
35-
@Test
36-
public void finalizeArchiveCreation(@Mocked Archiver archiver, @Mocked ArchiveEntry archiveEntryA, @Mocked ArchiveEntry archiveEntryB) throws IOException {
37-
38-
List<ArchiveEntry> archiveEntries = Arrays.asList(archiveEntryA, archiveEntryB);
39-
40-
new Expectations() {{
41-
archiver.getResources(); result = Utility.iterator(archiveEntries.iterator());
42-
archiveEntryA.getName(); result = "test";
43-
archiveEntryA.getType(); result = ArchiveEntry.FILE;
44-
archiveEntryA.getInputStream(); result = new ByteArrayInputStream(new byte[0]);
45-
archiveEntryB.getName(); result = "abc";
46-
archiveEntryB.getType(); result = ArchiveEntry.FILE;
47-
archiveEntryB.getInputStream(); result = new ByteArrayInputStream("abc".getBytes(StandardCharsets.US_ASCII));
48-
}};
49-
50-
MessageDigestContainerDescriptorHandler instance = new MessageDigestContainerDescriptorHandler();
51-
instance.finalizeArchiveCreation(archiver);
52-
53-
new Verifications() {{
54-
File file;
55-
archiver.addFile(file = withCapture(), "MD5SUMS");
56-
// see https://tools.ietf.org/html/rfc1321#appendix-A.5
57-
assertEquals(Arrays.asList(
58-
"900150983cd24fb0d6963f7d28e17f72 *abc",
59-
"d41d8cd98f00b204e9800998ecf8427e *test"),
60-
Files.readAllLines(file.toPath()));
61-
}};
62-
}
63-
64-
@Test
65-
public void finalizeArchiveCreationWithSha256(@Mocked Archiver archiver, @Mocked ArchiveEntry archiveEntryA, @Mocked ArchiveEntry archiveEntryB) throws IOException {
66-
67-
List<ArchiveEntry> archiveEntries = Arrays.asList(archiveEntryA, archiveEntryB);
68-
69-
new Expectations() {{
70-
archiver.getResources(); result = Utility.iterator(archiveEntries.iterator());
71-
archiveEntryA.getName(); result = "test";
72-
archiveEntryA.getType(); result = ArchiveEntry.FILE;
73-
archiveEntryA.getInputStream(); result = new ByteArrayInputStream(new byte[0]);
74-
archiveEntryB.getName(); result = "abc";
75-
archiveEntryB.getType(); result = ArchiveEntry.FILE;
76-
archiveEntryB.getInputStream(); result = new ByteArrayInputStream("abc".getBytes(StandardCharsets.US_ASCII));
77-
}};
78-
79-
MessageDigestContainerDescriptorHandler instance = new MessageDigestContainerDescriptorHandler();
80-
instance.setChecksumFile("SHA256SUM");
81-
instance.setMessageDigest("SHA-256");
82-
instance.finalizeArchiveCreation(archiver);
83-
84-
new Verifications() {{
85-
File file;
86-
archiver.addFile(file = withCapture(), "SHA256SUM");
87-
// see https://tools.ietf.org/html/rfc1321#appendix-A.5
88-
assertEquals(Arrays.asList(
89-
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad *abc",
90-
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *test"),
91-
Files.readAllLines(file.toPath()));
92-
}};
93-
}
94-
95-
@Test
96-
public void finalizeArchiveCreationWithExclusion(@Mocked Archiver archiver, @Mocked ArchiveEntry archiveEntry) {
97-
98-
List<ArchiveEntry> archiveEntries = Collections.singletonList(archiveEntry);
99-
100-
new Expectations() {{
101-
archiver.getResources(); result = Utility.iterator(archiveEntries.iterator());
102-
archiveEntry.getName(); result = "test";
103-
archiveEntry.getType(); result = ArchiveEntry.FILE;
104-
}};
105-
106-
MessageDigestContainerDescriptorHandler instance = new MessageDigestContainerDescriptorHandler();
107-
instance.setExclusionRegex("test");
108-
instance.finalizeArchiveCreation(archiver);
109-
110-
new Verifications() {{
111-
File file;
112-
archiver.addFile(file = withCapture(), "MD5SUMS");
113-
assertEquals(0, file.length()); // nothing in file, no matches
114-
}};
5+
@Override
6+
protected MessageDigestContainerDescriptorHandler newInstance() {
7+
return new MessageDigestContainerDescriptorHandler();
1158
}
1169
}
Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,24 @@
11
package de.sfuhrm.maven.assemblyplugin;
22

3-
import mockit.Expectations;
4-
import mockit.Mocked;
5-
import mockit.Verifications;
6-
import org.codehaus.plexus.archiver.ArchiveEntry;
7-
import org.codehaus.plexus.archiver.Archiver;
8-
import org.junit.jupiter.api.Test;
3+
public class SHA1Test extends AbstractMessageDigestTest {
94

10-
import java.io.ByteArrayInputStream;
11-
import java.io.File;
12-
import java.io.IOException;
13-
import java.nio.charset.StandardCharsets;
14-
import java.nio.file.Files;
15-
import java.util.Arrays;
16-
import java.util.List;
17-
18-
import static org.junit.jupiter.api.Assertions.assertEquals;
19-
20-
public class SHA1Test {
21-
22-
@Test
23-
public void finalizeArchiveCreation(@Mocked Archiver archiver, @Mocked ArchiveEntry archiveEntryA, @Mocked ArchiveEntry archiveEntryB) throws IOException {
24-
25-
List<ArchiveEntry> archiveEntries = Arrays.asList(archiveEntryA, archiveEntryB);
5+
@Override
6+
protected String getAbcSum() {
7+
return "a9993e364706816aba3e25717850c26c9cd0d89d";
8+
}
269

27-
new Expectations() {{
28-
archiver.getResources(); result = Utility.iterator(archiveEntries.iterator());
29-
archiveEntryA.getName(); result = "test";
30-
archiveEntryA.getType(); result = ArchiveEntry.FILE;
31-
archiveEntryA.getInputStream(); result = new ByteArrayInputStream(new byte[0]);
32-
archiveEntryB.getName(); result = "abc";
33-
archiveEntryB.getType(); result = ArchiveEntry.FILE;
34-
archiveEntryB.getInputStream(); result = new ByteArrayInputStream("abc".getBytes(StandardCharsets.US_ASCII));
35-
}};
10+
@Override
11+
protected String getTestSum() {
12+
return "da39a3ee5e6b4b0d3255bfef95601890afd80709";
13+
}
3614

37-
MessageDigestContainerDescriptorHandler instance = new SHA1();
38-
instance.finalizeArchiveCreation(archiver);
15+
@Override
16+
protected String getChecksumFile() {
17+
return "SHA1SUMS";
18+
}
3919

40-
new Verifications() {{
41-
File file;
42-
archiver.addFile(file = withCapture(), "SHA1SUMS");
43-
// see https://tools.ietf.org/html/rfc1321#appendix-A.5
44-
assertEquals(Arrays.asList(
45-
"a9993e364706816aba3e25717850c26c9cd0d89d *abc",
46-
"da39a3ee5e6b4b0d3255bfef95601890afd80709 *test"),
47-
Files.readAllLines(file.toPath()));
48-
}};
20+
@Override
21+
protected MessageDigestContainerDescriptorHandler newInstance() {
22+
return new SHA1();
4923
}
5024
}

0 commit comments

Comments
 (0)