001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.lang;
032:
033: import junit.framework.*;
034: import java.util.concurrent.*;
035:
036: /**
037: * @author Taras Puchko
038: */
039: public class ThreadLocal_TestCase extends TestCase {
040:
041: private ExecutorService service;
042:
043: protected void setUp() throws Exception {
044: super .setUp();
045: service = Executors.newSingleThreadExecutor();
046: }
047:
048: protected void tearDown() throws Exception {
049: service.shutdown();
050: super .tearDown();
051: }
052:
053: private void execute(Runnable runnable) throws Exception {
054: try {
055: service.submit(runnable).get();
056: } catch (ExecutionException e) {
057: Throwable cause = e.getCause();
058: if (cause instanceof Exception)
059: throw (Exception) cause;
060: if (cause instanceof Error)
061: throw (Error) cause;
062: throw e;
063: }
064: }
065:
066: public void test() throws Exception {
067: final ThreadLocal<String> threadLocal = new ThreadLocal<String>() {
068: protected String initialValue() {
069: return "a";
070: }
071: };
072: assertEquals("a", threadLocal.get());
073: threadLocal.set("b");
074: execute(new Runnable() {
075: public void run() {
076: assertEquals("a", threadLocal.get());
077: threadLocal.set("x");
078: assertEquals("x", threadLocal.get());
079: }
080: });
081: assertEquals("b", threadLocal.get());
082: threadLocal.set(null);
083: assertNull(threadLocal.get());
084: threadLocal.remove();
085: execute(new Runnable() {
086: public void run() {
087: assertEquals("x", threadLocal.get());
088: threadLocal.set("y");
089: }
090: });
091: assertEquals("a", threadLocal.get());
092: execute(new Runnable() {
093: public void run() {
094: assertEquals("y", threadLocal.get());
095: threadLocal.set(null);
096: threadLocal.remove();
097: assertEquals("a", threadLocal.get());
098: threadLocal.remove();
099: threadLocal.set(null);
100: assertNull(threadLocal.get());
101: }
102: });
103: }
104:
105: }
|