/* ClassroomTester.java * Csu370 :: 09/10/2007 * Test Program for 'Classroom'; Assignment #1 */ import java.util.Random; /* A program to perform Black Box testing of the Classroom class * In later assignments you may be asked to write a Test program * much like this, so you should review any of the concepts * used here that are not familiar to you. Or ask a TA * or Professor if you are otherwised confused. */ public class ClassroomTester { /** Main method * After compiling it can be run with: * java ClassroomTester * Or imported into eclipse */ public static void main(String[] args){ /* Create a new Tester, run the tests, and Print out the * results. */ ClassroomTester t = new ClassroomTester(); /* See definitions below */ t.testClassrooms(); t.testExceptions(); /* A few Stats... */ System.out.println("\nNumber of classrooms tested: " + t.totClassrooms); System.out.println("Number of tests performed: " + t.ntests); System.out.println("Number of errors found: " + t.nerr); } /* Variables which will track the progress of out Tests */ private int ntests = 0; // Total Tests Run private int nerr = 0; // Number of Errors private int totClassrooms = 0; // Number of Classrooms Created private int totCap = 0; // Total Capacity private int totEqu = 0; // Total Equipped Classrooms Created /* How Many Classrooms to Create? */ private static final int NUM_TO_TEST = 20; /* Create random Classrooms, and Test them */ private void testClassrooms(){ Random r = new Random(); for(int i = 0; i < NUM_TO_TEST; i++){ /* Random capacity, random Equip/Unequip, add one to make * sure that we don't cause an Exception... we check * those later. See javadocs for info on java.util.Random */ int cap = r.nextInt(100)+1; boolean equ = (r.nextInt(10) >= 5); /* Run the Tests */ testClassroom(cap, "Room #"+i, equ); } } /* Test a single Classroom, including static methods */ private void testClassroom(int cap, String name, boolean equ){ try{ /* Create the right type of Classroom (Equip/Unequip)*/ Classroom c = (equ?Classroom.equipped(cap,name): Classroom.unequipped(cap,name)); /* Front of all the Message Strings... */ String front = (equ?"E":"Une") + "quipped."; /* Update the numbers for our static method checks later*/ totClassrooms++; totCap += cap; if(equ)totEqu++; /* Check each of the accessor methods */ assertTrue(c.capacity() == cap, front+"capacity()"); assertTrue(c.roomName().equals(name), front+"roomName()"); assertTrue(c.isEquipped() == equ, front+"isEquipped()"); /* As seen above, the (bool ? ifTrue : ifFalse) expression * is almost the same as IF, but is 'inlined' instead of * needing to asssign anything. Look for docs online if * you're interested */ assertTrue(c.toString().equals(name + ", " + cap + " seats, with"+ (equ?"":"out")+" projector"), front+"toString() == \""+c.toString()+"\""); /* Test the static methods after each Classroom is created */ testStaticMethods(); }catch(RuntimeException e){ /* If there was an exception anywhere in there, then we * have a problem */ assertTrue(false, "Exception: "+e.getMessage()); } } /* Safe test for floating-point numbers. Sometimes due to rounding/ * arithmetic ordering it's possible to get two different results * for the same mathematical expression. I'm assuming here that * we will be at least within 1/10000 th of each other. */ private static boolean closeEnough(double a, double b) { return ((Math.abs(a-b)) < 0.0001); } /* Make sure each of the static methods agrees with our number (and * the specifications). */ private void testStaticMethods(){ assertTrue(Classroom.totalRooms() == totClassrooms, "Classroom.totalRooms()"); assertTrue(Classroom.totalCapacity() == totCap, "Classroom.totalCapacity()"); assertTrue(closeEnough(Classroom.meanCapacity(), totCap/(double)totClassrooms), "Classroom.meanCapacity()"); assertTrue(closeEnough(Classroom.equippedRatio(), totEqu/(double)totClassrooms), "Classroom.equippedRatio()"); } /* Make sure exceptions are thrown for border cases and not for * good capacity() numbers. Review try{...}catch(...){...} * if this looks fishy, or ask your friendly TA ;) */ /* We make sure that good tests get counted by using: * assertTrue(true,""); * and force bad tests to fail with: * assertTrue(false,"Failure Message"); * making sure that we test both the Equipped and Uneqipped * static creators. */ private void testExceptions(){ /* Zero Capacity... */ try{ if (Classroom.equipped(0, "No-seat classroom") != null) assertTrue (false, "[Equipped] No Exception with Capacity == 0"); }catch(RuntimeException e){ assertTrue (true, ""); } try{ if (Classroom.unequipped(0, "No-seat classroom") != null) assertTrue (false, "[Unequipped] No Exception with Capacity == 0"); }catch(RuntimeException e){ assertTrue (true, ""); } /* Negative Capacity... */ try{ if (Classroom.equipped(-1, "Neg-seat classroom") != null) assertTrue (false, "[Equipped] No Exception with Capacity < 0"); }catch(RuntimeException e){ assertTrue (true, ""); } try{ if (Classroom.unequipped(-1, "Neg-seat classroom") != null) assertTrue (false, "[Unequipped] No Exception with Capacity < 0"); }catch(RuntimeException e){ assertTrue (true, ""); } /* Capacity of One... should *not* throw an exception. Note the * swapping of true/false compared with above. */ try{ if (Classroom.equipped(1, "One-seat classroom") != null) assertTrue (true, ""); }catch(RuntimeException e) { assertTrue (false, "[Equipped] Exception with Capacity == 1"); } try{ if (Classroom.unequipped(1, "One-seat classroom") != null) assertTrue (true, ""); }catch(RuntimeException e) { assertTrue (false, "[Unequipped] Exception with Capacity == 1"); } } /* Number of dots to print before we go to the next line */ private static final int DOTS_PER_LINE = 50; /* Update the test counters based on the result given. Result * is expected to be true for passing tests, and false for * failing tests. If a test fails, we print out the provided * message so the user can see what might have gone wrong. * Be sure to review anything that doesn't make sense. */ private void assertTrue (boolean result, String msg){ ntests++; if(!result){ System.out.println("\n**ERROR**: test# " + ntests + " -- " + msg); nerr ++; } if(ntests % DOTS_PER_LINE == 0)System.out.println(); System.out.print("."); } }