001: /*
002: * $Id$
003: */
004: package com.mycompany.myproject.tests;
005:
006: import junit.framework.Test;
007: import junit.framework.TestCase;
008: import junit.framework.TestSuite;
009: import junit.textui.TestRunner;
010:
011: import org.xins.client.UnsuccessfulXINSCallException;
012: import org.xins.client.XINSCallRequest;
013: import org.xins.client.XINSCallResult;
014: import org.xins.client.XINSServiceCaller;
015:
016: import org.xins.common.service.TargetDescriptor;
017: import org.xins.common.xml.Element;
018: import org.xins.common.xml.ElementBuilder;
019:
020: /**
021: * Implementation of the <code>MyFunction</code> tests.
022: *
023: * @version $Revision$ $Date$
024: */
025: public class MyFunctionTests extends TestCase {
026:
027: /**
028: * The XINServiceCaller of the API.
029: */
030: private XINSServiceCaller caller;
031:
032: /**
033: * The URL of the tested API.
034: */
035: private String target;
036:
037: /**
038: * Constructs a new <code>MyFunctionTests</code> test suite with
039: * the specified name. The name will be passed to the superconstructor.
040: *
041: * @param name
042: * the name for this test suite.
043: */
044: public MyFunctionTests(String name) {
045: super (name);
046: }
047:
048: /**
049: * Returns a test suite with all test cases defined by this class.
050: *
051: * @return
052: * the test suite, never <code>null</code>.
053: */
054: public static Test suite() {
055: return new TestSuite(MyFunctionTests.class);
056: }
057:
058: /**
059: * Executes just this test.
060: */
061: public static void main(String args[]) {
062: TestRunner.run(suite());
063: }
064:
065: protected void setUp() throws Exception {
066: target = System.getProperty("test.environment");
067: if (target == null || target.trim().equals("")) {
068: target = "http://localhost:8080/myproject/";
069: }
070: caller = new XINSServiceCaller(new TargetDescriptor(target));
071: }
072:
073: protected void tearDown() throws Exception {
074: }
075:
076: /**
077: * Tests the MyFunction function by executing the example 1.
078: *
079: * Description: Missing parameter : lastName
080: *
081: * @throws Exception
082: * if an unexpected error occurs.
083: */
084: public void testMyFunctionExample1() throws Exception {
085: XINSCallRequest request = new XINSCallRequest("MyFunction");
086: request.setParameter("personLastName", "Bond 007");
087: try {
088: XINSCallResult result = caller.call(request);
089: fail("An error code \"_InvalidRequest\" was expected but did not occur.");
090: } catch (UnsuccessfulXINSCallException uxcex) {
091: assertEquals("Incorrect error code received: "
092: + uxcex.getErrorCode(), "_InvalidRequest", uxcex
093: .getErrorCode());
094: Element missing_param1 = (Element) uxcex.getDataElement()
095: .getChildElements().get(0);
096: assertEquals("Incorrect element.", "missing-param",
097: missing_param1.getLocalName());
098: assertEquals(
099: "The returned attribute \"param\" is incorrect.",
100: "gender", missing_param1.getAttribute("param"));
101: Element invalid_value_for_type2 = (Element) uxcex
102: .getDataElement().getChildElements().get(1);
103: assertEquals("Incorrect element.",
104: "invalid-value-for-type", invalid_value_for_type2
105: .getLocalName());
106: assertEquals(
107: "The returned attribute \"type\" is incorrect.",
108: "LastName", invalid_value_for_type2
109: .getAttribute("type"));
110: assertEquals(
111: "The returned attribute \"param\" is incorrect.",
112: "personLastName", invalid_value_for_type2
113: .getAttribute("param"));
114: }
115: }
116:
117: /**
118: * Tests the MyFunction function by executing the example 2.
119: *
120: * Description: The name does not contain any vowels.
121: *
122: * @throws Exception
123: * if an unexpected error occurs.
124: */
125: public void testMyFunctionExample2() throws Exception {
126: XINSCallRequest request = new XINSCallRequest("MyFunction");
127: request.setParameter("gender", "f");
128: request.setParameter("personLastName", "cqfd");
129: try {
130: XINSCallResult result = caller.call(request);
131: fail("An error code \"NoVowel\" was expected but did not occur.");
132: } catch (UnsuccessfulXINSCallException uxcex) {
133: assertEquals("Incorrect error code received: "
134: + uxcex.getErrorCode(), "NoVowel", uxcex
135: .getErrorCode());
136: }
137: }
138:
139: /**
140: * Tests the MyFunction function by executing the example 3.
141: *
142: * Description: Message returned.
143: *
144: * @throws Exception
145: * if an unexpected error occurs.
146: */
147: public void testMyFunctionExample3() throws Exception {
148: XINSCallRequest request = new XINSCallRequest("MyFunction");
149: request.setParameter("gender", "f");
150: request.setParameter("personLastName", "Lee");
151: XINSCallResult result = caller.call(request);
152: assertEquals(
153: "The returned parameter \"message\" is incorrect.",
154: "Hello Miss Lee", result.getParameter("message"));
155: }
156: }
|