Skip to content

Commit

Permalink
Use AssertJ fluent assertions java library (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck authored Apr 6, 2024
1 parent 6f67a52 commit ecd1acc
Show file tree
Hide file tree
Showing 23 changed files with 242 additions and 361 deletions.
8 changes: 0 additions & 8 deletions deb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
</dependency>

<!-- testing -->

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.eclipse.packager.deb.tests;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -22,38 +24,24 @@
import org.eclipse.packager.deb.build.DebianPackageWriter;
import org.eclipse.packager.deb.build.EntryInformation;
import org.eclipse.packager.deb.control.BinaryPackageControlFile;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.google.common.hash.Hashing;

public class BinaryPackageTest {
class BinaryPackageTest {
@SuppressWarnings("deprecation")
@Test
public void test1() throws IOException, InterruptedException {
void test1() throws IOException, InterruptedException {
final Path file1 = Files.createTempFile("test-1-", ".deb");
final Path file2 = Files.createTempFile("test-2-", ".deb");

final Instant now = Instant.now();
final Supplier<Instant> timestampProvider = () -> now;

createDebFile(file1, timestampProvider);
System.out.println("File: " + file1);
Assertions.assertTrue(Files.exists(file1), "File exists");

Thread.sleep(1_001); // sleep for a second to make sure that a timestamp might be changed

createDebFile(file2, timestampProvider);
System.out.println("File: " + file2);
Assertions.assertTrue(Files.exists(file2), "File exists");

final byte[] b1 = Files.readAllBytes(file1);
final String h1 = Hashing.md5().hashBytes(b1).toString();
final byte[] b2 = Files.readAllBytes(file2);
final String h2 = Hashing.md5().hashBytes(b2).toString();
System.out.println(h1);
System.out.println(h2);
Assertions.assertEquals(h1, h2);
assertThat(file2).hasSameBinaryContentAs(file1);
}

private void createDebFile(final Path file, final Supplier<Instant> timestampProvider) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
*/
package org.eclipse.packager.deb.tests;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.StringWriter;
import java.util.Optional;

import org.eclipse.packager.deb.ControlFileWriter;
import org.eclipse.packager.deb.FieldFormatter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ControlFileWriterTest {
Expand Down Expand Up @@ -49,9 +50,7 @@ private void testField(final ControlFieldDefinition field, final String value, f
try (final StringWriter sw = new StringWriter()) {
final ControlFileWriter writer = new ControlFileWriter(sw);
writer.writeEntry(field.name, value, Optional.ofNullable(field.formatter));
final String result = sw.toString();
System.out.println("Actual: '" + result + "'");
Assertions.assertEquals(expectedResult, result);
assertThat(sw).hasToString(expectedResult);
}
}
}
94 changes: 35 additions & 59 deletions deb/src/test/java/org/eclipse/packager/deb/tests/PackagesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
*/
package org.eclipse.packager.deb.tests;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand All @@ -30,12 +29,9 @@
import org.eclipse.packager.deb.ParserException;
import org.junit.jupiter.api.Test;

import com.google.common.io.CharStreams;

public class PackagesTest {

class PackagesTest {
@Test
public void test1FieldFormatters() throws IOException {
void test1FieldFormatters() throws IOException {
testFieldFormatterValue(FieldFormatter.SINGLE, "foo", "foo");
testFieldFormatter(FieldFormatter.SINGLE, "Foo", "bar", "Foo: bar");

Expand All @@ -48,7 +44,7 @@ public void test1FieldFormatters() throws IOException {
}

@Test
public void test1FieldFormattersCornerCases() throws IOException {
void test1FieldFormattersCornerCases() throws IOException {
testFieldFormatterValue(FieldFormatter.SINGLE, "foo\nbar", "foobar");
testFieldFormatter(FieldFormatter.SINGLE, "Foo", "bar\nbar", "Foo: barbar");

Expand All @@ -68,83 +64,63 @@ public void test1FieldFormattersCornerCases() throws IOException {
private void testFieldFormatter(final FieldFormatter formatter, final String key, final String input, final String expected) throws IOException {
final StringBuilder sb = new StringBuilder();
formatter.append(key, input, sb);
assertEquals(expected, formatter.format(key, input));
assertThat(formatter.format(key, input)).isEqualTo(expected);
}

private void testFieldFormatterValue(final FieldFormatter formatter, final String input, final String expected) throws IOException {
void testFieldFormatterValue(final FieldFormatter formatter, final String input, final String expected) throws IOException {
final StringBuilder sb = new StringBuilder();
formatter.appendValue(input, sb);

/*
* System.out.println ( "Expected ->" );
* System.out.println ( expected );
* System.out.println ( "Actual ->" );
* System.out.println ( sb.toString () );
*/

assertEquals(expected, formatter.formatValue(input));
assertThat(formatter.formatValue(input)).isEqualTo(expected);
}

@Test
public void test2() throws IOException, ParserException {
LinkedHashMap<String, String> control;
try (InputStream is = PackagesTest.class.getResourceAsStream("data/test1")) {
control = ControlFileParser.parse(is);
void test2() throws IOException, ParserException {
try (final InputStream is = PackagesTest.class.getResourceAsStream("data/test1")) {
final Map<String, String> control = ControlFileParser.parse(is);
assertThat(control).extractingByKey("Description").satisfies(s-> assertThat(Packages.makeDescriptionMd5(s)).isEqualTo("38d96b653196d5ef8c667efe23411a81"));
}

final String md5 = Packages.makeDescriptionMd5(control.get("Description"));

assertEquals("38d96b653196d5ef8c667efe23411a81", md5);
}

@Test
public void test3() throws IOException, ParserException {
LinkedHashMap<String, String> control;
try (InputStream is = PackagesTest.class.getResourceAsStream("data/test2")) {
control = ControlFileParser.parse(is);
void test3() throws IOException, ParserException {
try (final InputStream is = PackagesTest.class.getResourceAsStream("data/test2")) {
final Map<String, String> control = ControlFileParser.parse(is);
assertThat(control).extractingByKey("Package").isEqualTo("org.eclipse.scada.base.p2-incubation");
assertThat(control).extractingByKey("Installed-Size").isEqualTo("1100");
assertThat(control).extractingByKey("Description").isEqualTo("Eclipse SCADA P2 Repository - org.eclipse.scada.base.p2-incubation");
assertThat(control).extractingByKey("Conffiles").isEqualTo("\n/file1 1234\n/file2 1234");
}

assertEquals("org.eclipse.scada.base.p2-incubation", control.get("Package"));
assertEquals("1100", control.get("Installed-Size"));
assertEquals("Eclipse SCADA P2 Repository - org.eclipse.scada.base.p2-incubation", control.get("Description"));
assertEquals("\n/file1 1234\n/file2 1234", control.get("Conffiles"));
}

@Test
public void test4() throws IOException, ParserException {
void test4() throws IOException, ParserException {
encodeDecodeTest("data/test1");
encodeDecodeTest("data/test2");
}

private void encodeDecodeTest(final String resourceName) throws IOException, ParserException {
LinkedHashMap<String, String> control;
try (InputStream is = PackagesTest.class.getResourceAsStream(resourceName)) {
control = ControlFileParser.parse(is);
}

final StringBuilder sb = new StringBuilder();
final Map<String, FieldFormatter> map = new HashMap<>();
map.put("Description", FieldFormatter.MULTI);
map.put("Conffiles", FieldFormatter.MULTI);
new ControlFileWriter(sb, map).writeEntries(control);

String data;
try (InputStream is = PackagesTest.class.getResourceAsStream(resourceName)) {
data = CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));
}

System.out.println(sb.toString());
try (final InputStream is = PackagesTest.class.getResourceAsStream(resourceName)) {
final LinkedHashMap<String, String> control = ControlFileParser.parse(is);
final Map<String, FieldFormatter> map = new HashMap<>();
map.put("Description", FieldFormatter.MULTI);
map.put("Conffiles", FieldFormatter.MULTI);
new ControlFileWriter(sb, map).writeEntries(control);
}

assertEquals(data, sb.toString());
try (final InputStream is = PackagesTest.class.getResourceAsStream(resourceName)) {
assertThat(is).isNotNull();
final String data = new String(is.readAllBytes(), StandardCharsets.UTF_8);
assertThat(sb).hasToString(data);
}
}

@Test
public void testMultiFile1() throws IOException, ParserException {
List<Map<String, String>> result;
try (InputStream is = PackagesTest.class.getResourceAsStream("data/test3")) {
result = Packages.parseStatusFile(is);
void testMultiFile1() throws IOException, ParserException {
try (final InputStream is = PackagesTest.class.getResourceAsStream("data/test3")) {
final List<Map<String, String>> result = Packages.parseStatusFile(is);
assertThat(result).hasSize(2);
}

assertEquals(2, result.size());
}
}
31 changes: 31 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<java.release>11</java.release>
<java.source>11</java.source>

<assertj.version>3.25.3</assertj.version>
<bouncycastle.version>1.77</bouncycastle.version>
<commons-compress.version>1.26.1</commons-compress.version>
<commons-codec.version>1.16.1</commons-codec.version>
Expand Down Expand Up @@ -182,6 +183,12 @@
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
Expand All @@ -208,6 +215,30 @@

</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<!-- show log output during tests using logback as the slf4j backend. -->
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
Expand Down
13 changes: 0 additions & 13 deletions rpm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,6 @@
<optional>true</optional>
</dependency>
-->

<!-- testing -->

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.eclipse.packager.rpm.ReadableHeader;
import org.eclipse.packager.rpm.RpmTag;
Expand Down Expand Up @@ -136,7 +136,7 @@ private static List<Dependency> getDependencies(final ReadableHeader<RpmTag> hea
for (int i = 0; i < names.length; i++) {
final String name = names[i];
final String version = versions[i];
final EnumSet<RpmDependencyFlags> flagSet = RpmDependencyFlags.parse(flags[i]);
final Set<RpmDependencyFlags> flagSet = RpmDependencyFlags.parse(flags[i]);
result.add(new Dependency(name, version, flagSet));
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public static int encode(final Set<RpmDependencyFlags> flags) {
return value;
}

public static EnumSet<RpmDependencyFlags> parse(final Long flags) {
public static Set<RpmDependencyFlags> parse(final Long flags) {
if (flags == null) {
return null;
}

return parse(flags.intValue());
}

public static EnumSet<RpmDependencyFlags> parse(final int flags) {
public static Set<RpmDependencyFlags> parse(final int flags) {
final EnumSet<RpmDependencyFlags> result = EnumSet.noneOf(RpmDependencyFlags.class);

for (final RpmDependencyFlags f : values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
********************************************************************************/
package org.eclipse.packager.rpm.signature;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -84,7 +85,7 @@ public static void perform(Path rpm, InputStream privateKeyIn, String passphrase
PGPPrivateKey privateKey = getPrivateKey(privateKeyIn, passphrase);

// Get the information of the RPM
try (RpmInputStream rpmIn = new RpmInputStream(Files.newInputStream(rpm))) {
try (RpmInputStream rpmIn = new RpmInputStream(new BufferedInputStream(Files.newInputStream(rpm)))) {
signatureHeaderStart = rpmIn.getSignatureHeader().getStart();
signatureHeaderLength = rpmIn.getSignatureHeader().getLength();
payloadHeaderStart = rpmIn.getPayloadHeader().getStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import java.io.OutputStream;
import java.time.Instant;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
import java.util.function.Function;
Expand Down Expand Up @@ -335,7 +335,7 @@ private void addDependencies(final Element fmt, final String elementName, final
final Element ele = addElement(fmt, elementName);

for (final Dependency dep : deps) {
final EnumSet<RpmDependencyFlags> flags = RpmDependencyFlags.parse(dep.getFlags());
final Set<RpmDependencyFlags> flags = RpmDependencyFlags.parse(dep.getFlags());
if (flags.contains(RpmDependencyFlags.RPMLIB)) {
continue;
}
Expand Down
Loading

0 comments on commit ecd1acc

Please sign in to comment.