Skip to content

Commit

Permalink
JUnit 5/AssertJ/Testcontainers migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Apr 16, 2024
1 parent 700585e commit 2b0151d
Show file tree
Hide file tree
Showing 7 changed files with 496 additions and 75 deletions.
83 changes: 77 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@
<org.bouncycastle.bcpg.version>1.77</org.bouncycastle.bcpg.version>
<org.bouncycastle.bcprov.version>1.77</org.bouncycastle.bcprov.version>

<packager.version>0.20.0</packager.version>
<packager.version>0.20.1-SNAPSHOT</packager.version>
<assertj.version>3.25.3</assertj.version>
<junit.version>5.10.2</junit.version>
<logback.version>1.5.5</logback.version>
<slf4j.version>2.0.13</slf4j.version>
<testcontainers.version>1.19.7</testcontainers.version>
</properties>

<prerequisites>
Expand Down Expand Up @@ -136,15 +141,35 @@
<version>1.15</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>

<!-- EOTD -->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<dependencyManagement>
Expand All @@ -169,6 +194,34 @@
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-bom</artifactId>
<version>${assertj.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-bom</artifactId>
<version>${slf4j.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -205,6 +258,12 @@
<version>2.0.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.5</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
Expand Down Expand Up @@ -430,7 +489,7 @@
<debug>true</debug>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<preBuildHookScript>setup</preBuildHookScript>
<postBuildHookScript>verify</postBuildHookScript>
<!--<postBuildHookScript>verify</postBuildHookScript>-->
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
<profiles>
<!-- <profile>sign</profile> -->
Expand All @@ -451,6 +510,18 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
39 changes: 15 additions & 24 deletions src/main/java/de/dentrassi/rpm/builder/RpmUnpackMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -383,41 +384,31 @@ private void setFileOwnership(final InputHeader<RpmTag> payloadHeader,
}

private static String getName(final InputHeader<RpmTag> payloadHeader, final RpmTag tag, final long id) {
final Object values =
payloadHeader.getEntry(tag)
.orElseThrow(() -> new IllegalStateException("RPM lacks " + tag + " lookup table"))
.getValue();

if (!(values instanceof String[])) {
throw new IllegalStateException("RPM " + tag + " header is not a list of Strings, got " +
values.getClass());
final List<String> names = payloadHeader.getStringList(tag);

if (names == null) {
throw new IllegalStateException("RPM lacks " + tag + " lookup table");
}

final String[] names = (String[]) values;
if (id < 0 || names.length <= id) {
throw new IllegalArgumentException("id out of range [0," + names.length + ']');
if (id < 0 || names.size() <= id) {
throw new IllegalArgumentException("id out of range [0," + names.size() + ']');
}

return names[(int) id];
return names.get((int) id);
}

private static String getLinkTarget(final InputHeader<RpmTag> payloadHeader, final long inode) {
final Object values =
payloadHeader.getEntry(RpmTag.FILE_LINKTO)
.orElseThrow(() ->
new IllegalStateException("RPM contains symbolic link, but lacks linkTo header"))
.getValue();

if (!(values instanceof String[])) {
throw new IllegalStateException("RPM linkTo header is not a list of Strings, got " + values.getClass());
final List<String> linkTo = payloadHeader.getStringList(RpmTag.FILE_LINKTO);

if (linkTo == null) {
throw new IllegalStateException("RPM contains symbolic link, but lacks linkTo header");
}

final String[] linkTo = (String[]) values;
if (inode < 0 || linkTo.length <= inode) {
throw new IllegalArgumentException("Symbolic link inode out of range [0," + linkTo.length + ']');
if (inode < 0 || linkTo.size() <= inode) {
throw new IllegalArgumentException("Symbolic link inode out of range [0," + linkTo.size() + ']');
}

return linkTo[(int) inode];
return linkTo.get((int) inode);
}

public void setRpmFile(final File rpmFile) {
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/de/dentrassi/rpm/builder/EntryDetailsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

import org.eclipse.packager.rpm.FileFlags;
import org.eclipse.packager.rpm.build.FileInformation;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.Set;

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

/**
* Test class de.dentrassi.rpm.builder.EntryDetails.
*/
public class EntryDetailsTest {
class EntryDetailsTest {
/**
* Verify that empty {@link EntryDetails} result in empty set of {@link FileFlags}.
*/
@Test
public void applyEmpty() {
void applyEmpty() {
final EntryDetails entryDetails = new EntryDetails();
doTest(new FileFlags[]{}, false, entryDetails);
}
Expand All @@ -26,7 +25,7 @@ public void applyEmpty() {
* Verify that {@link EntryDetails#setReadme(java.lang.Boolean)} correctly controls {@link FileFlags#README}.
*/
@Test
public void applyReadmeTrue() {
void applyReadmeTrue() {
final EntryDetails entryDetails = new EntryDetails();
entryDetails.setReadme(true);
doTest(new FileFlags[]{FileFlags.README}, true, entryDetails);
Expand All @@ -36,7 +35,7 @@ public void applyReadmeTrue() {
* False negative? See https://github.com/ctron/rpm-builder/issues/42
*/
@Test
public void applyReadmeFalse() {
void applyReadmeFalse() {
final EntryDetails entryDetails = new EntryDetails();
entryDetails.setReadme(false);
doTest(new FileFlags[]{FileFlags.README}, true, entryDetails); // questionable
Expand All @@ -51,8 +50,9 @@ public void applyReadmeFalse() {
*/
private static void doTest(FileFlags[] expectedResult, boolean expectedApplied, final EntryDetails entryDetails) {
final FileInformation fileInformation = new FileInformation();
assertEquals(expectedApplied, entryDetails.apply(fileInformation));
assertThat(entryDetails.apply(fileInformation)).isEqualTo(expectedApplied);
final Set<FileFlags> fileFlags = fileInformation.getFileFlags();
assertArrayEquals(expectedResult, fileFlags.toArray());
assertThat(fileFlags.toArray()).isEqualTo(expectedResult);
}
}
}

44 changes: 15 additions & 29 deletions src/test/java/de/dentrassi/rpm/builder/PackageEntryTest.java
Original file line number Diff line number Diff line change
@@ -1,71 +1,57 @@
package de.dentrassi.rpm.builder;

import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;

import java.io.File;

import org.junit.Test;

public class PackageEntryTest {
public PackageEntryTest() {
super();
}
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class PackageEntryTest {
@Test
public void testValidateNameNull() {
void testValidateNameNull() {
final PackageEntry entry = new PackageEntry();
entry.setLinkTo("something-to-link-to");

assertThrows(IllegalStateException.class, () -> entry.validate());
assertThatThrownBy(entry::validate).isInstanceOf(IllegalStateException.class);
}

@Test
public void testValidateNameEmpty() {
void testValidateNameEmpty() {
final PackageEntry entry = new PackageEntry();
entry.setName("");
entry.setLinkTo("something-to-link-to");

assertThrows(IllegalStateException.class, () -> entry.validate());
assertThatThrownBy(entry::validate).isInstanceOf(IllegalStateException.class);
}

@Test
public void testValidateNoSource() {
void testValidateNoSource() {
final PackageEntry entry = new PackageEntry();
entry.setName("some-entry");

assertThrows(IllegalStateException.class, () -> entry.validate());
assertThatThrownBy(entry::validate).isInstanceOf(IllegalStateException.class);
}

@Test
public void testValidateGhostNull() {
void testValidateGhostNull() {
final PackageEntry entry = new PackageEntry();
entry.setName("some-entry");
entry.setGhost(null);

// no NullPointerException must be thrown
assertThrows(IllegalStateException.class, () -> entry.validate());
assertThatThrownBy(entry::validate).isInstanceOf(IllegalStateException.class);
}

@Test
public void testValidateGhostSource() {
final PackageEntry entry = new PackageEntry();
entry.setName("some-entry");
entry.setGhost(Boolean.TRUE);

try {
entry.validate();
} catch (final RuntimeException e) {
fail("Ghost entries do not require other sources, got error: " + e.getMessage());
}
assertThatCode(entry::validate).withFailMessage("Ghost entries do not require other sources").doesNotThrowAnyException();
}

@Test
public void testValidateMultipleSourcesGhost() {
void testValidateMultipleSourcesGhost() {
final PackageEntry entry = new PackageEntry();
entry.setName("some-entry");
entry.setFile(new File("some-file-entry"));
entry.setGhost(Boolean.TRUE);

assertThrows(IllegalStateException.class, () -> entry.validate());
assertThatThrownBy(entry::validate).isInstanceOf(IllegalStateException.class);
}
}
Loading

0 comments on commit 2b0151d

Please sign in to comment.