1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

updating cdltest

This commit is contained in:
stleary 2015-03-17 21:47:53 -05:00
parent 03192b0162
commit 5b56b57074

View file

@ -3,14 +3,23 @@ package org.json.junit;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.*; import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.CDL; import org.json.CDL;
/** /**
* Tests for {@link CDL}. * Tests for {@link CDL}.
* CDL provides an application level API, it is not actually used by the * CDL provides an application level API, it is not actually used by the
@ -26,36 +35,59 @@ public class CDLTest {
/** /**
* Compares a JSON array to the original string. The top row of the * Compares a JSON array to the original string. The top row of the
* string contains the JSONObject keys and the remaining rows contain * string contains the JSONObject keys and the remaining rows contain
* the values. The JSONObject rows are unordered and may differ between * the values. The JSONObject in each JSONArray row is expected to have
* rows. * an entry corresponding to each key/value pair in the string.
* Each JSONObject row is unordered in its own way.
* @param jsonArray the JSONArray which was created from the string * @param jsonArray the JSONArray which was created from the string
* @param str the string which was used to create the JSONArray * @param str the string which was used to create the JSONArray
* @return true if equal, otherwise false * @return null if equal, otherwise error description
*/ */
public boolean compareJSONArrayToString(JSONArray jsonArray, String str) { public String compareJSONArrayToString(JSONArray jsonArray, String str) {
boolean result = true;
int rows = jsonArray.length(); int rows = jsonArray.length();
StringReader sr = new StringReader(str); StringReader sr = new StringReader(str);
BufferedReader reader = new BufferedReader(sr); BufferedReader reader = new BufferedReader(sr);
try { try {
// first line contains the keys to the JSONObject array entries
String columnNames = reader.readLine(); String columnNames = reader.readLine();
String[] keys = columnNames.split(","); String[] keys = columnNames.split(",");
/**
* Each line contains the values for the corresponding
* JSONObject array entry
*/
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i) {
String row = reader.readLine(); String row = reader.readLine();
String[] values = row.split(","); String[] values = row.split(",");
JSONObject jsonObject = jsonArray.getJSONObject(i); // need a value for every key to proceed
if (keys.length != jsonObject.length()) { if (keys.length != values.length) {
break; return("row: " +i+ " key and value counts do not match");
} }
int colIndex = 0; JSONObject jsonObject = jsonArray.getJSONObject(i);
for (String key: keys) { // need a key for every JSONObject entry to proceed
if (keys.length != jsonObject.length()) {
Object obj = jsonObject.get(key); return("row: " +i+ " key and jsonObject counts do not match");
}
/**
* convert string entries into a natural order map. Trim the
* keys and values for tokener compatibility
*/
Map<String, String> strMap = new TreeMap<String, String>();
for (int j = 0; j < keys.length; ++j) {
strMap.put(keys[j].trim(), values[j].trim());
}
// put the JSONObjet key/value pairs in natural key order
Iterator<String> keyIt = jsonObject.keys();
Map<String, String> jsonObjectMap = new TreeMap<String, String>();
while (keyIt.hasNext()) {
String key = keyIt.next();
jsonObjectMap.put(key, jsonObject.get(key).toString());
}
if (!strMap.equals(jsonObjectMap)) {
return("row: " +i+ "string does not match jsonObject");
} }
} }
} catch (IOException ignore) {} } catch (IOException ignore) {
return result; } catch (JSONException ignore) {}
return null;
} }
@Test @Test
@ -69,8 +101,11 @@ public class CDLTest {
"val1, val2, val3, val4, val5, val6, val7\n" + "val1, val2, val3, val4, val5, val6, val7\n" +
"1, 2, 3, 4, 5, 6, 7\n"); "1, 2, 3, 4, 5, 6, 7\n");
JSONArray jsonArray = CDL.toJSONArray(lines); JSONArray jsonArray = CDL.toJSONArray(lines);
assertTrue("CDL should convert string to JSONArray correctly", String resultStr = compareJSONArrayToString(jsonArray, lines);
compareJSONArrayToString(jsonArray, lines)); if (resultStr != null) {
assertTrue("CDL should convert string to JSONArray correctly: " +
resultStr, false);
}
} }
} }