Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io.*; import java.util.*; import org.junit.*; import static org.junit.Assert.*; /** * This is a JUnit test class which verifies you have the correct * output

import java.io.*; import java.util.*; import org.junit.*; import static org.junit.Assert.*;

/** * This is a JUnit test class which verifies you have the correct * output for your AppleOrange program. * */ public class AppleOrangeTest { /** * These store the output from the program (instead of allowing * them to go to stdout and stderr). */ private static ByteArrayOutputStream localOut, localErr; /** * These keep track of stdout and stderr so we can put them back * when we're done comparing the outputs. */ private static PrintStream sysOut, sysErr; /** * This method tracks stdout and stderr for later use. */ @BeforeClass public static void setup() { sysOut = System.out; sysErr = System.err; }

/** * Before every test is run, reset the streams to capture stdout/stderr. */ @Before public void setupStreams() { localOut = new ByteArrayOutputStream(); localErr = new ByteArrayOutputStream(); System.setOut(new PrintStream(localOut)); System.setErr(new PrintStream(localErr)); }

/** * After every test, restore stdout/stderr. */ @After public void cleanUpStreams() { System.setOut(null); System.setErr(null); System.setOut(sysOut); System.setErr(sysErr); } /** * This is a template for all the tests. It takes the expected output * for stdout and stderr and the command line arguments to call, then * compares it to the actual (student generated) output. * * @param expectedOut the output we expect to see on stdout * @param expectedErr the output we expect to see on stderr * @param mainArgs the string array required by the main program */ public void runTest(String expectedOut, String expectedErr, String[] mainArgs) { //get expected output lines String[] expectedOutput = expectedOut.trim().split(" "); String[] expectedErrors = expectedErr.split(" "); //get student output lines AppleOrange.main(mainArgs); String[] studentOutput = localOut.toString().split(" "); String[] studentErrors = localErr.toString().split(" "); //compare assertOutputMatch(expectedOutput, studentOutput); assertOutputMatch(expectedErrors, studentErrors); } /** * This helper method compares each line of the output (for * multi-line outputs) and gives more helpful feedback then * just saying they did not match. * * @param expected the lines we expect to see * @param student the lines the student code generated */ public void assertOutputMatch(String[] expected, String[] student) { for(int i = 0; i < expected.length; i++) { //clean up output a little expected[i] = expected[i].replaceAll("\\s",""); student[i] = student[i].replaceAll("\\s",""); //check if there are missing lines if(i >= student.length) { fail("Missing line: \"" + expected[i] + "\""); } StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); //check that the two lines match assertTrue("Line " + (i+1) + " incorrect." + " Expected: "+expected[i] + " Got: "+student[i], student[i].equals(expected[i])); } //see if there are extra lines if(student.length > expected.length) { fail("Extra line: \"" + student[expected.length] + "\""); } } /** * Tests for the output for the numbers 1-10. */ @Test(timeout = 2000) public void test1() { String expectedOut = "1 2 apple 4 5 apple orange 8 apple 10"; String expectedErr = ""; String[] mainArgs = {"10"}; runTest(expectedOut, expectedErr, mainArgs); } /** * Tests for the output for the numbers 1-25. */ @Test(timeout = 2000) public void test2() { String expectedOut = "1 2 apple 4 5 apple orange 8 apple 10 11 apple 13 orange apple 16 17 apple 19 20 appleorange 22 23 apple 25"; String expectedErr = ""; String[] mainArgs = {"25"}; runTest(expectedOut, expectedErr, mainArgs); } /** * Tests for the output for the numbers 1-50. */ @Test(timeout = 2000) public void test3() { String expectedOut = "1 2 apple 4 5 apple orange 8 apple 10 11 apple 13 orange apple 16 17 apple 19 20 appleorange 22 23 apple 25 26 apple orange 29 apple 31 32 apple 34 orange apple 37 38 apple 40 41 appleorange 43 44 apple 46 47 apple orange 50"; String expectedErr = ""; String[] mainArgs = {"50"}; runTest(expectedOut, expectedErr, mainArgs); } /** * Tests for the output when no arguments are given. */ @Test(timeout = 2000) public void test4() { String expectedOut = ""; String expectedErr = "One positive number required as a command line argument. Example Usage: java AppleOrange [number]"; String[] mainArgs = {}; runTest(expectedOut, expectedErr, mainArgs); } /** * Tests for the output when an invalid number is given. */ @Test(timeout = 5000) public void test5() { String expectedOut = ""; String expectedErr = "One positive number required as a command line argument. Example Usage: java AppleOrange [number]"; String[] mainArgs = {"0"}; runTest(expectedOut, expectedErr, mainArgs); } /** * Tests for the output when too many arguments are given. */ @Test(timeout = 5000) public void test6() { String expectedOut = ""; String expectedErr = "One positive number required as a command line argument. Example Usage: java AppleOrange [number]"; String[] mainArgs = {"1","1"}; runTest(expectedOut, expectedErr, mainArgs); } /** * Tests for the output when something other than a number is given. */ @Test(timeout = 5000) public void test7() { String expectedOut = ""; String expectedErr = "One positive number required as a command line argument. Example Usage: java AppleOrange [number]"; String[] mainArgs = {"apple"}; runTest(expectedOut, expectedErr, mainArgs); } /** * Tests for the output for the numbers 1-100. */ @Test(timeout = 5000) public void test8() { String expectedOut = "1 2 apple 4 5 apple orange 8 apple 10 11 apple 13 orange apple 16 17 apple 19 20 appleorange 22 23 apple 25 26 apple orange 29 apple 31 32 apple 34 orange apple 37 38 apple 40 41 appleorange 43 44 apple 46 47 apple orange 50 apple 52 53 apple 55 orange apple 58 59 apple 61 62 appleorange 64 65 apple 67 68 apple orange 71 apple 73 74 apple 76 orange apple 79 80 apple 82 83 appleorange 85 86 apple 88 89 apple orange 92 apple 94 95 apple 97 orange apple 100"; String expectedErr = ""; String[] mainArgs = {"100"}; runTest(expectedOut, expectedErr, mainArgs); } /** * Main method that runs the tests. * @param args command line arguments (not used) */ public static void main(String[] args) { org.junit.runner.JUnitCore.main("AppleOrangeTest"); } }

public class AppleOrange { public static void main(String [] args) { } public void secondMethod(int args) { for (int i = 1; i <= 10; i++) { if(i%3 == 0 && i%7 == 0) System.out.print("appleorange"); try { if(i < 0) { throw new Exception("One positive number required as a command line arguement. " + "Example Usage: java AppleOrange [number]"); } } catch(Exception e) { System.out.println(e.getMessage()); } if(i%3==0) System.out.print("apple "); else if(i%7==0) System.out.print("orange "); else System.out.print(i + " "); }

} }

Your program should print the following (exactly) if the command line argument is missing, if the argument is not a positive number, or if too many arguments are given. This message should be sent to System.err not System.out.

Example commands which generate this error:

> java AppleOrange > java AppleOrange 0 > java AppleOrange 1 1 > java AppleOrange apple 

Error message:

One positive number required as a command line argument. Example Usage: java AppleOrange [number]

Exactly Matching Output

Seven unit tests have been provided to automate checking that you are exactly matching the output required (AppleOrangeTest.java) since it is hard to eyeball differences in text.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions

Question

4. What are three mobile social networks?

Answered: 1 week ago

Question

You have

Answered: 1 week ago