001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * InvocationTest.java
039: * JUnit based test
040: *
041: * Created on March 2, 2007, 11:44 AM
042: */
043:
044: package com.sun.xml.ws.policy.jaxws.xmlstreamwriter;
045:
046: import javax.xml.stream.XMLStreamWriter;
047: import junit.framework.*;
048: import java.lang.reflect.InvocationTargetException;
049: import java.lang.reflect.Method;
050: import java.util.ArrayList;
051: import java.util.Arrays;
052: import java.util.List;
053: import static com.sun.xml.ws.policy.jaxws.xmlstreamwriter.XmlStreamWriterMethodType.WRITE_CHARACTERS;
054:
055: /**
056: *
057: * @author Marek Potociar (marek.potociar at sun.com)
058: */
059: public class InvocationTest extends TestCase {
060:
061: public InvocationTest(String testName) {
062: super (testName);
063: }
064:
065: protected void setUp() throws Exception {
066: }
067:
068: protected void tearDown() throws Exception {
069: }
070:
071: /**
072: * Test of createInvocation method, of class com.sun.xml.ws.policy.jaxws.xmlstreamwriter.Invocation.
073: */
074: public void testCreateInvocation() throws Exception {
075: Method method = XMLStreamWriter.class.getMethod(
076: XmlStreamWriterMethodType.WRITE_START_ELEMENT
077: .getMethodName(), String.class);
078: Object[] args = new Object[] { "test" };
079: Invocation result = Invocation.createInvocation(method, args);
080:
081: assertNotNull(result);
082: }
083:
084: /**
085: * Test of getMethodName method, of class com.sun.xml.ws.policy.jaxws.xmlstreamwriter.Invocation.
086: */
087: public void testGetMethodName() throws Exception {
088: String methodName;
089: Method method;
090: Object[] args;
091: Invocation result;
092:
093: methodName = XmlStreamWriterMethodType.WRITE_START_ELEMENT
094: .getMethodName();
095: method = XMLStreamWriter.class.getMethod(methodName,
096: String.class);
097: args = new Object[] { "test" };
098: result = Invocation.createInvocation(method, args);
099: assertEquals(methodName, result.getMethodName());
100: method = XMLStreamWriter.class.getMethod(methodName,
101: String.class, String.class);
102: args = new Object[] { "test", "test" };
103: result = Invocation.createInvocation(method, args);
104: assertEquals(methodName, result.getMethodName());
105: method = XMLStreamWriter.class.getMethod(methodName,
106: String.class, String.class, String.class);
107: args = new Object[] { "test", "test", "test" };
108: result = Invocation.createInvocation(method, args);
109: assertEquals(methodName, result.getMethodName());
110:
111: methodName = XmlStreamWriterMethodType.WRITE_END_ELEMENT
112: .getMethodName();
113: method = XMLStreamWriter.class.getMethod(methodName);
114: args = null;
115: result = Invocation.createInvocation(method, args);
116: assertEquals(methodName, result.getMethodName());
117:
118: methodName = XmlStreamWriterMethodType.WRITE_ATTRIBUTE
119: .getMethodName();
120: method = XMLStreamWriter.class.getMethod(methodName,
121: String.class, String.class);
122: args = new Object[] { "test", "test" };
123: result = Invocation.createInvocation(method, args);
124: assertEquals(methodName, result.getMethodName());
125: method = XMLStreamWriter.class.getMethod(methodName,
126: String.class, String.class, String.class);
127: args = new Object[] { "test", "test", "test" };
128: result = Invocation.createInvocation(method, args);
129: assertEquals(methodName, result.getMethodName());
130: method = XMLStreamWriter.class.getMethod(methodName,
131: String.class, String.class, String.class, String.class);
132: args = new Object[] { "test", "test", "test", "test" };
133: result = Invocation.createInvocation(method, args);
134: assertEquals(methodName, result.getMethodName());
135:
136: methodName = XmlStreamWriterMethodType.WRITE_CHARACTERS
137: .getMethodName();
138: method = XMLStreamWriter.class.getMethod(methodName,
139: String.class);
140: args = new Object[] { "test" };
141: result = Invocation.createInvocation(method, args);
142: assertEquals(methodName, result.getMethodName());
143: method = XMLStreamWriter.class.getMethod(methodName,
144: char[].class, int.class, int.class);
145: args = new Object[] { new char[] { 't', 'e', 's', 't' }, 0, 4 };
146: result = Invocation.createInvocation(method, args);
147: assertEquals(methodName, result.getMethodName());
148:
149: methodName = "flush";
150: method = XMLStreamWriter.class.getMethod(methodName);
151: args = null;
152: result = Invocation.createInvocation(method, args);
153: assertEquals(methodName, result.getMethodName());
154:
155: }
156:
157: /**
158: * Test of getMethodType method, of class com.sun.xml.ws.policy.jaxws.xmlstreamwriter.Invocation.
159: */
160: public void testGetMethodType() throws Exception {
161: String methodName;
162: XmlStreamWriterMethodType methodType;
163: Method method;
164: Object[] args;
165: Invocation result;
166:
167: methodName = XmlStreamWriterMethodType.WRITE_START_ELEMENT
168: .getMethodName();
169: methodType = XmlStreamWriterMethodType.WRITE_START_ELEMENT;
170: method = XMLStreamWriter.class.getMethod(methodName,
171: String.class);
172: args = new Object[] { "test" };
173: result = Invocation.createInvocation(method, args);
174: assertEquals(methodType, result.getMethodType());
175: method = XMLStreamWriter.class.getMethod(methodName,
176: String.class, String.class);
177: args = new Object[] { "test", "test" };
178: result = Invocation.createInvocation(method, args);
179: assertEquals(methodType, result.getMethodType());
180: method = XMLStreamWriter.class.getMethod(methodName,
181: String.class, String.class, String.class);
182: args = new Object[] { "test", "test", "test" };
183: result = Invocation.createInvocation(method, args);
184: assertEquals(methodType, result.getMethodType());
185:
186: methodName = XmlStreamWriterMethodType.WRITE_END_ELEMENT
187: .getMethodName();
188: methodType = XmlStreamWriterMethodType.WRITE_END_ELEMENT;
189: method = XMLStreamWriter.class.getMethod(methodName);
190: args = null;
191: result = Invocation.createInvocation(method, args);
192: assertEquals(methodType, result.getMethodType());
193:
194: methodName = XmlStreamWriterMethodType.WRITE_ATTRIBUTE
195: .getMethodName();
196: methodType = XmlStreamWriterMethodType.WRITE_ATTRIBUTE;
197: method = XMLStreamWriter.class.getMethod(methodName,
198: String.class, String.class);
199: args = new Object[] { "test", "test" };
200: result = Invocation.createInvocation(method, args);
201: assertEquals(methodType, result.getMethodType());
202: method = XMLStreamWriter.class.getMethod(methodName,
203: String.class, String.class, String.class);
204: args = new Object[] { "test", "test", "test" };
205: result = Invocation.createInvocation(method, args);
206: assertEquals(methodType, result.getMethodType());
207: method = XMLStreamWriter.class.getMethod(methodName,
208: String.class, String.class, String.class, String.class);
209: args = new Object[] { "test", "test", "test", "test" };
210: result = Invocation.createInvocation(method, args);
211: assertEquals(methodType, result.getMethodType());
212:
213: methodName = XmlStreamWriterMethodType.WRITE_CHARACTERS
214: .getMethodName();
215: methodType = XmlStreamWriterMethodType.WRITE_CHARACTERS;
216: method = XMLStreamWriter.class.getMethod(methodName,
217: String.class);
218: args = new Object[] { "test" };
219: result = Invocation.createInvocation(method, args);
220: assertEquals(methodType, result.getMethodType());
221: method = XMLStreamWriter.class.getMethod(methodName,
222: char[].class, int.class, int.class);
223: args = new Object[] { new char[] { 't', 'e', 's', 't' }, 0, 4 };
224: result = Invocation.createInvocation(method, args);
225: assertEquals(methodType, result.getMethodType());
226:
227: methodName = "flush";
228: methodType = XmlStreamWriterMethodType.OTHER;
229: method = XMLStreamWriter.class.getMethod(methodName);
230: args = null;
231: result = Invocation.createInvocation(method, args);
232: assertEquals(methodType, result.getMethodType());
233: }
234:
235: /**
236: * Test of getArgument method, of class com.sun.xml.ws.policy.jaxws.xmlstreamwriter.Invocation.
237: */
238: public void testGetArgument() throws Exception {
239: String methodName;
240: Method method;
241: Object[] args;
242: Invocation result;
243:
244: methodName = XmlStreamWriterMethodType.WRITE_START_ELEMENT
245: .getMethodName();
246: method = XMLStreamWriter.class.getMethod(methodName,
247: String.class, String.class, String.class);
248: args = new Object[] { "aaa", "bbb", "ccc" };
249: result = Invocation.createInvocation(method, args);
250: for (int i = 0; i < args.length; i++) {
251: assertEquals(args[i], result.getArgument(i));
252: }
253:
254: methodName = "flush";
255: method = XMLStreamWriter.class.getMethod(methodName);
256: args = null;
257: result = Invocation.createInvocation(method, args);
258: try {
259: result.getArgument(1);
260: fail("ArrayIndexOutOfBoundsException expected.");
261: } catch (ArrayIndexOutOfBoundsException e) {
262: // ok
263: }
264: }
265:
266: /**
267: * Test of getArgumentsLength method, of class com.sun.xml.ws.policy.jaxws.xmlstreamwriter.Invocation.
268: */
269: public void testGetArgumentsLength() throws Exception {
270: String methodName;
271: Method method;
272: Object[] args;
273: Invocation result;
274:
275: methodName = XmlStreamWriterMethodType.WRITE_START_ELEMENT
276: .getMethodName();
277: method = XMLStreamWriter.class.getMethod(methodName,
278: String.class, String.class, String.class);
279: args = new Object[] { "aaa", "bbb", "ccc" };
280: result = Invocation.createInvocation(method, args);
281: assertEquals(args.length, result.getArgumentsCount());
282:
283: methodName = "flush";
284: method = XMLStreamWriter.class.getMethod(methodName);
285: args = null;
286: result = Invocation.createInvocation(method, args);
287: assertEquals(0, result.getArgumentsCount());
288: }
289:
290: /**
291: * Tests proper behaviour of defence copying of arguments of writeCharacters() method
292: */
293: public void testDefenceCopyInWriteCharactersMethod()
294: throws Exception {
295: String methodName = XmlStreamWriterMethodType.WRITE_CHARACTERS
296: .getMethodName();
297: Method method = XMLStreamWriter.class.getMethod(methodName,
298: char[].class, int.class, int.class);
299: Object[] args;
300: Invocation result;
301: char[] originalChars, expectedChars;
302: int originalStart, originalLength, expectedStart, expectedLength;
303:
304: originalChars = new char[] { 'x', 'y', 't', 'e', 's', 't', 'z' };
305: originalStart = 2;
306: originalLength = 4;
307:
308: expectedChars = new char[] { 't', 'e', 's', 't' };
309: expectedStart = 0;
310: expectedLength = 4;
311:
312: args = new Object[] { originalChars, originalStart,
313: originalLength };
314: result = Invocation.createInvocation(method, args);
315: // modifying the original char array to test defence copy
316: originalChars[2] = 'w';
317:
318: assertEquals(expectedStart, result.getArgument(1));
319: assertEquals(expectedLength, result.getArgument(2));
320: char[] resultChars = (char[]) result.getArgument(0);
321: for (int i = expectedStart; i < expectedLength; i++) {
322: assertEquals(expectedChars[i], resultChars[i]);
323: }
324:
325: }
326: }
|