001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.js.rhino;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.File;
022: import java.io.PrintStream;
023:
024: import org.easymock.classextension.EasyMock;
025: import org.junit.Assert;
026: import org.junit.Before;
027: import org.junit.Test;
028:
029: public class ServerAppTest extends Assert {
030:
031: private String epAddr = "http://cxf.apache.org/";
032:
033: private ProviderFactory phMock;
034: private String emptyFile;
035:
036: @Before
037: public void setUp() throws Exception {
038: phMock = EasyMock.createMock(ProviderFactory.class);
039: emptyFile = getClass().getResource("empty/empty.js").toURI()
040: .getPath();
041: }
042:
043: private ServerApp createServerApp() {
044: return new ServerApp() {
045: protected ProviderFactory createProviderFactory() {
046: return phMock;
047: }
048: };
049: }
050:
051: @Test
052: public void testNoArgs() {
053: EasyMock.replay(phMock);
054: try {
055: ServerApp app = createServerApp();
056: String[] args = {};
057: app.start(args);
058: fail("expected exception did not occur");
059: } catch (Exception ex) {
060: assertEquals("wrong exception message",
061: ServerApp.NO_FILES_ERR, ex.getMessage());
062: }
063: EasyMock.verify(phMock);
064: }
065:
066: @Test
067: public void testUknownOption() {
068: EasyMock.replay(phMock);
069: try {
070: ServerApp app = createServerApp();
071: String[] args = { "-x" };
072: app.start(args);
073: fail("expected exception did not occur");
074: } catch (Exception ex) {
075: assertTrue(ex.getMessage().startsWith(
076: ServerApp.UNKNOWN_OPTION));
077: }
078: EasyMock.verify(phMock);
079: }
080:
081: @Test
082: public void testMissingOptionA() {
083: EasyMock.replay(phMock);
084: try {
085: ServerApp app = createServerApp();
086: String[] args = { "-a" };
087: app.start(args);
088: fail("expected exception did not occur");
089: } catch (Exception ex) {
090: assertEquals("wrong exception message",
091: ServerApp.WRONG_ADDR_ERR, ex.getMessage());
092: }
093: EasyMock.verify(phMock);
094: }
095:
096: @Test
097: public void testBrokenOptionA() {
098: EasyMock.replay(phMock);
099: try {
100: ServerApp app = createServerApp();
101: String[] args = { "-a", "not-a-url" };
102: app.start(args);
103: fail("expected exception did not occur");
104: } catch (Exception ex) {
105: assertEquals("wrong exception message",
106: ServerApp.WRONG_ADDR_ERR, ex.getMessage());
107: }
108: EasyMock.verify(phMock);
109: }
110:
111: @Test
112: public void testMissingOptionB() {
113: EasyMock.replay(phMock);
114: try {
115: ServerApp app = createServerApp();
116: String[] args = { "-b" };
117: app.start(args);
118: fail("expected exception did not occur");
119: } catch (Exception ex) {
120: assertEquals("wrong exception message",
121: ServerApp.WRONG_BASE_ERR, ex.getMessage());
122: }
123: EasyMock.verify(phMock);
124: }
125:
126: @Test
127: public void testBrokenOptionB() {
128: EasyMock.replay(phMock);
129: try {
130: ServerApp app = createServerApp();
131: String[] args = { "-b", "not-a-url" };
132: app.start(args);
133: fail("expected exception did not occur");
134: } catch (Exception ex) {
135: assertEquals("wrong exception message",
136: ServerApp.WRONG_BASE_ERR, ex.getMessage());
137: }
138: EasyMock.verify(phMock);
139: }
140:
141: @Test
142: public void testFileOnly() throws Exception {
143: phMock.createAndPublish(new File(emptyFile), null, false);
144: EasyMock.replay(phMock);
145: ServerApp app = createServerApp();
146: String[] args = { emptyFile };
147: app.start(args);
148: EasyMock.verify(phMock);
149: }
150:
151: @Test
152: public void testOptionsAB() throws Exception {
153: phMock.createAndPublish(new File(emptyFile), epAddr, true);
154: EasyMock.replay(phMock);
155: ServerApp app = createServerApp();
156: String[] args = { "-a", epAddr, "-b", epAddr, emptyFile };
157: app.start(args);
158: EasyMock.verify(phMock);
159: }
160:
161: @Test
162: public void testOptionA() throws Exception {
163: phMock.createAndPublish(new File(emptyFile), epAddr, false);
164: EasyMock.replay(phMock);
165: ServerApp app = createServerApp();
166: String[] args = { "-a", epAddr, emptyFile };
167: app.start(args);
168: EasyMock.verify(phMock);
169: }
170:
171: @Test
172: public void testOptionAWithOptionV() throws Exception {
173: ByteArrayOutputStream bout = new ByteArrayOutputStream();
174: PrintStream pout = new PrintStream(bout);
175: PrintStream orig = System.out;
176: try {
177: System.setOut(pout);
178: phMock.createAndPublish(new File(emptyFile), epAddr, false);
179: EasyMock.replay(phMock);
180: ServerApp app = createServerApp();
181: String[] args = { "-a", epAddr, "-v", emptyFile };
182: app.start(args);
183: EasyMock.verify(phMock);
184: pout.flush();
185: assertTrue(new String(bout.toByteArray())
186: .contains("processing file"));
187: } finally {
188: System.setOut(orig);
189: }
190: }
191:
192: @Test
193: public void testOptionB() throws Exception {
194: phMock.createAndPublish(new File(emptyFile), epAddr, true);
195: EasyMock.replay(phMock);
196: ServerApp app = createServerApp();
197: String[] args = { "-b", epAddr, emptyFile };
198: app.start(args);
199: EasyMock.verify(phMock);
200: }
201:
202: @Test
203: public void testOptionBWithOptionV() throws Exception {
204: ByteArrayOutputStream bout = new ByteArrayOutputStream();
205: PrintStream pout = new PrintStream(bout);
206: PrintStream orig = System.out;
207: try {
208: System.setOut(pout);
209:
210: phMock.createAndPublish(new File(emptyFile), epAddr, true);
211: EasyMock.replay(phMock);
212: ServerApp app = createServerApp();
213: String[] args = { "-b", epAddr, "-v", emptyFile };
214: app.start(args);
215: EasyMock.verify(phMock);
216: assertTrue(new String(bout.toByteArray())
217: .contains("processing file"));
218: } finally {
219: System.setOut(orig);
220: }
221: }
222:
223: @Test
224: public void testDirectory() throws Exception {
225: File f = new File(emptyFile);
226: String dir = f.getParent();
227: assertTrue(dir != null);
228: EasyMock.checkOrder(phMock, false);
229: phMock.createAndPublish(new File(emptyFile), epAddr, true);
230: String file = getClass().getResource("empty/empty2.jsx")
231: .toURI().getPath();
232: phMock.createAndPublish(new File(file), epAddr, true);
233: file = getClass().getResource("empty/empty3.js").toURI()
234: .getPath();
235: phMock.createAndPublish(new File(file), epAddr, true);
236: file = getClass().getResource("empty/empty4.jsx").toURI()
237: .getPath();
238: phMock.createAndPublish(new File(file), epAddr, true);
239: EasyMock.replay(phMock);
240: ServerApp app = createServerApp();
241: String[] args = { "-b", epAddr, dir };
242: app.start(args);
243: EasyMock.verify(phMock);
244: }
245:
246: }
|