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/PolybiusSquare.java
//Mattrixwv
// Created: 01-04-22
//Modified: 07-09-22
//Modified: 04-29-23
package com.mattrixwv.cipherstream.polysubstitution;
@@ -12,10 +12,11 @@ import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class PolybiusSquare{
protected static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
protected static Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
//A class representing the location of a character in the grid
protected class CharLocation{
@@ -43,6 +44,41 @@ public class PolybiusSquare{
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
//Setting the character to be replaced
protected void setReplaced(char replaced) throws InvalidCharacterException{
logger.debug("Setting replaced");
logger.debug("Original character {}", replaced);
if(!Character.isAlphabetic(replaced)){
throw new InvalidCharacterException("The replaced character must be a letter");
}
logger.debug("Checking replacer");
if(replaced == replacer){
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
}
this.replaced = Character.toUpperCase(replaced);
logger.debug("Cleaned character {}", this.replaced);
}
//Setting the character that replaces replaced
protected void setReplacer(char replacer) throws InvalidCharacterException{
logger.debug("Setting replacer");
logger.debug("Original character {}", replacer);
if(!Character.isAlphabetic(replacer)){
throw new InvalidCharacterException("The replacer character must be a letter");
}
logger.debug("Checking replaced");
if(replaced == replacer){
throw new InvalidCharacterException("The replacer letter cannot be the same as the replaced letter");
}
this.replacer = Character.toUpperCase(replacer);
logger.debug("Cleaned character {}", this.replacer);
}
//Create the grid from the keyword
protected void createGrid(){
logger.debug("Creating grid from keyword");
@@ -57,18 +93,18 @@ public class PolybiusSquare{
logger.debug("Created grid\n{}", getGrid());
}
//Strips invalid characters from the string that needs encoded/decoded
protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
throw new InvalidInputException("Input cannot be null");
}
logger.debug("Setting input string for encoding '{}'", inputString);
//Make sure the string doesn't contain any numbers
logger.debug("Checking for digits");
for(char ch = '0';ch <= '9';++ch){
if(inputString.contains(Character.toString(ch))){
throw new InvalidCharacterException("Inputs for encoding cannot contain numbers");
for(char ch : inputString.toCharArray()){
if(Character.isDigit(ch)){
throw new InvalidInputException("Inputs for encoding cannot contain numbers");
}
}
@@ -107,13 +143,13 @@ public class PolybiusSquare{
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString;
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
if(this.inputString.isBlank() || getPreparedInputStringEncode().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
protected void setInputStringDecoding(String inputString) throws InvalidCharacterException, InvalidInputException{
protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
throw new InvalidInputException("Input cannot be null");
}
logger.debug("Setting input string for decoding '{}'", inputString);
@@ -127,11 +163,11 @@ public class PolybiusSquare{
++numberOfDigits;
}
else if(Character.isAlphabetic(ch)){
throw new InvalidCharacterException("Inputs for decoding cannot contains letters");
throw new InvalidInputException("Inputs for decoding cannot contains letters");
}
}
if((numberOfDigits % 2) != 0){
throw new InvalidCharacterException("There must be an even number of digits in an encoded string");
throw new InvalidInputException("There must be an even number of digits in an encoded string");
}
//Remove any whitespace if selected
@@ -152,12 +188,12 @@ public class PolybiusSquare{
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString;
if(this.inputString.isBlank() || getPreparedInputStringDecoding().isBlank()){
if(this.inputString.isBlank() || getPreparedInputStringDecode().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Returns the input string ready for encoding
protected String getPreparedInputStringEncoding(){
protected String getPreparedInputStringEncode(){
logger.debug("Preparing input string for encoding");
String cleanString = inputString.toUpperCase();
@@ -166,8 +202,8 @@ public class PolybiusSquare{
logger.debug("Prepared string '{}'", cleanString);
return cleanString;
}
protected String getPreparedInputStringDecoding(){
logger.debug("Prepareing input string for decoding");
protected String getPreparedInputStringDecode(){
logger.debug("Preparing input string for decoding");
String cleanString = inputString.replaceAll("\\D", "");
@@ -177,7 +213,7 @@ public class PolybiusSquare{
//Strips invalid characters from the keyword and creates the grid
protected void setKeyword(String keyword){
if(keyword == null){
throw new NullPointerException("Keyword cannot be null");
throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("Original keyword {}", keyword);
@@ -187,7 +223,7 @@ public class PolybiusSquare{
keyword = keyword.toUpperCase();
//Remove everything except capital letters
logger.debug("Removing all non-letters");
logger.debug("Removing all non-letter characters");
keyword = keyword.replaceAll("[^A-Z]", "");
//Add all letters in the alphabet to the key
@@ -202,7 +238,7 @@ public class PolybiusSquare{
StringBuilder uniqueKey = new StringBuilder();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
this.keyword = uniqueKey.toString();
logger.debug("Cleaned keyword {}", keyword);
logger.debug("Cleaned keyword {}", this.keyword);
//Create the grid from the sanitized keyword
createGrid();
@@ -229,11 +265,11 @@ public class PolybiusSquare{
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Working character {}", inputString.charAt(inputCnt));
for(char inputChar : inputString.toCharArray()){
logger.debug("Working character {}", inputChar);
//Add both numbers of any letters to the output
if(Character.isAlphabetic(inputString.charAt(inputCnt))){
if(Character.isAlphabetic(inputChar)){
logger.debug("Adding encoded characters");
fullOutput.append(cleanString.charAt(outputCnt++));
@@ -243,15 +279,15 @@ public class PolybiusSquare{
else{
logger.debug("Adding symbols");
fullOutput.append(inputString.charAt(inputCnt));
fullOutput.append(inputChar);
}
}
logger.debug("Formatted output '{}'", fullOutput);
outputString = fullOutput.toString();
logger.debug("Formatted output '{}'", outputString);
}
protected void addCharactersToCleanStringDecode(String cleanString){
logger.debug("Formatting output string to decoding");
logger.debug("Formatting output string for decoding");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
@@ -273,20 +309,20 @@ public class PolybiusSquare{
}
}
logger.debug("Formatted output '{}'", fullOutput);
outputString = fullOutput.toString();
logger.debug("Formatted output '{}'", outputString);
}
//Encodes inputString using the Playfair cipher and stores the result in outputString
private String encode() throws InvalidInputException{
protected void encode() throws InvalidInputException{
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
String cleanString = getPreparedInputStringEncoding();
String cleanString = getPreparedInputStringEncode();
for(int cnt = 0;cnt < cleanString.length();++cnt){
//Get the next character to be encoded
char ch = cleanString.charAt(cnt);
logger.debug("Working character {}", ch);
logger.debug("Current working character {}", ch);
//Find the letter in the grid
CharLocation location = findChar(ch);
@@ -299,16 +335,13 @@ public class PolybiusSquare{
//Add other characters to the output string
addCharactersToCleanStringEncode(output.toString());
//Return the output string
return outputString;
}
//Decodes inputString using the Playfair cipher and stores the result in outputString
private String decode(){
protected void decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
String cleanString = getPreparedInputStringDecoding();
String cleanString = getPreparedInputStringDecode();
for(int cnt = 0;cnt < cleanString.length();){
//Get the digits indicationg the location of the next character
char firstDigit = cleanString.charAt(cnt++);
@@ -327,9 +360,6 @@ public class PolybiusSquare{
//Add other characters to the output
addCharactersToCleanStringDecode(output.toString());
//Return the output string
return outputString;
}
public PolybiusSquare() throws InvalidCharacterException{
@@ -360,8 +390,9 @@ public class PolybiusSquare{
public String encode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setKeyword(keyword);
setInputStringEncoding(inputString);
return encode();
setInputStringEncode(inputString);
encode();
return outputString;
}
//Sets the keyword and inputString and decodes the message
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
@@ -370,8 +401,9 @@ public class PolybiusSquare{
public String decode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setKeyword(keyword);
setInputStringDecoding(inputString);
return decode();
setInputStringDecode(inputString);
decode();
return outputString;
}
//Makes sure all variables are empty
@@ -387,31 +419,9 @@ public class PolybiusSquare{
public char getReplaced(){
return replaced;
}
public void setReplaced(char replaced) throws InvalidCharacterException{
if(!Character.isAlphabetic(replaced)){
throw new InvalidCharacterException("The replaced character must be a letter");
}
if(replaced == replacer){
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
}
this.replaced = Character.toUpperCase(replaced);
}
public char getReplacer(){
return replacer;
}
public void setReplacer(char replacer) throws InvalidCharacterException{
if(!Character.isAlphabetic(replacer)){
throw new InvalidCharacterException("The replacer character must be a letter");
}
if(replaced == replacer){
throw new InvalidCharacterException("The replacer letter cannot be the same as the replaced letter");
}
this.replacer = replacer;
}
public String getKeyword(){
return keyword;
}