001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.services;
028:
029: import com.sun.midp.links.*;
030: import java.io.*;
031:
032: import com.sun.cldc.isolate.*;
033: import com.sun.midp.i3test.TestCase;
034:
035: /**
036: * Tests for the LinkPortal class.
037: */
038: public class TestServiceRequestProtocol extends TestCase {
039:
040: class SystemServiceRequestListenerImpl implements
041: SystemServiceRequestListener {
042:
043: // expected service ID
044: String serviceID = "";
045:
046: // precreated connection between service and client
047: SystemServiceConnectionLinks con = null;
048:
049: // true if connection links has been passed to client
050: boolean linksPassed = false;
051:
052: SystemServiceRequestListenerImpl(String serviceID,
053: SystemServiceConnectionLinks con) {
054:
055: this .serviceID = serviceID;
056: this .con = con;
057: this .linksPassed = false;
058: }
059:
060: public SystemServiceConnectionLinks onServiceRequest(
061: String serviceID) {
062: if (this .serviceID.equals(serviceID)) {
063: // got expected service ID, return connection to this service
064: return con;
065: } else {
066: return null;
067: }
068: }
069:
070: public void onLinksPassedToClient(
071: SystemServiceConnectionLinks links) {
072: linksPassed = true;
073: }
074: }
075:
076: class ServiceRequestHandlerThread extends Thread {
077: // Links used for requesting service
078: SystemServiceConnectionLinks requestLinks = null;
079:
080: // protocol for performing request
081: SystemServiceRequestProtocolAMS protocol = null;
082:
083: Object result = null;
084: boolean done = false;
085:
086: ServiceRequestHandlerThread(
087: SystemServiceConnectionLinks requestLinks,
088: SystemServiceRequestListener requestListener) {
089:
090: this .requestLinks = requestLinks;
091: this .protocol = new SystemServiceRequestProtocolAMS(
092: requestListener);
093:
094: this .start();
095: }
096:
097: Object await() {
098: try {
099: synchronized (this ) {
100: while (!done) {
101: wait();
102: }
103: }
104: } catch (InterruptedException ignore) {
105: }
106:
107: return result;
108: }
109:
110: public void run() {
111: try {
112: // wait for request
113: Link receiveLink = requestLinks.getReceiveLink();
114: LinkMessage msg = receiveLink.receive();
115:
116: // handle request
117: protocol.handleServiceRequest(requestLinks);
118: } catch (Throwable t) {
119: result = t;
120: }
121:
122: synchronized (this ) {
123: done = true;
124: notifyAll();
125: }
126: }
127: }
128:
129: /**
130: * Tests setting and getting of actual data.
131: */
132: void testActual() throws InterruptedIOException, IOException {
133: String serviceID = "Test service";
134: Isolate is = Isolate.currentIsolate();
135:
136: Link reqC2SLink = Link.newLink(is, is);
137: Link reqS2CLink = Link.newLink(is, is);
138: SystemServiceConnectionLinks requestLinksAMS = new SystemServiceConnectionLinks(
139: reqS2CLink, reqC2SLink);
140: SystemServiceConnectionLinks requestLinksClient = new SystemServiceConnectionLinks(
141: reqC2SLink, reqS2CLink);
142:
143: // precreated connection between service and client
144: Link conSendLink = Link.newLink(is, is);
145: Link conRecLink = Link.newLink(is, is);
146: SystemServiceConnectionLinks links = new SystemServiceConnectionLinks(
147: conRecLink, conSendLink);
148:
149: // start listening for requests
150: SystemServiceRequestListenerImpl l = new SystemServiceRequestListenerImpl(
151: serviceID, links);
152: ServiceRequestHandlerThread sp = new ServiceRequestHandlerThread(
153: requestLinksAMS, l);
154:
155: // request service
156: SystemServiceRequestProtocolClient protocol = new SystemServiceRequestProtocolClient(
157: requestLinksClient);
158: LinkMessage emptyMsg = LinkMessage.newStringMessage("");
159: reqC2SLink.send(emptyMsg);
160: protocol.requestService(serviceID);
161:
162: // check that service request went without errors
163: Object result = sp.await();
164: assertTrue("links sent without error", result == null);
165: assertTrue("links received without error",
166: l.linksPassed == true);
167:
168: // check that connection returned as result of request
169: // is expected one
170: SystemServiceConnectionLinks con = protocol
171: .getSystemServiceConnectionLinks();
172: assertNotSame("link not same", conSendLink, con.getSendLink());
173: assertTrue("links equal", conSendLink.equals(con.getSendLink()));
174: assertNotSame("link not same", conRecLink, con.getReceiveLink());
175: assertTrue("links equal", conRecLink.equals(con
176: .getReceiveLink()));
177: }
178:
179: /**
180: * Runs all tests.
181: */
182: public void runTests() throws InterruptedIOException, IOException {
183: declare("testActual");
184: testActual();
185: }
186: }
|