PageRenderTime 17ms CodeModel.GetById 7ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/razpub/test_src/com/razie/pub/base/test/TestNoStatic.java

http://razpub.googlecode.com/
Java | 73 lines | 42 code | 13 blank | 18 comment | 5 complexity | 829dfa855cc82545abae90c5341bddac MD5 | raw file
 1/**
 2 * Razvan's public code. Copyright 2008 based on Apache license (share alike) see LICENSE.txt for
 3 * details. No warranty implied nor any liability assumed for this code.
 4 */
 5package com.razie.pub.base.test;
 6
 7import junit.framework.TestCase;
 8
 9import com.razie.pub.base.NoStatic;
10import com.razie.pub.base.TLNoStatic;
11import com.razie.pub.base.ExecutionContext;
12
13/**
14 * test the light server
15 * 
16 * @author razvanc99
17 */
18public class TestNoStatic extends TestCase {
19	ExecutionContext t = new ExecutionContext(null);
20	static String failed = null;
21
22	// setup a static context - we test that in other threads i get differnet
23	// values
24	static NoStatic<Boolean> STATIC = new NoStatic<Boolean>("testing", Boolean.TRUE);
25	static TLNoStatic<Boolean> STATICTL = new TLNoStatic<Boolean>(Boolean.TRUE);
26
27	// setup two statics in different threads and make sure they work
28	public void testNoStatics() throws InterruptedException {
29		// we test this context in the second thread
30		t.enter();
31		failed = null;
32
33		// the value is set in a differnet context than default...for t2
34		STATIC.set(Boolean.FALSE);
35		STATICTL.set(Boolean.FALSE);
36
37		// t1 uses the default
38		Thread t1 = new Thread() {
39			@Override
40			public void run() {
41				if (!STATIC.get())
42					failed = "value changed!!!";
43				if (!STATICTL.get())
44					failed = "TL value changed!!!";
45			}
46		};
47
48		// t2 overwrites default
49		Thread t2 = new Thread() {
50			@Override
51			public void run() {
52				t.enter();
53				// we reuse the same thread context above - value will be FALSE
54				if (STATIC.get())
55					failed = "value DIDNT change!!!";
56
57				// threadLocal created a new one for this new thread - still TRUE...
58				if (!STATICTL.get())
59					failed = "TL value DIDNT change!!!";
60				t.exit();
61			}
62		};
63
64		t1.start();
65		t2.start();
66
67		t1.join();
68		t2.join();
69
70		assertFalse(failed, failed != null);
71	}
72
73}