From 50e3c0f76daf73a7e8ddf5123c244fd5807f7691 Mon Sep 17 00:00:00 2001 From: Max Kalika Date: Fri, 15 Apr 2016 10:56:25 -0700 Subject: [PATCH] Issue #109: Refact History object into a generic. --- .../github/mk23/jmxproxy/core/History.java | 90 ----- .../com/github/mk23/jmxproxy/core/Host.java | 9 +- .../com/github/mk23/jmxproxy/core/MBean.java | 53 +-- .../mk23/jmxproxy/jmx/ConnectionWorker.java | 11 +- .../github/mk23/jmxproxy/util/History.java | 103 ++++++ .../mk23/jmxproxy/util/package-info.java | 4 + .../mk23/jmxproxy/core/tests/HistoryTest.java | 296 --------------- .../mk23/jmxproxy/util/tests/HistoryTest.java | 349 ++++++++++++++++++ 8 files changed, 495 insertions(+), 420 deletions(-) delete mode 100644 src/main/java/com/github/mk23/jmxproxy/core/History.java create mode 100644 src/main/java/com/github/mk23/jmxproxy/util/History.java create mode 100644 src/main/java/com/github/mk23/jmxproxy/util/package-info.java delete mode 100644 src/test/java/com/github/mk23/jmxproxy/core/tests/HistoryTest.java create mode 100644 src/test/java/com/github/mk23/jmxproxy/util/tests/HistoryTest.java diff --git a/src/main/java/com/github/mk23/jmxproxy/core/History.java b/src/main/java/com/github/mk23/jmxproxy/core/History.java deleted file mode 100644 index 487ebe4..0000000 --- a/src/main/java/com/github/mk23/jmxproxy/core/History.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.github.mk23.jmxproxy.core; - -/** - *

Fixed size historical attribute value store.

