1 4 package com.tc.util.concurrent; 5 6 import junit.framework.TestCase; 7 8 13 public class SetOnceRefTest extends TestCase { 14 15 public void testAllowsNull() { 16 SetOnceRef ref = new SetOnceRef(false); 17 18 assertFalse(ref.allowsNull()); 19 20 try { 21 ref.set(null); 22 fail(); 23 } catch (IllegalArgumentException iae) { 24 } 26 27 Object val = new Object (); 28 ref.set(val); 29 30 assertTrue(ref.get() == val); 31 } 32 33 public void testSetNull() { 34 final SetOnceRef ref1 = new SetOnceRef(true); 35 ref1.set(null); 36 final SetOnceRef ref2 = new SetOnceRef(null, true); 37 38 assertTrue(ref1.get() == null); 39 assertTrue(ref2.get() == null); 40 } 41 42 public void testSetTwice() { 43 final Object val1 = new Object (); 44 final Object val2 = new Object (); 45 46 final SetOnceRef ref1 = new SetOnceRef(); 47 ref1.set(val1); 48 final SetOnceRef ref2 = new SetOnceRef(val2); 49 50 assertTrue(ref1.get() == val1); 51 assertTrue(ref2.get() == val2); 52 53 try { 54 ref1.set(val1); 55 fail(); 56 } catch (IllegalStateException e) { 57 } 59 60 try { 61 ref2.set(val2); 62 fail(); 63 } catch (IllegalStateException e) { 64 } 66 } 67 68 public void testGetMany() { 69 final Object JUnitGetsMeHard = new Object (); 70 final SetOnceRef ref = new SetOnceRef(JUnitGetsMeHard); 71 72 for (int i = 0; i < 1000; i++) { 73 assertTrue(ref.get() == JUnitGetsMeHard); 74 } 75 } 76 77 public void testThreadAccess() throws InterruptedException { 78 final Object val = new Object (); 79 80 final SetOnceRef ref = new SetOnceRef(); 81 82 Thread other = new Thread (new Runnable () { 83 public void run() { 84 ref.set(val); 85 } 86 }); 87 88 other.start(); 89 other.join(); 90 91 assertTrue(ref.get() == val); 92 } 93 94 } 95 | Popular Tags |