Skip to content

Commit

Permalink
junit5 migration
Browse files Browse the repository at this point in the history
* updated dependencies
* dropped sbt support
  • Loading branch information
gaborbata committed Mar 5, 2024
1 parent c05d995 commit ec205ac
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 108 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ How to compile

* Gradle: `gradle clean build` (preferred)
* Maven: `mvn clean package`
* sbt: `sbt clean package` (experimental)

Configuration
-------------
Expand Down
16 changes: 12 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
id 'org.gradle.crypto.checksum' version '1.4.0'
id 'checkstyle'
id 'jacoco'
id 'jvm-test-suite'
}

compileJava.options.encoding = 'UTF-8'
Expand All @@ -17,10 +18,17 @@ repositories {
}

dependencies {
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.15.3'
implementation group: 'com.formdev', name: 'flatlaf', version: '3.2.2'
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.16.1'
implementation group: 'com.formdev', name: 'flatlaf', version: '3.4'
implementation group: 'com.formdev', name: 'svgSalamander', version: '1.1.4'
testImplementation 'junit:junit:4.13.2'
}

testing {
suites {
test {
useJUnitJupiter()
}
}
}

java {
Expand Down Expand Up @@ -85,7 +93,7 @@ task bumpVersion {
if (!project.hasProperty('toVersion')) {
throw new GradleException("Please provide 'toVersion' property e.g. gradle bumpVersion -PtoVersion=$project.version")
}
def extensions = ['java', 'gradle', 'sbt', 'xml', 'md', 'bat', 'sh', 'txt', 'command'].collect { "**/*.$it" }.join(",")
def extensions = ['java', 'gradle', 'xml', 'md', 'bat', 'sh', 'txt', 'command'].collect { "**/*.$it" }.join(",")
def files = new groovy.util.FileNameFinder().getFileNames("$projectDir", extensions)
files.each { versionedFile ->
def file = new File(versionedFile)
Expand Down
15 changes: 0 additions & 15 deletions build.sbt

This file was deleted.

10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.15.3</version>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>com.formdev</groupId>
<artifactId>flatlaf</artifactId>
<version>3.2.2</version>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.formdev</groupId>
<artifactId>svgSalamander</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/jpass/crypt/Aes256Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import java.util.Arrays;
import java.util.Random;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Test values for the &quot;Advanced Encryption Standard&quot; (AES). These
Expand Down Expand Up @@ -47,11 +46,11 @@ public void shouldEntryptAndDecryptATestMessage() {
(byte) 0x45, (byte) 0xbf, (byte) 0xea, (byte) 0xfc, (byte) 0x49, (byte) 0x90, (byte) 0x4b, (byte) 0x49,
(byte) 0x60, (byte) 0x89};

Assert.assertTrue(Arrays.equals(expectedEncrypted, encrypted));
Assertions.assertTrue(Arrays.equals(expectedEncrypted, encrypted));

byte[] decrypted = new byte[16];
cipher.decrypt(expectedEncrypted, 0, decrypted, 0);
Assert.assertTrue(Arrays.equals(block, decrypted));
Assertions.assertTrue(Arrays.equals(block, decrypted));
}

/**
Expand All @@ -73,7 +72,7 @@ public void shouldEncryptAndDecryptRandomData() {
Aes256 cipher = new Aes256(key);
cipher.encrypt(data, 0, encrypted, 0);
cipher.decrypt(encrypted, 0, decrypted, 0);
Assert.assertTrue(Arrays.equals(data, decrypted));
Assertions.assertTrue(Arrays.equals(data, decrypted));
}
}
}
23 changes: 11 additions & 12 deletions src/test/java/jpass/crypt/CbcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Unit test for the CBC encryption. The test data will be encrypted and
Expand Down Expand Up @@ -51,7 +50,7 @@ public class CbcTest {
/**
* Sets the encryption and decryption instances up.
*/
@Before
@BeforeEach
public void setup() {
byte[] iv = {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
Expand Down Expand Up @@ -81,7 +80,7 @@ public void shouldEncryptAndDecryptASmallMessage() throws DecryptException, IOEx
_decrypt.decrypt(_encrypted.toByteArray());
_decrypt.finishDecryption();

Assert.assertTrue(Arrays.equals(source, _decrypted.toByteArray()));
Assertions.assertTrue(Arrays.equals(source, _decrypted.toByteArray()));
}

/**
Expand All @@ -100,9 +99,9 @@ public void shouldEncryptAndDecryptABigMessage() throws DecryptException, IOExce
_decrypt.finishDecryption();

byte[] d = _decrypted.toByteArray();
Assert.assertEquals(3000, d.length);
Assertions.assertEquals(3000, d.length);
for (int i = 0; i < d.length; ++i) {
Assert.assertEquals(0x81, d[i] & 0xff);
Assertions.assertEquals(0x81, d[i] & 0xff);
}
}

Expand Down Expand Up @@ -146,13 +145,13 @@ public void shouldWorkWithReferenceData() throws DecryptException, IOException {
encrypt.encrypt(plain);
encrypt.finishEncryption();

Assert.assertEquals(expected.length, _encrypted.toByteArray().length);
Assert.assertTrue(Arrays.equals(expected, _encrypted.toByteArray()));
Assertions.assertEquals(expected.length, _encrypted.toByteArray().length);
Assertions.assertTrue(Arrays.equals(expected, _encrypted.toByteArray()));

decrypt.decrypt(_encrypted.toByteArray());
decrypt.finishDecryption();

Assert.assertTrue(Arrays.equals(plain, _decrypted.toByteArray()));
Assertions.assertTrue(Arrays.equals(plain, _decrypted.toByteArray()));
}

/**
Expand All @@ -178,6 +177,6 @@ private void testRandom(Random rnd, int size) throws DecryptException, IOExcepti
_decrypt.decrypt(_encrypted.toByteArray());
_decrypt.finishDecryption();

Assert.assertTrue(Arrays.equals(data, _decrypted.toByteArray()));
Assertions.assertTrue(Arrays.equals(data, _decrypted.toByteArray()));
}
}
9 changes: 4 additions & 5 deletions src/test/java/jpass/crypt/io/CryptIOStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Tests {@link jpass.crypt.io.CryptInputStream} and
Expand Down Expand Up @@ -54,7 +53,7 @@ public void shouldDecryptAnEncryptedRandomMessage() throws IOException {
decrypted.close();
decrypter.close();

Assert.assertEquals(plain.length, decrypted.toByteArray().length);
Assert.assertTrue(Arrays.equals(plain, decrypted.toByteArray()));
Assertions.assertEquals(plain.length, decrypted.toByteArray().length);
Assertions.assertTrue(Arrays.equals(plain, decrypted.toByteArray()));
}
}
18 changes: 9 additions & 9 deletions src/test/java/jpass/data/DataModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@

import jpass.xml.bind.Entries;
import jpass.xml.bind.Entry;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class DataModelTest {
class DataModelTest {

public DataModel dataModel;

@Before
@BeforeEach
public void setup() {
dataModel = DataModel.getInstance();
dataModel.clear();
Expand Down
56 changes: 31 additions & 25 deletions src/test/java/jpass/data/EntriesRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
import java.io.IOException;
import jpass.xml.bind.Entries;
import jpass.xml.bind.Entry;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Unit test for {@link EntriesRepository}.
*
* @author Gabor Bata
*/
public class EntriesRepositoryTest {
class EntriesRepositoryTest {

private static final String TITLE = "Duff Beer Webshop";
private static final String URL = "http://duffbeer.com";
Expand All @@ -55,7 +55,7 @@ public class EntriesRepositoryTest {
private char[] correctKey;
private char[] incorrectKey;

@Before
@BeforeEach
public void setup() throws Exception {
File tempFile = File.createTempFile("jpass", "temp");
tempFile.deleteOnExit();
Expand All @@ -78,35 +78,38 @@ public void shouldWriteAndReadEncryptedFileWithCorrectPassword() throws Document
assertEquals(expectedEntries, readEntries);
}

@Test(expected = IOException.class)
@Test
public void shouldThrowExceptionWhenReadingDocumentWithIncorrectKey() throws DocumentProcessException, IOException {
// given
EntriesRepository.newInstance(filePath, correctKey).writeDocument(createEntries());

// when
EntriesRepository.newInstance(filePath, incorrectKey).readDocument();
Assertions.assertThrows(IOException.class,
() -> EntriesRepository.newInstance(filePath, incorrectKey).readDocument());
}

@Test(expected = IOException.class)
@Test
public void shouldThrowExceptionWhenReadingDocumentWithInvalidFormat() throws DocumentProcessException, IOException {
// given
try ( FileWriter writer = new FileWriter(filePath)) {
writer.append("invalid content");
} catch (Exception e) {
Assert.fail("could not prepare test data");
Assertions.fail("could not prepare test data");
}

// when
EntriesRepository.newInstance(filePath, correctKey).readDocument();
Assertions.assertThrows(IOException.class,
() -> EntriesRepository.newInstance(filePath, correctKey).readDocument());
}

@Test(expected = FileNotFoundException.class)
@Test
public void shouldThrowExceptionWhenReadingDocumentWithNonExistingFile() throws DocumentProcessException, IOException {
// given
EntriesRepository entriesRepository = EntriesRepository.newInstance("not_existing_path", correctKey);

// when
entriesRepository.readDocument();
Assertions.assertThrows(FileNotFoundException.class,
() -> entriesRepository.readDocument());
}

@Test
Expand All @@ -122,35 +125,38 @@ public void shouldBeAbleToWriteAndReadUnencryptedFile() throws DocumentProcessEx
assertEquals(expectedEntries, readEntries);
}

@Test(expected = IOException.class)
@Test
public void shouldNotBeAbleToReadEncryptedFileWithoutKey() throws DocumentProcessException, IOException {
// given
EntriesRepository.newInstance(filePath, correctKey).writeDocument(createEntries());

// when
EntriesRepository.newInstance(filePath).readDocument();
Assertions.assertThrows(IOException.class,
() -> EntriesRepository.newInstance(filePath).readDocument());
}

@Test(expected = IOException.class)
@Test
public void shouldThrowExceptionWhenReadingUnencryptedDocumentWithInvalidFormat() throws DocumentProcessException, IOException {
// given
try ( FileWriter writer = new FileWriter(filePath)) {
writer.append("invalid content");
} catch (Exception e) {
Assert.fail("could not prepare test data");
Assertions.fail("could not prepare test data");
}

// when
EntriesRepository.newInstance(filePath).readDocument();
Assertions.assertThrows(IOException.class,
() -> EntriesRepository.newInstance(filePath).readDocument());
}

@Test(expected = FileNotFoundException.class)
@Test
public void shouldThrowExceptionWhenReadingUnecrypredDocumentWithNonExistingFile() throws DocumentProcessException, IOException {
// given
EntriesRepository entriesRepository = EntriesRepository.newInstance("not_existing_path");

// when
entriesRepository.readDocument();
Assertions.assertThrows(FileNotFoundException.class,
() -> entriesRepository.readDocument());
}

@Test
Expand Down Expand Up @@ -206,15 +212,15 @@ private Entry createEntry() {
}

private void assertEquals(Entries expectedEntries, Entries actualEntries) {
Assert.assertEquals(expectedEntries.getEntry().size(), actualEntries.getEntry().size(), 1);
Assertions.assertEquals(expectedEntries.getEntry().size(), actualEntries.getEntry().size(), 1);
assertEquals(expectedEntries.getEntry().iterator().next(), actualEntries.getEntry().iterator().next());
}

private void assertEquals(Entry expectedEntry, Entry actualEntry) {
Assert.assertEquals(expectedEntry.getTitle(), actualEntry.getTitle());
Assert.assertEquals(expectedEntry.getUrl(), actualEntry.getUrl());
Assert.assertEquals(expectedEntry.getUser(), actualEntry.getUser());
Assert.assertEquals(expectedEntry.getPassword(), actualEntry.getPassword());
Assert.assertEquals(expectedEntry.getNotes(), actualEntry.getNotes());
Assertions.assertEquals(expectedEntry.getTitle(), actualEntry.getTitle());
Assertions.assertEquals(expectedEntry.getUrl(), actualEntry.getUrl());
Assertions.assertEquals(expectedEntry.getUser(), actualEntry.getUser());
Assertions.assertEquals(expectedEntry.getPassword(), actualEntry.getPassword());
Assertions.assertEquals(expectedEntry.getNotes(), actualEntry.getNotes());
}
}
Loading

0 comments on commit ec205ac

Please sign in to comment.