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.i3test;
028:
029: import com.sun.midp.security.SecurityToken;
030: import com.sun.midp.security.ImplicitlyTrustedClass;
031: import com.sun.midp.security.SecurityInitializer;
032:
033: class SelfTest {
034:
035: static final String pkgname = "com.sun.midp.i3test";
036:
037: static int internalErrorCount = 0;
038: static int selfTestCount = 0;
039: static String errorLog = "";
040:
041: static void check(String label, int eTotalCases, int eTotalTests,
042: int eTotalAsserts, int eTotalFailures,
043: int eErrorClassNotFound, int eErrorConstructorException,
044: int eErrorNotTestCase, int eErrorTestRunThrows,
045: int eErrorNoTests, int eErrorAssertWithoutTest,
046: int eErrorTestWithoutAssert) {
047: String buf = "";
048:
049: if (eTotalCases != TestCase.totalCases) {
050: buf += " totalCases: expected " + eTotalCases + ", got "
051: + TestCase.totalCases + "\n";
052: }
053:
054: if (eTotalTests != TestCase.totalTests) {
055: buf += " totalTests: expected " + eTotalTests + ", got "
056: + TestCase.totalTests + "\n";
057: }
058:
059: if (eTotalAsserts != TestCase.totalAsserts) {
060: buf += " totalAsserts: expected " + eTotalAsserts
061: + ", got " + TestCase.totalAsserts + "\n";
062: }
063:
064: if (eTotalFailures != TestCase.totalFailures) {
065: buf += " totalFailures: expected " + eTotalFailures
066: + ", got " + TestCase.totalFailures + "\n";
067: }
068:
069: if (eErrorClassNotFound != TestCase.errorClassNotFound) {
070: buf += " errorClassNotFound: expected "
071: + eErrorClassNotFound + ", got "
072: + TestCase.errorClassNotFound + "\n";
073: }
074:
075: if (eErrorConstructorException != TestCase.errorConstructorException) {
076: buf += " errorConstructorException: expected "
077: + eErrorConstructorException + ", got "
078: + TestCase.errorConstructorException + "\n";
079: }
080:
081: if (eErrorNotTestCase != TestCase.errorNotTestCase) {
082: buf += " errorNotTestCase: expected " + eErrorNotTestCase
083: + ", got " + TestCase.errorNotTestCase + "\n";
084: }
085:
086: if (eErrorTestRunThrows != TestCase.errorTestRunThrows) {
087: buf += " errorTestRunThrows: expected "
088: + eErrorTestRunThrows + ", got "
089: + TestCase.errorTestRunThrows + "\n";
090: }
091:
092: if (eErrorNoTests != TestCase.errorNoTests) {
093: buf += " errorNoTests: expected " + eErrorNoTests
094: + ", got " + TestCase.errorNoTests + "\n";
095: }
096:
097: if (eErrorAssertWithoutTest != TestCase.errorAssertWithoutTest) {
098: buf += " errorAssertWithoutTest: expected "
099: + eErrorAssertWithoutTest + ", got "
100: + TestCase.errorAssertWithoutTest + "\n";
101: }
102:
103: if (eErrorTestWithoutAssert != TestCase.errorTestWithoutAssert) {
104: buf += " errorTestWithoutAssert: expected "
105: + eErrorTestWithoutAssert + ", got "
106: + TestCase.errorTestWithoutAssert + "\n";
107: }
108:
109: if (!"".equals(buf)) {
110: internalErrorCount++;
111: errorLog += "INTERNAL ERROR in " + label + "\n" + buf;
112: }
113: }
114:
115: static void runOneTest(String clname) {
116: TestCase.reset();
117: TestCase.runTestCase(pkgname + "." + clname);
118: selfTestCount++;
119: }
120:
121: static void run() {
122: System.out.println();
123: System.out
124: .println("Starting self test. Ignore messages below.");
125: System.out.println();
126: System.out.println("========================================");
127: System.out.println();
128:
129: runOneTest("xyzzy.plugh");
130: check("NotFound", 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0);
131:
132: runOneTest("SelfTest$BadConstructor");
133: check("ConstructorException", 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0);
134:
135: runOneTest("SelfTest$NotTestCase");
136: check("NotTestCase", 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0);
137:
138: runOneTest("SelfTest$TestRunThrows1");
139: check("TestRunThrows1", 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0);
140:
141: runOneTest("SelfTest$TestRunThrows2");
142: check("TestRunThrows2", 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1);
143:
144: runOneTest("SelfTest$NoTests");
145: check("NoTests", 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0);
146:
147: runOneTest("SelfTest$AssertWithoutTest");
148: check("AssertWithoutTest", 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0);
149:
150: runOneTest("SelfTest$TestWithoutAssert1");
151: check("TestWithoutAssert1", 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1);
152:
153: runOneTest("SelfTest$TestWithoutAssert2");
154: check("TestWithoutAssert2", 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 1);
155:
156: runOneTest("SelfTest$TestWithoutAssert3");
157: check("TestWithoutAssert3", 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 1);
158:
159: runOneTest("SelfTest$TestAllSuccess");
160: check("TestAllSuccess", 1, 1, 24, 0, 0, 0, 0, 0, 0, 0, 0);
161:
162: runOneTest("SelfTest$TestAllFail");
163: check("TestAllFail", 1, 1, 24, 24, 0, 0, 0, 0, 0, 0, 0);
164:
165: runOneTest("SelfTest$TestRequestSecurityToken");
166: check("TestRequestSecurityToken", 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
167: 0);
168:
169: System.out.println();
170: System.out.println("========================================");
171: System.out.println();
172: if (!"".equals(errorLog)) {
173: System.out.print(errorLog);
174: }
175: System.out.println();
176: System.out.println("Self test run complete: " + selfTestCount
177: + " test(s), " + internalErrorCount + " error(s).");
178: }
179:
180: // the actual self-test cases
181:
182: static class BadConstructor extends TestCase {
183: BadConstructor() {
184: throw new RuntimeException("foo");
185: }
186:
187: public void runTests() {
188: }
189: }
190:
191: static class NotTestCase {
192: }
193:
194: static class TestRunThrows1 extends TestCase {
195: public void runTests() {
196: throw new RuntimeException("bar");
197: }
198: }
199:
200: static class TestRunThrows2 extends TestCase {
201: public void runTests() {
202: declare("bazz");
203: throw new RuntimeException("bletch");
204: }
205: }
206:
207: static class NoTests extends TestCase {
208: public void runTests() {
209: }
210: }
211:
212: static class AssertWithoutTest extends TestCase {
213: public void runTests() {
214: assertTrue(true);
215: }
216: }
217:
218: static class TestWithoutAssert1 extends TestCase {
219: public void runTests() {
220: declare("noAssertsFollow");
221: }
222: }
223:
224: static class TestWithoutAssert2 extends TestCase {
225: public void runTests() {
226: declare("test1");
227: declare("test2");
228: assertTrue(true);
229: }
230: }
231:
232: static class TestWithoutAssert3 extends TestCase {
233: public void runTests() {
234: declare("test1");
235: assertTrue(true);
236: declare("test2");
237: }
238: }
239:
240: static class TestAllSuccess extends TestCase {
241: public void runTests() {
242: declare("AllSuccess");
243: assertEquals(new Integer(5), new Integer(5));
244: assertEquals("this should succeed", new Integer(5),
245: new Integer(5));
246: assertEquals(null, null);
247: assertEquals("this should succeed", null, null);
248: Integer iobj = new Integer(7);
249: assertEquals(iobj, iobj);
250: assertEquals("this should succeed", iobj, iobj);
251: assertEquals(17, 17);
252: assertEquals("this should succeed", 18, 18);
253: assertFalse(false);
254: assertFalse("this should succeed", false);
255: assertNotNull(this );
256: assertNotNull("this should succeed", this );
257: assertNotSame(new Integer(8), new Integer(8));
258: assertNotSame("this should succeed", new Integer(9),
259: new Integer(9));
260: assertNotSame(new Integer(10), null);
261: assertNotSame("this should succeed", new Integer(10), null);
262: assertNotSame(null, new Integer(11));
263: assertNotSame("this should succeed", null, new Integer(11));
264: assertNull(null);
265: assertNull("this should succeed", null);
266: assertSame(this , this );
267: assertSame("this should succeed", this , this );
268: assertTrue(true);
269: assertTrue("this should succeed", true);
270: }
271: }
272:
273: static class TestAllFail extends TestCase {
274: public void runTests() {
275: declare("AllFail");
276: assertEquals(null, new Integer(1));
277: assertEquals("this should fail", null, new Integer(2));
278: assertEquals(new Integer(3), null);
279: assertEquals("this should fail", new Integer(4), null);
280: assertEquals(new Integer(5), new Integer(6));
281: assertEquals("this should fail", new Integer(5),
282: new Integer(6));
283: assertEquals(1, 2);
284: assertEquals("this should fail", 3, 4);
285: assertFalse(true);
286: assertFalse("this should fail", true);
287: assertNotNull(null);
288: assertNotNull("this should fail", null);
289: Integer iobj = new Integer(7);
290: assertNotSame(iobj, iobj);
291: assertNotSame("this should fail", iobj, iobj);
292: assertNotSame(null, null);
293: assertNotSame("this should fail", null, null);
294: assertNull(this );
295: assertNull("this should fail", this );
296: assertSame(new Integer(5), this );
297: assertSame("this should fail", new Integer(5), this );
298: assertTrue(false);
299: assertTrue("this should fail", false);
300: fail();
301: fail("this should fail");
302: }
303: }
304:
305: static class TestRequestSecurityToken extends TestCase {
306: /**
307: * Inner class to request security token from SecurityInitializer.
308: * SecurityInitializer should be able to check this inner class name.
309: */
310: static private class SecurityTrusted implements
311: ImplicitlyTrustedClass {
312: };
313:
314: public void runTests() {
315: declare("RequestSecurityToken");
316: SecurityToken tok = SecurityInitializer
317: .requestToken(new SecurityTrusted());
318: assertNotNull("token is null", tok);
319: }
320: }
321: }
|