0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package org.apache.harmony.luni.tests.java.lang;
0019:
0020: import java.lang.Thread.UncaughtExceptionHandler;
0021: import java.security.Permission;
0022: import java.util.Map;
0023:
0024: public class ThreadTest extends junit.framework.TestCase {
0025:
0026: static class SimpleThread implements Runnable {
0027: int delay;
0028:
0029: public void run() {
0030: try {
0031: synchronized (this ) {
0032: this .notify();
0033: this .wait(delay);
0034: }
0035: } catch (InterruptedException e) {
0036: return;
0037: }
0038:
0039: }
0040:
0041: public SimpleThread(int d) {
0042: if (d >= 0)
0043: delay = d;
0044: }
0045: }
0046:
0047: static class YieldThread implements Runnable {
0048: volatile int delay;
0049:
0050: public void run() {
0051: int x = 0;
0052: while (true) {
0053: ++x;
0054: }
0055: }
0056:
0057: public YieldThread(int d) {
0058: if (d >= 0)
0059: delay = d;
0060: }
0061: }
0062:
0063: static class ResSupThread implements Runnable {
0064: Thread parent;
0065:
0066: volatile int checkVal = -1;
0067:
0068: public void run() {
0069: try {
0070: synchronized (this ) {
0071: this .notify();
0072: }
0073: while (true) {
0074: checkVal++;
0075: zz();
0076: Thread.sleep(100);
0077: }
0078: } catch (InterruptedException e) {
0079: return;
0080: } catch (BogusException e) {
0081: try {
0082: // Give parent a chance to sleep
0083: Thread.sleep(500);
0084: } catch (InterruptedException x) {
0085: }
0086: parent.interrupt();
0087: while (!Thread.currentThread().isInterrupted()) {
0088: // Don't hog the CPU
0089: try {
0090: Thread.sleep(50);
0091: } catch (InterruptedException x) {
0092: // This is what we've been waiting for...don't throw it
0093: // away!
0094: break;
0095: }
0096: }
0097: }
0098: }
0099:
0100: public void zz() throws BogusException {
0101: }
0102:
0103: public ResSupThread(Thread t) {
0104: parent = t;
0105: }
0106:
0107: public synchronized int getCheckVal() {
0108: return checkVal;
0109: }
0110: }
0111:
0112: static class BogusException extends Throwable {
0113:
0114: private static final long serialVersionUID = 1L;
0115:
0116: public BogusException(String s) {
0117: super (s);
0118: }
0119: }
0120:
0121: Thread st, ct, spinner;
0122:
0123: static boolean calledMySecurityManager = false;
0124:
0125: /**
0126: * @tests java.lang.Thread#Thread()
0127: */
0128: public void test_Constructor() {
0129: // Test for method java.lang.Thread()
0130:
0131: Thread t;
0132: SecurityManager m = new SecurityManager() {
0133: @Override
0134: public ThreadGroup getThreadGroup() {
0135: calledMySecurityManager = true;
0136: return Thread.currentThread().getThreadGroup();
0137: }
0138:
0139: @Override
0140: public void checkPermission(Permission permission) {
0141: if (permission.getName().equals("setSecurityManager")) {
0142: return;
0143: }
0144: super .checkPermission(permission);
0145: }
0146: };
0147: try {
0148: // To see if it checks Thread creation with our SecurityManager
0149: System.setSecurityManager(m);
0150: t = new Thread();
0151: } finally {
0152: // restore original, no side-effects
0153: System.setSecurityManager(null);
0154: }
0155: assertTrue("Did not call SecurityManager.getThreadGroup ()",
0156: calledMySecurityManager);
0157: t.start();
0158: }
0159:
0160: /**
0161: * @tests java.lang.Thread#Thread(java.lang.Runnable)
0162: */
0163: public void test_ConstructorLjava_lang_Runnable() {
0164: // Test for method java.lang.Thread(java.lang.Runnable)
0165: ct = new Thread(new SimpleThread(10));
0166: ct.start();
0167: }
0168:
0169: /**
0170: * @tests java.lang.Thread#Thread(java.lang.Runnable, java.lang.String)
0171: */
0172: public void test_ConstructorLjava_lang_RunnableLjava_lang_String() {
0173: // Test for method java.lang.Thread(java.lang.Runnable,
0174: // java.lang.String)
0175: Thread st1 = new Thread(new SimpleThread(1), "SimpleThread1");
0176: assertEquals("Constructed thread with incorrect thread name",
0177: "SimpleThread1", st1.getName());
0178: st1.start();
0179: }
0180:
0181: /**
0182: * @tests java.lang.Thread#Thread(java.lang.String)
0183: */
0184: public void test_ConstructorLjava_lang_String() {
0185: // Test for method java.lang.Thread(java.lang.String)
0186: Thread t = new Thread("Testing");
0187: assertEquals("Created tread with incorrect name", "Testing", t
0188: .getName());
0189: t.start();
0190: }
0191:
0192: /**
0193: * @tests java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable)
0194: */
0195: public void test_ConstructorLjava_lang_ThreadGroupLjava_lang_Runnable() {
0196: // Test for method java.lang.Thread(java.lang.ThreadGroup,
0197: // java.lang.Runnable)
0198: ThreadGroup tg = new ThreadGroup("Test Group1");
0199: st = new Thread(tg, new SimpleThread(1), "SimpleThread2");
0200: assertTrue("Returned incorrect thread group", st
0201: .getThreadGroup() == tg);
0202: st.start();
0203: try {
0204: st.join();
0205: } catch (InterruptedException e) {
0206: }
0207: tg.destroy();
0208: }
0209:
0210: /**
0211: * @tests java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable,
0212: * java.lang.String)
0213: */
0214: public void test_ConstructorLjava_lang_ThreadGroupLjava_lang_RunnableLjava_lang_String() {
0215: // Test for method java.lang.Thread(java.lang.ThreadGroup,
0216: // java.lang.Runnable, java.lang.String)
0217: ThreadGroup tg = new ThreadGroup("Test Group2");
0218: st = new Thread(tg, new SimpleThread(1), "SimpleThread3");
0219: assertTrue("Constructed incorrect thread",
0220: (st.getThreadGroup() == tg)
0221: && st.getName().equals("SimpleThread3"));
0222: st.start();
0223: try {
0224: st.join();
0225: } catch (InterruptedException e) {
0226: }
0227: tg.destroy();
0228:
0229: Runnable r = new Runnable() {
0230: public void run() {
0231: }
0232: };
0233:
0234: ThreadGroup foo = null;
0235: try {
0236: new Thread(foo = new ThreadGroup("foo"), r, null);
0237: // Should not get here
0238: fail("Null cannot be accepted as Thread name");
0239: } catch (NullPointerException npe) {
0240: assertTrue("Null cannot be accepted as Thread name", true);
0241: foo.destroy();
0242: }
0243:
0244: }
0245:
0246: /**
0247: * @tests java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.String)
0248: */
0249: public void test_ConstructorLjava_lang_ThreadGroupLjava_lang_String() {
0250: // Test for method java.lang.Thread(java.lang.ThreadGroup,
0251: // java.lang.String)
0252: st = new Thread(new SimpleThread(1), "SimpleThread4");
0253: assertEquals("Returned incorrect thread name", "SimpleThread4",
0254: st.getName());
0255: st.start();
0256: }
0257:
0258: /**
0259: * @tests java.lang.Thread#activeCount()
0260: */
0261: public void test_activeCount() {
0262: // Test for method int java.lang.Thread.activeCount()
0263: Thread t = new Thread(new SimpleThread(10));
0264: int active = 0;
0265: synchronized (t) {
0266: t.start();
0267: active = Thread.activeCount();
0268: }
0269: assertTrue(
0270: "Incorrect activeCount for current group: " + active,
0271: active > 1);
0272: try {
0273: t.join();
0274: } catch (InterruptedException e) {
0275: }
0276: }
0277:
0278: /**
0279: * @tests java.lang.Thread#checkAccess()
0280: */
0281: public void test_checkAccess() {
0282: // Test for method void java.lang.Thread.checkAccess()
0283: ThreadGroup tg = new ThreadGroup("Test Group3");
0284: try {
0285: st = new Thread(tg, new SimpleThread(1), "SimpleThread5");
0286: st.checkAccess();
0287: assertTrue("CheckAccess passed", true);
0288: } catch (SecurityException e) {
0289: fail("CheckAccess failed : " + e.getMessage());
0290: }
0291: st.start();
0292: try {
0293: st.join();
0294: } catch (InterruptedException e) {
0295: }
0296: tg.destroy();
0297: }
0298:
0299: /**
0300: * @tests java.lang.Thread#countStackFrames()
0301: */
0302: @SuppressWarnings("deprecation")
0303: public void test_countStackFrames() {
0304: /*
0305: * Thread.countStackFrames() is unpredictable, so we just test that it
0306: * doesn't throw an exception.
0307: */
0308: Thread.currentThread().countStackFrames();
0309: }
0310:
0311: /**
0312: * @tests java.lang.Thread#currentThread()
0313: */
0314: public void test_currentThread() {
0315: assertNotNull(Thread.currentThread());
0316: }
0317:
0318: /**
0319: * @tests java.lang.Thread#destroy()
0320: */
0321: @SuppressWarnings("deprecation")
0322: public void test_destroy() {
0323: try {
0324: new Thread().destroy();
0325: // FIXME uncomment when IBM VME is updated
0326: //fail("NoSuchMethodError was not thrown");
0327: } catch (NoSuchMethodError e) {
0328: }
0329: }
0330:
0331: /**
0332: * @tests java.lang.Thread#enumerate(java.lang.Thread[])
0333: */
0334: public void test_enumerate$Ljava_lang_Thread() {
0335: // Test for method int java.lang.Thread.enumerate(java.lang.Thread [])
0336: // The test has been updated according to HARMONY-1974 JIRA issue.
0337:
0338: class MyThread extends Thread {
0339: MyThread(ThreadGroup tg, String name) {
0340: super (tg, name);
0341: }
0342:
0343: boolean failed = false;
0344: String failMessage = null;
0345:
0346: public void run() {
0347: SimpleThread st1 = null;
0348: SimpleThread st2 = null;
0349: ThreadGroup mytg = null;
0350: Thread firstOne = null;
0351: Thread secondOne = null;
0352: try {
0353: int arrayLength = 10;
0354: Thread[] tarray = new Thread[arrayLength];
0355: st1 = new SimpleThread(-1);
0356: st2 = new SimpleThread(-1);
0357: mytg = new ThreadGroup("jp");
0358: firstOne = new Thread(mytg, st1, "firstOne2");
0359: secondOne = new Thread(mytg, st2, "secondOne1");
0360: int count = Thread.enumerate(tarray);
0361: assertEquals("Incorrect value returned1", 1, count);
0362: synchronized (st1) {
0363: firstOne.start();
0364: try {
0365: st1.wait();
0366: } catch (InterruptedException e) {
0367: }
0368: }
0369: count = Thread.enumerate(tarray);
0370: assertEquals("Incorrect value returned2", 2, count);
0371: synchronized (st2) {
0372: secondOne.start();
0373: try {
0374: st2.wait();
0375: } catch (InterruptedException e) {
0376: }
0377: }
0378: count = Thread.enumerate(tarray);
0379: assertEquals("Incorrect value returned3", 3, count);
0380: } catch (junit.framework.AssertionFailedError e) {
0381: failed = true;
0382: failMessage = e.getMessage();
0383: } finally {
0384: synchronized (st1) {
0385: firstOne.interrupt();
0386: }
0387: synchronized (st2) {
0388: secondOne.interrupt();
0389: }
0390: try {
0391: firstOne.join();
0392: secondOne.join();
0393: } catch (InterruptedException e) {
0394: }
0395: mytg.destroy();
0396: }
0397: }
0398: }
0399: ;
0400:
0401: ThreadGroup tg = new ThreadGroup("tg");
0402: MyThread t = new MyThread(tg, "top");
0403: t.start();
0404: try {
0405: t.join();
0406: } catch (InterruptedException e) {
0407: fail("Unexpected interrupt");
0408: } finally {
0409: tg.destroy();
0410: }
0411: assertFalse(t.failMessage, t.failed);
0412: }
0413:
0414: /**
0415: * @tests java.lang.Thread#getContextClassLoader()
0416: */
0417: public void test_getContextClassLoader() {
0418: // Test for method java.lang.ClassLoader
0419: // java.lang.Thread.getContextClassLoader()
0420: Thread t = new Thread();
0421: assertTrue("Incorrect class loader returned", t
0422: .getContextClassLoader() == Thread.currentThread()
0423: .getContextClassLoader());
0424: t.start();
0425:
0426: }
0427:
0428: /**
0429: * @tests java.lang.Thread#getName()
0430: */
0431: public void test_getName() {
0432: // Test for method java.lang.String java.lang.Thread.getName()
0433: st = new Thread(new SimpleThread(1), "SimpleThread6");
0434: assertEquals("Returned incorrect thread name", "SimpleThread6",
0435: st.getName());
0436: st.start();
0437: }
0438:
0439: /**
0440: * @tests java.lang.Thread#getPriority()
0441: */
0442: public void test_getPriority() {
0443: // Test for method int java.lang.Thread.getPriority()
0444: st = new Thread(new SimpleThread(1));
0445: st.setPriority(Thread.MAX_PRIORITY);
0446: assertTrue("Returned incorrect thread priority", st
0447: .getPriority() == Thread.MAX_PRIORITY);
0448: st.start();
0449: }
0450:
0451: /**
0452: * @tests java.lang.Thread#getThreadGroup()
0453: */
0454: public void test_getThreadGroup() {
0455: // Test for method java.lang.ThreadGroup
0456: // java.lang.Thread.getThreadGroup()
0457: ThreadGroup tg = new ThreadGroup("Test Group4");
0458: st = new Thread(tg, new SimpleThread(1), "SimpleThread8");
0459: assertTrue("Returned incorrect thread group", st
0460: .getThreadGroup() == tg);
0461: st.start();
0462: try {
0463: st.join();
0464: } catch (InterruptedException e) {
0465: }
0466: assertNull("group should be null", st.getThreadGroup());
0467: assertNotNull("toString() should not be null", st.toString());
0468: tg.destroy();
0469:
0470: final Object lock = new Object();
0471: Thread t = new Thread() {
0472: @Override
0473: public void run() {
0474: synchronized (lock) {
0475: lock.notifyAll();
0476: }
0477: }
0478: };
0479: synchronized (lock) {
0480: t.start();
0481: try {
0482: lock.wait();
0483: } catch (InterruptedException e) {
0484: }
0485: }
0486: int running = 0;
0487: while (t.isAlive())
0488: running++;
0489: ThreadGroup group = t.getThreadGroup();
0490: assertNull("ThreadGroup is not null", group);
0491: }
0492:
0493: /**
0494: * @tests java.lang.Thread#interrupt()
0495: */
0496: public void test_interrupt() {
0497: // Test for method void java.lang.Thread.interrupt()
0498: final Object lock = new Object();
0499: class ChildThread1 extends Thread {
0500: Thread parent;
0501:
0502: boolean sync;
0503:
0504: @Override
0505: public void run() {
0506: if (sync) {
0507: synchronized (lock) {
0508: lock.notify();
0509: try {
0510: lock.wait();
0511: } catch (InterruptedException e) {
0512: }
0513: }
0514: }
0515: parent.interrupt();
0516: }
0517:
0518: public ChildThread1(Thread p, String name, boolean sync) {
0519: super (name);
0520: parent = p;
0521: this .sync = sync;
0522: }
0523: }
0524: boolean interrupted = false;
0525: try {
0526: ct = new ChildThread1(Thread.currentThread(),
0527: "Interrupt Test1", false);
0528: synchronized (lock) {
0529: ct.start();
0530: lock.wait();
0531: }
0532: } catch (InterruptedException e) {
0533: interrupted = true;
0534: }
0535: assertTrue("Failed to Interrupt thread1", interrupted);
0536:
0537: interrupted = false;
0538: try {
0539: ct = new ChildThread1(Thread.currentThread(),
0540: "Interrupt Test2", true);
0541: synchronized (lock) {
0542: ct.start();
0543: lock.wait();
0544: lock.notify();
0545: }
0546: Thread.sleep(20000);
0547: } catch (InterruptedException e) {
0548: interrupted = true;
0549: }
0550: assertTrue("Failed to Interrupt thread2", interrupted);
0551:
0552: }
0553:
0554: /**
0555: * @tests java.lang.Thread#interrupted()
0556: */
0557: public void test_interrupted() {
0558: assertFalse(
0559: "Interrupted returned true for non-interrupted thread",
0560: Thread.interrupted());
0561: Thread.currentThread().interrupt();
0562: assertTrue(
0563: "Interrupted returned true for non-interrupted thread",
0564: Thread.interrupted());
0565: assertFalse("Failed to clear interrupted flag", Thread
0566: .interrupted());
0567: }
0568:
0569: /**
0570: * @tests java.lang.Thread#isAlive()
0571: */
0572: public void test_isAlive() {
0573: // Test for method boolean java.lang.Thread.isAlive()
0574: SimpleThread simple;
0575: st = new Thread(simple = new SimpleThread(500));
0576: assertFalse("A thread that wasn't started is alive.", st
0577: .isAlive());
0578: synchronized (simple) {
0579: st.start();
0580: try {
0581: simple.wait();
0582: } catch (InterruptedException e) {
0583: }
0584: }
0585: assertTrue("Started thread returned false", st.isAlive());
0586: try {
0587: st.join();
0588: } catch (InterruptedException e) {
0589: fail("Thread did not die");
0590: }
0591: assertTrue("Stopped thread returned true", !st.isAlive());
0592: }
0593:
0594: /**
0595: * @tests java.lang.Thread#isDaemon()
0596: */
0597: public void test_isDaemon() {
0598: // Test for method boolean java.lang.Thread.isDaemon()
0599: st = new Thread(new SimpleThread(1), "SimpleThread10");
0600: assertTrue("Non-Daemon thread returned true", !st.isDaemon());
0601: st.setDaemon(true);
0602: assertTrue("Daemon thread returned false", st.isDaemon());
0603: st.start();
0604: }
0605:
0606: /**
0607: * @tests java.lang.Thread#isInterrupted()
0608: */
0609: public void test_isInterrupted() {
0610: // Test for method boolean java.lang.Thread.isInterrupted()
0611: class SpinThread implements Runnable {
0612: public volatile boolean done = false;
0613:
0614: public void run() {
0615: while (!Thread.currentThread().isInterrupted())
0616: ;
0617: while (!done)
0618: ;
0619: }
0620: }
0621:
0622: SpinThread spin = new SpinThread();
0623: spinner = new Thread(spin);
0624: spinner.start();
0625: Thread.yield();
0626: try {
0627: assertTrue("Non-Interrupted thread returned true", !spinner
0628: .isInterrupted());
0629: spinner.interrupt();
0630: assertTrue("Interrupted thread returned false", spinner
0631: .isInterrupted());
0632: spin.done = true;
0633: } finally {
0634: spinner.interrupt();
0635: spin.done = true;
0636: }
0637: }
0638:
0639: /**
0640: * @tests java.lang.Thread#join()
0641: */
0642: public void test_join() {
0643: // Test for method void java.lang.Thread.join()
0644: SimpleThread simple;
0645: try {
0646: st = new Thread(simple = new SimpleThread(100));
0647: // cause isAlive() to be compiled by the JIT, as it must be called
0648: // within 100ms below.
0649: assertTrue("Thread is alive", !st.isAlive());
0650: synchronized (simple) {
0651: st.start();
0652: simple.wait();
0653: }
0654: st.join();
0655: } catch (InterruptedException e) {
0656: fail("Join failed ");
0657: }
0658: assertTrue("Joined thread is still alive", !st.isAlive());
0659: boolean result = true;
0660: Thread th = new Thread("test");
0661: try {
0662: th.join();
0663: } catch (InterruptedException e) {
0664: result = false;
0665: }
0666: assertTrue("Hung joining a non-started thread", result);
0667: th.start();
0668: }
0669:
0670: /**
0671: * @tests java.lang.Thread#join(long)
0672: */
0673: public void test_joinJ() {
0674: // Test for method void java.lang.Thread.join(long)
0675: SimpleThread simple;
0676: try {
0677: st = new Thread(simple = new SimpleThread(1000),
0678: "SimpleThread12");
0679: // cause isAlive() to be compiled by the JIT, as it must be called
0680: // within 100ms below.
0681: assertTrue("Thread is alive", !st.isAlive());
0682: synchronized (simple) {
0683: st.start();
0684: simple.wait();
0685: }
0686: st.join(10);
0687: } catch (InterruptedException e) {
0688: fail("Join failed ");
0689: }
0690: assertTrue("Join failed to timeout", st.isAlive());
0691:
0692: st.interrupt();
0693: try {
0694: st = new Thread(simple = new SimpleThread(100),
0695: "SimpleThread13");
0696: synchronized (simple) {
0697: st.start();
0698: simple.wait();
0699: }
0700: st.join(1000);
0701: } catch (InterruptedException e) {
0702: fail("Join failed : " + e.getMessage());
0703: return;
0704: }
0705: assertTrue("Joined thread is still alive", !st.isAlive());
0706:
0707: final Object lock = new Object();
0708: final Thread main = Thread.currentThread();
0709: Thread killer = new Thread(new Runnable() {
0710: public void run() {
0711: try {
0712: synchronized (lock) {
0713: lock.notify();
0714: }
0715: Thread.sleep(100);
0716: } catch (InterruptedException e) {
0717: return;
0718: }
0719: main.interrupt();
0720: }
0721: });
0722: boolean result = true;
0723: Thread th = new Thread("test");
0724: try {
0725: synchronized (lock) {
0726: killer.start();
0727: lock.wait();
0728: }
0729: th.join(200);
0730: } catch (InterruptedException e) {
0731: result = false;
0732: }
0733: killer.interrupt();
0734: assertTrue("Hung joining a non-started thread", result);
0735: th.start();
0736: }
0737:
0738: /**
0739: * @tests java.lang.Thread#join(long, int)
0740: */
0741: public void test_joinJI() throws Exception {
0742: // Test for method void java.lang.Thread.join(long, int)
0743: SimpleThread simple;
0744: st = new Thread(simple = new SimpleThread(1000), "Squawk1");
0745: assertTrue("Thread is alive", !st.isAlive());
0746: synchronized (simple) {
0747: st.start();
0748: simple.wait();
0749: }
0750:
0751: long firstRead = System.currentTimeMillis();
0752: st.join(100, 999999);
0753: long secondRead = System.currentTimeMillis();
0754: assertTrue("Did not join by appropriate time: " + secondRead
0755: + "-" + firstRead + "=" + (secondRead - firstRead),
0756: secondRead - firstRead <= 300);
0757: assertTrue("Joined thread is not alive", st.isAlive());
0758: st.interrupt();
0759:
0760: final Object lock = new Object();
0761: final Thread main = Thread.currentThread();
0762: Thread killer = new Thread(new Runnable() {
0763: public void run() {
0764: try {
0765: synchronized (lock) {
0766: lock.notify();
0767: }
0768: Thread.sleep(100);
0769: } catch (InterruptedException e) {
0770: return;
0771: }
0772: main.interrupt();
0773: }
0774: });
0775: boolean result = true;
0776: Thread th = new Thread("test");
0777: try {
0778: synchronized (lock) {
0779: killer.start();
0780: lock.wait();
0781: }
0782: th.join(200, 20);
0783: } catch (InterruptedException e) {
0784: result = false;
0785: }
0786: killer.interrupt();
0787: assertTrue("Hung joining a non-started thread", result);
0788: th.start();
0789: }
0790:
0791: /**
0792: * @tests java.lang.Thread#resume()
0793: */
0794: @SuppressWarnings("deprecation")
0795: public void test_resume() {
0796: // Test for method void java.lang.Thread.resume()
0797: int orgval;
0798: ResSupThread t;
0799: try {
0800: t = new ResSupThread(Thread.currentThread());
0801: synchronized (t) {
0802: ct = new Thread(t, "Interrupt Test2");
0803: ct.start();
0804: t.wait();
0805: }
0806: ct.suspend();
0807: // Wait to be sure the suspend has occurred
0808: Thread.sleep(500);
0809: orgval = t.getCheckVal();
0810: // Wait to be sure the thread is suspended
0811: Thread.sleep(500);
0812: assertTrue("Failed to suspend thread", orgval == t
0813: .getCheckVal());
0814: ct.resume();
0815: // Wait to be sure the resume has occurred.
0816: Thread.sleep(500);
0817: assertTrue("Failed to resume thread", orgval != t
0818: .getCheckVal());
0819: ct.interrupt();
0820: } catch (InterruptedException e) {
0821: fail("Unexpected interrupt occurred : " + e.getMessage());
0822: }
0823: }
0824:
0825: /**
0826: * @tests java.lang.Thread#run()
0827: */
0828: public void test_run() {
0829: // Test for method void java.lang.Thread.run()
0830: class RunThread implements Runnable {
0831: boolean didThreadRun = false;
0832:
0833: public void run() {
0834: didThreadRun = true;
0835: }
0836: }
0837: RunThread rt = new RunThread();
0838: Thread t = new Thread(rt);
0839: try {
0840: t.start();
0841: int count = 0;
0842: while (!rt.didThreadRun && count < 20) {
0843: Thread.sleep(100);
0844: count++;
0845: }
0846: assertTrue("Thread did not run", rt.didThreadRun);
0847: t.join();
0848: } catch (InterruptedException e) {
0849: assertTrue("Joined thread was interrupted", true);
0850: }
0851: assertTrue("Joined thread is still alive", !t.isAlive());
0852: }
0853:
0854: /**
0855: * @tests java.lang.Thread#setDaemon(boolean)
0856: */
0857: public void test_setDaemonZ() {
0858: // Test for method void java.lang.Thread.setDaemon(boolean)
0859: st = new Thread(new SimpleThread(1), "SimpleThread14");
0860: st.setDaemon(true);
0861: assertTrue("Failed to set thread as daemon thread", st
0862: .isDaemon());
0863: st.start();
0864: }
0865:
0866: /**
0867: * @tests java.lang.Thread#setName(java.lang.String)
0868: */
0869: public void test_setNameLjava_lang_String() {
0870: // Test for method void java.lang.Thread.setName(java.lang.String)
0871: st = new Thread(new SimpleThread(1), "SimpleThread15");
0872: st.setName("Bogus Name");
0873: assertEquals("Failed to set thread name", "Bogus Name", st
0874: .getName());
0875: try {
0876: st.setName(null);
0877: fail("Null should not be accepted as a valid name");
0878: } catch (NullPointerException e) {
0879: // success
0880: assertTrue("Null should not be accepted as a valid name",
0881: true);
0882: }
0883: st.start();
0884: }
0885:
0886: /**
0887: * @tests java.lang.Thread#setPriority(int)
0888: */
0889: public void test_setPriorityI() {
0890: // Test for method void java.lang.Thread.setPriority(int)
0891: st = new Thread(new SimpleThread(1));
0892: st.setPriority(Thread.MAX_PRIORITY);
0893: assertTrue("Failed to set priority",
0894: st.getPriority() == Thread.MAX_PRIORITY);
0895: st.start();
0896: }
0897:
0898: /**
0899: * @tests java.lang.Thread#sleep(long)
0900: */
0901: public void test_sleepJ() {
0902: // Test for method void java.lang.Thread.sleep(long)
0903:
0904: // TODO : Test needs enhancing.
0905: long stime = 0, ftime = 0;
0906: try {
0907: stime = System.currentTimeMillis();
0908: Thread.sleep(1000);
0909: ftime = System.currentTimeMillis();
0910: } catch (InterruptedException e) {
0911: fail("Unexpected interrupt received");
0912: }
0913: assertTrue("Failed to sleep long enough",
0914: (ftime - stime) >= 800);
0915: }
0916:
0917: /**
0918: * @tests java.lang.Thread#sleep(long, int)
0919: */
0920: public void test_sleepJI() {
0921: // Test for method void java.lang.Thread.sleep(long, int)
0922:
0923: // TODO : Test needs revisiting.
0924: long stime = 0, ftime = 0;
0925: try {
0926: stime = System.currentTimeMillis();
0927: Thread.sleep(1000, 999999);
0928: ftime = System.currentTimeMillis();
0929: } catch (InterruptedException e) {
0930: fail("Unexpected interrupt received");
0931: }
0932: long result = ftime - stime;
0933: assertTrue("Failed to sleep long enough: " + result,
0934: result >= 900 && result <= 1100);
0935: }
0936:
0937: /**
0938: * @tests java.lang.Thread#start()
0939: */
0940: public void test_start() {
0941: // Test for method void java.lang.Thread.start()
0942: try {
0943: ResSupThread t = new ResSupThread(Thread.currentThread());
0944: synchronized (t) {
0945: ct = new Thread(t, "Interrupt Test4");
0946: ct.start();
0947: t.wait();
0948: }
0949: assertTrue("Thread is not running1", ct.isAlive());
0950: // Let the child thread get going.
0951: int orgval = t.getCheckVal();
0952: Thread.sleep(150);
0953: assertTrue("Thread is not running2", orgval != t
0954: .getCheckVal());
0955: ct.interrupt();
0956: } catch (InterruptedException e) {
0957: fail("Unexpected interrupt occurred");
0958: }
0959: }
0960:
0961: /**
0962: * @tests java.lang.Thread#stop()
0963: */
0964: @SuppressWarnings("deprecation")
0965: public void test_stop() {
0966: // Test for method void java.lang.Thread.stop()
0967: try {
0968: Runnable r = new ResSupThread(null);
0969: synchronized (r) {
0970: st = new Thread(r, "Interupt Test5");
0971: st.start();
0972: r.wait();
0973: }
0974:
0975: } catch (InterruptedException e) {
0976: fail("Unexpected interrupt received");
0977: }
0978: st.stop();
0979:
0980: try {
0981: st.join(10000);
0982: } catch (InterruptedException e1) {
0983: st.interrupt();
0984: fail("Failed to stopThread before 10000 timeout");
0985: }
0986: assertTrue("Failed to stopThread", !st.isAlive());
0987: }
0988:
0989: /**
0990: * @tests java.lang.Thread#stop()
0991: */
0992: @SuppressWarnings("deprecation")
0993: public void test_stop_subtest0() {
0994: Thread t = new Thread("t");
0995: class MySecurityManager extends SecurityManager {
0996: public boolean intest = false;
0997:
0998: @Override
0999: public void checkAccess(Thread t) {
1000: if (intest) {
1001: fail("checkAccess called");
1002: }
1003: }
1004:
1005: @Override
1006: public void checkPermission(Permission permission) {
1007: if (permission.getName().equals("setSecurityManager")) {
1008: return;
1009: }
1010: super .checkPermission(permission);
1011: }
1012: }
1013: MySecurityManager sm = new MySecurityManager();
1014: System.setSecurityManager(sm);
1015: try {
1016: sm.intest = true;
1017: try {
1018: t.stop();
1019: // Ignore any SecurityExceptions, may not have stopThread
1020: // permission
1021: } catch (SecurityException e) {
1022: }
1023: sm.intest = false;
1024: t.start();
1025: try {
1026: t.join(2000);
1027: } catch (InterruptedException e) {
1028: }
1029: sm.intest = true;
1030: try {
1031: t.stop();
1032: // Ignore any SecurityExceptions, may not have stopThread
1033: // permission
1034: } catch (SecurityException e) {
1035: }
1036: sm.intest = false;
1037: } finally {
1038: System.setSecurityManager(null);
1039: }
1040: }
1041:
1042: /**
1043: * @tests java.lang.Thread#stop(java.lang.Throwable)
1044: */
1045: @SuppressWarnings("deprecation")
1046: public void test_stopLjava_lang_Throwable_subtest0() {
1047: Thread t = new Thread("t");
1048: class MySecurityManager extends SecurityManager {
1049: public boolean intest = false;
1050:
1051: public boolean checkAccess = false;
1052:
1053: @Override
1054: public void checkAccess(Thread t) {
1055: if (intest) {
1056: checkAccess = true;
1057: }
1058: }
1059:
1060: @Override
1061: public void checkPermission(Permission permission) {
1062: if (permission.getName().equals("setSecurityManager")) {
1063: return;
1064: }
1065: super .checkPermission(permission);
1066: }
1067: }
1068: MySecurityManager sm = new MySecurityManager();
1069: System.setSecurityManager(sm);
1070: try {
1071: sm.intest = true;
1072: try {
1073: t.stop(new ThreadDeath());
1074: // Ignore any SecurityExceptions, may not have stopThread
1075: // permission
1076: } catch (SecurityException e) {
1077: }
1078: sm.intest = false;
1079: assertTrue("no checkAccess 1", sm.checkAccess);
1080: t.start();
1081: try {
1082: t.join(2000);
1083: } catch (InterruptedException e) {
1084: }
1085: sm.intest = true;
1086: sm.checkAccess = false;
1087: try {
1088: t.stop(new ThreadDeath());
1089: // Ignore any SecurityExceptions, may not have stopThread
1090: // permission
1091: } catch (SecurityException e) {
1092: }
1093: assertTrue("no checkAccess 2", sm.checkAccess);
1094: sm.intest = false;
1095: } finally {
1096: System.setSecurityManager(null);
1097: }
1098: }
1099:
1100: /**
1101: * @tests java.lang.Thread#stop(java.lang.Throwable)
1102: */
1103: @SuppressWarnings("deprecation")
1104: public void test_stopLjava_lang_Throwable() {
1105: // Test for method void java.lang.Thread.stop(java.lang.Throwable)
1106: ResSupThread t = new ResSupThread(Thread.currentThread());
1107: synchronized (t) {
1108: st = new Thread(t, "StopThread");
1109: st.setPriority(Thread.MAX_PRIORITY);
1110: st.start();
1111: try {
1112: t.wait();
1113: } catch (InterruptedException e) {
1114: }
1115: }
1116: try {
1117: st.stop(new BogusException("Bogus"));
1118: Thread.sleep(20000);
1119: } catch (InterruptedException e) {
1120: assertTrue("Stopped child with exception not alive", st
1121: .isAlive());
1122: st.interrupt();
1123: return;
1124: }
1125: st.interrupt();
1126: fail("Stopped child did not throw exception");
1127: }
1128:
1129: /**
1130: * @tests java.lang.Thread#suspend()
1131: */
1132: @SuppressWarnings("deprecation")
1133: public void test_suspend() {
1134: // Test for method void java.lang.Thread.suspend()
1135: int orgval;
1136: ResSupThread t = new ResSupThread(Thread.currentThread());
1137: try {
1138: synchronized (t) {
1139: ct = new Thread(t, "Interupt Test6");
1140: ct.start();
1141: t.wait();
1142: }
1143: ct.suspend();
1144: // Wait to be sure the suspend has occurred
1145: Thread.sleep(500);
1146: orgval = t.getCheckVal();
1147: // Wait to be sure the thread is suspended
1148: Thread.sleep(500);
1149: assertTrue("Failed to suspend thread", orgval == t
1150: .getCheckVal());
1151: ct.resume();
1152: // Wait to be sure the resume has occurred.
1153: Thread.sleep(500);
1154: assertTrue("Failed to resume thread", orgval != t
1155: .getCheckVal());
1156: ct.interrupt();
1157: } catch (InterruptedException e) {
1158: fail("Unexpected interrupt occurred");
1159: }
1160:
1161: final Object notify = new Object();
1162: Thread t1 = new Thread(new Runnable() {
1163: public void run() {
1164: synchronized (notify) {
1165: notify.notify();
1166: }
1167: Thread.currentThread().suspend();
1168: }
1169: });
1170: try {
1171: synchronized (notify) {
1172: t1.start();
1173: notify.wait();
1174: }
1175: // wait for Thread to suspend
1176: Thread.sleep(500);
1177: assertTrue("Thread should be alive", t1.isAlive());
1178: t1.resume();
1179: t1.join();
1180: } catch (InterruptedException e) {
1181: }
1182: }
1183:
1184: /**
1185: * @tests java.lang.Thread#toString()
1186: */
1187: public void test_toString() {
1188: // Test for method java.lang.String java.lang.Thread.toString()
1189: ThreadGroup tg = new ThreadGroup("Test Group5");
1190: st = new Thread(tg, new SimpleThread(1), "SimpleThread17");
1191: final String stString = st.toString();
1192: final String expected = "Thread[SimpleThread17,5,Test Group5]";
1193: assertTrue("Returned incorrect string: " + stString
1194: + "\t(expecting :" + expected + ")", stString
1195: .equals(expected));
1196: st.start();
1197: try {
1198: st.join();
1199: } catch (InterruptedException e) {
1200: }
1201: tg.destroy();
1202: }
1203:
1204: /**
1205: * @tests java.lang.Thread#getAllStackTraces()
1206: */
1207: public void test_getAllStackTraces() {
1208: Map<Thread, StackTraceElement[]> stMap = Thread
1209: .getAllStackTraces();
1210: assertNotNull(stMap);
1211: //TODO add security-based tests
1212: }
1213:
1214: /**
1215: * @tests java.lang.Thread#getDefaultUncaughtExceptionHandler
1216: * @tests java.lang.Thread#setDefaultUncaughtExceptionHandler
1217: */
1218: public void test_get_setDefaultUncaughtExceptionHandler() {
1219: class Handler implements UncaughtExceptionHandler {
1220: public void uncaughtException(Thread thread, Throwable ex) {
1221: }
1222: }
1223:
1224: final Handler handler = new Handler();
1225: Thread.setDefaultUncaughtExceptionHandler(handler);
1226: assertSame(handler, Thread.getDefaultUncaughtExceptionHandler());
1227:
1228: Thread.setDefaultUncaughtExceptionHandler(null);
1229: assertNull(Thread.getDefaultUncaughtExceptionHandler());
1230: //TODO add security-based tests
1231: }
1232:
1233: /**
1234: * @tests java.lang.Thread#getStackTrace()
1235: */
1236: public void test_getStackTrace() {
1237: StackTraceElement[] stackTrace = Thread.currentThread()
1238: .getStackTrace();
1239:
1240: assertNotNull(stackTrace);
1241:
1242: stack_trace_loop: {
1243: for (int i = 0; i < stackTrace.length; i++) {
1244: StackTraceElement e = stackTrace[i];
1245: if (getClass().getName().equals(e.getClassName())) {
1246: if ("test_getStackTrace".equals(e.getMethodName())) {
1247: break stack_trace_loop;
1248: }
1249: }
1250: }
1251: fail("class and method not found in stack trace");
1252: }
1253:
1254: //TODO add security-based tests
1255: }
1256:
1257: /**
1258: * @tests java.lang.Thread#getState()
1259: */
1260: public void test_getState() {
1261: Thread.State state = Thread.currentThread().getState();
1262: assertNotNull(state);
1263: assertEquals(Thread.State.RUNNABLE, state);
1264: //TODO add additional state tests
1265: }
1266:
1267: /**
1268: * @tests java.lang.Thread#getUncaughtExceptionHandler
1269: * @tests java.lang.Thread#setUncaughtExceptionHandler
1270: */
1271: public void test_get_setUncaughtExceptionHandler() {
1272: class Handler implements UncaughtExceptionHandler {
1273: public void uncaughtException(Thread thread, Throwable ex) {
1274: }
1275: }
1276:
1277: final Handler handler = new Handler();
1278: Thread.currentThread().setUncaughtExceptionHandler(handler);
1279: assertSame(handler, Thread.currentThread()
1280: .getUncaughtExceptionHandler());
1281:
1282: Thread.currentThread().setUncaughtExceptionHandler(null);
1283:
1284: //TODO add security-based tests
1285: }
1286:
1287: /**
1288: * @tests java.lang.Thread#getId()
1289: */
1290: public void test_getId() {
1291: assertTrue("current thread's ID is not positive", Thread
1292: .currentThread().getId() > 0);
1293:
1294: //check all the current threads for positive IDs
1295: Map<Thread, StackTraceElement[]> stMap = Thread
1296: .getAllStackTraces();
1297: for (Thread thread : stMap.keySet()) {
1298: assertTrue("thread's ID is not positive: "
1299: + thread.getName(), thread.getId() > 0);
1300: }
1301: }
1302:
1303: @Override
1304: protected void tearDown() {
1305: try {
1306: if (st != null)
1307: st.interrupt();
1308: } catch (Exception e) {
1309: }
1310: try {
1311: if (spinner != null)
1312: spinner.interrupt();
1313: } catch (Exception e) {
1314: }
1315: try {
1316: if (ct != null)
1317: ct.interrupt();
1318: } catch (Exception e) {
1319: }
1320:
1321: try {
1322: spinner = null;
1323: st = null;
1324: ct = null;
1325: System.runFinalization();
1326: } catch (Exception e) {
1327: }
1328: }
1329: }
|