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: package com.sun.midp.io.j2me.jcrmi;
027:
028: import javax.microedition.jcrmi.*;
029: import com.sun.midp.i3test.TestCase;
030:
031: import javax.microedition.jcrmi.JavaCardRMIConnection;
032: import java.rmi.RemoteException;
033:
034: import com.sun.cardreader.CardDeviceException;
035: import com.sun.cardreader.SlotFactory;
036:
037: /**
038: * This test class verifies Java Card RMI Client API.
039: */
040: public class DataTest extends TestCase {
041:
042: byte[] byteValues = { -128, -65, -64, -1, 0, 1, 88, 127 };
043: boolean[] booleanValues = { true, false, true };
044: short[] shortValues = { -32768, -4097, -4096, -256, -128, -1, 0, 1,
045: 255, 256, 32767 };
046: String appletURL = "jcrmi:0;AID=a0.0.0.0.62.bb.bb.bb.bb";
047:
048: /**
049: * Tests data transfer.
050: */
051: public void testData() throws java.io.IOException,
052: CardDeviceException {
053: JavaCardRMIConnection con = null;
054: Remote1 rem;
055: boolean stub_flag = false;
056:
057: try {
058: SlotFactory.init();
059: } catch (CardDeviceException e) {
060: if (e.getMessage().equals("stub")) {
061: stub_flag = true;
062: } else {
063: throw e;
064: }
065: }
066:
067: if (!stub_flag) {
068: con = (JavaCardRMIConnection) new com.sun.midp.io.j2me.jcrmi.Protocol();
069: ((com.sun.midp.io.j2me.jcrmi.Protocol) con).openPrim(
070: appletURL, 0, false);
071: rem = (Remote1) con.getInitialReference();
072: assertNotNull("Initial reference", rem);
073: rem.voidMethod();
074: assertTrue("void method", true);
075: byte[] b = rem.getByteArray(byteValues);
076: if (b == null || b.length != byteValues.length) {
077: assertTrue("byte[] method(byte[]) bad return value",
078: false);
079: } else {
080: for (int i = 0; i < b.length; i++) {
081: if (b[i] != byteValues[i]) {
082: assertEquals("byte array", b[i], byteValues[i]);
083: break;
084: }
085: }
086: assertTrue("byte[] method(byte[])", true);
087: }
088:
089: short[] s = rem.getShortArray(shortValues);
090: if (s == null || s.length != shortValues.length) {
091: assertTrue("short[] method(short[]) bad return value",
092: false);
093: } else {
094: for (int i = 0; i < s.length; i++) {
095: if (s[i] != shortValues[i]) {
096: assertEquals("short array", s[i],
097: shortValues[i]);
098: break;
099: }
100: }
101: assertTrue("short[] method(short[])", true);
102: }
103:
104: boolean[] l = rem.getBooleanArray(booleanValues);
105: if (l == null || l.length != booleanValues.length) {
106: assertTrue(
107: "boolean[] method(boolean[]) bad return value",
108: false);
109: } else {
110: for (int i = 0; i < l.length; i++) {
111: if (l[i] != booleanValues[i]) {
112: assertTrue("boolean array",
113: (l[i] == booleanValues[i]));
114: break;
115: }
116: }
117: assertTrue("boolean[] method(boolean[])", true);
118: }
119:
120: con.close();
121: }
122: assertTrue(true);
123: }
124:
125: /**
126: * Tests exception handling.
127: */
128: public void testException() throws java.io.IOException,
129: CardDeviceException {
130: JavaCardRMIConnection con = null;
131: Remote1 rem;
132: boolean stub_flag = false;
133:
134: try {
135: SlotFactory.init();
136: } catch (CardDeviceException e) {
137: if (e.getMessage().equals("stub")) {
138: stub_flag = true;
139: } else {
140: throw e;
141: }
142: }
143:
144: if (!stub_flag) {
145: con = (JavaCardRMIConnection) new com.sun.midp.io.j2me.jcrmi.Protocol();
146: ((com.sun.midp.io.j2me.jcrmi.Protocol) con).openPrim(
147: appletURL, 0, false);
148: rem = (Remote1) con.getInitialReference();
149: assertNotNull("Initial reference", rem);
150:
151: try {
152: rem.throwException((short) 12);
153: } catch (java.rmi.RemoteException e) {
154: assertTrue("proper exception", true);
155: } catch (Throwable e1) {
156: assertTrue("improper exception: " + e1, false);
157: }
158:
159: try {
160: rem.throwException((short) 22);
161: } catch (javacard.framework.service.ServiceException e) {
162: assertTrue("proper exception", true);
163: } catch (Throwable e1) {
164: assertTrue("improper exception: " + e1, false);
165: }
166:
167: try {
168: rem.throwException((short) 11);
169: } catch (java.io.IOException e) {
170: assertTrue("proper exception", true);
171: } catch (Throwable e1) {
172: assertTrue("improper exception: " + e1, false);
173: }
174:
175: try {
176: rem.throwSubclass((short) 11);
177: } catch (java.io.IOException e) {
178: assertTrue("proper exception", true);
179: } catch (Throwable e1) {
180: assertTrue("improper exception: " + e1, false);
181: }
182:
183: con.close();
184: } else {
185: assertTrue(true);
186: }
187: }
188:
189: /**
190: * Run tests.
191: */
192: public void runTests() {
193: try {
194: declare("testData");
195: testData();
196:
197: declare("testException");
198: testException();
199:
200: } catch (Throwable t) {
201: fail("" + t);
202: }
203: }
204: }
|