Commit 8701cfe5 authored by Marvin Plettner's avatar Marvin Plettner
Browse files

Added command line parameter code

parent c32a8bb8
package com.sodapopsoftware.mq.util;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.sodapopsoftware.mq.util.ArgumentUtil;
class ArgumentrUtilTest {
@Test
@DisplayName("Verify that default values are returned when no arguments supplied")
public void checkDefaultValues() {
final String[] ARGUMENTS = { "programName" };
final ArgumentUtil argumentUtil = new ArgumentUtil(ARGUMENTS);
Assertions.assertEquals(argumentUtil.getWarehouseNumber(), ArgumentUtil.DEFAULT_WAREHOUSE_NUMBER);
Assertions.assertEquals(argumentUtil.getHorizontalOffset(), ArgumentUtil.DEFAULT_OFFSET_HORIZONTAL);
Assertions.assertEquals(argumentUtil.getVerticalOffset(), ArgumentUtil.DEFAULT_OFFSET_VERTICAL);
}
@Test
@DisplayName("Verify that command line values are returned when provided")
public void checkSuppliedValues() {
final int offsetHoriz = 10;
final int offsetVert = 100;
final int warehouseNumber = 50;
final String[] ARGUMENTS = { "programName", "-x", "" + offsetHoriz, "-y", "" + offsetVert, "-w",
"" + warehouseNumber };
final ArgumentUtil argumentUtil = new ArgumentUtil(ARGUMENTS);
Assertions.assertEquals(warehouseNumber, argumentUtil.getWarehouseNumber());
Assertions.assertEquals(offsetHoriz, argumentUtil.getHorizontalOffset());
Assertions.assertEquals(offsetVert, argumentUtil.getVerticalOffset());
}
}
......@@ -19,7 +19,7 @@ look at (and fun to code.)
### Building This Project
You'll need the following things to be able to build this project:
I wrote and built this code using the following:
* An internet connection
* Java 15
......@@ -28,8 +28,6 @@ You'll need the following things to be able to build this project:
Earlier versions of the software might work fine.
RabbitMQ should be running during build for tests to run.
To build the project, first clone, cd into the project directory, and run Maven:
mvn clean package
......@@ -63,8 +61,23 @@ Assuming you have Docker installed and that you're on Linux, you can start
RabbitMQ with default settings as follows:
sudo docker run -d --hostname my-rabbit --rm -p 5672:5672 --name managed_rabbit rabbitmq:3-management
I've found this to be the easiest way to get RabbitMQ installed and running.
## Usage
The way I envisaged this project being used was to start the Publisher and then
start some number of Warehouses. Hit the "send one" button on the publisher
to send a single order to one of the warehouses, or hit the green "automate"
button to start sending orders every 500 milliseconds or so. Use the up and
down arrows to adjust the speed. You can see the buttons I'm talking about
in the screenshots below.
What you should see when you have these things running is the publisher logging
the orders it's sending, and one of the warehouses receiving and "processing"
the order.
## Screenshots
![SwingPublisher](images/swingpublisher-screenshot.png)
......
package com.sodapopsoftware.mq.util;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.sodapopsoftware.mq.model.BookTag;
import com.sodapopsoftware.mq.util.RandomUtils;
class RandomUtilsTest {
private static final int MAX_ITERATIONS = 1000;
private final RandomUtils randomUtils = new RandomUtils();
@Test
@DisplayName("Create a number from zero to max")
public void createFromZeroTo() {
final int expectedMax = 9;
final int frequency[] = new int[expectedMax + 1];
for (int index = 0; index < MAX_ITERATIONS; index++) {
final int actual = this.randomUtils.createFromZeroTo(expectedMax);
frequency[actual]++;
assertTrue(actual <= expectedMax);
}
/*
* for (int index = 0; index < frequencies.length; index++) { System.out.println("" + index + " : "
* + frequencies[index]); }
*/
}
@Test
@DisplayName("Create a number from min to max")
public void createFromMinToMax() {
final int expectedMin = 100;
final int expectedMax = 199;
for (int index = 0; index < MAX_ITERATIONS; index++) {
final int actual = this.randomUtils.createBetween(expectedMin, expectedMax);
assertTrue(actual >= expectedMin);
assertTrue(actual <= expectedMax);
// System.out.println(actual);
}
}
@Test
@DisplayName("Create a random price")
public void createPrice() {
for (int index = 0; index < MAX_ITERATIONS; index++) {
final float actual = this.randomUtils.createPrice(10, 19);
assertTrue(actual >= 10.0f && actual < 20.00f, "Value should be 10.00 <= x < 20");
// System.out.println(actual);
}
}
@Test
@DisplayName("Create a lower case letter")
public void createLowerCaseLetter() {
final char actual = this.randomUtils.createLowerCaseLetter();
assertTrue(Character.isLowerCase(actual), "Should be lower case");
}
@Test
@DisplayName("Create an upper case letter")
public void createUpperCaseLetter() {
final char actual = this.randomUtils.createUpperCaseLetter();
assertTrue(Character.isUpperCase(actual));
}
@Test
@DisplayName("Create a random capitalized word")
public void createCapitalizedWord() {
final int expectedMaxLength = 10;
for (int index = 0; index < MAX_ITERATIONS; index++) {
final String actual = this.randomUtils.createCapitalizedWord(expectedMaxLength);
assertNotNull(actual);
assertTrue(actual.length() <= expectedMaxLength,
"Should be fewer than " + expectedMaxLength + " characters");
assertTrue(actual.length() > 0, "Should be nonzero length");
assertTrue(Character.isUpperCase(actual.charAt(0)), "First letter should be upper case");
}
}
@Test
@DisplayName("Create a random lowercase word")
public void createLowercaseWord() {
final int expectedMaxLength = 10;
for (int index = 0; index < MAX_ITERATIONS; index++) {
final String actual = this.randomUtils.createWord(expectedMaxLength);
assertNotNull(actual);
assertTrue(actual.length() <= expectedMaxLength,
"Should be fewer than " + expectedMaxLength + " characters");
assertTrue(actual.length() > 0, "Should be nonzero length");
assertTrue(Character.isLowerCase(actual.charAt(0)), "First letter should be lower case");
}
}
@Test
@DisplayName("Create a random product title")
public void createTitle() {
for (int index = 0; index < MAX_ITERATIONS; index++) {
final String actual = this.randomUtils.createTitle();
assertNotNull(actual);
assertTrue(actual.length() > 0);
// System.out.println(actual);
}
}
@Test
@DisplayName("Create an author name")
public void createAuthorName() {
for (int index = 0; index < MAX_ITERATIONS; index++) {
final String actual = this.randomUtils.createAuthorName();
assertNotNull(actual);
assertTrue(actual.length() > 0);
// System.out.println(actual);
}
}
@Test
@DisplayName("Create book tag")
public void createBookTag() {
for (int index = 0; index < MAX_ITERATIONS; index++) {
final BookTag actual = this.randomUtils.createBookTag();
assertNotNull(actual);
}
}
@Test
@DisplayName("Create book tag list")
public void createBookTagList() {
for (int index = 0; index < MAX_ITERATIONS; index++) {
final List<BookTag> actual = this.randomUtils.createBookTagList();
assertNotNull(actual);
assertTrue(actual.size() >= 1);
}
}
}
......@@ -32,7 +32,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.1</version>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
......@@ -68,8 +68,14 @@
<artifactId>jfreechart</artifactId>
<version>1.5.3</version>
</dependency>
</dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
......
package com.sodapopsoftware.mq.util;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import com.sodapopsoftware.mq.AppException;
public class ArgumentUtil {
public static final String ARG_WAREHOUSE = "w";
public static final String ARG_OFFSET_HORIZONTAL = "x";
public static final String ARG_OFFSET_VERTICAL = "y";
public static final int DEFAULT_OFFSET_HORIZONTAL = 100;
public static final int DEFAULT_OFFSET_VERTICAL = 100;
public static final int DEFAULT_WAREHOUSE_NUMBER = 0;
private final Options options = new Options();
private CommandLineParser commandLineParser;
private CommandLine commandLine;
public ArgumentUtil(String[] arguments) {
init(arguments);
}
private void init(String[] arguments) {
this.options.addOption(ARG_WAREHOUSE, "warehouse", true, "Warehouse Number (default=0");
this.options.addOption(ARG_OFFSET_HORIZONTAL, "horizontalOffset", true,
"Horizontal screen location offset (default=0)");
this.options.addOption(ARG_OFFSET_VERTICAL, "verticalOffset", true,
"Vertical screen location offset (default=0)");
this.commandLineParser = new DefaultParser();
try {
this.commandLine = this.commandLineParser.parse(this.options, arguments);
} catch (final ParseException exception) {
throw new AppException("Problem parsing command line options", exception);
}
}
public int getHorizontalOffset() {
return convertToInt(this.commandLine.getOptionValue(ARG_OFFSET_HORIZONTAL), DEFAULT_OFFSET_HORIZONTAL);
}
public int getVerticalOffset() {
return convertToInt(this.commandLine.getOptionValue(ARG_OFFSET_VERTICAL), DEFAULT_OFFSET_VERTICAL);
}
public int getWarehouseNumber() {
return convertToInt(this.commandLine.getOptionValue(ARG_WAREHOUSE), DEFAULT_WAREHOUSE_NUMBER);
}
private int convertToInt(String s, int defaultValue) {
if (s == null || s.strip().length() == 0) {
return defaultValue;
}
try {
return Integer.parseInt(s.strip());
} catch (final NumberFormatException exception) {
throw new AppException("Command line argument was expected to be an integer: " + s, exception);
}
}
}
......@@ -5,7 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class RandomProductFactoryTest {
class ProductFactoryTest {
private final ProductFactory productFactory = new ProductFactory();
......@@ -15,7 +15,7 @@ class RandomProductFactoryTest {
for (int index = 0; index < 1000; index++) {
final Product product = this.productFactory.create();
assertNotNull(product);
System.out.println(product);
// System.out.println(product);
}
}
}
package com.sodapopsoftware.mq.util;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.sodapopsoftware.mq.AppException;
class ArgumentrUtilTest {
@Test
@DisplayName("Verify that default values are returned when no arguments supplied")
public void checkDefaultValues() {
final String[] ARGUMENTS = { "programName" };
final ArgumentUtil argumentUtil = new ArgumentUtil(ARGUMENTS);
Assertions.assertEquals(argumentUtil.getWarehouseNumber(), ArgumentUtil.DEFAULT_WAREHOUSE_NUMBER);
Assertions.assertEquals(argumentUtil.getHorizontalOffset(), ArgumentUtil.DEFAULT_OFFSET_HORIZONTAL);
Assertions.assertEquals(argumentUtil.getVerticalOffset(), ArgumentUtil.DEFAULT_OFFSET_VERTICAL);
}
@Test
@DisplayName("Verify that command line values are returned when provided")
public void checkSuppliedValues() {
final int offsetHoriz = 10;
final int offsetVert = 100;
final int warehouseNumber = 50;
final String[] ARGUMENTS = { "programName", "-x", "" + offsetHoriz, "-y", "" + offsetVert, "-w",
"" + warehouseNumber };
final ArgumentUtil argumentUtil = new ArgumentUtil(ARGUMENTS);
Assertions.assertEquals(warehouseNumber, argumentUtil.getWarehouseNumber());
Assertions.assertEquals(offsetHoriz, argumentUtil.getHorizontalOffset());
Assertions.assertEquals(offsetVert, argumentUtil.getVerticalOffset());
}
@Test
@DisplayName("Prove that method throws an exception on bad arguments")
public void invalidCommandLineArgument() {
final int offsetHoriz = 10;
final int offsetVert = 100;
final int warehouseNumber = 50;
final String[] ARGUMENTS = { "programName", "-x", "" + offsetHoriz, "-y", "" + offsetVert, "-o", "-w",
"" + warehouseNumber };
Assertions.assertThrows(AppException.class, () -> new ArgumentUtil(ARGUMENTS),
"Expected an exception because too many arguments present");
}
}
......@@ -9,6 +9,7 @@ import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.sodapopsoftware.mq.model.BookTag;
import com.sodapopsoftware.mq.util.RandomUtils;
class RandomUtilsTest {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment