You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@reef.apache.org by we...@apache.org on 2015/01/23 00:46:43 UTC
[10/51] [partial] incubator-reef git commit: [REEF-93] Move java
sources to lang/java
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/53ea32cc/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestBindSingleton.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestBindSingleton.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestBindSingleton.java
new file mode 100644
index 0000000..e148d6a
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestBindSingleton.java
@@ -0,0 +1,420 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.reef.tang;
+
+import org.apache.reef.tang.exceptions.BindException;
+import org.apache.reef.tang.exceptions.InjectionException;
+import org.apache.reef.tang.formats.ConfigurationFile;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.inject.Inject;
+
+import static org.junit.Assert.assertTrue;
+
+public class TestBindSingleton {
+
+ @Before
+ public void before() {
+ InbredSingletons.A.count = 0;
+ InbredSingletons.B.count = 0;
+ InbredSingletons.C.count = 0;
+
+ IncestuousSingletons.A.count = 0;
+ IncestuousSingletons.B.count = 0;
+ IncestuousSingletons.BN.count = 0;
+ IncestuousSingletons.C.count = 0;
+
+ IncestuousInterfaceSingletons.A.count = 0;
+ IncestuousInterfaceSingletons.B.count = 0;
+ IncestuousInterfaceSingletons.BN.count = 0;
+ IncestuousInterfaceSingletons.C.count = 0;
+ }
+
+ @Test
+ public void testSingletonRoundTrip() throws BindException, InjectionException {
+
+ final JavaConfigurationBuilder b = Tang.Factory.getTang()
+ .newConfigurationBuilder();
+ b.bindImplementation(A.class, B.class);
+ final Configuration src = b.build();
+
+ final JavaConfigurationBuilder dest = Tang.Factory.getTang()
+ .newConfigurationBuilder();
+ ConfigurationFile.addConfiguration(dest, ConfigurationFile.toConfigurationString(src));
+ final Injector i = Tang.Factory.getTang().newInjector(dest.build());
+ final A a1 = i.getInstance(A.class);
+ final A a2 = i.getInstance(A.class);
+ final B b1 = i.getInstance(B.class);
+
+ assertTrue("Two singletons should be the same", a1 == a2);
+ assertTrue("Both instances should be of class B", a1 instanceof B);
+ assertTrue("Both instances should be of class B", a2 instanceof B);
+ assertTrue("Singleton and not singleton should be the same", a1 == b1);
+
+ final Injector injector2 = Tang.Factory.getTang().newInjector(src);
+ final A a3 = injector2.getInstance(A.class);
+ assertTrue(
+ "Two different injectors should return two different singletons",
+ a3 != a1);
+
+ final Injector injector3 = injector2.forkInjector();
+ final A a4 = injector3.getInstance(A.class);
+ assertTrue(
+ "Child Injectors should return the same singletons as their parents",
+ a3 == a4);
+ }
+
+ @Test
+ public void testLateBoundVolatileInstanceWithSingletonX()
+ throws BindException, InjectionException {
+ Tang tang = Tang.Factory.getTang();
+ JavaConfigurationBuilder cb = tang.newConfigurationBuilder();
+ cb.bindImplementation(LateBoundVolatile.A.class,
+ LateBoundVolatile.B.class);
+ final Injector i = tang.newInjector(cb.build());
+ i.bindVolatileInstance(LateBoundVolatile.C.class, new LateBoundVolatile.C());
+ i.getInstance(LateBoundVolatile.A.class);
+ }
+
+ @Test
+ public void testMultipleInjectorInstaceWithSingleton() throws BindException, InjectionException {
+ final JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+
+ final Injector i1 = Tang.Factory.getTang().newInjector(cb.build());
+ final Injector i2 = Tang.Factory.getTang().newInjector(cb.build());
+
+ assertTrue("Different injectors should return different singleton object instances", i1.getInstance(AA.class) != i2.getInstance(AA.class));
+
+ final Configuration c = cb.build();
+
+ final Injector i3 = Tang.Factory.getTang().newInjector(c);
+ final Injector i4 = Tang.Factory.getTang().newInjector(c);
+
+ assertTrue("Different injectors should return different singleton object instances", i3.getInstance(AA.class) != i4.getInstance(AA.class));
+
+ }
+
+ @Test
+ public void testLateBoundVolatileInstanceWithSingletonY()
+ throws BindException, InjectionException {
+ Tang tang = Tang.Factory.getTang();
+ JavaConfigurationBuilder cb = tang.newConfigurationBuilder();
+ Injector i = tang.newInjector(cb.build());
+ i.bindVolatileInstance(LateBoundVolatile.C.class, new LateBoundVolatile.C());
+ i.getInstance(LateBoundVolatile.C.class);
+ }
+
+ @Test
+ public void testLateBoundVolatileInstanceWithSingletonZ()
+ throws BindException, InjectionException {
+ Tang tang = Tang.Factory.getTang();
+ JavaConfigurationBuilder cb = tang.newConfigurationBuilder();
+ cb.bindImplementation(LateBoundVolatile.B.class,
+ LateBoundVolatile.B.class);
+ Injector i = tang.newInjector(cb.build());
+ i.bindVolatileInstance(LateBoundVolatile.C.class, new LateBoundVolatile.C());
+ i.getInstance(LateBoundVolatile.B.class);
+ }
+
+ @Test
+ public void testInbredSingletons() throws BindException, InjectionException {
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder b = t.newConfigurationBuilder();
+ b.bindImplementation(InbredSingletons.A.class,
+ InbredSingletons.A.class);
+ b.bindImplementation(InbredSingletons.B.class,
+ InbredSingletons.B.class);
+ b.bindImplementation(InbredSingletons.C.class,
+ InbredSingletons.C.class);
+ Injector i = t.newInjector(b.build());
+ i.getInstance(InbredSingletons.A.class);
+ }
+
+ @Test
+ public void testIncestuousSingletons() throws BindException,
+ InjectionException {
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder b = t.newConfigurationBuilder();
+ b.bindImplementation(IncestuousSingletons.A.class,
+ IncestuousSingletons.A.class);
+ b.bindImplementation(IncestuousSingletons.B.class,
+ IncestuousSingletons.B.class);
+ b.bindImplementation(IncestuousSingletons.C.class,
+ IncestuousSingletons.C.class);
+ Injector i = t.newInjector(b.build());
+ i.getInstance(IncestuousSingletons.A.class);
+ }
+
+ @Test
+ public void testIncestuousSingletons2() throws BindException,
+ InjectionException {
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder b = t.newConfigurationBuilder();
+ b.bindImplementation(IncestuousSingletons.A.class,
+ IncestuousSingletons.A.class);
+ b.bindImplementation(IncestuousSingletons.B.class,
+ IncestuousSingletons.BN.class);
+ b.bindImplementation(IncestuousSingletons.C.class,
+ IncestuousSingletons.C.class);
+ Injector i = t.newInjector(b.build());
+ i.getInstance(IncestuousSingletons.A.class);
+ }
+
+ @Test
+ public void testIncestuousInterfaceSingletons() throws BindException,
+ InjectionException {
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder b = t.newConfigurationBuilder();
+ b.bindImplementation(IncestuousInterfaceSingletons.AI.class,
+ IncestuousInterfaceSingletons.A.class);
+ b.bindImplementation(IncestuousInterfaceSingletons.BI.class,
+ IncestuousInterfaceSingletons.BN.class);
+ b.bindImplementation(IncestuousInterfaceSingletons.CI.class,
+ IncestuousInterfaceSingletons.C.class);
+ Injector i = t.newInjector(b.build());
+ i.getInstance(IncestuousInterfaceSingletons.AI.class);
+ }
+
+ @Test
+ public void testIncestuousInterfaceSingletons2() throws BindException,
+ InjectionException {
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder b = t.newConfigurationBuilder();
+ b.bindImplementation(IncestuousInterfaceSingletons.AI.class,
+ IncestuousInterfaceSingletons.A.class);
+ b.bindImplementation(IncestuousInterfaceSingletons.BI.class,
+ IncestuousInterfaceSingletons.B.class);
+ // TODO: Should we require bind(A,B), then bind(B,B) if B has subclasses?
+ b.bindImplementation(IncestuousInterfaceSingletons.B.class,
+ IncestuousInterfaceSingletons.B.class);
+ b.bindImplementation(IncestuousInterfaceSingletons.CI.class,
+ IncestuousInterfaceSingletons.C.class);
+ Injector i = t.newInjector(b.build());
+ i.getInstance(IncestuousInterfaceSingletons.AI.class);
+ }
+
+ @Test
+ public void testIsBrokenClassInjectable() throws BindException {
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder b = t.newConfigurationBuilder();
+ b.bind(IsBrokenClassInjectable.class, IsBrokenClassInjectable.class);
+ Assert.assertTrue(t.newInjector(b.build()).isInjectable(
+ IsBrokenClassInjectable.class));
+ }
+
+ @Test
+ public void testIsBrokenSingletonClassInjectable() throws BindException {
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder b = t.newConfigurationBuilder();
+ b.bindImplementation(IsBrokenClassInjectable.class,
+ IsBrokenClassInjectable.class);
+ Assert.assertTrue(t.newInjector(b.build()).isInjectable(
+ IsBrokenClassInjectable.class));
+ }
+
+ @Test(expected = InjectionException.class)
+ public void testBrokenSingletonClassCantInject() throws BindException, InjectionException {
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder b = t.newConfigurationBuilder();
+ b.bindImplementation(IsBrokenClassInjectable.class,
+ IsBrokenClassInjectable.class);
+ Assert.assertTrue(t.newInjector(b.build()).isInjectable(
+ IsBrokenClassInjectable.class));
+ t.newInjector(b.build()).getInstance(IsBrokenClassInjectable.class);
+ }
+
+ public static class A {
+ @Inject
+ public A() {
+ // Intentionally blank
+ }
+
+ }
+
+ public static class AA {
+ @Inject
+ public AA() {
+ // Intentionally blank
+ }
+
+ }
+
+ public static class B extends A {
+ @Inject
+ public B() {
+ // intentionally blank
+ }
+ }
+}
+
+class LateBoundVolatile {
+ static class A {
+ }
+
+ static class B extends A {
+ @Inject
+ B(C c) {
+ }
+ }
+
+ static class C {
+ }
+}
+
+class InbredSingletons {
+ static class A {
+ static int count = 0;
+
+ @Inject
+ A(B b) {
+ Assert.assertEquals(0, count);
+ count++;
+ }
+ }
+
+ static class B {
+ static int count = 0;
+
+ @Inject
+ B(C c) {
+ Assert.assertEquals(0, count);
+ count++;
+ }
+ }
+
+ static class C {
+ static int count = 0;
+
+ @Inject
+ C() {
+ Assert.assertEquals(0, count);
+ count++;
+ }
+ }
+}
+
+class IncestuousSingletons {
+ static class A {
+ static int count = 0;
+
+ @Inject
+ A(C c, B b) {
+ Assert.assertEquals(0, count);
+ count++;
+ }
+ }
+
+ static class B {
+ static int count = 0;
+
+ protected B() {
+ }
+
+ @Inject
+ B(C c) {
+ Assert.assertEquals(0, count);
+ count++;
+ }
+ }
+
+ static class BN extends B {
+ static int count = 0;
+
+ @Inject
+ BN(C c) {
+ super();
+ Assert.assertEquals(0, count);
+ count++;
+ }
+ }
+
+ static class C {
+ static int count = 0;
+
+ @Inject
+ C() {
+ Assert.assertEquals(0, count);
+ count++;
+ }
+ }
+}
+
+class IncestuousInterfaceSingletons {
+ interface AI {
+ }
+
+ interface BI {
+ }
+
+ interface CI {
+ }
+
+ static class A implements AI {
+ static int count = 0;
+
+ @Inject
+ A(CI c, BI b) {
+ Assert.assertEquals(0, count);
+ count++;
+ }
+ }
+
+ static class B implements BI {
+ static int count = 0;
+
+ protected B() {
+ }
+
+ @Inject
+ B(CI c) {
+ Assert.assertEquals(0, count);
+ count++;
+ }
+ }
+
+ static class BN extends B {
+ static int count = 0;
+
+ @Inject
+ BN(CI c) {
+ super();
+ Assert.assertEquals(0, count);
+ count++;
+ }
+ }
+
+ static class C implements CI {
+ static int count = 0;
+
+ @Inject
+ C() {
+ Assert.assertEquals(0, count);
+ count++;
+ }
+ }
+}
+
+class IsBrokenClassInjectable {
+ @Inject
+ public IsBrokenClassInjectable() {
+ throw new UnsupportedOperationException();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/53ea32cc/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestClassLoaders.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestClassLoaders.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestClassLoaders.java
new file mode 100644
index 0000000..4394abb
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestClassLoaders.java
@@ -0,0 +1,118 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.reef.tang;
+
+import org.apache.reef.tang.exceptions.BindException;
+import org.apache.reef.tang.exceptions.InjectionException;
+import org.apache.reef.tang.exceptions.NameResolutionException;
+import org.apache.reef.tang.types.ClassNode;
+import org.junit.Assert;
+
+import java.io.File;
+import java.net.MalformedURLException;
+
+public class TestClassLoaders {
+
+ // @Test
+ public void testOneJar() throws MalformedURLException,
+ ClassNotFoundException, NameResolutionException, BindException {
+ Tang.Factory
+ .getTang()
+ .newConfigurationBuilder(
+ new File("../tang-test-jarA/target/tang-test-jarA-1.0-SNAPSHOT.jar")
+ .toURI().toURL()).getClassHierarchy().getNode("org.apache.reef.tang.examples.A");
+
+ }
+
+ // @Test
+ public void testTwoJars() throws MalformedURLException,
+ ClassNotFoundException, BindException, InjectionException, NameResolutionException {
+ Tang t = Tang.Factory.getTang();
+
+ JavaConfigurationBuilder cbA = t.newConfigurationBuilder(new File(
+ "../tang-test-jarA/target/tang-test-jarA-1.0-SNAPSHOT.jar").toURI()
+ .toURL());
+ JavaConfigurationBuilder cbB = t.newConfigurationBuilder(new File(
+ "../tang-test-jarB/target/tang-test-jarB-1.0-SNAPSHOT.jar").toURI()
+ .toURL());
+ cbA.addConfiguration(cbB.build());
+
+ cbA.getClassHierarchy().getNode("org.apache.reef.tang.examples.A");
+ cbA.getClassHierarchy().getNode("org.apache.reef.tang.examples.B");
+
+ t.newInjector(cbA.build());
+ }
+
+ // @Test
+ public void testTwoClasses() throws MalformedURLException,
+ ClassNotFoundException, BindException, InjectionException, NameResolutionException {
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder cbA = t.newConfigurationBuilder(new File(
+ "../tang-test-jarAB/target/tang-test-jarAB-1.0-SNAPSHOT.jar").toURI()
+ .toURL());
+ JavaConfigurationBuilder cbB = t.newConfigurationBuilder(new File(
+ "../tang-test-jarAB/target/tang-test-jarAB-1.0-SNAPSHOT.jar").toURI()
+ .toURL());
+ cbA.addConfiguration(cbB.build());
+
+ cbA.getClassHierarchy().getNode("org.apache.reef.tang.examples.A");
+ cbA.getClassHierarchy().getNode("org.apache.reef.tang.examples.B");
+
+ t.newInjector(cbA.build());
+ }
+
+ // @Test
+ public void aliasingNameSameDifferentTypes()
+ throws MalformedURLException, InjectionException, BindException,
+ ClassNotFoundException, NameResolutionException {
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder cbA1 = t.newConfigurationBuilder(new File(
+ "../tang-test-jarAB/target/tang-test-jarAB-1.0-SNAPSHOT.jar").toURI()
+ .toURL());
+ JavaConfigurationBuilder cbA2 = t.newConfigurationBuilder(new File(
+ "../tang-test-jarAB/target/tang-test-jarAB-1.0-SNAPSHOT.jar").toURI()
+ .toURL());
+ cbA1.getClassHierarchy().getNode("org.apache.reef.tang.examples.A");
+ cbA1.bind("org.apache.reef.tang.examples.A", "org.apache.reef.tang.examples.B");
+ cbA2.bind("org.apache.reef.tang.examples.A", "org.apache.reef.tang.examples.B");
+ Object o = t.newInjector(cbA1.build()).getInstance("org.apache.reef.tang.examples.A");
+ Object p = t.newInjector(cbA2.build()).getInstance("org.apache.reef.tang.examples.A");
+ Assert.assertSame(o.getClass(), p.getClass());
+ JavaConfigurationBuilder cbAother = t.newConfigurationBuilder(new File(
+ "../tang-test-jarA/target/tang-test-jarA-1.0-SNAPSHOT.jar").toURI()
+ .toURL());
+
+ Assert.assertEquals(1, ((ClassNode<?>) (cbA1.getClassHierarchy().getNode("org.apache.reef.tang.examples.A"))).getInjectableConstructors().length);
+ Assert.assertEquals(0, ((ClassNode<?>) (cbAother.getClassHierarchy().getNode("org.apache.reef.tang.examples.A"))).getInjectableConstructors().length);
+
+ }
+
+ // @Test
+ public void testOneClassOneJar()
+ throws MalformedURLException, InjectionException, BindException,
+ ClassNotFoundException, NameResolutionException {
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder cbA1 = t.newConfigurationBuilder(new File(
+ "../tang-test-jarAB/target/tang-test-jarAB-1.0-SNAPSHOT.jar").toURI()
+ .toURL());
+ cbA1.bind("org.apache.reef.tang.examples.A", "org.apache.reef.tang.examples.B");
+ cbA1.getClassHierarchy().getNode("org.apache.reef.tang.examples.A");
+ t.newInjector(cbA1.build()).getInstance("org.apache.reef.tang.examples.B");
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/53ea32cc/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestConfFileParser.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestConfFileParser.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestConfFileParser.java
new file mode 100644
index 0000000..694351f
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestConfFileParser.java
@@ -0,0 +1,119 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.reef.tang;
+
+import org.apache.reef.tang.annotations.Name;
+import org.apache.reef.tang.annotations.NamedParameter;
+import org.apache.reef.tang.exceptions.BindException;
+import org.apache.reef.tang.exceptions.InjectionException;
+import org.apache.reef.tang.formats.ConfigurationFile;
+import org.apache.reef.tang.implementation.TangImpl;
+import org.apache.reef.tang.util.ReflectionUtilities;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.inject.Inject;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+public class TestConfFileParser {
+
+ @Before
+ public void setUp() {
+ TangImpl.reset();
+ }
+
+ @Test
+ public void testRoundTrip() throws BindException {
+ // TODO: This likely only passes on windows, as it relies on newlines
+ // being \r\n, and on java.lang.Object having a lower hash code than
+ // org.apache.reef.tang.TestConfFileParser
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder cb = t.newConfigurationBuilder();
+ String in = "org.apache.reef.tang.TestConfFileParser=org.apache.reef.tang.TestConfFileParser\n";
+ ConfigurationFile.addConfiguration(cb, in);
+ String out = ConfigurationFile.toConfigurationString(cb.build());
+ Assert.assertEquals(in, out);
+ }
+
+ @Test
+ public void testBindSingleton() throws BindException {
+ // TODO: This likely only passes on windows, as it relies on newlines
+ // being \r\n, and on java.lang.Object having a lower hash code than
+ // org.apache.reef.tang.TestConfFileParser
+ Tang t = Tang.Factory.getTang();
+ JavaConfigurationBuilder cb = t.newConfigurationBuilder();
+ cb.bindImplementation(SingleTest.A.class, SingleTest.B.class);
+
+ String out = ConfigurationFile.toConfigurationString(cb.build());
+ String in = "org.apache.reef.tang.SingleTest$A=org.apache.reef.tang.SingleTest$B\n";
+ Assert.assertEquals(in, out);
+ }
+
+ @Test
+ public void testNamedParameter() throws BindException {
+ Tang t = Tang.Factory.getTang();
+ String conf = "org.apache.reef.tang.TestConfFileParser$Foo=woot";
+ ConfigurationBuilder cb = t.newConfigurationBuilder();
+ ConfigurationFile.addConfiguration(cb, conf);
+ Assert.assertTrue(t.newInjector(cb.build()).isParameterSet(Foo.class));
+ }
+
+ @Test
+ public void testNamedParameter2() throws BindException, IOException, InjectionException {
+
+ final String value = "socket://131.179.176.216:19278";
+ final File tmp = File.createTempFile("test", "conf");
+
+ try (final FileOutputStream fout = new FileOutputStream(tmp)) {
+ final String line = ReflectionUtilities.getFullName(RemoteIdentifier.class) + "=" + value;
+ fout.write(line.getBytes());
+ }
+
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ ConfigurationFile.addConfiguration(cb, tmp);
+ final Injector i = Tang.Factory.getTang().newInjector(cb.build());
+ Assert.assertEquals(value, i.getNamedInstance(RemoteIdentifier.class));
+ }
+
+ @NamedParameter(doc = "remote id.")
+ private final static class RemoteIdentifier implements Name<String> {
+ }
+
+ @NamedParameter()
+ class Foo implements Name<String> {
+ }
+}
+
+@NamedParameter
+final class Foo implements Name<String> {
+}
+
+class SingleTest {
+ static class A {
+ }
+
+ static class B extends A {
+ @Inject
+ B() {
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/53ea32cc/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestExternalConstructor.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestExternalConstructor.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestExternalConstructor.java
new file mode 100644
index 0000000..66748c4
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestExternalConstructor.java
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.reef.tang;
+
+import org.apache.reef.tang.exceptions.BindException;
+import org.apache.reef.tang.exceptions.InjectionException;
+import org.junit.Test;
+
+import javax.inject.Inject;
+
+public class TestExternalConstructor {
+
+ @Test
+ public void testExternalConstructor() throws BindException,
+ InjectionException {
+
+ final JavaConfigurationBuilder b = Tang.Factory.getTang()
+ .newConfigurationBuilder();
+ b.bindConstructor(A.class, ACons.class);
+ b.bindConstructor(B.class, BCons.class);
+
+ Tang.Factory.getTang().newInjector(b.build()).getInstance(B.class);
+ }
+
+ static final class A {
+ A() {
+ }
+ }
+
+ static final class B {
+ B(final A a) {
+ }
+ }
+
+ static final class ACons implements ExternalConstructor<A> {
+
+ @Inject
+ ACons() {
+ }
+
+ @Override
+ public A newInstance() {
+ return new A();
+ }
+ }
+
+ static final class BCons implements ExternalConstructor<B> {
+
+ @Inject
+ BCons(final A a) {
+ }
+
+ @Override
+ public B newInstance() {
+ return new B(null);
+ }
+ }
+
+/* @Test
+ public void testExplicitExternalConstructorIsSingleton() throws BindException, InjectionException {
+ final JavaConfigurationBuilder b = Tang.Factory.getTang()
+ .newConfigurationBuilder();
+ b.bindConstructor(A.class, ACons.class);
+
+ Injector i = Tang.Factory.getTang().newInjector(b.build());
+
+ A a = i.getInstance(A.class);
+ A aa = (i.getInstance(ACons.class)).newInstance();
+
+ Assert.assertTrue(a == aa);
+
+ } */
+}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/53ea32cc/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestImplicitConversions.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestImplicitConversions.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestImplicitConversions.java
new file mode 100644
index 0000000..cc6bf9e
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestImplicitConversions.java
@@ -0,0 +1,167 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.reef.tang;
+
+import org.apache.reef.tang.annotations.Name;
+import org.apache.reef.tang.annotations.NamedParameter;
+import org.apache.reef.tang.exceptions.BindException;
+import org.apache.reef.tang.exceptions.InjectionException;
+import org.apache.reef.tang.formats.ConfigurationFile;
+import org.apache.reef.tang.types.NamedParameterNode;
+import org.apache.reef.tang.util.ReflectionUtilities;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.inject.Inject;
+
+public class TestImplicitConversions {
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testBindFromString() throws BindException, InjectionException {
+ JavaConfigurationBuilder b = Tang.Factory.getTang().newConfigurationBuilder(IdentifierParser.class);
+ b.bindNamedParameter(IdName.class, "b://b");
+
+ Configuration c = b.build();
+ String s = ConfigurationFile.toConfigurationString(c);
+
+ JavaConfigurationBuilder b2 = Tang.Factory.getTang().newConfigurationBuilder(IdentifierParser.class);
+ ConfigurationFile.addConfiguration(b2, s);
+ Configuration c2 = b2.build();
+
+ Assert.assertEquals("b://b", c2.getNamedParameter((NamedParameterNode<?>) c2.getClassHierarchy().getNode(ReflectionUtilities.getFullName(IdName.class))));
+ Injector i = Tang.Factory.getTang().newInjector(c2);
+
+ Assert.assertEquals("b://b", i.getNamedInstance(IdName.class).toString());
+ Assert.assertTrue(i.getNamedInstance(IdName.class) instanceof BIdentifier);
+
+ }
+
+ ;
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testBindSubclassFromString() throws BindException, InjectionException {
+ JavaConfigurationBuilder b = Tang.Factory.getTang().newConfigurationBuilder(IdentifierParser.class);
+ b.bindNamedParameter(AIdName.class, "a://a");
+ b.bindNamedParameter(BIdName.class, "b://b");
+
+ Configuration c = b.build();
+ String s = ConfigurationFile.toConfigurationString(c);
+
+ JavaConfigurationBuilder b2 = Tang.Factory.getTang().newConfigurationBuilder(IdentifierParser.class);
+ ConfigurationFile.addConfiguration(b2, s);
+ Configuration c2 = b2.build();
+
+ Assert.assertEquals("b://b", c2.getNamedParameter((NamedParameterNode<?>) c2.getClassHierarchy().getNode(ReflectionUtilities.getFullName(BIdName.class))));
+ Injector i = Tang.Factory.getTang().newInjector(c2);
+
+ Assert.assertEquals("b://b", i.getNamedInstance(BIdName.class).toString());
+ Assert.assertTrue(i.getNamedInstance(BIdName.class) instanceof BIdentifier);
+ Assert.assertEquals("a://a", i.getNamedInstance(AIdName.class).toString());
+ Assert.assertTrue(i.getNamedInstance(AIdName.class) instanceof AIdentifier);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test(expected = ClassCastException.class)
+ public void testBindWrongSubclassFromString() throws BindException, InjectionException {
+ JavaConfigurationBuilder b = Tang.Factory.getTang().newConfigurationBuilder(IdentifierParser.class);
+ b.bindNamedParameter(AIdName.class, "b://b");
+ }
+
+ ;
+
+ @Test(expected = InjectionException.class)
+ public void testInjectUnboundParsable() throws BindException, InjectionException {
+ @SuppressWarnings("unchecked")
+ JavaConfigurationBuilder b = Tang.Factory.getTang().newConfigurationBuilder(IdentifierParser.class);
+ Tang.Factory.getTang().newInjector(b.build()).getNamedInstance(IdName.class);
+ }
+
+ static interface Identifier {
+
+ }
+
+ ;
+
+ static interface AIdentifier extends Identifier {
+
+ }
+
+ static interface BIdentifier extends Identifier {
+
+ }
+
+ static class AIdentifierImpl implements AIdentifier {
+ private final String aString;
+
+ @Inject
+ AIdentifierImpl(String aString) {
+ this.aString = aString;
+ }
+
+ @Override
+ public String toString() {
+ return aString;
+ }
+ }
+
+ static class BIdentifierImpl implements BIdentifier {
+ private final String bString;
+
+ @Inject
+ BIdentifierImpl(String bString) {
+ this.bString = bString;
+ }
+
+ @Override
+ public String toString() {
+ return bString;
+ }
+ }
+
+ static class IdentifierParser implements ExternalConstructor<Identifier> {
+ final Identifier id;
+
+ @Inject
+ public IdentifierParser(String id) {
+ this.id = id.startsWith("a://") ? new AIdentifierImpl(id) : id.startsWith("b://") ? new BIdentifierImpl(id) : null;
+ if (this.id == null) {
+ throw new IllegalArgumentException("Need string that starts with a:// or b://!");
+ }
+ }
+
+ @Override
+ public Identifier newInstance() {
+ return id;
+ }
+ }
+
+ @NamedParameter
+ class IdName implements Name<Identifier> {
+ }
+
+ @NamedParameter
+ class AIdName implements Name<AIdentifier> {
+ }
+
+ @NamedParameter
+ class BIdName implements Name<BIdentifier> {
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/53ea32cc/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestInjectionFuture.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestInjectionFuture.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestInjectionFuture.java
new file mode 100644
index 0000000..dca2359
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestInjectionFuture.java
@@ -0,0 +1,195 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.reef.tang;
+
+import org.apache.reef.tang.annotations.DefaultImplementation;
+import org.apache.reef.tang.annotations.Name;
+import org.apache.reef.tang.annotations.NamedParameter;
+import org.apache.reef.tang.annotations.Parameter;
+import org.apache.reef.tang.exceptions.BindException;
+import org.apache.reef.tang.exceptions.InjectionException;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.inject.Inject;
+
+interface A {
+}
+
+@DefaultImplementation(C.class)
+interface B extends A {
+}
+
+public class TestInjectionFuture {
+ @Test
+ public void testFutures() throws InjectionException, BindException {
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ Injector i = Tang.Factory.getTang().newInjector(cb.build());
+ Injector i2 = Tang.Factory.getTang().newInjector(cb.build());
+
+ Futurist f = i.getInstance(Futurist.class);
+ Assert.assertTrue(f == f.getMyCar().getDriver());
+ Assert.assertTrue(f.getMyCar() == f.getMyCar().getDriver().getMyCar());
+
+ Futurist f2 = i2.getInstance(Futurist.class);
+ Assert.assertTrue(f2 == f2.getMyCar().getDriver());
+ Assert.assertTrue(f2.getMyCar() == f2.getMyCar().getDriver().getMyCar());
+
+ Assert.assertTrue(f != f2.getMyCar().getDriver());
+ Assert.assertTrue(f.getMyCar() != f2.getMyCar().getDriver().getMyCar());
+
+ }
+
+ @Test
+ public void testFutures2() throws InjectionException, BindException {
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ Injector i = Tang.Factory.getTang().newInjector(cb.build());
+ Injector i2 = i.forkInjector();
+
+ FlyingCar c = i.getInstance(FlyingCar.class);
+ Assert.assertTrue(c == c.getDriver().getMyCar());
+ Assert.assertTrue(c.getDriver() == c.getDriver().getMyCar().getDriver());
+
+ FlyingCar c2 = i2.getInstance(FlyingCar.class);
+ Assert.assertTrue(c2 == c2.getDriver().getMyCar());
+ Assert.assertTrue(c2.getDriver() == c2.getDriver().getMyCar().getDriver());
+
+ Assert.assertTrue(c2 != c.getDriver().getMyCar());
+ Assert.assertTrue(c2.getDriver() != c.getDriver().getMyCar().getDriver());
+
+ }
+
+ @Test
+ public void testNamedParameterInjectionFuture() throws InjectionException, BindException {
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindImplementation(FlyingCar.class, FlyingCar.class);
+ Injector i = Tang.Factory.getTang().newInjector(cb.build());
+ PickyFuturist f = i.getInstance(PickyFuturist.class);
+ Assert.assertNotNull(f.getMyCar());
+ }
+
+ @Test
+ public void testNamedParameterInjectionFutureDefaultImpl() throws InjectionException, BindException {
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ Injector i = Tang.Factory.getTang().newInjector(cb.build());
+ PickyFuturist f = i.getInstance(PickyFuturist.class);
+ Assert.assertNotNull(f.getMyCar());
+ }
+
+ @Test
+ public void testNamedParameterInjectionFutureBindImpl() throws InjectionException, BindException {
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindImplementation(Futurist.class, PickyFuturist.class);
+ cb.bindNamedParameter(MyFlyingCar.class, BigFlyingCar.class);
+ Injector i = Tang.Factory.getTang().newInjector(cb.build());
+ PickyFuturist f = i.getInstance(PickyFuturist.class);
+ Assert.assertNotNull((BigFlyingCar) f.getMyCar());
+ }
+
+ @Test
+ public void testNamedParameterBoundToDelegatingInterface() throws InjectionException, BindException {
+ Injector i = Tang.Factory.getTang().newInjector();
+ @SuppressWarnings("unused")
+ C c = (C) i.getNamedInstance(AName.class);
+ }
+
+ @Test
+ public void testBoundToDelegatingInterface() throws InjectionException, BindException {
+ Injector i = Tang.Factory.getTang().newInjector();
+ @SuppressWarnings("unused")
+ C c = (C) i.getInstance(B.class);
+ }
+
+
+ @DefaultImplementation(Futurist.class)
+ public static class Futurist {
+ private final InjectionFuture<FlyingCar> f_car;
+
+ @Inject
+ public Futurist(InjectionFuture<FlyingCar> car) {
+ this.f_car = car;
+ }
+
+ public FlyingCar getMyCar() {
+ FlyingCar c = f_car.get();
+ return c;
+ }
+ }
+
+ public static class PickyFuturist extends Futurist {
+ private final InjectionFuture<FlyingCar> f_car;
+
+ @Inject
+ public PickyFuturist(@Parameter(MyFlyingCar.class) InjectionFuture<FlyingCar> myFlyingCar) {
+ super(myFlyingCar);
+ f_car = myFlyingCar;
+ }
+
+ public FlyingCar getMyCar() {
+ FlyingCar c = f_car.get();
+ return c;
+ }
+ }
+
+ @DefaultImplementation(FlyingCar.class)
+ public static class FlyingCar {
+ private final String color;
+ private final Futurist driver;
+
+ @Inject
+ FlyingCar(@Parameter(Color.class) String color, Futurist driver) {
+ this.color = color;
+ this.driver = driver;
+ }
+
+ public String getColor() {
+ return color;
+ }
+
+ public Futurist getDriver() {
+ return driver;
+ }
+
+ @NamedParameter(default_value = "blue")
+ class Color implements Name<String> {
+ }
+ }
+
+ public static class BigFlyingCar extends FlyingCar {
+ @Inject
+ BigFlyingCar(@Parameter(Color.class) String color, Futurist driver) {
+ super(color, driver);
+ }
+ }
+
+ @NamedParameter(default_class = FlyingCar.class)
+ public static class MyFlyingCar implements Name<FlyingCar> {
+ }
+
+}
+
+@NamedParameter(default_class = B.class)
+class AName implements Name<A> {
+}
+
+class C implements B {
+ @Inject
+ C() {
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/53ea32cc/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestListInjection.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestListInjection.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestListInjection.java
new file mode 100644
index 0000000..9abc82d
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestListInjection.java
@@ -0,0 +1,344 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.reef.tang;
+
+import org.apache.reef.tang.annotations.Name;
+import org.apache.reef.tang.annotations.NamedParameter;
+import org.apache.reef.tang.annotations.Parameter;
+import org.apache.reef.tang.exceptions.InjectionException;
+import org.apache.reef.tang.formats.ConfigurationModule;
+import org.apache.reef.tang.formats.ConfigurationModuleBuilder;
+import org.apache.reef.tang.formats.RequiredParameter;
+import org.apache.reef.tang.types.ClassNode;
+import org.apache.reef.tang.types.NamedParameterNode;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.inject.Inject;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Tests for list injection in Tang.
+ */
+public class TestListInjection {
+
+ /**
+ * Test code for injecting default list with string elements
+ *
+ * @throws InjectionException
+ */
+ @Test
+ public void testStringInjectDefault() throws InjectionException {
+ List<String> actual = Tang.Factory.getTang().newInjector().getInstance(StringClass.class).stringList;
+ List<String> expected = new ArrayList<>();
+ expected.add("bye");
+ expected.add("hello");
+ expected.add("hi");
+ Assert.assertEquals(expected, actual);
+ }
+
+ /**
+ * Test code for injecting default list with non-string values
+ *
+ * @throws InjectionException
+ */
+ @Test
+ public void testIntegerInjectDefault() throws InjectionException {
+ List<Integer> actual = Tang.Factory.getTang().newInjector().getInstance(IntegerClass.class).integerList;
+ List<Integer> expected = new ArrayList<>();
+ expected.add(1);
+ expected.add(2);
+ expected.add(3);
+ Assert.assertEquals(expected, actual);
+ }
+
+ /**
+ * Test code for injecting default list with implementations
+ *
+ * @throws InjectionException
+ */
+ @Test
+ public void testObjectInjectDefault() throws InjectionException {
+ Integer integer = 1;
+ Float ffloat = 1.001f;
+
+ Injector injector = Tang.Factory.getTang().newInjector();
+ injector.bindVolatileInstance(Integer.class, integer);
+ injector.bindVolatileInstance(Float.class, ffloat);
+ List<Number> actual = injector.getInstance(NumberClass.class).numberList;
+ List<Number> expected = new ArrayList<>();
+ expected.add(integer);
+ expected.add(ffloat);
+ Assert.assertEquals(expected, actual);
+ }
+
+ /**
+ * Test code for injecting list with String elements
+ *
+ * @throws InjectionException
+ */
+ @Test
+ public void testStringInjectBound() throws InjectionException {
+ List<String> injected = new ArrayList<>();
+ injected.add("hi");
+ injected.add("hello");
+ injected.add("bye");
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindList(StringList.class, injected);
+ List<String> actual = Tang.Factory.getTang().newInjector(cb.build()).getInstance(StringClass.class).stringList;
+ List<String> expected = new ArrayList<>();
+ expected.add("hi");
+ expected.add("hello");
+ expected.add("bye");
+ Assert.assertEquals(expected, actual);
+ }
+
+ /**
+ * Test code for injecting list with parsable non-string values
+ *
+ * @throws InjectionException
+ */
+ @Test
+ public void testIntegerInjectBound() throws InjectionException {
+ List<String> injected = new ArrayList<>();
+ injected.add("1");
+ injected.add("2");
+ injected.add("3");
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindList(IntegerList.class, injected);
+
+ List<Integer> actual = Tang.Factory.getTang().newInjector(cb.build()).getInstance(IntegerClass.class).integerList;
+ List<Integer> expected = new ArrayList<>();
+ expected.add(1);
+ expected.add(2);
+ expected.add(3);
+ Assert.assertEquals(expected, actual);
+ }
+
+ /**
+ * Test code for injecting list with implementations
+ *
+ * @throws InjectionException
+ */
+ @Test
+ public void testObjectInjectBound() throws InjectionException {
+ Integer integer = 1;
+ Float ffloat = 1.001f;
+
+ // Inject implementations via class object
+ List<Class> injected1 = new ArrayList<>();
+ injected1.add(Integer.class);
+ injected1.add(Float.class);
+ JavaConfigurationBuilder cb1 = Tang.Factory.getTang().newConfigurationBuilder();
+ cb1.bindList(NumberList.class, injected1);
+ Injector injector1 = Tang.Factory.getTang().newInjector(cb1.build());
+ injector1.bindVolatileInstance(Integer.class, integer);
+ injector1.bindVolatileInstance(Float.class, ffloat);
+ List<Number> actual1 = injector1.getInstance(NumberClass.class).numberList;
+
+ // Inject implementations via class name
+ List<String> injected2 = new ArrayList<>();
+ injected2.add("java.lang.Integer");
+ injected2.add("java.lang.Float");
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindList(NumberList.class, injected2);
+ Injector injector2 = Tang.Factory.getTang().newInjector(cb.build());
+ injector2.bindVolatileInstance(Integer.class, integer);
+ injector2.bindVolatileInstance(Float.class, ffloat);
+ List<Number> actual2 = injector2.getInstance(NumberClass.class).numberList;
+
+ List<Number> expected = new ArrayList<>();
+ expected.add(integer);
+ expected.add(ffloat);
+ Assert.assertEquals(expected, actual1);
+ Assert.assertEquals(expected, actual2);
+ }
+
+ // TODO: Make tests for list serialization/deserialization after implementing those features.
+
+ /**
+ * Test code for Tang selectivity.
+ *
+ * @throws InjectionException
+ */
+ @Test
+ public void testInjectSelectiveConstructor() throws InjectionException {
+ // Test injection without list binding
+ List<String> actual1 = Tang.Factory.getTang().newInjector().getInstance(SelectiveConsructorClass.class).list;
+ List<String> expected1 = new ArrayList<>();
+ Assert.assertEquals(expected1, actual1);
+ // Test injection with list binding
+ List<String> injected = new ArrayList<>();
+ injected.add("hi");
+ injected.add("hello");
+ injected.add("bye");
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindList(SelectiveInjectTestList.class, injected);
+ List<String> actual2 = Tang.Factory.getTang().newInjector(cb.build()).getInstance(SelectiveConsructorClass.class)
+ .list;
+ List<String> expected2 = new ArrayList<>();
+ expected2.add("hi");
+ expected2.add("hello");
+ expected2.add("bye");
+ Assert.assertEquals(expected2, actual2);
+ }
+
+ /**
+ * Test code for injecting list of strings with ConfigurationBuilder
+ *
+ * @throws InjectionException
+ */
+ @Test
+ public void testStringInjectConfigurationBuilder() throws InjectionException {
+ JavaClassHierarchy namespace = Tang.Factory.getTang().getDefaultClassHierarchy();
+ NamedParameterNode<List<String>> np = (NamedParameterNode) namespace.getNode(StringList.class);
+ List<String> injected = new ArrayList<>();
+ injected.add("hi");
+ injected.add("hello");
+ injected.add("bye");
+
+ ConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindList(np, injected);
+ List<String> actual = Tang.Factory.getTang().newInjector(cb.build()).getInstance(StringClass.class).stringList;
+ List<String> expected = new ArrayList<>();
+ expected.add("hi");
+ expected.add("hello");
+ expected.add("bye");
+ Assert.assertEquals(expected, actual);
+ }
+
+ /**
+ * Test code for injecting list of implementations with ConfigurationBuilder
+ *
+ * @throws InjectionException
+ */
+ @Test
+ public void testObjectInjectConfigurationBuilder() throws InjectionException {
+ Integer integer = 1;
+ Float ffloat = 1.001f;
+
+ JavaClassHierarchy namespace = Tang.Factory.getTang().getDefaultClassHierarchy();
+ NamedParameterNode<List<Class>> np = (NamedParameterNode) namespace.getNode(NumberList.class);
+ List<ClassNode> injected = new ArrayList<>();
+ injected.add((ClassNode) namespace.getNode(Integer.class));
+ injected.add((ClassNode) namespace.getNode(Float.class));
+
+ ConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindList(np, injected);
+
+ Injector injector = Tang.Factory.getTang().newInjector(cb.build());
+ injector.bindVolatileInstance(Integer.class, integer);
+ injector.bindVolatileInstance(Float.class, ffloat);
+ List<Number> actual = injector.getInstance(NumberClass.class).numberList;
+ List<Number> expected = new ArrayList<>();
+ expected.add(integer);
+ expected.add(ffloat);
+ Assert.assertEquals(expected, actual);
+ }
+
+ /**
+ * Test code for injectiong list with ConfigurationModule
+ *
+ * @throws InjectionException
+ */
+ @Test
+ public void testInjectConfigurationModule() throws InjectionException {
+ List<String> injected = new ArrayList<>();
+ injected.add("hi");
+ injected.add("hello");
+ injected.add("bye");
+ Configuration conf = StringClassConfiguration.CONF
+ .set(StringClassConfiguration.STRING_LIST, injected)
+ .build();
+ List<String> actual = Tang.Factory.getTang().newInjector(conf).getInstance(StringClass.class).stringList;
+ List<String> expected = new ArrayList<>();
+ expected.add("hi");
+ expected.add("hello");
+ expected.add("bye");
+ Assert.assertEquals(expected, actual);
+ }
+
+ // ConfigurationModuleBuilder for StringClass
+ public static class StringClassConfiguration extends ConfigurationModuleBuilder {
+ public static final RequiredParameter<List> STRING_LIST = new RequiredParameter<>();
+
+ public static final ConfigurationModule CONF = new StringClassConfiguration()
+ .bindList(StringList.class, StringClassConfiguration.STRING_LIST)
+ .build();
+ }
+}
+
+@NamedParameter(default_values = {"bye", "hello", "hi"})
+class StringList implements Name<List<String>> {
+}
+
+@NamedParameter(default_values = {"1", "2", "3"})
+class IntegerList implements Name<List<Integer>> {
+}
+
+@NamedParameter(default_values = {"java.lang.Integer", "java.lang.Float"})
+class NumberList implements Name<List<Number>> {
+}
+
+@NamedParameter
+class SelectiveInjectTestList implements Name<List<String>> {
+}
+
+class SelectiveConsructorClass {
+ public final List<String> list;
+
+ @Inject
+ SelectiveConsructorClass() {
+ list = new ArrayList<>();
+ }
+
+ @Inject
+ SelectiveConsructorClass(@Parameter(SelectiveInjectTestList.class) final List<String> list) {
+ this.list = list;
+ }
+
+}
+
+class StringClass {
+ public final List<String> stringList;
+
+ @Inject
+ StringClass(@Parameter(StringList.class) final List<String> stringList) {
+ this.stringList = stringList;
+ }
+}
+
+class IntegerClass {
+ public final List<Integer> integerList;
+
+ @Inject
+ IntegerClass(@Parameter(IntegerList.class) final List<Integer> integerList) {
+ this.integerList = integerList;
+ }
+}
+
+class NumberClass {
+ public final List<Number> numberList;
+
+ @Inject
+ NumberClass(@Parameter(NumberList.class) final List<Number> numberList) {
+ this.numberList = numberList;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/53ea32cc/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestNamedParameterRoundTrip.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestNamedParameterRoundTrip.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestNamedParameterRoundTrip.java
new file mode 100644
index 0000000..6ba9ab5
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestNamedParameterRoundTrip.java
@@ -0,0 +1,105 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.reef.tang;
+
+import org.apache.reef.tang.annotations.Name;
+import org.apache.reef.tang.annotations.NamedParameter;
+import org.apache.reef.tang.exceptions.BindException;
+import org.apache.reef.tang.exceptions.InjectionException;
+import org.apache.reef.tang.formats.ConfigurationFile;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class TestNamedParameterRoundTrip {
+
+ @Test
+ public void testRoundTrip() throws BindException, InjectionException {
+ final int d = 10;
+ final double eps = 1e-5;
+ final JavaConfigurationBuilder b = Tang.Factory.getTang().newConfigurationBuilder();
+ b.bindNamedParameter(Dimensionality.class, String.valueOf(d));
+ b.bindNamedParameter(Eps.class, String.valueOf(eps));
+ final Configuration conf = b.build();
+
+ {
+ final Injector i = Tang.Factory.getTang().newInjector(conf);
+
+ final int readD = i.getNamedInstance(Dimensionality.class).intValue();
+ final double readEps = i.getNamedInstance(Eps.class).doubleValue();
+
+ assertEquals(eps, readEps, 1e-12);
+ assertEquals(d, readD);
+ }
+
+
+ {
+ JavaConfigurationBuilder roundTrip = Tang.Factory.getTang().newConfigurationBuilder();
+ ConfigurationFile.addConfiguration(roundTrip, ConfigurationFile.toConfigurationString(conf));
+ final Injector i = Tang.Factory.getTang().newInjector(roundTrip.build());
+
+ final int readD = i.getNamedInstance(Dimensionality.class).intValue();
+ final double readEps = i.getNamedInstance(Eps.class).doubleValue();
+
+ assertEquals(eps, readEps, 1e-12);
+ assertEquals(d, readD);
+ }
+
+ {
+ final Injector parent = Tang.Factory.getTang().newInjector(Tang.Factory.getTang().newConfigurationBuilder().build());
+ final Injector i = parent.forkInjector(conf);
+
+ final int readD = i.getNamedInstance(Dimensionality.class).intValue();
+ final double readEps = i.getNamedInstance(Eps.class).doubleValue();
+
+ assertEquals(eps, readEps, 1e-12);
+ assertEquals(d, readD);
+ }
+
+ {
+ final Injector parent = Tang.Factory.getTang().newInjector(Tang.Factory.getTang().newConfigurationBuilder().build());
+ final JavaConfigurationBuilder roundTrip = Tang.Factory.getTang().newConfigurationBuilder();
+ ConfigurationFile.addConfiguration(roundTrip,
+ ConfigurationFile.toConfigurationString(conf));
+ final Injector i = parent.forkInjector(roundTrip.build());
+
+ final int readD = i.getNamedInstance(Dimensionality.class).intValue();
+ final double readEps = i.getNamedInstance(Eps.class).doubleValue();
+
+ assertEquals(eps, readEps, 1e-12);
+ assertEquals(d, readD);
+ }
+
+ }
+
+ @NamedParameter()
+ public final class Dimensionality implements Name<Integer> {
+ // Intentionally Empty
+ }
+
+ /**
+ * Break criterion for the optimizer. If the progress in mean loss between
+ * two iterations is less than this, the optimization stops.
+ */
+ @NamedParameter()
+ public final class Eps implements Name<Double> {
+ // Intentionally Empty
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/53ea32cc/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestSetInjection.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestSetInjection.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestSetInjection.java
new file mode 100644
index 0000000..0901a4d
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestSetInjection.java
@@ -0,0 +1,181 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.reef.tang;
+
+import org.apache.reef.tang.annotations.Name;
+import org.apache.reef.tang.annotations.NamedParameter;
+import org.apache.reef.tang.annotations.Parameter;
+import org.apache.reef.tang.exceptions.BindException;
+import org.apache.reef.tang.exceptions.InjectionException;
+import org.apache.reef.tang.formats.AvroConfigurationSerializer;
+import org.apache.reef.tang.formats.ConfigurationSerializer;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.inject.Inject;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+public class TestSetInjection {
+ @Test
+ public void testStringInjectDefault() throws InjectionException {
+ Set<String> actual = Tang.Factory.getTang().newInjector().getInstance(Box.class).strings;
+
+ Set<String> expected = new HashSet<>();
+ expected.add("one");
+ expected.add("two");
+ expected.add("three");
+
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testObjectInjectDefault() throws InjectionException, BindException {
+ Injector i = Tang.Factory.getTang().newInjector();
+ i.bindVolatileInstance(Integer.class, 42);
+ i.bindVolatileInstance(Float.class, 42.0001f);
+ Set<Number> actual = i.getInstance(Pool.class).numbers;
+ Set<Number> expected = new HashSet<>();
+ expected.add(42);
+ expected.add(42.0001f);
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testStringInjectBound() throws InjectionException, BindException {
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindSetEntry(SetOfStrings.class, "four");
+ cb.bindSetEntry(SetOfStrings.class, "five");
+ cb.bindSetEntry(SetOfStrings.class, "six");
+ Set<String> actual = Tang.Factory.getTang().newInjector(cb.build()).getInstance(Box.class).strings;
+
+ Set<String> expected = new HashSet<>();
+ expected.add("four");
+ expected.add("five");
+ expected.add("six");
+
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testObjectInjectBound() throws InjectionException, BindException {
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindSetEntry(SetOfClasses.class, Short.class);
+ cb.bindSetEntry(SetOfClasses.class, Float.class);
+
+ Injector i = Tang.Factory.getTang().newInjector(cb.build());
+ i.bindVolatileInstance(Short.class, (short) 4);
+ i.bindVolatileInstance(Float.class, 42.0001f);
+ Set<Number> actual = i.getInstance(Pool.class).numbers;
+ Set<Number> expected = new HashSet<>();
+ expected.add((short) 4);
+ expected.add(42.0001f);
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testStringInjectRoundTrip() throws InjectionException, BindException, IOException {
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindSetEntry(SetOfStrings.class, "four");
+ cb.bindSetEntry(SetOfStrings.class, "five");
+ cb.bindSetEntry(SetOfStrings.class, "six");
+
+ ConfigurationSerializer serializer = new AvroConfigurationSerializer();
+
+ String s = serializer.toString(cb.build());
+ JavaConfigurationBuilder cb2 = Tang.Factory.getTang().newConfigurationBuilder();
+ Configuration conf = serializer.fromString(s);
+ cb2.addConfiguration(conf);
+
+ Set<String> actual = Tang.Factory.getTang().newInjector(cb2.build()).getInstance(Box.class).strings;
+
+ Set<String> expected = new HashSet<>();
+ expected.add("four");
+ expected.add("five");
+ expected.add("six");
+
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testObjectInjectRoundTrip() throws InjectionException, BindException, IOException {
+ JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
+ cb.bindSetEntry(SetOfClasses.class, Short.class);
+ cb.bindSetEntry(SetOfClasses.class, Float.class);
+
+ ConfigurationSerializer serializer = new AvroConfigurationSerializer();
+
+ String s = serializer.toString(cb.build());
+ JavaConfigurationBuilder cb2 = Tang.Factory.getTang().newConfigurationBuilder();
+ Configuration conf = serializer.fromString(s);
+ cb2.addConfiguration(conf);
+
+ Injector i = Tang.Factory.getTang().newInjector(cb2.build());
+ i.bindVolatileInstance(Short.class, (short) 4);
+ i.bindVolatileInstance(Float.class, 42.0001f);
+ Set<Number> actual = i.getInstance(Pool.class).numbers;
+ Set<Number> expected = new HashSet<>();
+ expected.add((short) 4);
+ expected.add(42.0001f);
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testDefaultAsClass() throws InjectionException, BindException {
+ Injector i = Tang.Factory.getTang().newInjector();
+ i.bindVolatileInstance(Integer.class, 1);
+ i.bindVolatileInstance(Float.class, 2f);
+ Set<Number> actual = i.getNamedInstance(SetOfClassesDefaultClass.class);
+ Set<Number> expected = new HashSet<>();
+ expected.add(1);
+ Assert.assertEquals(expected, actual);
+ }
+
+}
+
+@NamedParameter(default_values = {"one", "two", "three"})
+class SetOfStrings implements Name<Set<String>> {
+}
+
+class Box {
+ public final Set<String> strings;
+
+ @Inject
+ Box(@Parameter(SetOfStrings.class) Set<String> strings) {
+ this.strings = strings;
+ }
+}
+
+@NamedParameter(default_values = {"java.lang.Integer", "java.lang.Float"})
+class SetOfClasses implements Name<Set<Number>> {
+}
+
+class Pool {
+ public final Set<Number> numbers;
+
+ @Inject
+ Pool(@Parameter(SetOfClasses.class) Set<Number> numbers) {
+ this.numbers = numbers;
+ }
+}
+
+@NamedParameter(default_class = Integer.class)
+class SetOfClassesDefaultClass implements Name<Set<Number>> {
+}
\ No newline at end of file