HolderTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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       // Holder.get() should fail if no value has been stored.
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       // Holder.set() should fail if a value has already been stored.
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 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49