Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.concurrent;
00018
00019 import static org.junit.Assert.assertEquals;
00020 import static org.junit.Assert.assertFalse;
00021 import static org.junit.Assert.assertTrue;
00022 import static org.junit.Assert.fail;
00023
00024 import org.junit.Test;
00025
00026 import java.util.concurrent.ExecutorService;
00027 import java.util.concurrent.Executors;
00028 import java.util.concurrent.TimeUnit;
00029
00033 public class HolderTest {
00034
00035 @Test
00036 public void testHolderFailsWithNoValue() {
00037 Holder<String> holder = Holder.newEmpty();
00038 try {
00039 holder.get();
00040 fail();
00041 } catch (Exception e) {
00042
00043 }
00044 }
00045
00046 @Test
00047 public void testHolderFailsIfSetTwice() {
00048 Holder<String> holder = Holder.newEmpty();
00049 holder.set("Hello, world!");
00050 try {
00051 holder.set("Goodbye, world!");
00052 fail();
00053 } catch (Exception e) {
00054
00055 }
00056 }
00057
00058 @Test
00059 public void testHolderEquality() {
00060 Holder<String> holder1 = Holder.newEmpty();
00061 Holder<String> holder2 = Holder.newEmpty();
00062 assertTrue(holder1.equals(holder1));
00063 assertFalse(holder1.equals(holder2));
00064 }
00065
00066 @Test
00067 public void testHolderAwait() throws InterruptedException {
00068 final Holder<String> holder = Holder.newEmpty();
00069 final String message = "Hello, world!";
00070 ExecutorService executorService = Executors.newCachedThreadPool();
00071 executorService.execute(new Runnable() {
00072 @Override
00073 public void run() {
00074 holder.set(message);
00075 }
00076 });
00077 assertTrue(holder.await(1, TimeUnit.SECONDS));
00078 assertEquals(message, holder.get());
00079 }
00080 }