Skip to content

Commit 1679e9c

Browse files
committed
renaming of test resources, added test for conversion to JSON
1 parent 68c75b2 commit 1679e9c

File tree

7 files changed

+193
-33
lines changed

7 files changed

+193
-33
lines changed

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@
8585
<dependency>
8686
<groupId>junit</groupId>
8787
<artifactId>junit</artifactId>
88-
<version>4.11</version>
88+
<version>4.12</version>
89+
<scope>test</scope>
90+
</dependency>
91+
<dependency>
92+
<groupId>net.minidev</groupId>
93+
<artifactId>json-smart</artifactId>
94+
<version>2.1.1</version>
8995
<scope>test</scope>
9096
</dependency>
9197
</dependencies>

src/test/java/com/igormaznitsa/jbbp/it/AbstractParserIntegrationTest.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,39 @@
1717

1818
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
1919
import com.igormaznitsa.jbbp.utils.JBBPUtils;
20-
import java.io.InputStream;
21-
import static org.junit.Assert.assertArrayEquals;
20+
import java.io.*;
21+
import static org.junit.Assert.*;
2222

2323
public abstract class AbstractParserIntegrationTest {
2424

25+
public void assertFile(final String fileName, final String text) throws Exception {
26+
final InputStream in = this.getClass().getResourceAsStream(fileName);
27+
assertNotNull("Can't find file [" + fileName + "]", in);
28+
Reader reader = null;
29+
String fileText = null;
30+
try {
31+
reader = new InputStreamReader(in, "UTF-8");
32+
final StringWriter wr = new StringWriter();
33+
34+
while (true) {
35+
final int chr = reader.read();
36+
if (chr < 0) {
37+
break;
38+
}
39+
wr.write(chr);
40+
}
41+
wr.close();
42+
fileText = wr.toString();
43+
}
44+
finally {
45+
if (reader != null) {
46+
reader.close();
47+
}
48+
}
49+
50+
assertEquals("File content must be equals", fileText, text);
51+
}
52+
2553
public InputStream getResourceAsInputStream(final String resourceName) throws Exception {
2654
final InputStream result = this.getClass().getResourceAsStream(resourceName);
2755
if (result == null) {
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright 2015 Igor Maznitsa.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.igormaznitsa.jbbp.it;
17+
18+
import com.igormaznitsa.jbbp.JBBPParser;
19+
import com.igormaznitsa.jbbp.model.*;
20+
import java.io.InputStream;
21+
import net.minidev.json.*;
22+
import org.junit.Test;
23+
24+
public class ConvertToJSONTest extends AbstractParserIntegrationTest {
25+
26+
public static JSONObject convertToJSon(final JSONObject jsn, final JBBPAbstractField field) {
27+
final JSONObject json = jsn == null ? new JSONObject() : jsn;
28+
29+
final String fieldName = field.getFieldName() == null ? "nonamed" : field.getFieldName();
30+
if (field instanceof JBBPAbstractArrayField) {
31+
final JSONArray jsonArray = new JSONArray();
32+
if (field instanceof JBBPFieldArrayBit) {
33+
for (final byte b : ((JBBPFieldArrayBit) field).getArray()) {
34+
jsonArray.add(b);
35+
}
36+
}
37+
else if (field instanceof JBBPFieldArrayBoolean) {
38+
for (final boolean b : ((JBBPFieldArrayBoolean) field).getArray()) {
39+
jsonArray.add(b);
40+
}
41+
}
42+
else if (field instanceof JBBPFieldArrayByte) {
43+
for (final byte b : ((JBBPFieldArrayByte) field).getArray()) {
44+
jsonArray.add(b);
45+
}
46+
}
47+
else if (field instanceof JBBPFieldArrayInt) {
48+
for (final int b : ((JBBPFieldArrayInt) field).getArray()) {
49+
jsonArray.add(b);
50+
}
51+
}
52+
else if (field instanceof JBBPFieldArrayLong) {
53+
for (final long b : ((JBBPFieldArrayLong) field).getArray()) {
54+
jsonArray.add(b);
55+
}
56+
}
57+
else if (field instanceof JBBPFieldArrayShort) {
58+
for (final short b : ((JBBPFieldArrayShort) field).getArray()) {
59+
jsonArray.add(b);
60+
}
61+
}
62+
else if (field instanceof JBBPFieldArrayStruct) {
63+
final JBBPFieldArrayStruct array = (JBBPFieldArrayStruct) field;
64+
for (int i = 0; i < array.size(); i++) {
65+
jsonArray.add(convertToJSon(new JSONObject(), array.getElementAt(i)));
66+
}
67+
}
68+
else if (field instanceof JBBPFieldArrayUByte) {
69+
for (final byte b : ((JBBPFieldArrayUByte) field).getArray()) {
70+
jsonArray.add(b & 0xFF);
71+
}
72+
}
73+
else if (field instanceof JBBPFieldArrayUShort) {
74+
for (final short b : ((JBBPFieldArrayUShort) field).getArray()) {
75+
jsonArray.add(b & 0xFFFF);
76+
}
77+
}
78+
else {
79+
throw new Error("Unexpected field type");
80+
}
81+
json.put(fieldName, jsonArray);
82+
}
83+
else {
84+
if (field instanceof JBBPFieldBit) {
85+
json.put(fieldName, ((JBBPFieldBit) field).getAsInt());
86+
}
87+
else if (field instanceof JBBPFieldBoolean) {
88+
json.put(fieldName, ((JBBPFieldBoolean) field).getAsBool());
89+
}
90+
else if (field instanceof JBBPFieldByte) {
91+
json.put(fieldName, ((JBBPFieldByte) field).getAsInt());
92+
}
93+
else if (field instanceof JBBPFieldInt) {
94+
json.put(fieldName, ((JBBPFieldInt) field).getAsInt());
95+
}
96+
else if (field instanceof JBBPFieldLong) {
97+
json.put(fieldName, ((JBBPFieldLong) field).getAsLong());
98+
}
99+
else if (field instanceof JBBPFieldShort) {
100+
json.put(fieldName, ((JBBPFieldShort) field).getAsInt());
101+
}
102+
else if (field instanceof JBBPFieldStruct) {
103+
final JBBPFieldStruct struct = (JBBPFieldStruct) field;
104+
final JSONObject obj = new JSONObject();
105+
for (final JBBPAbstractField f : struct.getArray()) {
106+
convertToJSon(obj, f);
107+
}
108+
if (jsn == null) {
109+
json.putAll(obj);
110+
}
111+
else {
112+
json.put(fieldName, obj);
113+
}
114+
}
115+
else if (field instanceof JBBPFieldUByte) {
116+
json.put(fieldName, ((JBBPFieldUByte) field).getAsInt());
117+
}
118+
else if (field instanceof JBBPFieldUShort) {
119+
json.put(fieldName, ((JBBPFieldUShort) field).getAsInt());
120+
}
121+
else {
122+
throw new Error("Unexpected field");
123+
}
124+
}
125+
return json;
126+
}
127+
128+
@Test
129+
public void testConvertToJSON() throws Exception {
130+
final InputStream pngStream = getResourceAsInputStream("picture.png");
131+
try {
132+
133+
final JBBPParser pngParser = JBBPParser.prepare(
134+
"long header;"
135+
+ "// chunks\n"
136+
+ "chunk [_]{"
137+
+ " int length; "
138+
+ " int type; "
139+
+ " byte[length] data; "
140+
+ " int crc;"
141+
+ "}"
142+
);
143+
144+
final JSONObject json = convertToJSon(null, pngParser.parse(pngStream));
145+
final String jsonText = json.toJSONString(JSONStyle.MAX_COMPRESS);
146+
System.out.println(jsonText);
147+
assertFile("pnginjson.txt", jsonText);
148+
}
149+
finally {
150+
pngStream.close();
151+
}
152+
}
153+
}

src/test/java/com/igormaznitsa/jbbp/utils/JBBPTextWriterTest.java

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,6 @@ public class JBBPTextWriterTest extends AbstractParserIntegrationTest {
3131

3232
private JBBPTextWriter writer;
3333

34-
private void assertFile(final String fileName, final String text) throws Exception {
35-
final InputStream in = this.getClass().getResourceAsStream(fileName);
36-
assertNotNull("Can't find file [" + fileName + "]", in);
37-
Reader reader = null;
38-
String fileText = null;
39-
try {
40-
reader = new InputStreamReader(in, "UTF-8");
41-
final StringWriter wr = new StringWriter();
42-
43-
while (true) {
44-
final int chr = reader.read();
45-
if (chr < 0) {
46-
break;
47-
}
48-
wr.write(chr);
49-
}
50-
wr.close();
51-
fileText = wr.toString();
52-
}
53-
finally {
54-
if (reader != null) {
55-
reader.close();
56-
}
57-
}
58-
59-
assertEquals("File content must be equals", fileText, text);
60-
}
61-
6234
@Before
6335
public void before() {
6436
writer = new JBBPTextWriter(new StringWriter(), JBBPByteOrder.BIG_ENDIAN, "\n", 16, "0x", ".", ";","~", ",");
@@ -721,7 +693,7 @@ public void testBin_AllEasyTypes_NonMappedRawStruct() throws Exception {
721693

722694
final String text = writer.SetMaxValuesPerLine(16).Bin(parser.parse(testArray)).Close().toString();
723695
System.out.println(text);
724-
assertFile("raweasytypeswriterbin.txt", text);
696+
assertFile("txtwrtrjbbpobj1.txt", text);
725697
}
726698

727699
@Test
@@ -731,7 +703,7 @@ public void testBin_AllEasyTypes_Anonymous_NonMappedRawStruct() throws Exception
731703

732704
final String text = writer.SetMaxValuesPerLine(16).Bin(parser.parse(testArray)).Close().toString();
733705
System.out.println(text);
734-
assertFile("raweasytypeswriterbin_an.txt", text);
706+
assertFile("txtwrtrjbbpobj2.txt", text);
735707
}
736708

737709
@Test

0 commit comments

Comments
 (0)