Skip to content

Commit cd5195a

Browse files
author
Sascha Toennies
committed
some more code embellishment
1 parent 96880a5 commit cd5195a

File tree

4 files changed

+45
-41
lines changed

4 files changed

+45
-41
lines changed

src/main/java/eu/toennies/javahttpobservatoryapi/Console.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static void main(String[] args) {
105105
}
106106
}
107107
} catch (UnsupportedEncodingException e) {
108-
108+
System.err.println("Could not write to System.out using UTF-8 encoding.");
109109
} finally {
110110
if (pw != null) {
111111
pw.close();

src/main/java/eu/toennies/javahttpobservatoryapi/HelpFormatter.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private Appendable renderWrappedTextBlock(StringBuffer sb, int width, int nextLi
306306
*
307307
* @return the StringBuffer with the rendered Options contents.
308308
*/
309-
protected StringBuffer renderWrappedText(StringBuffer sb, int width, int nextLineTabStop, String text) {
309+
protected StringBuffer renderWrappedText(StringBuffer sb, int width, final int nextLineTabStop, String text) {
310310
int pos = findWrapPos(text, width, 0);
311311

312312
if (pos == -1) {
@@ -316,14 +316,17 @@ protected StringBuffer renderWrappedText(StringBuffer sb, int width, int nextLin
316316
}
317317
sb.append(rtrim(text.substring(0, pos))).append(getNewLine());
318318

319+
int currentNextLineTabStop;
319320
if (nextLineTabStop >= width) {
320321
// stops infinite loop happening
321-
nextLineTabStop = 1;
322+
currentNextLineTabStop = 1;
323+
} else {
324+
currentNextLineTabStop = nextLineTabStop;
322325
}
323326

324327
// all following lines must be padded with nextLineTabStop space
325328
// characters
326-
final String padding = createPadding(nextLineTabStop);
329+
final String padding = createPadding(currentNextLineTabStop);
327330

328331
while (true) {
329332
text = padding + text.substring(pos).trim();
@@ -335,7 +338,7 @@ protected StringBuffer renderWrappedText(StringBuffer sb, int width, int nextLin
335338
return sb;
336339
}
337340

338-
if (text.length() > width && pos == nextLineTabStop - 1) {
341+
if (text.length() > width && pos == currentNextLineTabStop - 1) {
339342
pos = width;
340343
}
341344

src/main/java/eu/toennies/javahttpobservatoryapi/commands/InvokeAssessmentCommand.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,18 @@ public JSONObject run(List<String> arguments) throws IllegalArgumentException {
4848
throw new IllegalArgumentException("The mandatory argument host is not given.");
4949
}
5050

51-
String host = ConsoleUtilities.listValueMatchRegex(arguments, "host=(.+)");
52-
5351
JSONObject json = null;
54-
Api api = new Api();
55-
Map<String, String> postParameters = new HashMap<String, String>();
56-
postParameters.put("rescan", arguments.contains("rescan") ? "true" : "false");
57-
postParameters.put("hidden", arguments.contains("hidden") ? "true" : "false");
58-
5952
try {
60-
json = new JSONObject(api.sendApiPostRequest(getApiCommand() + "?host=" + host, postParameters));
53+
Api api = new Api();
54+
55+
Map<String, String> postParameters = new HashMap<String, String>();
56+
postParameters.put("rescan", arguments.contains("rescan") ? "true" : "false");
57+
postParameters.put("hidden", arguments.contains("hidden") ? "true" : "false");
58+
59+
String host = ConsoleUtilities.listValueMatchRegex(arguments, "host=(.+)");
60+
final String commandUrl = getApiCommand() + "?host=" + host;
61+
62+
json = new JSONObject(api.sendApiPostRequest(commandUrl, postParameters));
6163
checkForError(json);
6264
} catch (JSONException e) {
6365
Logger.getGlobal().severe("Could not build result: " + e.getLocalizedMessage());

src/test/java/eu/toennies/javahttpobservatoryapi/ApiTest.java

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void init() {
4444
public void testGradeDistributionCommand() {
4545
GradeDistributionCommand cmd = new GradeDistributionCommand();
4646
JSONObject gradeDistribution = cmd.run(null);
47-
47+
4848
assertNotNull("JSONObject is null", gradeDistribution);
4949
ApiAssert.assertApiDataFetched(gradeDistribution);
5050
}
@@ -70,24 +70,21 @@ public void testScannerStatesCommand() {
7070
@Test
7171
public void testInvokeAssessmentCommand() {
7272
InvokeAssessmentCommand cmd = new InvokeAssessmentCommand();
73-
73+
7474
JSONObject assessment = new JSONObject();
7575
try {
7676
// Argument host is mandatory. Should fail
7777
assessment = cmd.run(null);
78-
fail("InvokeAssessmentCommand called without an host argument. This should not be possible.");
79-
} catch(IllegalArgumentException e) {
80-
//ignore
78+
fail("Should have raised an IllegalArgumentException.");
79+
} catch (IllegalArgumentException e) {
80+
List<String> arguments = new ArrayList<String>();
81+
arguments.add("host=google.com");
82+
arguments.add("hidden");
83+
assessment = cmd.run(arguments);
84+
85+
assertNotNull("JSONObject is null", assessment);
86+
ApiAssert.assertApiDataFetched(assessment);
8187
}
82-
83-
84-
List<String> arguments = new ArrayList<String>();
85-
arguments.add("host=google.com");
86-
arguments.add("hidden");
87-
assessment = cmd.run(arguments);
88-
89-
assertNotNull("JSONObject is null", assessment);
90-
ApiAssert.assertApiDataFetched(assessment);
9188
}
9289

9390
/**
@@ -110,12 +107,12 @@ public void testRetrieveTestResultsCommand() {
110107
try {
111108
// Argument id is mandatory. Should fail
112109
cmd.run(null);
113-
} catch(IllegalArgumentException e) {
114-
assertTrue(true);
110+
fail("Should have raised an IllegalArgumentException.");
111+
} catch (IllegalArgumentException e) {
112+
return;
115113
}
116114
}
117115

118-
119116
/**
120117
* A recent scan call should not be null.
121118
*/
@@ -126,36 +123,38 @@ public void testRecentScansCommand() {
126123
JSONObject json = cmd.run(null);
127124
assertNotNull("JSONObject is null", json);
128125
ApiAssert.assertApiDataFetched(json);
129-
126+
130127
// Test the min argument
131128
List<String> arguments = new ArrayList<String>();
132129
arguments.add("min=100");
133130
json = cmd.run(arguments);
134131
String key;
135-
for(Iterator<String> it = json.keys(); it.hasNext();) {
132+
for (Iterator<String> it = json.keys(); it.hasNext();) {
136133
key = it.next();
137134
try {
138-
assertTrue("Calling recent scan with a minimum score of 100 should only retrieve A+ score", json.get(key).equals("A+"));
135+
assertTrue("Calling recent scan with a minimum score of 100 should only retrieve A+ score",
136+
json.get(key).equals("A+"));
139137
} catch (JSONException e) {
140-
e.printStackTrace();
138+
fail();
141139
}
142140
}
143-
141+
144142
arguments.clear();
145143
arguments.add("max=80");
146144
json = cmd.run(arguments);
147-
key = new String();
148-
for(Iterator<String> it = json.keys(); it.hasNext();) {
145+
for (Iterator<String> it = json.keys(); it.hasNext();) {
149146
try {
150147
key = it.next();
151-
assertFalse("Calling recent scan with a maximum score of 80 should only retrieve A scores", json.get(key).equals("A-"));
152-
assertFalse("Calling recent scan with a maximum score of 80 should only retrieve A scores", json.get(key).equals("A"));
153-
assertFalse("Calling recent scan with a maximum score of 80 should only retrieve A scores", json.get(key).equals("A+"));
148+
assertFalse("Calling recent scan with a maximum score of 80 should only retrieve A scores",
149+
json.get(key).equals("A-"));
150+
assertFalse("Calling recent scan with a maximum score of 80 should only retrieve A scores",
151+
json.get(key).equals("A"));
152+
assertFalse("Calling recent scan with a maximum score of 80 should only retrieve A scores",
153+
json.get(key).equals("A+"));
154154
} catch (JSONException e) {
155155
e.printStackTrace();
156156
}
157157
}
158-
159158

160159
}
161160
}

0 commit comments

Comments
 (0)