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.File;
021:
022: import javax.xml.ws.Service;
023:
024: import org.easymock.classextension.EasyMock;
025:
026: import org.junit.Assert;
027: import org.junit.Before;
028: import org.junit.Test;
029: import org.mozilla.javascript.EvaluatorException;
030: import org.mozilla.javascript.Scriptable;
031:
032: public class ProviderFactoryTest extends Assert {
033:
034: private String epAddr = "http://cxf.apache.org/";
035:
036: private ProviderFactory ph;
037: private AbstractDOMProvider dpMock;
038:
039: @Before
040: public void setUp() throws Exception {
041: dpMock = EasyMock.createMock(AbstractDOMProvider.class);
042: ph = new ProviderFactory(epAddr) {
043: protected AbstractDOMProvider createProvider(
044: Service.Mode mode, Scriptable scope,
045: Scriptable wspVar, String epAddress,
046: boolean isBase, boolean e4x) throws Exception {
047: return dpMock;
048: }
049: };
050: }
051:
052: @Test
053: public void testMsgJSFile() throws Exception {
054: dpMock.publish();
055: dpMock.publish();
056: EasyMock.replay(dpMock);
057: File f = new File(getClass().getResource("msg.js").toURI()
058: .getPath());
059: ph.createAndPublish(f);
060: EasyMock.verify(dpMock);
061: }
062:
063: @Test
064: public void testBadJSFile() throws Exception {
065: EasyMock.replay(dpMock);
066: final String fname = "broken.js";
067: File f = new File(getClass().getResource(fname).toURI()
068: .getPath());
069: try {
070: ph.createAndPublish(f);
071: fail("expected exception did not occur");
072: } catch (EvaluatorException ex) {
073: assertTrue("wrong exception", ex.getMessage().startsWith(
074: "syntax error")
075: || ex.getMessage().startsWith("erreur de syntaxe"));
076: }
077: EasyMock.verify(dpMock);
078: }
079:
080: @Test
081: public void testEmptyJSFile() throws Exception {
082: EasyMock.replay(dpMock);
083: final String fname = "empty.js";
084: File f = new File(getClass().getResource(fname).toURI()
085: .getPath());
086: try {
087: ph.createAndPublish(f);
088: fail("expected exception did not occur");
089: } catch (Exception ex) {
090: assertEquals("wrong exception message", f.getPath()
091: + ProviderFactory.NO_PROVIDER, ex.getMessage());
092: }
093: EasyMock.verify(dpMock);
094: }
095:
096: @Test
097: public void testNoSuchJSFile() throws Exception {
098: EasyMock.replay(dpMock);
099: final String fname = "none.js";
100: File f = new File(fname);
101: try {
102: ph.createAndPublish(f);
103: fail("expected exception did not occur");
104: } catch (Exception ex) {
105: assertEquals("wrong exception message", f.getPath()
106: + ProviderFactory.NO_SUCH_FILE, ex.getMessage());
107: }
108: EasyMock.verify(dpMock);
109: }
110:
111: @Test
112: public void testIllegalServiceMode() throws Exception {
113: EasyMock.replay(dpMock);
114: final String fname = "illegal1.js";
115: File f = new File(getClass().getResource(fname).toURI()
116: .getPath());
117: try {
118: ph.createAndPublish(f);
119: fail("expected exception did not occur");
120: } catch (Exception ex) {
121: assertEquals("wrong exception message", f.getPath()
122: + ProviderFactory.ILLEGAL_SVCMD_MODE + "bogus", ex
123: .getMessage());
124: }
125: EasyMock.verify(dpMock);
126: }
127:
128: @Test
129: public void testIllegalServiceModeType() throws Exception {
130: EasyMock.replay(dpMock);
131: final String fname = "illegal2.js";
132: File f = new File(getClass().getResource(fname).toURI()
133: .getPath());
134: try {
135: ph.createAndPublish(f);
136: fail("expected exception did not occur");
137: } catch (Exception ex) {
138: assertEquals("wrong exception message", f.getPath()
139: + ProviderFactory.ILLEGAL_SVCMD_TYPE, ex
140: .getMessage());
141: }
142: EasyMock.verify(dpMock);
143: }
144:
145: @Test
146: public void testProviderException() throws Exception {
147: dpMock.publish();
148: EasyMock.expectLastCall().andThrow(
149: new AbstractDOMProvider.JSDOMProviderException(
150: AbstractDOMProvider.NO_EP_ADDR));
151: EasyMock.replay(dpMock);
152: File f = new File(getClass().getResource("msg.js").toURI()
153: .getPath());
154: try {
155: ph.createAndPublish(f);
156: fail("expected exception did not occur");
157: } catch (Exception ex) {
158: assertEquals("wrong exception message", f.getPath() + ": "
159: + AbstractDOMProvider.NO_EP_ADDR, ex.getMessage());
160: }
161: EasyMock.verify(dpMock);
162: }
163: }
|