package com.microsoft.tang;

import static org.junit.Assert.assertTrue;

import java.io.File;

import javax.inject.Inject;

import org.junit.Test;

import com.microsoft.tang.exceptions.BindException;
import com.microsoft.tang.exceptions.InjectionException;

public class BindSingletonTest {

    private static Configuration roundtrip(final Configuration conf) {
        try {
            final File f = File.createTempFile("TANGConf-", ".conf");
            conf.writeConfigurationFile(f);
            final ConfigurationBuilder b = Tang.Factory.getTang().newConfigurationBuilder();
            b.addConfiguration(f);
            return b.build();
        } catch (final Exception e) {
            throw new RuntimeException("Unable to roundtrip a TANG Configuration.", e);
        }
    }

    public static class A {
        @Inject
        public A() {
            // Intentionally blank
        }

    }

    public static class B extends A {
        @Inject
        public B() {
            // intentionally blank
        }
    }

    
    
    
    @SuppressWarnings("static-method")
    @Test
    public void testSingletonRoundTrip() throws BindException, InjectionException {

        final ConfigurationBuilder b = Tang.Factory.getTang().newConfigurationBuilder();
        b.bindSingletonImplementation(A.class, B.class);
        final Configuration src = b.build();

        final Configuration dest = roundtrip(src);
        final Injector i = Tang.Factory.getTang().newInjector(dest);
        final A a1 = i.getInstance(A.class);
        final A a2 = i.getInstance(A.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);

        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.createChildInjector();
        final A a4 = injector3.getInstance(A.class);
        assertTrue("Child Injectors should return the same singletons as their parents", a3 == a4);
    }

}

@@ -2259,7 +2259,1666 @@
   %7D%0A
+    %0A    @Test public void testLateBoundVolatilteInstanceWithSingleton() throws BindException, InjectionException %7B%0A      Tang tang = Tang.Factory.getTang();%0A      ConfigurationBuilder cb = tang.newConfigurationBuilder();%0A      cb.bindSingletonImplementation(LateBoundVolatile.A.class, LateBoundVolatile.B.class);%0A      Injector i = tang.newInjector(cb.build());%0A      i = i.bindVolatileInstance(LateBoundVolatile.C.class, new LateBoundVolatile.C());%0A      LateBoundVolatile.A a = i.getInstance(LateBoundVolatile.A.class);%0A    %7D%0A    @Test public void testLateBoundOneVolatilteInstanceWithSingleton() throws BindException, InjectionException %7B%0A      Tang tang = Tang.Factory.getTang();%0A      ConfigurationBuilder cb = tang.newConfigurationBuilder();%0A      cb.bindSingleton(LateBoundVolatile.C.class);%0A      Injector i = tang.newInjector(cb.build());%0A      i = i.bindVolatileInstance(LateBoundVolatile.C.class, new LateBoundVolatile.C());%0A      LateBoundVolatile.C a = i.getInstance(LateBoundVolatile.C.class);%0A    %7D%0A    @Test public void testLateBoundXXVolatilteInstanceWithSingleton() throws BindException, InjectionException %7B%0A      Tang tang = Tang.Factory.getTang();%0A      ConfigurationBuilder cb = tang.newConfigurationBuilder();%0A      cb.bindSingletonImplementation(LateBoundVolatile.B.class, LateBoundVolatile.B.class);%0A      Injector i = tang.newInjector(cb.build());%0A      i = i.bindVolatileInstance(LateBoundVolatile.C.class, new LateBoundVolatile.C());%0A      LateBoundVolatile.A a = i.getInstance(LateBoundVolatile.B.class);%0A    %7D%0A%7D%0A%0Aclass LateBoundVolatile %7B%0A  static class A %7B%7D%0A  static class B extends A %7B @Inject B(C c) %7B %7D %7D%0A  static class C %7B %7D
 %0A%7D
-%0A

update bind singleton for late volatiles