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 InheritableThreadLocal_TestCase extends TestCase {
040:
041: private ExecutorService service;
042:
043: private static class TestInheritableThreadLocal extends
044: InheritableThreadLocal<String> {
045: protected String initialValue() {
046: return "a";
047: }
048:
049: protected String childValue(String parentValue) {
050: return "(" + parentValue + ")";
051: }
052: }
053:
054: protected void setUp() throws Exception {
055: super .setUp();
056: service = Executors.newSingleThreadExecutor();
057: }
058:
059: protected void tearDown() throws Exception {
060: service.shutdown();
061: super .tearDown();
062: }
063:
064: private void execute(Runnable runnable) throws Exception {
065: try {
066: service.submit(runnable).get();
067: } catch (ExecutionException e) {
068: Throwable cause = e.getCause();
069: if (cause instanceof Exception)
070: throw (Exception) cause;
071: if (cause instanceof Error)
072: throw (Error) cause;
073: throw e;
074: }
075: }
076:
077: public void test_Inheritance() throws Exception {
078: final InheritableThreadLocal<String> threadLocal = new TestInheritableThreadLocal();
079: assertEquals("a", threadLocal.get());
080: threadLocal.set("b");
081: execute(new Runnable() {
082: public void run() {
083: assertEquals("(b)", threadLocal.get());
084: threadLocal.set("x");
085: }
086: });
087: assertEquals("b", threadLocal.get());
088: threadLocal.set(null);
089: assertNull(threadLocal.get());
090: threadLocal.remove();
091: execute(new Runnable() {
092: public void run() {
093: assertEquals("x", threadLocal.get());
094: }
095: });
096: assertEquals("a", threadLocal.get());
097: }
098:
099: public void test_NoInheritance() throws Exception {
100: final InheritableThreadLocal<String> threadLocal = new TestInheritableThreadLocal();
101: execute(new Runnable() {
102: public void run() {
103: assertEquals("a", threadLocal.get());
104: threadLocal.set("x");
105: }
106: });
107: assertEquals("a", threadLocal.get());
108: threadLocal.set("b");
109: execute(new Runnable() {
110: public void run() {
111: assertEquals("x", threadLocal.get());
112: }
113: });
114: assertEquals("b", threadLocal.get());
115: }
116:
117: public void test_Removed() throws Exception {
118: final InheritableThreadLocal<String> threadLocal = new TestInheritableThreadLocal();
119: assertEquals("a", threadLocal.get());
120: threadLocal.remove();
121: execute(new Runnable() {
122: public void run() {
123: assertEquals("a", threadLocal.get());
124: threadLocal.set("x");
125: }
126: });
127: assertEquals("a", threadLocal.get());
128: threadLocal.set("b");
129: execute(new Runnable() {
130: public void run() {
131: assertEquals("x", threadLocal.get());
132: }
133: });
134: assertEquals("b", threadLocal.get());
135: }
136:
137: }
|