diff --git a/src/main/java/com/topsy/jmxproxy/core/Attribute.java b/src/main/java/com/topsy/jmxproxy/core/Attribute.java index 83f283c..9d53168 100644 --- a/src/main/java/com/topsy/jmxproxy/core/Attribute.java +++ b/src/main/java/com/topsy/jmxproxy/core/Attribute.java @@ -33,6 +33,10 @@ public Attribute(Object attributeValue) { this.attributeValue = attributeValue; } + public Object getAttributeValue() { + return attributeValue; + } + public void serialize(JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException { buildJson(jgen, attributeValue); } diff --git a/src/test/java/com/topsy/jmxproxy/core/tests/HistoryTest.java b/src/test/java/com/topsy/jmxproxy/core/tests/HistoryTest.java new file mode 100644 index 0000000..244b237 --- /dev/null +++ b/src/test/java/com/topsy/jmxproxy/core/tests/HistoryTest.java @@ -0,0 +1,243 @@ +package com.topsy.jmxproxy.core.tests; + +import com.topsy.jmxproxy.core.Attribute; +import com.topsy.jmxproxy.core.History; + +import java.util.ArrayList; + +import org.junit.Test; + +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 { + 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()]); + } + + @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 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 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 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 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 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); + } +}