From 122a8e467084e0a8b7a95821fac74fc3eece82cb Mon Sep 17 00:00:00 2001 From: Ingmar Steiner Date: Wed, 20 Jun 2018 17:27:57 +0200 Subject: [PATCH 1/6] break TextGridSerializerTest#testEquals --- src/test/resources/org/m2ci/msp/jtgt/io/tg1_validated.TextGrid | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/org/m2ci/msp/jtgt/io/tg1_validated.TextGrid b/src/test/resources/org/m2ci/msp/jtgt/io/tg1_validated.TextGrid index b9d957d..9beac37 100644 --- a/src/test/resources/org/m2ci/msp/jtgt/io/tg1_validated.TextGrid +++ b/src/test/resources/org/m2ci/msp/jtgt/io/tg1_validated.TextGrid @@ -29,7 +29,7 @@ item []: intervals [1]: xmin = 0.8297278911564626 xmax = 2 - text = "test" + text = "test1" item [3]: class = "TextTier" name = "Tilt" From 11c99c7a3724ab076325bc32eff5324c91aa3a2c Mon Sep 17 00:00:00 2001 From: Ingmar Steiner Date: Wed, 20 Jun 2018 17:40:10 +0200 Subject: [PATCH 2/6] add quick and simple toString helper from commons-lang3 --- build.gradle | 1 + src/main/java/org/m2ci/msp/jtgt/Annotation.java | 6 ++++++ src/main/java/org/m2ci/msp/jtgt/TextGrid.java | 7 +++++++ src/main/java/org/m2ci/msp/jtgt/Tier.java | 7 +++++++ 4 files changed, 21 insertions(+) diff --git a/build.gradle b/build.gradle index 61d82b2..a3036bc 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,7 @@ repositories { } dependencies { + compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7' testCompile group: 'org.testng', name: 'testng', version: '6.8.13' } diff --git a/src/main/java/org/m2ci/msp/jtgt/Annotation.java b/src/main/java/org/m2ci/msp/jtgt/Annotation.java index 107acdb..03098cf 100644 --- a/src/main/java/org/m2ci/msp/jtgt/Annotation.java +++ b/src/main/java/org/m2ci/msp/jtgt/Annotation.java @@ -1,5 +1,7 @@ package org.m2ci.msp.jtgt; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; /** * TextGrid annotation wrapper class @@ -104,6 +106,10 @@ public boolean equals(Object o) { (getEnd() == ((Annotation) o).getEnd()) && getText().equals(((Annotation) o).getText())); } + + public String toString() { + return new ReflectionToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).toString(); + } } diff --git a/src/main/java/org/m2ci/msp/jtgt/TextGrid.java b/src/main/java/org/m2ci/msp/jtgt/TextGrid.java index 169625d..63b0f56 100644 --- a/src/main/java/org/m2ci/msp/jtgt/TextGrid.java +++ b/src/main/java/org/m2ci/msp/jtgt/TextGrid.java @@ -1,5 +1,8 @@ package org.m2ci.msp.jtgt; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + import java.util.ArrayList; /** @@ -243,6 +246,10 @@ public boolean equals(Object o) { (((getFilename() == null) && (((TextGrid) o).getFilename() == null)) || getFilename().equals(((TextGrid) o).getFilename())) && (getTiers().equals(((TextGrid) o).getTiers()))); } + + public String toString() { + return new ReflectionToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).toString(); + } } diff --git a/src/main/java/org/m2ci/msp/jtgt/Tier.java b/src/main/java/org/m2ci/msp/jtgt/Tier.java index 07c964e..58b596d 100644 --- a/src/main/java/org/m2ci/msp/jtgt/Tier.java +++ b/src/main/java/org/m2ci/msp/jtgt/Tier.java @@ -1,5 +1,8 @@ package org.m2ci.msp.jtgt; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + import java.util.ArrayList; /** @@ -183,6 +186,10 @@ public boolean equals(Object o) { getName().equals(((Tier) o).getName()) && getAnnotations().equals(((Tier) o).getAnnotations())); } + + public String toString() { + return new ReflectionToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).toString(); + } } /* Tier.java ends here */ From f5c95cf3f90a24db57104737273d6db1f9df3c40 Mon Sep 17 00:00:00 2001 From: Ingmar Steiner Date: Wed, 20 Jun 2018 17:46:54 +0200 Subject: [PATCH 3/6] use pretty-printed JSON toString representation --- build.gradle | 1 + src/main/java/org/m2ci/msp/jtgt/Annotation.java | 4 +++- src/main/java/org/m2ci/msp/jtgt/TextGrid.java | 4 +++- src/main/java/org/m2ci/msp/jtgt/Tier.java | 4 +++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index a3036bc..7a4d1be 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,7 @@ repositories { dependencies { compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7' + compile group: 'org.json', name: 'json', version: '20180130' testCompile group: 'org.testng', name: 'testng', version: '6.8.13' } diff --git a/src/main/java/org/m2ci/msp/jtgt/Annotation.java b/src/main/java/org/m2ci/msp/jtgt/Annotation.java index 03098cf..527c8e2 100644 --- a/src/main/java/org/m2ci/msp/jtgt/Annotation.java +++ b/src/main/java/org/m2ci/msp/jtgt/Annotation.java @@ -2,6 +2,7 @@ import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; +import org.json.JSONObject; /** * TextGrid annotation wrapper class @@ -108,7 +109,8 @@ public boolean equals(Object o) { } public String toString() { - return new ReflectionToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).toString(); + String jsonStr = new ReflectionToStringBuilder(this, ToStringStyle.JSON_STYLE).toString(); + return new JSONObject(jsonStr).toString(4); } } diff --git a/src/main/java/org/m2ci/msp/jtgt/TextGrid.java b/src/main/java/org/m2ci/msp/jtgt/TextGrid.java index 63b0f56..e9427e4 100644 --- a/src/main/java/org/m2ci/msp/jtgt/TextGrid.java +++ b/src/main/java/org/m2ci/msp/jtgt/TextGrid.java @@ -2,6 +2,7 @@ import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; +import org.json.JSONObject; import java.util.ArrayList; @@ -248,7 +249,8 @@ public boolean equals(Object o) { } public String toString() { - return new ReflectionToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).toString(); + String jsonStr = new ReflectionToStringBuilder(this, ToStringStyle.JSON_STYLE).toString(); + return new JSONObject(jsonStr).toString(4); } } diff --git a/src/main/java/org/m2ci/msp/jtgt/Tier.java b/src/main/java/org/m2ci/msp/jtgt/Tier.java index 58b596d..8b881bd 100644 --- a/src/main/java/org/m2ci/msp/jtgt/Tier.java +++ b/src/main/java/org/m2ci/msp/jtgt/Tier.java @@ -2,6 +2,7 @@ import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; +import org.json.JSONObject; import java.util.ArrayList; @@ -188,7 +189,8 @@ public boolean equals(Object o) { } public String toString() { - return new ReflectionToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).toString(); + String jsonStr = new ReflectionToStringBuilder(this, ToStringStyle.JSON_STYLE).toString(); + return new JSONObject(jsonStr).toString(4); } } From 671f80f4e26cc205a48feda5a0cccd070155fe5a Mon Sep 17 00:00:00 2001 From: Ingmar Steiner Date: Wed, 20 Jun 2018 23:07:42 +0200 Subject: [PATCH 4/6] Revert "break TextGridSerializerTest#testEquals" This reverts commit 122a8e467084e0a8b7a95821fac74fc3eece82cb. --- src/test/resources/org/m2ci/msp/jtgt/io/tg1_validated.TextGrid | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/org/m2ci/msp/jtgt/io/tg1_validated.TextGrid b/src/test/resources/org/m2ci/msp/jtgt/io/tg1_validated.TextGrid index 9beac37..b9d957d 100644 --- a/src/test/resources/org/m2ci/msp/jtgt/io/tg1_validated.TextGrid +++ b/src/test/resources/org/m2ci/msp/jtgt/io/tg1_validated.TextGrid @@ -29,7 +29,7 @@ item []: intervals [1]: xmin = 0.8297278911564626 xmax = 2 - text = "test1" + text = "test" item [3]: class = "TextTier" name = "Tilt" From b795e8c9346c9fca285db3f9372d8316d4d54766 Mon Sep 17 00:00:00 2001 From: Ingmar Steiner Date: Wed, 20 Jun 2018 23:04:47 +0200 Subject: [PATCH 5/6] test TextGrid#toString --- build.gradle | 1 + .../java/org/m2ci/msp/jtgt/TextGridTest.java | 37 +++++++++++++++++++ .../resources/org/m2ci/msp/jtgt/foobar.json | 24 ++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 src/test/java/org/m2ci/msp/jtgt/TextGridTest.java create mode 100644 src/test/resources/org/m2ci/msp/jtgt/foobar.json diff --git a/build.gradle b/build.gradle index 7a4d1be..fca49b7 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,7 @@ dependencies { compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7' compile group: 'org.json', name: 'json', version: '20180130' testCompile group: 'org.testng', name: 'testng', version: '6.8.13' + testCompile group: 'commons-io', name: 'commons-io', version: '2.6' } test { diff --git a/src/test/java/org/m2ci/msp/jtgt/TextGridTest.java b/src/test/java/org/m2ci/msp/jtgt/TextGridTest.java new file mode 100644 index 0000000..3f0a2e1 --- /dev/null +++ b/src/test/java/org/m2ci/msp/jtgt/TextGridTest.java @@ -0,0 +1,37 @@ +package org.m2ci.msp.jtgt; + +import org.apache.commons.io.IOUtils; +import org.json.JSONObject; +import org.m2ci.msp.jtgt.annotation.IntervalAnnotation; +import org.m2ci.msp.jtgt.tier.IntervalTier; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +public class TextGridTest { + + @Test + public void testToString() throws IOException { + // create TextGrid + IntervalAnnotation foo = new IntervalAnnotation(0, 0.5, "foo"); + IntervalAnnotation bar = new IntervalAnnotation(0.5, 1, "bar"); + IntervalTier tier = new IntervalTier("foobar"); + tier.addAnnotation(foo); + tier.addAnnotation(bar); + tier.setStart(foo.getStart()); + tier.setEnd(bar.getEnd()); + TextGrid tg = new TextGrid(); + tg.addTier(tier); + tg.setStart(tier.getStart()); + tg.setEnd(tier.getEnd()); + // compare as JSON + String actualStr = tg.toString(); + String expectedStr = IOUtils.resourceToString("/org/m2ci/msp/jtgt/foobar.json", StandardCharsets.UTF_8); + Map actual = new JSONObject(actualStr).toMap(); + Map expected = new JSONObject(expectedStr).toMap(); + Assert.assertEquals(actual, expected); + } +} diff --git a/src/test/resources/org/m2ci/msp/jtgt/foobar.json b/src/test/resources/org/m2ci/msp/jtgt/foobar.json new file mode 100644 index 0000000..6eeb151 --- /dev/null +++ b/src/test/resources/org/m2ci/msp/jtgt/foobar.json @@ -0,0 +1,24 @@ +{ + "_filename": null, + "_start": 0, + "_end": 1, + "_tiers": [ + { + "_start": 0, + "_end": 1, + "_name": "foobar", + "_annotations": [ + { + "_start": 0, + "_end": 0.5, + "_text": "foo" + }, + { + "_start": 0.5, + "_end": 1, + "_text": "bar" + } + ] + } + ] +} From b0cacbfb79764a450c1b22e1d79f778ad25435dc Mon Sep 17 00:00:00 2001 From: Ingmar Steiner Date: Wed, 20 Jun 2018 23:16:26 +0200 Subject: [PATCH 6/6] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02399e9..37ca7dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ JTGT - publish Javadoc artifact - link to hosted Javadoc +- JSON-compatible String representation of TextGrid objects ### Fixed