Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2007 Google Inc.
      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.inject.util;
     18 
     19 import com.google.common.base.Objects;
     20 import com.google.common.testing.EqualsTester;
     21 import com.google.inject.Provider;
     22 
     23 import junit.framework.TestCase;
     24 
     25 import javax.inject.Inject;
     26 
     27 /**
     28  * Unit tests for {@link Providers}.
     29  *
     30  * @author Kevin Bourrillion (kevinb9n (at) gmail.com)
     31  */
     32 public class ProvidersTest extends TestCase {
     33 
     34   public void testOfInstance() {
     35     String foo = "foo";
     36     Provider<String> p = Providers.of(foo);
     37     assertSame(foo, p.get());
     38     assertSame(foo, p.get());
     39   }
     40 
     41   public void testOfNull() {
     42     Provider<String> p = Providers.of(null);
     43     assertNull(p.get());
     44   }
     45 
     46   public void testOfEquality() {
     47     new EqualsTester()
     48         .addEqualityGroup(
     49             Providers.of(null),
     50             Providers.of(null))
     51         .addEqualityGroup(
     52             Providers.of("Hello"),
     53             Providers.of("Hello"))
     54         .testEquals();
     55   }
     56 
     57   public void testGuicifyEquality() {
     58     new EqualsTester()
     59         .addEqualityGroup(
     60             Providers.guicify(new JavaxProvider(10)),
     61             Providers.guicify(new JavaxProvider(10)))
     62         .addEqualityGroup(
     63             Providers.guicify(new JavaxProvider(11)),
     64             Providers.guicify(new JavaxProvider(11)))
     65         .addEqualityGroup(
     66             Providers.guicify(new JavaxProviderWithDependencies()),
     67             Providers.guicify(new JavaxProviderWithDependencies()))
     68         .testEquals();
     69   }
     70 
     71   private static class JavaxProvider implements javax.inject.Provider<Integer> {
     72     private final int value;
     73 
     74     public JavaxProvider(int value) {
     75       this.value = value;
     76     }
     77 
     78     public Integer get() {
     79       return value;
     80     }
     81 
     82     @Override public int hashCode() {
     83       return Objects.hashCode(value);
     84     }
     85 
     86     @Override public boolean equals(Object obj) {
     87       return (obj instanceof JavaxProvider) && (value == ((JavaxProvider) obj).value);
     88     }
     89   }
     90 
     91   private static class JavaxProviderWithDependencies implements javax.inject.Provider<Integer> {
     92     private int value;
     93 
     94     @Inject void setValue(int value) {
     95       this.value = value;
     96     }
     97 
     98     public Integer get() {
     99       return value;
    100     }
    101 
    102     @Override public int hashCode() {
    103       return 42;
    104     }
    105 
    106     @Override public boolean equals(Object obj) {
    107       return (obj instanceof JavaxProviderWithDependencies);
    108     }
    109   }
    110 }
    111