Skip to content

Commit ee0a53d

Browse files
committed
Verify exception messages. Move method comments so JavaDoc will pick them up.
1 parent f69466f commit ee0a53d

1 file changed

Lines changed: 78 additions & 54 deletions

File tree

CDLTest.java

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -42,113 +42,137 @@ public class CDLTest {
4242
"{Col 1:\"0.23\", Col 2:\"57.42\", Col 3:\"5e27\", Col 4:\"-234.879\", Col 5:\"2.34e5\", Col 6:\"0.0\", Col 7:\"9e-3\"}, "+
4343
"{Col 1:\"va\tl1\", Col 2:\"v\bal2\", Col 3:val3, Col 4:\"val\f4\", Col 5:val5, Col 6:va\'l6, Col 7:val7}]");
4444

45+
/**
46+
* Attempts to create a JSONArray from a null string.
47+
* Expect a NullPointerException.
48+
*/
4549
@Test(expected=NullPointerException.class)
4650
public void exceptionOnNullString() {
47-
/**
48-
* Attempts to create a JSONArray from a null string
49-
*/
5051
String nullStr = null;
5152
CDL.toJSONArray(nullStr);
5253
}
5354

54-
@Test(expected=JSONException.class)
55+
/**
56+
* Attempts to create a JSONArray from a string with unbalanced quotes
57+
* in column title line. Expects a JSONException.
58+
*/
59+
@Test
5560
public void unbalancedQuoteInName() {
56-
/**
57-
* Attempts to create a JSONArray from a string with unbalanced quotes
58-
* in column title line
59-
*/
6061
String badLine = "Col1, \"Col2\nVal1, Val2";
61-
CDL.toJSONArray(badLine);
62+
try {
63+
CDL.toJSONArray(badLine);
64+
assertTrue("Expecting an exception", false);
65+
} catch (JSONException e) {
66+
assertTrue("Expecting an exception message",
67+
"Missing close quote '\"'. at 12 [character 0 line 2]".
68+
equals(e.getMessage()));
69+
}
6270
}
6371

64-
@Test(expected=JSONException.class)
72+
/**
73+
* Attempts to create a JSONArray from a string with unbalanced quotes
74+
* in value line. Expects a JSONException.
75+
*/
76+
@Test
6577
public void unbalancedQuoteInValue() {
66-
/**
67-
* Attempts to create a JSONArray from a string with unbalanced quotes
68-
* in value line
69-
*/
7078
String badLine = "Col1, Col2\n\"Val1, Val2";
71-
CDL.toJSONArray(badLine);
79+
try {
80+
CDL.toJSONArray(badLine);
81+
assertTrue("Expecting an exception", false);
82+
} catch (JSONException e) {
83+
assertTrue("Expecting an exception message",
84+
"Missing close quote '\"'. at 23 [character 12 line 3]".
85+
equals(e.getMessage()));
86+
87+
}
7288
}
7389

74-
@Test(expected=JSONException.class)
90+
/**
91+
* Attempts to create a JSONArray from a string with null char
92+
* in column title line. Expects a JSONException.
93+
*/
94+
@Test
7595
public void nullInName() {
76-
/**
77-
* Attempts to create a JSONArray from a string with null char
78-
* in column title line
79-
*/
8096
String badLine = "C\0ol1, Col2\nVal1, Val2";
81-
CDL.toJSONArray(badLine);
97+
try {
98+
CDL.toJSONArray(badLine);
99+
assertTrue("Expecting an exception", false);
100+
} catch (JSONException e) {
101+
assertTrue("Expecting an exception message",
102+
"Bad character 'o' (111). at 3 [character 4 line 1]".
103+
equals(e.getMessage()));
104+
105+
}
82106
}
83107

108+
/**
109+
* call toString with a null array
110+
*/
84111
@Test(expected=NullPointerException.class)
85112
public void nullJSONArrayToString() {
86-
/**
87-
* call toString with a null array
88-
*/
89113
CDL.toString((JSONArray)null);
90114
}
91115

