Update unit test coverage

This commit is contained in:
2023-04-22 10:52:16 -04:00
parent 494293c311
commit 59885b8df6
21 changed files with 2784 additions and 2005 deletions

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Substitution.java
//Mattrixwv
// Created: 02-22-22
//Modified: 07-09-22
//Modified: 04-18-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,18 +13,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Substitution{
private static final Logger logger = LoggerFactory.getLogger(Substitution.class);
protected static Logger logger = LoggerFactory.getLogger(Substitution.class);
//Fields
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
private String key; //The keyword used to encode/decode the input
private boolean preserveCapitals; //Whether to respect capitals in the output string
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
private boolean preserveSymbols; //Whether to respect symbols in the output string
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string
protected String keyword; //The keyword used to encode/decode the input
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
//Ensures key constraints are followed
private void setKey(String key) throws InvalidKeywordException{
protected void setKeyword(String key) throws InvalidKeywordException{
if(key == null){
throw new InvalidKeywordException("Key cannot be null");
}
@@ -37,8 +37,9 @@ public class Substitution{
//Make sure the key contains no duplicate mappings
logger.debug("Ensuring there are no duplicate mappings");
String tempKey = key.replaceAll("(.)\\1{2}", "");
if(!tempKey.equals(key)){
StringBuilder uniqueKey = new StringBuilder();
key.chars().distinct().forEach(c -> uniqueKey.append((char)c));
if(!key.equals(uniqueKey.toString())){
throw new InvalidKeywordException("The key cannot contain duplicate mappings");
}
@@ -47,16 +48,16 @@ public class Substitution{
logger.debug("Ensuring there are only letters in the key");
//Make sure the key contains all valid characters
tempKey = key.replaceAll("[^A-Z]", "");
String tempKey = key.replaceAll("[^A-Z]", "");
if(!tempKey.equals(key)){
throw new InvalidKeywordException("The key must contain all letters");
}
}
else if(key.length() == 36){
logger.debug("Ensure there are only alpha-numeric characters in the key");
logger.debug("Ensuring there are only alpha-numeric characters in the key");
//Make sure the key contains all valid characters
tempKey = key.replaceAll("[^A-Z0-9]", "");
String tempKey = key.replaceAll("[^A-Z0-9]", "");
if(!tempKey.equals(key)){
throw new InvalidKeywordException("The key must contain all letters and can contain all numbers");
}
@@ -67,10 +68,10 @@ public class Substitution{
//Save the key
logger.debug("Cleaned key '{}'", key);
this.key = key;
this.keyword = key;
}
//Ensure intput constraints are followed
private void setInputString(String inputString) throws InvalidInputException{
protected void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
@@ -104,7 +105,7 @@ public class Substitution{
}
}
//Encodes the inputString and stores the result in outputString
private void encode(){
protected void encode(){
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
@@ -115,15 +116,15 @@ public class Substitution{
if(Character.isUpperCase(ch)){
logger.debug("Encoding uppercase");
output.append(Character.toUpperCase(key.charAt(ch - 'A')));
output.append(Character.toUpperCase(keyword.charAt(ch - 'A')));
}
else if(Character.isLowerCase(ch)){
logger.debug("Encoding lowercase");
output.append(Character.toLowerCase(key.charAt(ch - 'a')));
output.append(Character.toLowerCase(keyword.charAt(ch - 'a')));
}
else if(Character.isDigit(ch) && (key.length() == 36)){
else if(Character.isDigit(ch) && (keyword.length() == 36)){
logger.debug("Encoding digit");
output.append(key.charAt('Z' - 'A' + Integer.valueOf(Character.toString(ch)) + 1));
output.append(keyword.charAt('Z' - 'A' + Integer.valueOf(Character.toString(ch)) + 1));
}
else{
logger.debug("Passing symbol through");
@@ -132,11 +133,11 @@ public class Substitution{
}
//Save the output
logger.debug("Encoded message '{}'", output);
this.outputString = output.toString();
logger.debug("Encoded message '{}'", outputString);
}
//Decodes the inputString and stores the result in outputString
private void decode(){
protected void decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
@@ -148,27 +149,27 @@ public class Substitution{
if(Character.isUpperCase(ch)){
logger.debug("Encoding uppercase");
output.append((char)('A' + key.indexOf(Character.toUpperCase(ch))));
output.append((char)('A' + keyword.indexOf(Character.toUpperCase(ch))));
}
else if(Character.isLowerCase(ch)){
logger.debug("Encoding lowercase");
output.append((char)('a' + key.indexOf(Character.toUpperCase(ch))));
output.append((char)('a' + keyword.indexOf(Character.toUpperCase(ch))));
}
else if(Character.isDigit(ch) && (key.length() == 36)){
else if(Character.isDigit(ch) && (keyword.length() == 36)){
logger.debug("Encoding digit");
output.append((char)('0' + (key.indexOf(Character.toUpperCase(ch)) - 26)));
output.append((char)('0' + (keyword.indexOf(Character.toUpperCase(ch)) - 26)));
}
else{
logger.debug("Passing through symbol");
logger.debug("Passing symbol through");
output.append(ch);
}
}
//Save the output
logger.debug("Encoded message '{}'", output);
this.outputString = output.toString();
logger.debug("Decoded message '{}'", outputString);
}
public Substitution(){
@@ -190,16 +191,16 @@ public class Substitution{
return outputString;
}
public String getKeyword(){
return key;
return keyword;
}
public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKey(key);
setKeyword(key);
setInputString(inputString);
encode();
return outputString;
}
public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKey(key);
setKeyword(key);
setInputString(inputString);
decode();
return outputString;
@@ -209,6 +210,6 @@ public class Substitution{
inputString = "";
outputString = "";
key = "";
keyword = "";
}
}