Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2008 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.testing;
     18 
     19 import com.google.common.annotations.GwtCompatible;
     20 
     21 import junit.framework.TestCase;
     22 
     23 import java.util.EnumSet;
     24 import java.util.concurrent.TimeUnit;
     25 
     26 /**
     27  * Unit test for {@link FakeTicker}.
     28  *
     29  * @author Jige Yu
     30  */
     31 @GwtCompatible(emulated = true)
     32 public class FakeTickerTest extends TestCase {
     33 
     34   public void testAdvance() {
     35     FakeTicker ticker = new FakeTicker();
     36     assertEquals(0, ticker.read());
     37     assertSame(ticker, ticker.advance(10));
     38     assertEquals(10, ticker.read());
     39     ticker.advance(1, TimeUnit.MILLISECONDS);
     40     assertEquals(1000010L, ticker.read());
     41   }
     42 
     43   public void testAutoIncrementStep_returnsSameInstance() {
     44     FakeTicker ticker = new FakeTicker();
     45     assertSame(ticker, ticker.setAutoIncrementStep(10, TimeUnit.NANOSECONDS));
     46   }
     47 
     48   public void testAutoIncrementStep_nanos() {
     49     FakeTicker ticker = new FakeTicker().setAutoIncrementStep(10, TimeUnit.NANOSECONDS);
     50     assertEquals(0, ticker.read());
     51     assertEquals(10, ticker.read());
     52     assertEquals(20, ticker.read());
     53   }
     54 
     55   public void testAutoIncrementStep_millis() {
     56     FakeTicker ticker = new FakeTicker().setAutoIncrementStep(1, TimeUnit.MILLISECONDS);
     57     assertEquals(0, ticker.read());
     58     assertEquals(1000000, ticker.read());
     59     assertEquals(2000000, ticker.read());
     60   }
     61 
     62   public void testAutoIncrementStep_seconds() {
     63     FakeTicker ticker = new FakeTicker().setAutoIncrementStep(3, TimeUnit.SECONDS);
     64     assertEquals(0, ticker.read());
     65     assertEquals(3000000000L, ticker.read());
     66     assertEquals(6000000000L, ticker.read());
     67   }
     68 
     69   public void testAutoIncrementStep_resetToZero() {
     70     FakeTicker ticker = new FakeTicker().setAutoIncrementStep(10, TimeUnit.NANOSECONDS);
     71     assertEquals(0, ticker.read());
     72     assertEquals(10, ticker.read());
     73     assertEquals(20, ticker.read());
     74 
     75     for (TimeUnit timeUnit : EnumSet.allOf(TimeUnit.class)) {
     76       ticker.setAutoIncrementStep(0, timeUnit);
     77       assertEquals(
     78           "Expected no auto-increment when setting autoIncrementStep to 0 " + timeUnit,
     79           30, ticker.read());
     80     }
     81   }
     82 
     83   public void testAutoIncrement_negative() {
     84     FakeTicker ticker = new FakeTicker();
     85     try {
     86       ticker.setAutoIncrementStep(-1, TimeUnit.NANOSECONDS);
     87       fail("Expected IllegalArgumentException");
     88     } catch (IllegalArgumentException expected) {
     89     }
     90   }
     91 }
     92 
     93