250 likes | 377 Views
Java IO and Testing made simple. Viera K. Proulx and Richard Rasala College of Computer and Information Science Northeastern University Boston MA vkp@ccs.neu.edu rasala@ccs.neu.edu. Java IO and Testing Made Simple. Teaching with Java – many problems We address three of them:
E N D
Java IOand Testingmade simple Viera K. Proulx and Richard Rasala College of Computer and Information Science Northeastern University Boston MA vkp@ccs.neu.edu rasala@ccs.neu.edu Java IO and Testing Made Simple
Java IO and Testing Made Simple Teaching with Java – many problems We address three of them: • User interactions • Test suite development • Small vs. large data sets for algorithms Java IO and Testing Made Simple
Java IO and Testing Made Simple Reading user input from console or from GUI • complicated • buggy • convoluted • repetitious • illustrates bad programming in the best way Java IO and Testing Made Simple
Java IO and Testing Made Simple Test suite development • ad hoc • cumbersome • no organization • (or) overwhelming (JUnit) • unreadable • bad habits early Java IO and Testing Made Simple
Java IO and Testing Made Simple Small vs. large data sets for algorithms • internal data vs. external data: treated differently programs are re-written for file input omitted from most text books no understanding of the need for stress tests ... bad habits early... Java IO and Testing Made Simple
Java IO and Testing Made Simple Solutions • toolkit support for type-safe robust input from console and GUI with well designed user interface • framework to support separate test class to act as a client class; encapsulates the test suite • leverage abstractions of traversals via iterators to supply data for algorithm testing Java IO and Testing Made Simple
User interactions Reading user input from console or from GUI requires parsing, conversion, error reporting JPT toolkit one method does it all uniform for console and GUI Java IO and Testing Made Simple
User interactions • prompt displayed in the console • input string is parsed • expressions are evaluated and converted • errors reported; awaiting user correction • value of the correcttype delivered • option to cancel input is available Java IO and Testing Made Simple
User interactions In student code: int x = console.in.demandInt(“Number:”); In console: program outputuser input system error Number:34x Expected end of expression. 34x ^ Number:34 Java IO and Testing Made Simple
User interactions or for GUI TextFieldView xTFV: int x; try{ x = xTFV.requestDouble( ); } catch (CancelledException e) { System.out.println(“Input ends”); } Java IO and Testing Made Simple
User interactions Input of compound data: entire object • in real life: Serializable (unreadable) For student programs we want to deliver an instantiated object in one method call: Person p = testClass.demandPerson(“P:”); Java IO and Testing Made Simple
User interactions Person p = testClass.demandPerson(“P:”); Person demandPerson(String prompt){ console.out.println(prompt); return new Person( console.in.demandString(“Name:”), console.in.demandInt(“Age:”), console.in.demandBoolean(“Married?:”)); } Java IO and Testing Made Simple
User interactions for GUIs: we can build a custom GUI that delivers compete Person instance: PersonView pView = new PersonView(...); Person p = pView.demandPerson( ); (JPT makes this easy - we really mean it) Java IO and Testing Made Simple
interface Comparable... class Person... class Address... abstract class Job... class Clerk extends Job... Test goals: Create objects in classes Person, Address, Clerk Test methods in all classes Tests illustrate the use of objects and classes in real programs Test Suite Development Java IO and Testing Made Simple
Test Suite Development Test class requirements: needs to instantiate objects in target classes needs to test methods in target classes they should be grouped by the target class there should be a comment on the purpose needs an option to run only some tests access to user input and output; graphics, GUI Java IO and Testing Made Simple
Test Suite Development Java Power Framework: TestClass constructor invokes an application that provides: access to the JPT console and its methods access to a buffered graphics window a button for each proper method in TestClass proper method: void, no arguments Java IO and Testing Made Simple
Test Suite Development public class TestSuite extends JPFalt{ public static void main(String[ ] args){ new TestSuite( ); } Person p = new Person(“Roger”, 34, true); void testIsOld( ){ testHeader(“isOld”); expected(false); actual(p.isOld( )); } } Java IO and Testing Made Simple
Test Suite Development Sample test results in the console: Testing method isOld: Expected: false Actual: false expected(arg) and actual(arg) methods are implemented for all primitive types and for Object with toString() method (Students implement toString() for all classes) Java IO and Testing Made Simple
Input of Data Sets Sorting algorithms, Hash tables, Graphs, etc. sort a List of Person-s by age sort an Array of Person-s by name List, Array: a Collection by age, by name: a Comparator what data to sort? - typically students change the code for different sources Java IO and Testing Made Simple
Input of Data Sets Goal: sort any data that implements Collection use the given Comparator use the given data source Solution: provide an iterator for the data source copy the data into your kind of Collection Java IO and Testing Made Simple
Input of Data Sets Iterator for the data source (interface IRange) modified to fit Java for-loop delivers current object without advancing For each data set used in algorithms provide method in algorithm setup class: DataSet initialize(IRange it); Java IO and Testing Made Simple
Input of Data Sets Implementing the iterator: for input from the console we already can input complete objects for input from GUI– same idea for input from existing test data for input from a file helper extracts object data from a line Java IO and Testing Made Simple
Input of Data Sets Examples: DataSet ds1 = algSetup.initialize( new MyDataRange(someData)); DataSet ds2 = algSetup.initialize( new MyConsoleRange()); DataSet ds3 = algSetup.initialize( new MyFileRange(“myFile.txt”)); Java IO and Testing Made Simple
Java IO and Testing Made Simple User interactions Test suite development Input of data sets Good Habits Early Student experience: uniformly positive Java IO and Testing Made Simple
Java IO and Testing Made Simple http://www.ccs.neu.edu/jpt New initiatives and a workshop: http://www.ccs.neu.edu/home/vkp Java IO and Testing Made Simple