001: package org.objectweb.celtix.js.rhino;
002:
003: import java.io.File;
004:
005: import junit.framework.TestCase;
006:
007: import org.easymock.classextension.EasyMock;
008:
009: public class ServerAppTest extends TestCase {
010:
011: private String epAddr = "http://celtix.objectweb.org/";
012:
013: private ProviderFactory phMock;
014: private String emptyFile;
015:
016: protected void setUp() throws Exception {
017: super .setUp();
018: phMock = EasyMock.createMock(ProviderFactory.class);
019: emptyFile = getClass().getResource("empty/empty.js").getFile();
020: }
021:
022: protected void tearDown() throws Exception {
023: super .tearDown();
024: }
025:
026: private ServerApp createServerApp() {
027: return new ServerApp() {
028: protected ProviderFactory createProviderFactory() {
029: return phMock;
030: }
031: };
032: }
033:
034: public void testNoArgs() {
035: EasyMock.replay(phMock);
036: try {
037: ServerApp app = createServerApp();
038: String[] args = {};
039: app.start(args);
040: fail("expected exception did not occur");
041: } catch (Exception ex) {
042: assertEquals("wrong exception message",
043: ServerApp.NO_FILES_ERR, ex.getMessage());
044: }
045: EasyMock.verify(phMock);
046: }
047:
048: public void testUknownOption() {
049: EasyMock.replay(phMock);
050: try {
051: ServerApp app = createServerApp();
052: String[] args = { "-x" };
053: app.start(args);
054: fail("expected exception did not occur");
055: } catch (Exception ex) {
056: assertTrue(ex.getMessage().startsWith(
057: ServerApp.UNKNOWN_OPTION));
058: }
059: EasyMock.verify(phMock);
060: }
061:
062: public void testMissingOptionA() {
063: EasyMock.replay(phMock);
064: try {
065: ServerApp app = createServerApp();
066: String[] args = { "-a" };
067: app.start(args);
068: fail("expected exception did not occur");
069: } catch (Exception ex) {
070: assertEquals("wrong exception message",
071: ServerApp.WRONG_ADDR_ERR, ex.getMessage());
072: }
073: EasyMock.verify(phMock);
074: }
075:
076: public void testBrokenOptionA() {
077: EasyMock.replay(phMock);
078: try {
079: ServerApp app = createServerApp();
080: String[] args = { "-a", "not-a-url" };
081: app.start(args);
082: fail("expected exception did not occur");
083: } catch (Exception ex) {
084: assertEquals("wrong exception message",
085: ServerApp.WRONG_ADDR_ERR, ex.getMessage());
086: }
087: EasyMock.verify(phMock);
088: }
089:
090: public void testMissingOptionB() {
091: EasyMock.replay(phMock);
092: try {
093: ServerApp app = createServerApp();
094: String[] args = { "-b" };
095: app.start(args);
096: fail("expected exception did not occur");
097: } catch (Exception ex) {
098: assertEquals("wrong exception message",
099: ServerApp.WRONG_BASE_ERR, ex.getMessage());
100: }
101: EasyMock.verify(phMock);
102: }
103:
104: public void testBrokenOptionB() {
105: EasyMock.replay(phMock);
106: try {
107: ServerApp app = createServerApp();
108: String[] args = { "-b", "not-a-url" };
109: app.start(args);
110: fail("expected exception did not occur");
111: } catch (Exception ex) {
112: assertEquals("wrong exception message",
113: ServerApp.WRONG_BASE_ERR, ex.getMessage());
114: }
115: EasyMock.verify(phMock);
116: }
117:
118: public void testFileOnly() throws Exception {
119: phMock.createAndPublish(new File(emptyFile), null, false);
120: EasyMock.replay(phMock);
121: ServerApp app = createServerApp();
122: String[] args = { emptyFile };
123: app.start(args);
124: EasyMock.verify(phMock);
125: }
126:
127: public void testOptionsAB() throws Exception {
128: phMock.createAndPublish(new File(emptyFile), epAddr, true);
129: EasyMock.replay(phMock);
130: ServerApp app = createServerApp();
131: String[] args = { "-a", epAddr, "-b", epAddr, emptyFile };
132: app.start(args);
133: EasyMock.verify(phMock);
134: }
135:
136: public void testOptionA() throws Exception {
137: phMock.createAndPublish(new File(emptyFile), epAddr, false);
138: EasyMock.replay(phMock);
139: ServerApp app = createServerApp();
140: String[] args = { "-a", epAddr, emptyFile };
141: app.start(args);
142: EasyMock.verify(phMock);
143: }
144:
145: public void testOptionAWithOptionV() throws Exception {
146: phMock.createAndPublish(new File(emptyFile), epAddr, false);
147: EasyMock.replay(phMock);
148: ServerApp app = createServerApp();
149: String[] args = { "-a", epAddr, "-v", emptyFile };
150: app.start(args);
151: EasyMock.verify(phMock);
152: }
153:
154: public void testOptionB() throws Exception {
155: phMock.createAndPublish(new File(emptyFile), epAddr, true);
156: EasyMock.replay(phMock);
157: ServerApp app = createServerApp();
158: String[] args = { "-b", epAddr, emptyFile };
159: app.start(args);
160: EasyMock.verify(phMock);
161: }
162:
163: public void testOptionBWithOptionV() throws Exception {
164: phMock.createAndPublish(new File(emptyFile), epAddr, true);
165: EasyMock.replay(phMock);
166: ServerApp app = createServerApp();
167: String[] args = { "-b", epAddr, "-v", emptyFile };
168: app.start(args);
169: EasyMock.verify(phMock);
170: }
171:
172: public void testDirectory() throws Exception {
173: File f = new File(emptyFile);
174: String dir = f.getParent();
175: assertTrue(dir != null);
176: EasyMock.checkOrder(phMock, false);
177: phMock.createAndPublish(new File(emptyFile), epAddr, true);
178: String file = getClass().getResource("empty/empty2.jsx")
179: .getFile();
180: phMock.createAndPublish(new File(file), epAddr, true);
181: file = getClass().getResource("empty/empty3.js").getFile();
182: phMock.createAndPublish(new File(file), epAddr, true);
183: file = getClass().getResource("empty/empty4.jsx").getFile();
184: phMock.createAndPublish(new File(file), epAddr, true);
185: EasyMock.replay(phMock);
186: ServerApp app = createServerApp();
187: String[] args = { "-b", epAddr, dir };
188: app.start(args);
189: EasyMock.verify(phMock);
190: }
191:
192: }
|