- * - * Saves a JMX {@link Attribute} value object into a fixed size round robin store. - * Allows clients to retreive latest value, a subset of latest values, or the - * full stored history of values. When number of stored values exceeds the fixed - * size of the store, the oldest values are overwritten. - * - * @since 2015-05-11 - * @author mk23 - * @version 3.2.0 - */ -public class History { - private Attribute[] attributes; - private int current; - - /** - *

Default constructor.

- * - * Initializes the {@link Attribute} store array to the given history size. - * - * @param size number of {@link Attribute} values to keep in history. - */ - public History(final int size) { - attributes = new Attribute[size]; - } - - /** - *

Adds a JMX Attribute value to the store.

- * - * Adds a new JMX {@link Attribute} value to the history store. If the - * number of values exceeds the size of the store, the oldest value is - * overwritten. - * - * @param attributeValue Object of the newest value to add to the store. - */ - public final void addAttributeValue(final Object attributeValue) { - Attribute attribute = new Attribute(attributeValue); - attributes[++current % attributes.length] = attribute; - if (current == Integer.MAX_VALUE) { - current = current % attributes.length + attributes.length; - } - } - - /** - *

Gets the latest attribute value from the history store.

- * - * Gets the latest single {@link Attribute} value from the history store. - * - * @return Latest value from the history store or null if empty. - */ - public final Attribute getAttribute() { - return attributes[current % attributes.length]; - } - - /** - *

Gets the full attribute value history from the store.

- * - * Gets the full {@link Attribute} value history from the store as an array. - * - * @return Array of the full value history. May be empty if history has no items. - */ - public final Attribute[] getAttributes() { - return getAttributes(attributes.length); - } - - /** - *

Gets a limited attribute value history from the store.

- * - * Gets a limited {@link Attribute} value history from the store as an array, based - * on requested item count. If the specified count is 0 or greater than the number - * of entries, a full history is returned instead. - * - * @param limit number of values to retreive from the history store. - * - * @return Array of the requested value history. May be empty if history has no items. - */ - public final Attribute[] getAttributes(final int limit) { - int size = Math.min(limit != 0 ? limit : Integer.MAX_VALUE, Math.min(current, attributes.length)); - Attribute[] rval = new Attribute[size]; - - for (int i = 0; i < size; i++) { - rval[i] = attributes[(current - i) % attributes.length]; - } - - return rval; - } -} diff --git a/src/main/java/com/github/mk23/jmxproxy/core/Host.java b/src/main/java/com/github/mk23/jmxproxy/core/Host.java index cd97d72..06e358c 100644 --- a/src/main/java/com/github/mk23/jmxproxy/core/Host.java +++ b/src/main/java/com/github/mk23/jmxproxy/core/Host.java @@ -74,9 +74,9 @@ public final void removeMBean(final String mbeanName) { /** *

Sets the thread local history request limit.

* - * Set the thread local limit for all {@link History} when serializing to JSON. - * Because this method returns its object, requesting serialization can be done - * with a single statement. + * Set the thread local limit for all {@link com.github.mk23.jmxproxy.util.History} when + * serializing to JSON. Because this method returns its object, requesting serialization + * can be done * with a single statement. * *

For example:

* @@ -84,7 +84,8 @@ public final void removeMBean(final String mbeanName) { * * @see javax.ws.rs.core.Response * - * @param bound the number of items to retreive from {@link History} for this thread. + * @param bound the number of items to retreive from {@link com.github.mk23.jmxproxy.util.History} + * for this thread. * * @return this host object for chaining calls. */ diff --git a/src/main/java/com/github/mk23/jmxproxy/core/MBean.java b/src/main/java/com/github/mk23/jmxproxy/core/MBean.java index 997019d..8ad4029 100644 --- a/src/main/java/com/github/mk23/jmxproxy/core/MBean.java +++ b/src/main/java/com/github/mk23/jmxproxy/core/MBean.java @@ -1,5 +1,7 @@ package com.github.mk23.jmxproxy.core; +import com.github.mk23.jmxproxy.util.History; + import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonSerializable; @@ -8,7 +10,9 @@ import java.io.IOException; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; @@ -26,7 +30,7 @@ * @version 3.2.0 */ public class MBean implements JsonSerializable { - private final Map attributes; + private final Map> attributes; private final ThreadLocal limit = new ThreadLocal() { @Override protected Integer initialValue() { @@ -40,27 +44,30 @@ protected Integer initialValue() { * Creates a map of {@link Attribute} name to {@link History} of associated values. */ public MBean() { - attributes = new HashMap(); + attributes = new HashMap>(); } /** *

Inserts a new Attribute name to History association.

* - * Creates a new {@link Attribute} value {@link History} object and inserts into - * the map store associating it to the specified attribute name. + * Optionally creates a new {@link History} object and inserts into the map store associating + * it to the specified attribute name. Appends the specified {@link Attribute} value to the + * {@link History}. * * @param attributeName name of the {@link Attribute} used as the map key. - * @param size number of items to preserve in the associated {@link History}. - * - * @return the newly created empty {@link History} object. + * @param attributeObject value of the {@link Attribute} added to history. + * @param historySize number of items to preserve in the associated {@link History}. */ - public final History addHistory(final String attributeName, final int size) { + public final void addAttribute( + final String attributeName, + final Object attributeObject, + final int historySize + ) { if (!attributes.containsKey(attributeName)) { - History history = new History(size); - attributes.put(attributeName, history); + attributes.put(attributeName, new History(historySize)); } - return attributes.get(attributeName); + attributes.get(attributeName).add(new Attribute(attributeObject)); } /** @@ -120,12 +127,11 @@ public final Set getAttributes() { * @return latest {@link Attribute} object from {@link History} if found, null otherwise. */ public final Attribute getAttribute(final String attribute) { - History history = attributes.get(attribute); - if (history == null) { - return null; + if (hasAttribute(attribute)) { + return attributes.get(attribute).getLast(); } - return history.getAttribute(); + return null; } /** @@ -137,15 +143,14 @@ public final Attribute getAttribute(final String attribute) { * @param attribute name of the {@link Attribute} to look up in the map store. * @param bound size of the resulting array or full history if this exceeds capacity or less than 1. * - * @return array of the latest {@link Attribute} objects from {@link History} if found, empty otherwise. + * @return {@link List} of the latest {@link Attribute} objects from {@link History} if found, empty otherwise. */ - public final Attribute[] getAttributes(final String attribute, final int bound) { - History history = attributes.get(attribute); - if (history == null) { - return new Attribute[0]; + public final List getAttributes(final String attribute, final int bound) { + if (hasAttribute(attribute)) { + return attributes.get(attribute).get(bound); } - return history.getAttributes(bound); + return Collections.emptyList(); } /** {@inheritDoc} */ @@ -171,11 +176,11 @@ private void buildJson(final JsonGenerator jgen) throws IOException, JsonProcess int bound = limit.get(); jgen.writeStartObject(); - for (Map.Entry attributeEntry : attributes.entrySet()) { + for (Map.Entry> attributeEntry : attributes.entrySet()) { if (bound < 0) { - jgen.writeObjectField(attributeEntry.getKey(), attributeEntry.getValue().getAttribute()); + jgen.writeObjectField(attributeEntry.getKey(), attributeEntry.getValue().getLast()); } else { - jgen.writeObjectField(attributeEntry.getKey(), attributeEntry.getValue().getAttributes(bound)); + jgen.writeObjectField(attributeEntry.getKey(), attributeEntry.getValue().get(bound)); } } jgen.writeEndObject(); diff --git a/src/main/java/com/github/mk23/jmxproxy/jmx/ConnectionWorker.java b/src/main/java/com/github/mk23/jmxproxy/jmx/ConnectionWorker.java index a34c838..899bb60 100644 --- a/src/main/java/com/github/mk23/jmxproxy/jmx/ConnectionWorker.java +++ b/src/main/java/com/github/mk23/jmxproxy/jmx/ConnectionWorker.java @@ -1,6 +1,5 @@ package com.github.mk23.jmxproxy.jmx; -import com.github.mk23.jmxproxy.core.History; import com.github.mk23.jmxproxy.core.Host; import com.github.mk23.jmxproxy.core.MBean; @@ -36,7 +35,8 @@ * to a JMX endpoint. At every execution, discovers and fetches all remote registered mbeans * and populates the local {@link Host} object with assicated {@link MBean}s and their * {@link com.github.mk23.jmxproxy.core.Attribute}. Also maintains the {@link Host} access - * time to allow reaping of unaccessed workers. + * time to allow reaping of + * unaccessed workers. * * @since 2015-05-11 * @author mk23 @@ -70,8 +70,8 @@ public class ConnectionWorker { * * @param hostName host:port {@link String} JMX agent target. * @param cacheDuration period in milliseconds for how often to connect to the JMX agent. - * @param historySize number of {@link com.github.mk23.jmxproxy.core.Attribute}s to keep per - * {@link MBean} {@link History}. + * @param historySize number of {@link com.github.mk23.jmxproxy.core.Attribute}s to keep + * per {@link MBean} {@link com.github.mk23.jmxproxy.util.History}. * * @throws MalformedURLException if the specified host is not a valid host:port {@link String}. */ @@ -197,8 +197,7 @@ private void fetchJMXValues() { try { Object attribute = server.getAttribute(mbeanName, attributeObject.getName()); - History history = mbean.addHistory(attributeObject.getName(), historySize); - history.addAttributeValue(attribute); + mbean.addAttribute(attributeObject.getName(), attribute, historySize); } catch (java.lang.NullPointerException e) { LOG.error("failed to add attribute " + attributeObject.toString() + ": " + e); } catch (java.rmi.UnmarshalException e) { diff --git a/src/main/java/com/github/mk23/jmxproxy/util/History.java b/src/main/java/com/github/mk23/jmxproxy/util/History.java new file mode 100644 index 0000000..ccb5da4 --- /dev/null +++ b/src/main/java/com/github/mk23/jmxproxy/util/History.java @@ -0,0 +1,103 @@ +package com.github.mk23.jmxproxy.util; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + *

Fixed size historical attribute value store.

+ * + * Saves an object into a fixed size round robin store. Allows clients to retreive latest + * value, a subset of latest values, or the full stored history of values. When number + * of stored values exceeds the fixed size of the store, the oldest values are overwritten. + * + * @param type of objects held in this History. + * + * @since 2015-05-11 + * @author mk23 + * @version 3.3.2 + */ +public class History { + private List objects; + private int length; + private int pointer; + private boolean wrapped; + + /** + *

Default constructor.

+ * + * Initializes the {@link Object} store {@link List} to the given history size. + * + * @param length number of {@link Object} values to keep in history. + */ + public History(final int length) { + this.length = length; + + pointer = -1; + objects = new ArrayList(Collections.nCopies(length, (T) null)); + } + + /** + *

Adds a item to the store.

+ * + * Adds a new item to the history store. If the number of items exceeds the size + * of the store, the oldest item is overwritten. + * + * @param item newest item to add to the store. + */ + public final void add(final T item) { + wrapped = ++pointer == length; + pointer = pointer % length; + + objects.set(pointer, item); + } + + /** + *

Gets the latest item from the history store.

+ * + * Gets the latest single item from the history store. + * + * @return Latest item from the history store or null if empty. + */ + public final T getLast() { + return pointer < 0 ? null : objects.get(pointer % length); + } + + /** + *

Gets the full item history from the store.

+ * + * Gets the full item history from the store as a {@link List}. + * + * @return {@link List} of the full history. May be empty if history has no items. + */ + public final List get() { + return get(length); + } + + /** + *

Gets a limited item history from the store.

+ * + * Gets a limited history from the store as a {@link List}, based + * on requested item count. If the specified count is less than 0 or greater than + * the number of entries, a full history is returned instead. + * + * @param limit number of items to retreive from the history store. + * + * @return {@link List} of the requested slice of history. May be empty if history has no items. + */ + public final List get(final int limit) { + if (pointer < 0) { + return Collections.emptyList(); + } + + int head = pointer + length * Boolean.compare(wrapped, false); + int size = Math.min(limit > 0 ? limit : Integer.MAX_VALUE, Math.min((head + 1), length)); + List rval = new ArrayList(size); + + for (int i = 0; i < size; i++) { + rval.add(objects.get((head - i) % length)); + } + + return rval; + } +} diff --git a/src/main/java/com/github/mk23/jmxproxy/util/package-info.java b/src/main/java/com/github/mk23/jmxproxy/util/package-info.java new file mode 100644 index 0000000..5d62d20 --- /dev/null +++ b/src/main/java/com/github/mk23/jmxproxy/util/package-info.java @@ -0,0 +1,4 @@ +/** + *

Utility classes for the entire application.

+ */ +package com.github.mk23.jmxproxy.util; diff --git a/src/test/java/com/github/mk23/jmxproxy/core/tests/HistoryTest.java b/src/test/java/com/github/mk23/jmxproxy/core/tests/HistoryTest.java deleted file mode 100644 index bb62bd4..0000000 --- a/src/test/java/com/github/mk23/jmxproxy/core/tests/HistoryTest.java +++ /dev/null @@ -1,296 +0,0 @@ -package com.github.mk23.jmxproxy.core.tests; - -import com.github.mk23.jmxproxy.core.Attribute; -import com.github.mk23.jmxproxy.core.History; - -import java.util.ArrayList; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -public class HistoryTest { - @Rule public TestName name = new TestName(); - - private Object[] getAttributeValues(Attribute[] attributes) { - ArrayList al = new ArrayList(attributes.length); - for (Attribute a : attributes) { - al.add(a.getAttributeValue()); - } - return al.toArray(new Object[al.size()]); - } - - @Before - public void printTestName() { - System.out.println(" -> " + name.getMethodName()); - } - - @Test - public void check0Single() throws Exception { - History history = new History(3); - assertNull(history.getAttribute()); - } - @Test - public void check0ArrayFull() throws Exception { - History history = new History(3); - assertTrue(history.getAttributes().length == 0); - } - @Test - public void check0ArrayZero() throws Exception { - History history = new History(3); - assertTrue(history.getAttributes(0).length == 0); - } - @Test - public void check0ArrayTen() throws Exception { - History history = new History(3); - assertTrue(history.getAttributes(10).length == 0); - } - @Test - public void check0ArrayTwo() throws Exception { - History history = new History(3); - assertTrue(history.getAttributes(2).length == 0); - } - @Test - public void check0ArrayOne() throws Exception { - History history = new History(3); - assertTrue(history.getAttributes(1).length == 0); - } - - @Test - public void check1Single() throws Exception { - History history = new History(3); - String target = new String("1"); - - history.addAttributeValue(new String("1")); - assertEquals(history.getAttribute().getAttributeValue(), target); - } - @Test - public void check1ArrayFull() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("1") }; - - history.addAttributeValue(new String("1")); - assertArrayEquals(getAttributeValues(history.getAttributes()), target); - } - @Test - public void check1ArrayZero() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("1") }; - - history.addAttributeValue(new String("1")); - assertArrayEquals(getAttributeValues(history.getAttributes(0)), target); - } - @Test - public void check1ArrayTen() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("1") }; - - history.addAttributeValue(new String("1")); - assertArrayEquals(getAttributeValues(history.getAttributes(10)), target); - } - @Test - public void check1ArrayTwo() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("1") }; - - history.addAttributeValue(new String("1")); - assertArrayEquals(getAttributeValues(history.getAttributes(2)), target); - } - @Test - public void check1ArrayOne() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("1") }; - - history.addAttributeValue(new String("1")); - assertArrayEquals(getAttributeValues(history.getAttributes(1)), target); - } - - @Test - public void check2Single() throws Exception { - History history = new History(3); - String target = new String("2"); - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - assertEquals(history.getAttribute().getAttributeValue(), target); - } - @Test - public void check2ArrayFull() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("2"), new String("1") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - assertArrayEquals(getAttributeValues(history.getAttributes()), target); - } - @Test - public void check2ArrayZero() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("2"), new String("1") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - assertArrayEquals(getAttributeValues(history.getAttributes(0)), target); - } - @Test - public void check2ArrayTen() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("2"), new String("1") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - assertArrayEquals(getAttributeValues(history.getAttributes(10)), target); - } - @Test - public void check2ArrayTwo() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("2"), new String("1") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - assertArrayEquals(getAttributeValues(history.getAttributes(2)), target); - } - @Test - public void check2ArrayOne() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("2") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - assertArrayEquals(getAttributeValues(history.getAttributes(1)), target); - } - - @Test - public void check3Single() throws Exception { - History history = new History(3); - String target = new String("3"); - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - assertEquals(history.getAttribute().getAttributeValue(), target); - } - @Test - public void check3ArrayFull() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("3"), new String("2"), new String("1") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - assertArrayEquals(getAttributeValues(history.getAttributes()), target); - } - @Test - public void check3ArrayZero() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("3"), new String("2"), new String("1") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - assertArrayEquals(getAttributeValues(history.getAttributes(0)), target); - } - @Test - public void check3ArrayTen() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("3"), new String("2"), new String("1") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - assertArrayEquals(getAttributeValues(history.getAttributes(10)), target); - } - @Test - public void check3ArrayTwo() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("3"), new String("2") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - assertArrayEquals(getAttributeValues(history.getAttributes(2)), target); - } - @Test - public void check3ArrayOne() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("3") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - assertArrayEquals(getAttributeValues(history.getAttributes(1)), target); - } - - @Test - public void check4Single() throws Exception { - History history = new History(3); - String target = new String("4"); - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - history.addAttributeValue(new String("4")); - assertEquals(history.getAttribute().getAttributeValue(), target); - } - @Test - public void check4ArrayFull() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("4"), new String("3"), new String("2") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - history.addAttributeValue(new String("4")); - assertArrayEquals(getAttributeValues(history.getAttributes()), target); - } - @Test - public void check4ArrayZero() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("4"), new String("3"), new String("2") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - history.addAttributeValue(new String("4")); - assertArrayEquals(getAttributeValues(history.getAttributes(0)), target); - } - @Test - public void check4ArrayTen() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("4"), new String("3"), new String("2") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - history.addAttributeValue(new String("4")); - assertArrayEquals(getAttributeValues(history.getAttributes(10)), target); - } - @Test - public void check4ArrayTwo() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("4"), new String("3") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - history.addAttributeValue(new String("4")); - assertArrayEquals(getAttributeValues(history.getAttributes(2)), target); - } - @Test - public void check4ArrayOne() throws Exception { - History history = new History(3); - String[] target = new String[] { new String("4") }; - - history.addAttributeValue(new String("1")); - history.addAttributeValue(new String("2")); - history.addAttributeValue(new String("3")); - history.addAttributeValue(new String("4")); - assertArrayEquals(getAttributeValues(history.getAttributes(1)), target); - } -} diff --git a/src/test/java/com/github/mk23/jmxproxy/util/tests/HistoryTest.java b/src/test/java/com/github/mk23/jmxproxy/util/tests/HistoryTest.java new file mode 100644 index 0000000..85765e9 --- /dev/null +++ b/src/test/java/com/github/mk23/jmxproxy/util/tests/HistoryTest.java @@ -0,0 +1,349 @@ +package com.github.mk23.jmxproxy.util.tests; + +import com.github.mk23.jmxproxy.util.History; + +import java.util.ArrayList; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class HistoryTest { + @Rule public TestName name = new TestName(); + + @Before + public void printTestName() { + System.out.println(" -> " + name.getMethodName()); + } + + @Test + public void check0Single() throws Exception { + History history = new History(3); + assertNull(history.getLast()); + } + @Test + public void check0ArrayFull() throws Exception { + History history = new History(3); + assertTrue(history.get().size() == 0); + } + @Test + public void check0ArrayZero() throws Exception { + History history = new History(3); + assertTrue(history.get(0).size() == 0); + } + @Test + public void check0ArrayTen() throws Exception { + History history = new History(3); + assertTrue(history.get(10).size() == 0); + } + @Test + public void check0ArrayTwo() throws Exception { + History history = new History(3); + assertTrue(history.get(2).size() == 0); + } + @Test + public void check0ArrayOne() throws Exception { + History history = new History(3); + assertTrue(history.get(1).size() == 0); + } + + @Test + public void check1Single() throws Exception { + History history = new History(3); + String target = new String("1"); + + history.add(new String("1")); + assertEquals(target, history.getLast()); + } + @Test + public void check1ArrayFull() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("1") }; + + history.add(new String("1")); + assertArrayEquals(target, history.get().toArray()); + } + @Test + public void check1ArrayZero() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("1") }; + + history.add(new String("1")); + assertArrayEquals(target, history.get(0).toArray()); + } + @Test + public void check1ArrayTen() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("1") }; + + history.add(new String("1")); + assertArrayEquals(target, history.get(10).toArray()); + } + @Test + public void check1ArrayTwo() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("1") }; + + history.add(new String("1")); + assertArrayEquals(target, history.get(2).toArray()); + } + @Test + public void check1ArrayOne() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("1") }; + + history.add(new String("1")); + assertArrayEquals(target, history.get(1).toArray()); + } + + @Test + public void check2Single() throws Exception { + History history = new History(3); + String target = new String("2"); + + history.add(new String("1")); + history.add(new String("2")); + assertEquals(target, history.getLast()); + } + @Test + public void check2ArrayFull() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("2"), new String("1") }; + + history.add(new String("1")); + history.add(new String("2")); + assertArrayEquals(target, history.get().toArray()); + } + @Test + public void check2ArrayZero() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("2"), new String("1") }; + + history.add(new String("1")); + history.add(new String("2")); + assertArrayEquals(target, history.get(0).toArray()); + } + @Test + public void check2ArrayTen() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("2"), new String("1") }; + + history.add(new String("1")); + history.add(new String("2")); + assertArrayEquals(target, history.get(10).toArray()); + } + @Test + public void check2ArrayTwo() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("2"), new String("1") }; + + history.add(new String("1")); + history.add(new String("2")); + assertArrayEquals(target, history.get(2).toArray()); + } + @Test + public void check2ArrayOne() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("2") }; + + history.add(new String("1")); + history.add(new String("2")); + assertArrayEquals(target, history.get(1).toArray()); + } + + @Test + public void check3Single() throws Exception { + History history = new History(3); + String target = new String("3"); + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + assertEquals(target, history.getLast()); + } + @Test + public void check3ArrayFull() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("3"), new String("2"), new String("1") }; + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + assertArrayEquals(target, history.get().toArray()); + } + @Test + public void check3ArrayZero() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("3"), new String("2"), new String("1") }; + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + assertArrayEquals(target, history.get(0).toArray()); + } + @Test + public void check3ArrayTen() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("3"), new String("2"), new String("1") }; + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + assertArrayEquals(target, history.get(10).toArray()); + } + @Test + public void check3ArrayTwo() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("3"), new String("2") }; + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + assertArrayEquals(target, history.get(2).toArray()); + } + @Test + public void check3ArrayOne() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("3") }; + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + assertArrayEquals(target, history.get(1).toArray()); + } + + @Test + public void check4Single() throws Exception { + History history = new History(3); + String target = new String("4"); + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + history.add(new String("4")); + assertEquals(target, history.getLast()); + } + @Test + public void check4ArrayFull() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("4"), new String("3"), new String("2") }; + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + history.add(new String("4")); + assertArrayEquals(target, history.get().toArray()); + } + @Test + public void check4ArrayZero() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("4"), new String("3"), new String("2") }; + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + history.add(new String("4")); + assertArrayEquals(target, history.get(0).toArray()); + } + @Test + public void check4ArrayTen() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("4"), new String("3"), new String("2") }; + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + history.add(new String("4")); + assertArrayEquals(target, history.get(10).toArray()); + } + @Test + public void check4ArrayTwo() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("4"), new String("3") }; + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + history.add(new String("4")); + assertArrayEquals(target, history.get(2).toArray()); + } + @Test + public void check4ArrayOne() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("4") }; + + history.add(new String("1")); + history.add(new String("2")); + history.add(new String("3")); + history.add(new String("4")); + assertArrayEquals(target, history.get(1).toArray()); + } + + @Test + public void check100Single() throws Exception { + History history = new History(3); + String target = new String("99"); + + for (int i = 0; i < 100; i++) { + history.add(new String(Integer.toString(i))); + } + assertEquals(target, history.getLast()); + } + @Test + public void check100ArrayFull() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("99"), new String("98"), new String("97"), }; + + for (int i = 0; i < 100; i++) { + history.add(new String(Integer.toString(i))); + } + assertArrayEquals(target, history.get().toArray()); + } + @Test + public void check100ArrayZero() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("99"), new String("98"), new String("97"), }; + + for (int i = 0; i < 100; i++) { + history.add(new String(Integer.toString(i))); + } + assertArrayEquals(target, history.get(0).toArray()); + } + @Test + public void check100ArrayTen() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("99"), new String("98"), new String("97"), }; + + for (int i = 0; i < 100; i++) { + history.add(new String(Integer.toString(i))); + } + assertArrayEquals(target, history.get(10).toArray()); + } + @Test + public void check100ArrayTwo() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("99"), new String("98") }; + + for (int i = 0; i < 100; i++) { + history.add(new String(Integer.toString(i))); + } + assertArrayEquals(target, history.get(2).toArray()); + } + @Test + public void check100ArrayOne() throws Exception { + History history = new History(3); + String[] target = new String[] { new String("99") }; + + for (int i = 0; i < 100; i++) { + history.add(new String(Integer.toString(i))); + } + + assertArrayEquals(target, history.get(1).toArray()); + } +}