import cs1705.*; import java.io.*; // ------------------------------------------------------------------------- /** * Write a one-sentence summary of your test class here. * Summarize what your test objectives are. * * @author your-pid (and partner's, if in lab) * @version (place the date here) */ public class RotDecryptorTest extends junit.framework.TestCase { RotDecryptor decryptor; //~ Constructor ........................................................... // ---------------------------------------------------------- /** * Creates a new RotDecryptorTest test object. */ public RotDecryptorTest() { } //~ Methods ............................................................... // ---------------------------------------------------------- /** * Sets up the test fixture. * Called before every test case method. */ protected void setUp() { decryptor = new RotDecryptor(); } // ---------------------------------------------------------- /** * Tears down the test fixture. * Called after every test case method. */ protected void tearDown() { } //~ Test cases for rotateCharacter() ...................................... // ---------------------------------------------------------- /** * Try rotating a capital 'A'. */ public void testRotateA() { assertEquals( 'N', decryptor.rotateCharacter( 'A' ) ); } // ---------------------------------------------------------- /** * Try rotating a lowercase 'z'. */ public void testRotatez() { assertEquals( 'm', decryptor.rotateCharacter( 'z' ) ); } // ---------------------------------------------------------- /** * Try rotating all ASCII character codes. Can you think of * a way to make the assertions tighter? */ public void testRotate() { int chr = 0; while ( chr < 128 ) { int rotated = decryptor.rotateCharacter( chr ); if ( Character.isLetter( (char)chr ) ) { assertTrue( Character.isLetter( (char)rotated ) ); assertEquals( 13, Math.abs( rotated - chr ) ); } ++chr; } } //~ Test cases for decrypt() .............................................. // ---------------------------------------------------------- /** * How can we test decryptFile()? This test checks to see * that an output file was produced, but doesn't check its * contents. */ public void testMessage1() { decryptor.decryptFile( "message1.txt", "output1.txt" ); assertTrue( IOHelper.getFile( "output1.txt" ).exists() ); } // ---------------------------------------------------------- /** * Instead, let's create some streams that are directly * connected to String objects, instead of to files. See * the corresponding section of the I/O tutorial for more info. */ public void testMessage1Again() { try { BufferedReader in = IOHelper.createBufferedReaderForString( "Uvfgbel vf n gbby hfrq ol cbyvgvpvnaf " + "gb whfgvsl gurve vagragvbaf." ); StringWriter result = new StringWriter(); PrintWriter out = new PrintWriter( result ); decryptor.decrypt( in, out ); in.close(); out.close(); assertEquals( "History is a tool used by politicians " + "to justify their intentions.", result.toString() ); } catch ( Exception e ) { e.printStackTrace(); } } // ---------------------------------------------------------- /** * It is annoying to have to cut-and-paste the test method * above every time we want to write a different input/output * pair. Instead, let's turn it into its own method by taking * out the specific strings tested above, and putting in * parameters instead. This method calls assertEquals() to * compare the actual results to what was expected. * * @param input the input text to decrypt * @param output the expected output that should be produced */ public void assertDecryption( String input, String expected ) { try { BufferedReader in = IOHelper.createBufferedReaderForString( input ); StringWriter result = new StringWriter(); PrintWriter out = new PrintWriter( result ); decryptor.decrypt( in, out ); in.close(); out.close(); assertEquals( expected, result.toString() ); } catch ( Exception e ) { e.printStackTrace(); } } // ---------------------------------------------------------- /** * Now we can simplify our original test of decrypt() using * our new assertDecryption() method. */ public void testMessage1Simpler() { assertDecryption( "Uvfgbel vf n gbby hfrq ol cbyvgvpvnaf " + "gb whfgvsl gurve vagragvbaf.", "History is a tool used by politicians " + "to justify their intentions." ); } // ---------------------------------------------------------- /** * Writing new test cases becomes much simpler with the * assertDecryption() method. */ public void testMessage2() { assertDecryption( "Gur ernfba jul jbeel xvyyf zber crbcyr guna " + "jbex vf gung zber crbcyr jbeel guna jbex.", "The reason why worry kills more people than " + "work is that more people worry than work." ); } }