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: import java.util.*;
032: import com.sun.cldc.isolate.*;
033: import com.sun.midp.i3test.TestCase;
034:
035: /**
036: * Tests for system service requesting functionality
037: */
038: public class TestSystemServiceRequest extends TestCase {
039: public final static String SERVICE_ID = "42";
040:
041: class DummySystemServiceManager extends SystemServiceManager {
042: SystemService service = null;
043:
044: DummySystemServiceManager() {
045: }
046:
047: public void registerService(SystemService service) {
048: this .service = service;
049: }
050:
051: public SystemService getService(String serviceID) {
052: if (service.getServiceID().equals(serviceID)) {
053: return service;
054: } else {
055: return null;
056: }
057: }
058:
059: public void shutdown() {
060: }
061: }
062:
063: class DummySystemService implements SystemService {
064: boolean wasRequested = false;
065:
066: public String getServiceID() {
067: return SERVICE_ID;
068: }
069:
070: public void start() {
071: }
072:
073: public void stop() {
074: }
075:
076: public void acceptConnection(SystemServiceConnection connection) {
077: wasRequested = true;
078: }
079: }
080:
081: void testRemote() throws IsolateStartupException,
082: InterruptedIOException, IOException, ClosedLinkException {
083:
084: SystemServiceManager serviceManager = new DummySystemServiceManager();
085: DummySystemService service = new DummySystemService();
086: serviceManager.registerService(service);
087:
088: SystemServiceRequestHandler requestHandler = new SystemServiceRequestHandler(
089: serviceManager);
090:
091: Isolate serviceIsolate = Isolate.currentIsolate();
092: Isolate clientIsolate = new Isolate(
093: "com.sun.midp.services.SystemServiceRequestIsolate",
094: null);
095: clientIsolate.start();
096:
097: IsolateSystemServiceRequestHandler isolateRequestHandler = requestHandler
098: .newIsolateRequestHandler(clientIsolate);
099:
100: Link namedPortalLink = Link.newLink(serviceIsolate,
101: clientIsolate);
102: Link[] clientLinks = { namedPortalLink };
103: LinkPortal.setLinks(clientIsolate, clientLinks);
104: NamedLinkPortal.sendLinks(namedPortalLink);
105:
106: requestHandler.handleIsolateRequests(isolateRequestHandler);
107:
108: clientIsolate.waitForExit();
109:
110: assertTrue("Service requested", service.wasRequested);
111: }
112:
113: void testLocal() {
114: SystemServiceManager serviceManager = new DummySystemServiceManager();
115: DummySystemService service = new DummySystemService();
116: serviceManager.registerService(service);
117:
118: SystemServiceRequestorLocal serviceRequestor = new SystemServiceRequestorLocal(
119: serviceManager);
120: serviceRequestor.requestService(SERVICE_ID);
121:
122: assertTrue("Service requested", service.wasRequested);
123:
124: }
125:
126: /**
127: * Runs all tests.
128: */
129: public void runTests() throws InterruptedIOException,
130: IsolateStartupException, ClosedLinkException, IOException {
131:
132: declare("testRemote");
133: testRemote();
134:
135: declare("testLocal");
136: testLocal();
137: }
138: }
|