1 package org.objectweb.celtix.js.rhino; 2 3 import java.io.File ; 4 5 import junit.framework.TestCase; 6 7 import org.easymock.classextension.EasyMock; 8 9 public class ServerAppTest extends TestCase { 10 11 private String epAddr = "http://celtix.objectweb.org/"; 12 13 private ProviderFactory phMock; 14 private String emptyFile; 15 16 protected void setUp() throws Exception { 17 super.setUp(); 18 phMock = EasyMock.createMock(ProviderFactory.class); 19 emptyFile = getClass().getResource("empty/empty.js").getFile(); 20 } 21 22 protected void tearDown() throws Exception { 23 super.tearDown(); 24 } 25 26 private ServerApp createServerApp() { 27 return new ServerApp() { 28 protected ProviderFactory createProviderFactory() { 29 return phMock; 30 } 31 }; 32 } 33 34 public void testNoArgs() { 35 EasyMock.replay(phMock); 36 try { 37 ServerApp app = createServerApp(); 38 String [] args = {}; 39 app.start(args); 40 fail("expected exception did not occur"); 41 } catch (Exception ex) { 42 assertEquals("wrong exception message", ServerApp.NO_FILES_ERR, ex.getMessage()); 43 } 44 EasyMock.verify(phMock); 45 } 46 47 public void testUknownOption() { 48 EasyMock.replay(phMock); 49 try { 50 ServerApp app = createServerApp(); 51 String [] args = {"-x"}; 52 app.start(args); 53 fail("expected exception did not occur"); 54 } catch (Exception ex) { 55 assertTrue(ex.getMessage().startsWith(ServerApp.UNKNOWN_OPTION)); 56 } 57 EasyMock.verify(phMock); 58 } 59 60 public void testMissingOptionA() { 61 EasyMock.replay(phMock); 62 try { 63 ServerApp app = createServerApp(); 64 String [] args = {"-a"}; 65 app.start(args); 66 fail("expected exception did not occur"); 67 } catch (Exception ex) { 68 assertEquals("wrong exception message", ServerApp.WRONG_ADDR_ERR, ex.getMessage()); 69 } 70 EasyMock.verify(phMock); 71 } 72 73 public void testBrokenOptionA() { 74 EasyMock.replay(phMock); 75 try { 76 ServerApp app = createServerApp(); 77 String [] args = {"-a", "not-a-url"}; 78 app.start(args); 79 fail("expected exception did not occur"); 80 } catch (Exception ex) { 81 assertEquals("wrong exception message", ServerApp.WRONG_ADDR_ERR, ex.getMessage()); 82 } 83 EasyMock.verify(phMock); 84 } 85 86 public void testMissingOptionB() { 87 EasyMock.replay(phMock); 88 try { 89 ServerApp app = createServerApp(); 90 String [] args = {"-b"}; 91 app.start(args); 92 fail("expected exception did not occur"); 93 } catch (Exception ex) { 94 assertEquals("wrong exception message", ServerApp.WRONG_BASE_ERR, ex.getMessage()); 95 } 96 EasyMock.verify(phMock); 97 } 98 99 public void testBrokenOptionB() { 100 EasyMock.replay(phMock); 101 try { 102 ServerApp app = createServerApp(); 103 String [] args = {"-b", "not-a-url"}; 104 app.start(args); 105 fail("expected exception did not occur"); 106 } catch (Exception ex) { 107 assertEquals("wrong exception message", ServerApp.WRONG_BASE_ERR, ex.getMessage()); 108 } 109 EasyMock.verify(phMock); 110 } 111 112 public void testFileOnly() throws Exception { 113 phMock.createAndPublish(new File (emptyFile), null, false); 114 EasyMock.replay(phMock); 115 ServerApp app = createServerApp(); 116 String [] args = {emptyFile}; 117 app.start(args); 118 EasyMock.verify(phMock); 119 } 120 121 public void testOptionsAB() throws Exception { 122 phMock.createAndPublish(new File (emptyFile), epAddr, true); 123 EasyMock.replay(phMock); 124 ServerApp app = createServerApp(); 125 String [] args = {"-a", epAddr, "-b", epAddr, emptyFile}; 126 app.start(args); 127 EasyMock.verify(phMock); 128 } 129 130 public void testOptionA() throws Exception { 131 phMock.createAndPublish(new File (emptyFile), epAddr, false); 132 EasyMock.replay(phMock); 133 ServerApp app = createServerApp(); 134 String [] args = {"-a", epAddr, emptyFile}; 135 app.start(args); 136 EasyMock.verify(phMock); 137 } 138 139 public void testOptionAWithOptionV() throws Exception { 140 phMock.createAndPublish(new File (emptyFile), epAddr, false); 141 EasyMock.replay(phMock); 142 ServerApp app = createServerApp(); 143 String [] args = {"-a", epAddr, "-v", emptyFile}; 144 app.start(args); 145 EasyMock.verify(phMock); 146 } 147 148 public void testOptionB() throws Exception { 149 phMock.createAndPublish(new File (emptyFile), epAddr, true); 150 EasyMock.replay(phMock); 151 ServerApp app = createServerApp(); 152 String [] args = {"-b", epAddr, emptyFile}; 153 app.start(args); 154 EasyMock.verify(phMock); 155 } 156 157 public void testOptionBWithOptionV() throws Exception { 158 phMock.createAndPublish(new File (emptyFile), epAddr, true); 159 EasyMock.replay(phMock); 160 ServerApp app = createServerApp(); 161 String [] args = {"-b", epAddr, "-v", emptyFile}; 162 app.start(args); 163 EasyMock.verify(phMock); 164 } 165 166 public void testDirectory() throws Exception { 167 File f = new File (emptyFile); 168 String dir = f.getParent(); 169 assertTrue(dir != null); 170 EasyMock.checkOrder(phMock, false); 171 phMock.createAndPublish(new File (emptyFile), epAddr, true); 172 String file = getClass().getResource("empty/empty2.jsx").getFile(); 173 phMock.createAndPublish(new File (file), epAddr, true); 174 file = getClass().getResource("empty/empty3.js").getFile(); 175 phMock.createAndPublish(new File (file), epAddr, true); 176 file = getClass().getResource("empty/empty4.jsx").getFile(); 177 phMock.createAndPublish(new File (file), epAddr, true); 178 EasyMock.replay(phMock); 179 ServerApp app = createServerApp(); 180 String [] args = {"-b", epAddr, dir}; 181 app.start(args); 182 EasyMock.verify(phMock); 183 } 184 185 } 186 | Popular Tags |