Skip to content
This repository has been archived by the owner on Nov 24, 2024. It is now read-only.

Commit

Permalink
Move tests to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Oct 21, 2023
1 parent 0e6e693 commit 8017c9e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 51 deletions.
5 changes: 2 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@
<scope>provided</scope>
</dependency>
<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>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,35 @@
*/
package org.sonatype.plexus.components.cipher;

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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Test the Plexus Cipher container
*
* @author Oleg Gusakov
*/
public class DefaultPlexusCipherTest {
class DefaultPlexusCipherTest {
private final String passPhrase = "testtest";

final String str = "my testing phrase";

final String encStr = "cYrPoOelYU0HGlsn3nERAIyiLVVgnsn/KC5ZqeAPG0beOZCYrFwWwBTp3uyxt/yx";
PlexusCipher pc;

DefaultPlexusCipher pc;

// -------------------------------------------------------------
@Before
public void prepare() {
@BeforeEach
void prepare() {
pc = new DefaultPlexusCipher();
}

@Test
public void testIsEncryptedString() {
void testIsEncryptedString() {
String noBraces = "This is a test";
String normalBraces = "Comment {This is a test} other comment with a: }";
String escapedBraces = "\\{This is a test\\}";
Expand All @@ -58,7 +56,7 @@ public void testIsEncryptedString() {
}

@Test
public void testUnDecorate_BracesPermutations() throws PlexusCipherException {
void testUnDecorate_BracesPermutations() throws PlexusCipherException {
String noBraces = "This is a test";
String normalBraces = "Comment {This is a test} other comment with a: }";
String mixedBraces = "Comment {foo\\{This is a test\\}} other comment with a: }";
Expand All @@ -71,9 +69,9 @@ public void testUnDecorate_BracesPermutations() throws PlexusCipherException {
// -------------------------------------------------------------

@Test
public void testDefaultAlgorithmExists() throws Exception {
void testDefaultAlgorithmExists() throws Exception {
String[] res = DefaultPlexusCipher.getCryptoImpls("Cipher");
assertNotNull("No Cipher providers found in the current environment", res);
assertNotNull(res, "No Cipher providers found in the current environment");

System.out.println("\n=== Available ciphers :");
for (String re : res) {
Expand All @@ -91,12 +89,12 @@ public void testDefaultAlgorithmExists() throws Exception {
// -------------------------------------------------------------

@Test
public void stestFindDefaultAlgorithm() {
void stestFindDefaultAlgorithm() {
String[] res = DefaultPlexusCipher.getServiceTypes();
assertNotNull("No service types found in the current environment", res);
assertNotNull(res, "No service types found in the current environment");

String[] impls = DefaultPlexusCipher.getCryptoImpls("Cipher");
assertNotNull("No Cipher providers found in the current environment", impls);
assertNotNull(impls, "No Cipher providers found in the current environment");

for (String impl : impls)
try {
Expand All @@ -110,20 +108,20 @@ public void stestFindDefaultAlgorithm() {

// -------------------------------------------------------------
@Test
public void testEncrypt() throws Exception {
void testEncrypt() throws Exception {
String xRes = pc.encrypt(str, passPhrase);

System.out.println(xRes);

String res = pc.decrypt(xRes, passPhrase);

assertEquals("Encryption/Decryption did not produce desired result", str, res);
assertEquals(str, res, "Encryption/Decryption did not produce desired result");
}

// -------------------------------------------------------------

@Test
public void testEncryptVariableLengths() throws Exception {
void testEncryptVariableLengths() throws Exception {
String pass = "g";

for (int i = 0; i < 64; i++) {
Expand All @@ -135,44 +133,44 @@ public void testEncryptVariableLengths() throws Exception {

String res = pc.decrypt(xRes, pass);

assertEquals("Encryption/Decryption did not produce desired result", str, res);
assertEquals(str, res, "Encryption/Decryption did not produce desired result");
}
}

@Test
public void testDecrypt() {
try {
String res = pc.decrypt(encStr, passPhrase);
assertEquals("Decryption did not produce desired result", str, res);
} catch (Exception e) {
fail("Decryption failed: " + e.getMessage());
}
void testDecrypt() {
assertDoesNotThrow(
() -> {
String res = pc.decrypt(encStr, passPhrase);
assertEquals(str, res, "Decryption did not produce desired result");
},
"Decryption failed: ");
}

// -------------------------------------------------------------

@Test
public void testDecorate() {
void testDecorate() {
String res = pc.decorate("aaa");
assertEquals(
"Decoration failed",
PlexusCipher.ENCRYPTED_STRING_DECORATION_START + "aaa" + PlexusCipher.ENCRYPTED_STRING_DECORATION_STOP,
res);
res,
"Decoration failed");
}

// -------------------------------------------------------------

@Test
public void testUnDecorate() throws Exception {
void testUnDecorate() throws Exception {
String res = pc.unDecorate(
PlexusCipher.ENCRYPTED_STRING_DECORATION_START + "aaa" + PlexusCipher.ENCRYPTED_STRING_DECORATION_STOP);
assertEquals("Decoration failed", "aaa", res);
assertEquals("aaa", res, "Decoration failed");
}

// -------------------------------------------------------------

@Test
public void testEncryptAndDecorate() throws Exception {
void testEncryptAndDecorate() throws Exception {
String res = pc.encryptAndDecorate("my-password", "12345678");

assertEquals('{', res.charAt(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ Licensed to the Apache Software Foundation (ASF) under one

package org.sonatype.plexus.components.cipher;

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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @author Oleg Gusakov
*/
public class PBECipherTest {
class PBECipherTest {
PBECipher pbeCipher;

final String clearText = "veryOpenText";
Expand All @@ -38,13 +38,13 @@ public class PBECipherTest {

final String password = "testtest";

@Before
public void prepare() {
@BeforeEach
void prepare() {
pbeCipher = new PBECipher();
}

@Test
public void testEncrypt() throws Exception {
void testEncrypt() throws Exception {
String enc = pbeCipher.encrypt64(clearText, password);

assertNotNull(enc);
Expand All @@ -61,14 +61,14 @@ public void testEncrypt() throws Exception {
}

@Test
public void testDecrypt() throws Exception {
void testDecrypt() throws Exception {
String clear = pbeCipher.decrypt64(encryptedText, password);

assertEquals(clearText, clear);
}

@Test
public void testEncoding() throws Exception {
void testEncoding() throws Exception {
System.out.println("file.encoding=" + System.getProperty("file.encoding"));

String pwd = "äüöÜÖÄß\"§$%&/()=?é";
Expand Down

0 comments on commit 8017c9e

Please sign in to comment.