Added more unit test coverage

This commit is contained in:
2023-04-29 11:14:45 -04:00
parent 3966f46024
commit bbcd6ea416
8 changed files with 1166 additions and 795 deletions

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Bifid.java
//Mattrixwv
// Created: 03-03-22
//Modified: 07-09-22
//Modified: 04-23-23
package com.mattrixwv.cipherstream.polysubstitution;
@@ -14,20 +14,20 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Bifid{
private static final Logger logger = LoggerFactory.getLogger(Bifid.class);
protected static Logger logger = LoggerFactory.getLogger(Bifid.class);
//Fields
private String inputString; //The message that needs to be encoded/decoded
private String outputString; //The encoded/decoded message
private String keyword; //The keyword used to create the grid
private PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters
private boolean preserveCapitals; //Persist capitals in the output string
private boolean preserveWhitespace; //Persist whitespace in the output string
private boolean preserveSymbols; //Persist symbols in the output string
protected String inputString; //The message that needs to be encoded/decoded
protected String outputString; //The encoded/decoded message
protected String keyword; //The keyword used to create the grid
protected PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Strips invalid characters from the keyword and creates the grid
private void setKeyword(String keyword) throws InvalidKeywordException{
protected void setKeyword(String keyword) throws InvalidKeywordException{
//Ensure the keyword isn't null
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
@@ -39,7 +39,7 @@ public class Bifid{
this.keyword = keyword;
}
//Ensures inputString constraints
private void setInputString(String inputString) throws InvalidInputException{
protected void setInputString(String inputString) throws InvalidInputException{
//Ensure the input string isn't null
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -74,7 +74,7 @@ public class Bifid{
}
}
//Adds all non-letter characters back to the output string
private void formatOutput(String outputString){
protected void formatOutput(String outputString){
logger.debug("Formatting output");
//Keep track of where you are in the output
int outputCnt = 0;
@@ -100,15 +100,15 @@ public class Bifid{
}
//Save the output
logger.debug("Formatted output string '{}'", output);
this.outputString = output.toString();
logger.debug("Formatted output string '{}'", this.outputString);
}
//Encodes inputString using a polybius square and stores the result in outputString
private void encode() throws InvalidCharacterException, InvalidInputException{
protected void encode() throws InvalidCharacterException, InvalidInputException{
logger.debug("Encoding");
//Get the encoded numbers from a polybius square
logger.debug("Encoding polybius");
logger.debug("Encoding Polybius");
String polybiusMessage = polybiusSquare.encode(keyword, inputString).replaceAll("\\s", "");
keyword = polybiusSquare.getKeyword(); //Save the cleaned keyword
@@ -118,7 +118,7 @@ public class Bifid{
boolean firstNum = true;
logger.debug("Splitting Polybius Square message");
for(char ch : polybiusMessage.toCharArray()){
logger.debug("Current character {}", ch);
logger.debug("Current character '{}'", ch);
if(firstNum){
row0.append(ch);
@@ -140,7 +140,7 @@ public class Bifid{
formatOutput(letterResult);
}
//Decodes inputString using a polybius square and stores the result in outputString
private void decode() throws InvalidCharacterException, InvalidInputException{
protected void decode() throws InvalidCharacterException, InvalidInputException{
logger.debug("Decoding");
//Get the decoded number from a polybius square