HTML and CSS Reference
In-Depth Information
// Process response by reading the content into a StringBuilder
BufferedReader rd = new
BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder apiResult = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
apiResult.append(line);
}
// Use the GSON Library to parse the JSON response from randomtext.me
JsonElement jsonElement = jsonParser.parse(apiResult.toString());
JsonObject jsonObject = jsonElement.getAsJsonObject();
return jsonObject.get(PROPERTY_CONTAINING_OUTPUT).getAsString();
}
}
This could easily have been copied directly into the implementation of UIComponent in the next step, but then
you end up mixing business logic with component representation, and that will most likely present maintenance
challenges when you need to upgrade the component. To demonstrate a benefit of separating the development of
the model and the component, you will find a test case in Listing 6-2 that would have been harder to implement if the
business logic had been mixed with the UIComponent implementation.
Listing 6-2. Simple Unit Tests for RandomTextAPI.java
package com.apress.projsf2html5.chapter6.components;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import static org.junit.Assert.*;
public class RandomTextAPITest {
int outputCount = 10;
int wordCountLower = 3;
int wordCountUpper = 15;
@Test
public void testListRandomText() throws Exception {
RandomTextAPI.TextType type = RandomTextAPI.TextType.gibberish;
RandomTextAPI.OutputTag output = RandomTextAPI.OutputTag.ul;
RandomTextAPI instance = new RandomTextAPI();
String result = instance.getRandomText(type, output, outputCount, wordCountLower,
wordCountUpper);
int paragraphCount = StringUtils.countMatches(result, "<li>");
assertEquals("Incorrect number of items in the list", outputCount, paragraphCount);
}
Search WWH ::




Custom Search