/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. */
  5. package com.razie.pub.base.test;
  6. import junit.framework.TestCase;
  7. import com.razie.pub.base.NoStatic;
  8. import com.razie.pub.base.TLNoStatic;
  9. import com.razie.pub.base.ExecutionContext;
  10. /**
  11. * test the light server
  12. *
  13. * @author razvanc99
  14. */
  15. public class TestNoStatic extends TestCase {
  16. ExecutionContext t = new ExecutionContext(null);
  17. static String failed = null;
  18. // setup a static context - we test that in other threads i get differnet
  19. // values
  20. static NoStatic<Boolean> STATIC = new NoStatic<Boolean>("testing", Boolean.TRUE);
  21. static TLNoStatic<Boolean> STATICTL = new TLNoStatic<Boolean>(Boolean.TRUE);
  22. // setup two statics in different threads and make sure they work
  23. public void testNoStatics() throws InterruptedException {
  24. // we test this context in the second thread
  25. t.enter();
  26. failed = null;
  27. // the value is set in a differnet context than default...for t2
  28. STATIC.set(Boolean.FALSE);
  29. STATICTL.set(Boolean.FALSE);
  30. // t1 uses the default
  31. Thread t1 = new Thread() {
  32. @Override
  33. public void run() {
  34. if (!STATIC.get())
  35. failed = "value changed!!!";
  36. if (!STATICTL.get())
  37. failed = "TL value changed!!!";
  38. }
  39. };
  40. // t2 overwrites default
  41. Thread t2 = new Thread() {
  42. @Override
  43. public void run() {
  44. t.enter();
  45. // we reuse the same thread context above - value will be FALSE
  46. if (STATIC.get())
  47. failed = "value DIDNT change!!!";
  48. // threadLocal created a new one for this new thread - still TRUE...
  49. if (!STATICTL.get())
  50. failed = "TL value DIDNT change!!!";
  51. t.exit();
  52. }
  53. };
  54. t1.start();
  55. t2.start();
  56. t1.join();
  57. t2.join();
  58. assertFalse(failed, failed != null);
  59. }
  60. }