Issue 87: simple Providers.of(instance) method to facilitate your unit tests

git-svn-id: https://google-guice.googlecode.com/svn/trunk@332 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/src/com/google/inject/util/Providers.java b/src/com/google/inject/util/Providers.java
new file mode 100644
index 0000000..92da1cb
--- /dev/null
+++ b/src/com/google/inject/util/Providers.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2007 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.inject.util;
+
+import com.google.inject.Provider;
+
+/**
+ * Static utility methods for creating and working with instances of
+ * {@link Provider}.
+ *
+ * @author Kevin Bourrillion (kevinb9n@gmail.com)
+ */
+public final class Providers {
+
+  private Providers() {}
+
+  /**
+   * Returns a provider which always provides {@code instance}.  This should not
+   * be necessary to use in your application, but is helpful for several types
+   * of unit tests.
+   *
+   * @param instance the instance that should always be provided.  This is also
+   *     permitted to be null, to enable aggressive testing, although in real
+   *     life a Guice-supplied Provider will never return null.
+   */
+  public static <T> Provider<T> of(final T instance) {
+    return new Provider<T>() {
+      public T get() {
+        return instance;
+      }
+    };
+  }
+}
diff --git a/test/com/google/inject/AllTests.java b/test/com/google/inject/AllTests.java
index 300a168..4d8715b 100644
--- a/test/com/google/inject/AllTests.java
+++ b/test/com/google/inject/AllTests.java
@@ -21,6 +21,7 @@
 import com.google.inject.internal.ReferenceMapTest;
 import com.google.inject.internal.ReferenceMapTestSuite;
 import com.google.inject.matcher.MatcherTest;
+import com.google.inject.util.ProvidersTest;
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
@@ -63,6 +64,8 @@
 
     suite.addTestSuite(IntegrationTest.class);
 
+    suite.addTestSuite(ProvidersTest.class);
+
     return suite;
   }
 }
diff --git a/test/com/google/inject/util/ProvidersTest.java b/test/com/google/inject/util/ProvidersTest.java
new file mode 100644
index 0000000..b99d126
--- /dev/null
+++ b/test/com/google/inject/util/ProvidersTest.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2007 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.inject.util;
+
+import com.google.inject.Provider;
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for {@link Providers}.
+ *
+ * @author Kevin Bourrillion (kevinb9n@gmail.com)
+ */
+public class ProvidersTest extends TestCase {
+
+  public void testOfInstance() {
+    String foo = "foo";
+    Provider<String> p = Providers.of(foo);
+    assertSame(foo, p.get());
+    assertSame(foo, p.get());
+  }
+
+  public void testOfNull() {
+    Provider<String> p = Providers.of(null);
+    assertNull(p.get());
+  }
+}