1 package jester.tests; 2 3 import jester.FileExistenceChecker; 4 import jester.JesterArgumentException; 5 import jester.MainArguments; 6 import junit.framework.Test; 7 import junit.framework.TestCase; 8 import junit.framework.TestSuite; 9 10 public class MainArgumentsTest extends TestCase { 11 FileExistenceChecker anythingFileExistenceChecker = new FileExistenceChecker(){ 12 public boolean exists(String fileName) { 13 return true; 14 } 15 }; 16 FileExistenceChecker nothingFileExistenceChecker = new FileExistenceChecker(){ 17 public boolean exists(String fileName) { 18 return false; 19 } 20 }; 21 22 public MainArgumentsTest(String name) { 23 super(name); 24 } 25 public static void main(String args[]) { 26 junit.awtui.TestRunner.main(new String [] { MainArgumentsTest.class.getName() }); 27 } 28 public static Test suite() { 29 TestSuite suite = new TestSuite(MainArgumentsTest.class); 30 return suite; 31 } 32 33 public void testMissingArgumentsCauseExceptionToBeThrown() throws Exception { 34 try { 35 new MainArguments(new String []{}, anythingFileExistenceChecker); 36 fail(); 37 } 38 catch (JesterArgumentException e) { 39 } 41 42 try { 43 new MainArguments(new String []{"a"}, anythingFileExistenceChecker); 44 fail(); 45 } 46 catch (JesterArgumentException e) { 47 } 49 } 50 51 public void testMissingFileCauseExceptionToBeThrown() throws Exception { 52 try { 53 new MainArguments(new String []{"a", "b"}, nothingFileExistenceChecker); 54 fail("Expected exception to be thrown because file doesn't exist"); 55 } 56 catch (JesterArgumentException e) { 57 } 59 } 60 61 public void testMandatoryArguments() throws JesterArgumentException { 62 MainArguments args = new MainArguments(new String []{"a", "b"}, anythingFileExistenceChecker); 63 assertEquals("a", args.getTestClassName()); 64 assertEquals(new String []{"b"}, args.getDirectoryOrFileNames()); 65 } 66 67 public void testThatMultipleMutationDirectoriesOrFilesAreAllowed() throws JesterArgumentException { 68 MainArguments args = new MainArguments(new String []{"a", "b1", "b2", "b3"}, anythingFileExistenceChecker); 69 assertEquals("a", args.getTestClassName()); 70 assertEquals(new String []{"b1", "b2", "b3"}, args.getDirectoryOrFileNames()); 71 } 72 73 public void testClasspathArgument() throws JesterArgumentException { 74 MainArguments args = new MainArguments(new String []{"-cp=a;b;c", "a", "b"}, anythingFileExistenceChecker); 75 assertEquals("a", args.getTestClassName()); 76 assertEquals(new String []{"b"}, args.getDirectoryOrFileNames()); 77 78 assertEquals("a;b;c", System.getProperty(MainArguments.CLASSPATH_PROPERTY)); 79 } 80 81 public void testThatOneCanSpecifyOptionToNotShowProgressDialog() throws JesterArgumentException { 82 MainArguments args = new MainArguments(new String []{"a", "b"}, anythingFileExistenceChecker); 83 assertTrue(args.shouldShowProgressDialog()); 84 85 args = new MainArguments(new String []{"a", "-q", "b"}, anythingFileExistenceChecker); 86 assertFalse(args.shouldShowProgressDialog()); 87 } 88 89 public void testThatClasspathArgumentCanAppearFirst() throws JesterArgumentException { 90 checkThatClasspathArgumentCanAppearInAnyOrder(new String []{"-cp=a;b;c", "a", "b1", "b2"}); 91 } 92 public void testThatClasspathArgumentCanAppearSecond() throws JesterArgumentException { 93 checkThatClasspathArgumentCanAppearInAnyOrder(new String []{"a", "-cp=a;b;c", "b1", "b2"}); 94 } 95 public void testThatClasspathArgumentCanAppearLast() throws JesterArgumentException { 96 checkThatClasspathArgumentCanAppearInAnyOrder(new String []{"a", "b1", "b2", "-cp=a;b;c"}); 97 } 98 public void testThatClasspathArgumentCanAppearInMiddleOfDirectoriesBecauseThereIsNoHarmInIt() throws JesterArgumentException { 99 checkThatClasspathArgumentCanAppearInAnyOrder(new String []{"a", "b1", "-cp=a;b;c", "b2"}); 100 } 101 102 private void checkThatClasspathArgumentCanAppearInAnyOrder(String [] args) throws JesterArgumentException { 103 MainArguments mainArgs = new MainArguments(args, anythingFileExistenceChecker); 104 assertEquals("a", mainArgs.getTestClassName()); 105 assertEquals(new String []{"b1", "b2"}, mainArgs.getDirectoryOrFileNames()); 106 107 assertEquals("a;b;c", System.getProperty(MainArguments.CLASSPATH_PROPERTY)); 108 } 109 110 private void assertEquals(String [] expected, String [] actual){ 111 assertEquals(expected.length,actual.length); 112 for (int i = 0; i < actual.length; i++) { 113 assertEquals(expected[i],actual[i]); 114 } 115 } 116 } | Popular Tags |