diff --git a/pom.xml b/pom.xml index 537fcc931e..9e6b07955d 100755 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,7 @@ runnel management client-message-tracker + state-format diff --git a/state-format/pom.xml b/state-format/pom.xml new file mode 100755 index 0000000000..cf19860b65 --- /dev/null +++ b/state-format/pom.xml @@ -0,0 +1,77 @@ + + + + + 4.0.0 + + + org.terracotta.internal + build-parent + 5.3-SNAPSHOT + ../build-parent + + + state-format + jar + state-format + + + 1.8 + + + + + org.terracotta + entity-common-api + ${terracotta-apis.version} + + + com.fasterxml.jackson.core + jackson-databind + 2.8.5 + + + junit + junit + 4.12 + test + + + org.hamcrest + hamcrest-core + 1.3 + test + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19 + + 2C + false + ${project.build.directory}/test-fork-${surefire.forkNumber} + -Xmx2G -Djava.awt.headless=true + + + + + diff --git a/state-format/src/main/java/com/tc/state/ObjectStateReporter.java b/state-format/src/main/java/com/tc/state/ObjectStateReporter.java new file mode 100644 index 0000000000..80010ce568 --- /dev/null +++ b/state-format/src/main/java/com/tc/state/ObjectStateReporter.java @@ -0,0 +1,48 @@ +/* + * Copyright Terracotta, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.tc.state; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import java.io.IOException; +import org.terracotta.entity.StateDumpCollector; + +/** + * + */ +public class ObjectStateReporter { + private final StateDumpCollector collector; + private final ObjectMapper mapper; + private final ObjectNode object; + + public ObjectStateReporter(StateDumpCollector collector) { + this.collector = collector; + this.mapper = new ObjectMapper(); + this.object = mapper.createObjectNode(); + } + + public ObjectNode getBaseObject() { + return object; + } + + public void finish() { + try { + collector.addState(StateDumpCollector.JSON_STATE_KEY, mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this.object)); + } catch (IOException ioe) { + collector.addState("error", ioe.getMessage()); + } + } +} diff --git a/state-format/src/test/java/com/tc/state/ObjectStateReporterTest.java b/state-format/src/test/java/com/tc/state/ObjectStateReporterTest.java new file mode 100644 index 0000000000..399b780be9 --- /dev/null +++ b/state-format/src/test/java/com/tc/state/ObjectStateReporterTest.java @@ -0,0 +1,77 @@ +/* + * Copyright Terracotta, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.tc.state; + +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.terracotta.entity.StateDumpCollector; +/** + * + */ +public class ObjectStateReporterTest { + + public ObjectStateReporterTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + @Test + public void testCheckFormatting() throws Throwable { + ObjectStateReporter reporter = new ObjectStateReporter(new StateDumpCollector() { + @Override + public StateDumpCollector subStateDumpCollector(String string) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void addState(String string, String string1) { + Assert.assertEquals(StateDumpCollector.JSON_STATE_KEY, string); + System.out.println(string1); + } + }); + + ObjectNode node = reporter.getBaseObject(); + node.put("test", 1); + node.put("testString", "check"); + ObjectNode sub = node.with("subNode"); + sub.put("subIsHere", true); + ArrayNode array = sub.withArray("array"); + for (int x=0;x<5;x++) { + array.add(x); + } + reporter.finish(); + } +}