116+
/**
117+
* Create a JSONArray from an empty string
118+
*/
92119
@Test
93120
public void emptyString() {
94-
/**
95-
* Create a JSONArray from an empty string
96-
*/
97121
String emptyStr = "";
98122
JSONArray jsonArray = CDL.toJSONArray(emptyStr);
99123
assertTrue("CDL should return null when the input string is empty",
100124
jsonArray == null);
101125
}
102126

127+
/**
128+
* Create a JSONArray with only 1 row
129+
*/
103130
@Test
104131
public void onlyColumnNames() {
105-
/**
106-
* Create a JSONArray with only 1 row
107-
*/
108132
String columnNameStr = "col1, col2, col3";
109133
JSONArray jsonArray = CDL.toJSONArray(columnNameStr);
110134
assertTrue("CDL should return null when only 1 row is given",
111135
jsonArray == null);
112136
}
113137

138+
/**
139+
* Create a JSONArray from string containing only whitespace and commas
140+
*/
114141
@Test
115142
public void emptyLinesToJSONArray() {
116-
/**
117-
* Create a JSONArray from string containing only whitespace and commas
118-
*/
119143
String str = " , , , \n , , , ";
120144
JSONArray jsonArray = CDL.toJSONArray(str);
121145
assertTrue("JSONArray should be null for no content",
122146
jsonArray == null);
123147
}
124148

149+
/**
150+
* call toString with a null array
151+
*/
125152
@Test
126153
public void emptyJSONArrayToString() {
127-
/**
128-
* call toString with a null array
129-
*/
130154
JSONArray jsonArray = new JSONArray();
131155
String str = CDL.toString(jsonArray);
132156
assertTrue("CDL should return null for toString(null)",
133157
str == null);
134158
}
135159

160+
/**
161+
* call toString with a null arrays for names and values
162+
*/
136163
@Test
137164
public void nullJSONArraysToString() {
138-
/**
139-
* call toString with a null arrays for names and values
140-
*/
141165
String str = CDL.toString(null, null);
142166
assertTrue("CDL should return null for toString(null)",
143167
str == null);
144168
}
145169

170+
/**
171+
* Given a JSONArray that was not built by CDL, some chars may be
172+
* found that would otherwise be filtered out by CDL.
173+
*/
146174
@Test
147175
public void checkSpecialChars() {
148-
/**
149-
* Given a JSONArray that was not built by CDL, some chars may be
150-
* found that would otherwise be filtered out by CDL.
151-
*/
152176
JSONArray jsonArray = new JSONArray();
153177
JSONObject jsonObject = new JSONObject();
154178
jsonArray.put(jsonObject);
@@ -165,22 +189,22 @@ public void checkSpecialChars() {
165189
assertTrue(cdlStr.contains("\"V2\""));
166190
}
167191

192+
/**
193+
* Create a JSONArray from a string of lines
194+
*/
168195
@Test
169196
public void textToJSONArray() {
170-
/**
171-
* Create a JSONArray from a string of lines
172-
*/
173197
JSONArray jsonArray = CDL.toJSONArray(lines);
174198
JSONArray expectedJsonArray = new JSONArray(expectedLines);
175199
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
176200
}
177201

202+
/**
203+
* Create a JSONArray from a JSONArray of titles and a
204+
* string of value lines
205+
*/
178206
@Test
179207
public void jsonArrayToJSONArray() {
180-
/**
181-
* Create a JSONArray from a JSONArray of titles and a
182-
* string of value lines
183-
*/
184208
String nameArrayStr = "[Col1, Col2]";
185209
String values = "V1, V2";
186210
JSONArray nameJSONArray = new JSONArray(nameArrayStr);
@@ -189,12 +213,12 @@ public void jsonArrayToJSONArray() {
189213
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
190214
}
191215

216+
/**
217+
* Create a JSONArray from a string of lines,
218+
* then convert to string and then back to JSONArray
219+
*/
192220
@Test
193221
public void textToJSONArrayAndBackToString() {
194-
/**
195-
* Create a JSONArray from a string of lines,
196-
* then convert to string and then back to JSONArray
197-
*/
198222
JSONArray jsonArray = CDL.toJSONArray(lines);
199223
String jsonStr = CDL.toString(jsonArray);
200224
JSONArray finalJsonArray = CDL.toJSONArray(jsonStr);

0 commit comments

Comments
 (0)