Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge remote-tracking branch 'origin/topic-wrapping' into multi-line-…

…tale

Conflicts:
	src/com/example/classproject/HelloWorld.java
  • Loading branch information...
commit 3f0c6a7eb7a32c255a4ad94d1136e56d6a9c73ad 2 parents 4401220 + 1ae67ba
Ken Judy authored
View
6 build.xml
@@ -8,7 +8,8 @@
<!--This is the name of our distribution file -->
<property name="junit.jar" value="${lib.dir}/junit4.8.2/junit-4.8.2.jar"/>
- <property name="classpath" value="${junit.jar}"/>
+ <property name="commonslang.jar" value="${lib.dir}/commons-lang-2.6.jar"/>
+ <property name="classpath" value="${commonslang.jar};${junit.jar}"/>
<target name="clean" description="Erase all generated files">
<delete dir="${build.dir}" failonerror="true"/>
@@ -21,7 +22,7 @@
<target name="test" depends="compile">
<junit haltonfailure="true">
- <classpath path="${junit.jar};${classes.dir}"/>
+ <classpath path="${junit.jar};${classes.dir};${commonslang.jar}"/>
<formatter type="brief" usefile="false"/>
<batchtest>
<fileset dir="${src.dir}">
@@ -42,6 +43,7 @@
value="com.example.classproject.HelloWorld"/>
</manifest>
</jar>
+ <copy file="${commonslang.jar}" tofile="commons-lang-2.6.jar" />
</target>
<target name="release" depends="clean, jar" description="Create release package">
View
BIN  commons-lang-2.6.jar
Binary file not shown
View
BIN  lib/commons-lang-2.6.jar
Binary file not shown
View
17 src/com/example/classproject/HelloWorld.java
@@ -2,17 +2,23 @@
import java.io.*;
import java.util.Random;
+import org.apache.commons.lang.WordUtils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
public class HelloWorld {
static String _tale;
public static void main(String[] args) throws IOException {
- runProgram(args[0], System.in);
+ runProgram(args[0], "story", System.in);
}
- public static void runProgram(String filename, InputStream userInputStream) throws IOException {
- Program program = new Program("story");
+ public static void runProgram(String filename, String storyRoot, InputStream userInputStream) throws IOException {
+ Program program = new Program(storyRoot);
_tale = program.readFile(filename);
printText(_tale, userInputStream);
@@ -28,6 +34,10 @@ public static void runProgram(String filename, InputStream userInputStream) thro
printText(_tale, userInputStream);
}
+ public static String buildWrappedText(String string) {
+ return WordUtils.wrap(string, 60);
+ }
+
public static String promptForInput(InputStream userInputStream) throws IOException
{
System.out.println("That doesn't look right. Please try again.");
@@ -36,6 +46,7 @@ public static String promptForInput(InputStream userInputStream) throws IOExce
}
public static void printText(String text, InputStream userInputStream) throws IOException {
+ text = buildWrappedText(text);
String[] lines = text.split("\n");
int i = 0;
for (String line : lines) {
View
43 src/com/example/classproject/HelloWorldTest.java
@@ -25,7 +25,7 @@ public void testPrintsCorrectStory() {
ByteArrayInputStream input = getInputStream("1");
String text = null;
try {
- text = readFile("story/1.txt");
+ text = readFile("test-stories/1.txt");
} catch (IOException e) {
e.printStackTrace();
}
@@ -33,14 +33,14 @@ public void testPrintsCorrectStory() {
System.setOut(new PrintStream(output));
try {
- HelloWorld.runProgram("dummy.txt", input);
+ HelloWorld.runProgram("dummy.txt", "test-stories", input);
String outputString = output.toString();
-// assertEquals("", outputString);
boolean contains = outputString.contains(text);
assertTrue(contains);
} catch (IOException e) {
// fail("exception: " + e.getMessage());
e.printStackTrace();
+ fail("exception");
}
}
@@ -128,6 +128,43 @@ public void testShouldPaginateText() {
assertTrue(output.toString().endsWith(secondChunk));
}
+ @Test
+ public void testShouldWrapLongLines() {
+ String aLongLine = "This is a really long line of text that should be wrapped so that it doesn't wrap in a really dumb way.";
+ String result = HelloWorld.buildWrappedText(aLongLine);
+
+ String line1 = "This is a really long line of text that should be wrapped so";
+ String line2 = "that it doesn't wrap in a really dumb way.";
+
+ String[] lines = result.split("\n");
+ assertEquals(lines[0], line1);
+ assertEquals(lines[1], line2);
+ }
+
+ @Test
+ public void testPrintTextShouldWrapWords() {
+ ByteArrayInputStream input = getInputStream("\n");
+ String aLongLine = "This is a really long line of text that should be wrapped so that it doesn't wrap in a really dumb way.";
+ String line1 = "This is a really long line of text that should be wrapped so";
+ String line2 = "that it doesn't wrap in a really dumb way.";
+
+
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ System.setOut(new PrintStream(output));
+
+ try {
+ HelloWorld.printText(aLongLine, input);
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail("exception");
+
+ }
+
+ String outputText = output.toString();
+ String expectedText = line1 + System.getProperty("line.separator") + line2;
+ assertTrue(outputText.contains(expectedText));
+ }
+
private ByteArrayInputStream getInputStream(String inputStream) {
return new ByteArrayInputStream(inputStream.getBytes());
}
View
2  test-stories/1.1.txt
@@ -0,0 +1,2 @@
+line1
+line2
View
1  test-stories/dummy.txt
@@ -0,0 +1 @@
+this is dummy text
Please sign in to comment.
Something went wrong with that request. Please try again.