Skip to content

Commit

Permalink
Fix Terracotta-OSS#345 - Add a rich object reporter for state collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Myron Scott committed Jul 6, 2017
1 parent edaa29e commit a5abbf7
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<module>runnel</module>
<module>management</module>
<module>client-message-tracker</module>
<module>state-format</module>
</modules>

<dependencyManagement>
Expand Down
77 changes: 77 additions & 0 deletions state-format/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.terracotta.internal</groupId>
<artifactId>build-parent</artifactId>
<version>5.3-SNAPSHOT</version>
<relativePath>../build-parent</relativePath>
</parent>

<artifactId>state-format</artifactId>
<packaging>jar</packaging>
<name>state-format</name>

<properties>
<java.build.version>1.8</java.build.version>
</properties>

<dependencies>
<dependency>
<groupId>org.terracotta</groupId>
<artifactId>entity-common-api</artifactId>
<version>${terracotta-apis.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<forkCount>2C</forkCount>
<reuseForks>false</reuseForks>
<workingDirectory>${project.build.directory}/test-fork-${surefire.forkNumber}</workingDirectory>
<argLine>-Xmx2G -Djava.awt.headless=true</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
48 changes: 48 additions & 0 deletions state-format/src/main/java/com/tc/state/ObjectStateReporter.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit a5abbf7

Please sign in to comment.