Fix sonarqube issues
This commit is contained in:
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/monosubstitution/VigenereTest.java
|
||||
//Mattrixwv
|
||||
// Created: 07-25-21
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -12,6 +8,7 @@ import static org.mockito.Mockito.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
@@ -30,13 +27,19 @@ public class VigenereTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String inputString = "MeSsage to^encode";
|
||||
private static final String inputStringClean = "MESSAGETOENCODE";
|
||||
private static final String outputString = "WiQooxh ds^cjqfgo";
|
||||
private static final String outputStringClean = "WIQOOXHDSCJQFGO";
|
||||
private static final String keyword = "ke yw*ord";
|
||||
private static final String keywordClean = "KEYWORD";
|
||||
private ArrayList<Integer> offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3));
|
||||
private static final String INPUT_STRING = "MeSsage to^encode";
|
||||
private static final String INPUT_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String OUTPUT_STRING = "WiQooxh ds^cjqfgo";
|
||||
private static final String OUTPUT_STRING_CLEAN = "WIQOOXHDSCJQFGO";
|
||||
private static final String KEYWORD = "ke yw*ord";
|
||||
private static final String KEYWORD_CLEAN = "KEYWORD";
|
||||
private ArrayList<Integer> offset;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup(){
|
||||
offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@@ -93,11 +96,11 @@ public class VigenereTest{
|
||||
|
||||
@Test
|
||||
public void testSetOffset(){
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
|
||||
cipher.setOffset();
|
||||
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
verify(logger, times(1)).debug("Setting offset array from keyword");
|
||||
verify(logger, times(1)).debug("Offset {}", offset);
|
||||
@@ -108,17 +111,17 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -126,17 +129,17 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = false;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -144,17 +147,17 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -162,17 +165,17 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -180,7 +183,7 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -200,7 +203,7 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -217,14 +220,14 @@ public class VigenereTest{
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", keyword);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", KEYWORD);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing all non-letter characters");
|
||||
verify(logger, times(1)).debug("Clean keyword '{}'", keywordClean);
|
||||
verify(logger, times(1)).debug("Clean keyword '{}'", KEYWORD_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -260,13 +263,13 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.inputString = inputString;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(OUTPUT_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(2)).debug("Encoding uppercase");
|
||||
@@ -274,7 +277,7 @@ public class VigenereTest{
|
||||
verify(logger, times(13)).debug("Encoding lowercase");
|
||||
verify(logger, times(5)).debug("Wrapping around to a");
|
||||
verify(logger, times(17)).debug(eq("Encoded character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Encoded message '{}'", outputString);
|
||||
verify(logger, times(1)).debug("Encoded message '{}'", OUTPUT_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -282,13 +285,13 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.inputString = outputString;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = OUTPUT_STRING;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(inputString, cipher.outputString);
|
||||
assertEquals(INPUT_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(2)).debug("Decoding uppercase");
|
||||
@@ -296,27 +299,27 @@ public class VigenereTest{
|
||||
verify(logger, times(13)).debug("Decoding lowercase");
|
||||
verify(logger, times(5)).debug("Wrapping around to z");
|
||||
verify(logger, times(17)).debug(eq("Decoded character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Decoded message '{}'", inputString);
|
||||
verify(logger, times(1)).debug("Decoded message '{}'", INPUT_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.outputString = outputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
cipher.outputString = OUTPUT_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.offset = offset;
|
||||
|
||||
assertEquals(inputString, cipher.getInputString());
|
||||
assertEquals(outputString, cipher.getOutputString());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(INPUT_STRING, cipher.getInputString());
|
||||
assertEquals(OUTPUT_STRING, cipher.getOutputString());
|
||||
assertEquals(KEYWORD, cipher.getKeyword());
|
||||
assertEquals(offset, cipher.getOffsets());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.outputString = outputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
cipher.outputString = OUTPUT_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.reset();
|
||||
@@ -332,51 +335,51 @@ public class VigenereTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Vigenere(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
String output = cipher.encode(KEYWORD, INPUT_STRING);
|
||||
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(INPUT_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(outputString, output);
|
||||
assertEquals(OUTPUT_STRING, cipher.outputString);
|
||||
assertEquals(OUTPUT_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Vigenere(false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
String output = cipher.encode(KEYWORD, INPUT_STRING);
|
||||
|
||||
assertEquals(inputStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(INPUT_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(outputStringClean, cipher.outputString);
|
||||
assertEquals(outputStringClean, output);
|
||||
assertEquals(OUTPUT_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(OUTPUT_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Vigenere(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
String output = cipher.decode(KEYWORD, OUTPUT_STRING);
|
||||
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(OUTPUT_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(inputString, cipher.outputString);
|
||||
assertEquals(inputString, output);
|
||||
assertEquals(INPUT_STRING, cipher.outputString);
|
||||
assertEquals(INPUT_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new Vigenere(false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
String output = cipher.decode(KEYWORD, OUTPUT_STRING);
|
||||
|
||||
assertEquals(outputStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(OUTPUT_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(inputStringClean, cipher.outputString);
|
||||
assertEquals(inputStringClean, output);
|
||||
assertEquals(INPUT_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(INPUT_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user