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.security.*;
030: import com.sun.midp.links.*;
031: import java.io.*;
032: import java.util.*;
033: import com.sun.cldc.isolate.*;
034: import com.sun.midp.i3test.TestCase;
035:
036: /**
037: * Tests for system service requesting functionality
038: */
039: public class TestSystemService extends TestCase {
040: public final static String SERVICE_ID = "42";
041:
042: private static SecurityToken token = SecurityTokenProvider
043: .getToken();
044:
045: class SimpleSystemService implements SystemService, Runnable {
046: private final static String testString = "just a test string";
047:
048: Thread serviceThread = null;
049: SystemServiceConnection con = null;
050: boolean stringsMatch = false;
051:
052: public String getServiceID() {
053: return SERVICE_ID;
054: }
055:
056: public void start() {
057: }
058:
059: public void stop() {
060: try {
061: serviceThread.join();
062: } catch (InterruptedException e) {
063: }
064: }
065:
066: public void acceptConnection(SystemServiceConnection con) {
067: this .con = con;
068:
069: serviceThread = new Thread(this );
070: serviceThread.start();
071: }
072:
073: public void run() {
074: try {
075: // send test string to client
076: SystemServiceMessage msg = SystemServiceMessage
077: .newMessage();
078: msg.getDataOutput().writeUTF(testString);
079: con.send(msg);
080:
081: // get a response string from client
082: msg = con.receive();
083: String responseString = msg.getDataInput().readUTF();
084:
085: // compare strings
086: stringsMatch = testString.toUpperCase().equals(
087: responseString);
088: } catch (Throwable t) {
089: }
090: }
091: }
092:
093: void testRemote() throws IsolateStartupException,
094: InterruptedIOException, IOException, ClosedLinkException {
095:
096: SystemServiceManager manager = SystemServiceManager
097: .getInstance(token);
098: SimpleSystemService service = new SimpleSystemService();
099: manager.registerService(service);
100:
101: SystemServiceRequestHandler requestHandler = new SystemServiceRequestHandler(
102: manager);
103:
104: Isolate serviceIsolate = Isolate.currentIsolate();
105: Isolate clientIsolate = new Isolate(
106: "com.sun.midp.services.SystemServiceIsolate", null);
107: clientIsolate.start();
108:
109: IsolateSystemServiceRequestHandler isolateRequestHandler = requestHandler
110: .newIsolateRequestHandler(clientIsolate);
111:
112: Link namedPortalLink = Link.newLink(serviceIsolate,
113: clientIsolate);
114: Link[] clientLinks = { namedPortalLink };
115: LinkPortal.setLinks(clientIsolate, clientLinks);
116: NamedLinkPortal.sendLinks(namedPortalLink);
117:
118: requestHandler.handleIsolateRequests(isolateRequestHandler);
119:
120: clientIsolate.waitForExit();
121:
122: manager.shutdown();
123:
124: assertTrue("Strings match", service.stringsMatch);
125: }
126:
127: void testLocal() {
128: SystemServiceManager manager = SystemServiceManager
129: .getInstance(token);
130: SimpleSystemService service = new SimpleSystemService();
131: manager.registerService(service);
132:
133: SystemServiceRequestor serviceRequestor = SystemServiceRequestor
134: .getInstance(token);
135:
136: SystemServiceConnection con = null;
137: con = serviceRequestor
138: .requestService(TestSystemService.SERVICE_ID);
139:
140: try {
141: // receive string from service
142: SystemServiceMessage msg = con.receive();
143: String testString = msg.getDataInput().readUTF();
144:
145: // convert string to upper case and sent it back to service
146: msg = SystemServiceMessage.newMessage();
147: msg.getDataOutput().writeUTF(testString.toUpperCase());
148: con.send(msg);
149: } catch (Throwable t) {
150: System.err.println("Exception: " + t);
151: }
152:
153: manager.shutdown();
154:
155: assertTrue("Strings match", service.stringsMatch);
156: }
157:
158: /**
159: * Runs all tests.
160: */
161: public void runTests() throws InterruptedIOException,
162: IsolateStartupException, ClosedLinkException, IOException {
163:
164: declare("testRemote");
165: testRemote();
166:
167: declare("testLocal");
168: testLocal();
169: }
170: }
|