package com.github.ruediste.salta.jsr330.wikiChecks;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import javax.inject.Inject;

import org.junit.Test;

import com.github.ruediste.salta.jsr330.AbstractModule;
import com.github.ruediste.salta.jsr330.InjectionOptional;
import com.github.ruediste.salta.jsr330.Salta;

public class InjectionsTest {

	private static class B {
	}

	private class C {
	}

	private static class A {
		@Inject
		@InjectionOptional
		C field;
		private C methodValue;
		private C methodArgValue;
		private B methodB;
		private B methodArgB;
		private C constructorArgValue;
		private B constructorArgB;

		@Inject
		@InjectionOptional
		void method(C value, B b) {
			this.methodValue = value;
			this.methodB = b;
		}

		@Inject
		void methodArg(@InjectionOptional C value, B b) {
			this.methodArgValue = value;
			this.methodArgB = b;
		}

		@Inject
		A(@InjectionOptional C value, B b) {
			this.constructorArgValue = value;
			this.constructorArgB = b;
		}
	}

	@Test
	public void testOptionalNotPresent() {
		A a = Salta.createInjector().getInstance(A.class);

		assertNull(a.field);

		assertNull(a.methodB);
		assertNull(a.methodValue);

		assertNotNull(a.methodArgB);
		assertNull(a.methodArgValue);

		assertNotNull(a.constructorArgB);
		assertNull(a.constructorArgValue);
	}

	@Test
	public void testOptionalPresent() {
		A a = Salta.createInjector(new AbstractModule() {

			@Override
			protected void configure() throws Exception {
				bind(C.class).toInstance(new C());
			}
		}).getInstance(A.class);

		assertNotNull(a.field);

		assertNotNull(a.methodB);
		assertNotNull(a.methodValue);

		assertNotNull(a.methodArgB);
		assertNotNull(a.methodArgValue);

		assertNotNull(a.constructorArgB);
		assertNotNull(a.constructorArgValue);
	}
}

@@ -140,61 +140,195 @@
 ll;%0A
-%0Aimport javax.inject.Inject;%0A%0Aimport org.junit.Test;%0A
+import static org.junit.Assert.assertTrue;%0Aimport static org.junit.Assert.fail;%0A%0Aimport javax.inject.Inject;%0A%0Aimport org.junit.Test;%0A%0Aimport com.github.ruediste.salta.core.SaltaException;
 %0Aimp
@@ -1947,11 +1947,580 @@
 lue);%0A%09%7D
+%0A%0A%09private static class Circular1 %7B%0A%09%09@Inject%0A%09%09Circular2 other;%0A%09%7D%0A%0A%09private static class Circular2 %7B%0A%09%09@Inject%0A%09%09Circular1 other;%0A%09%7D%0A%0A%09@Test%0A%09public void circularInstances() %7B%0A%09%09try %7B%0A%09%09%09Salta.createInjector(new AbstractModule() %7B%0A%0A%09%09%09%09@Override%0A%09%09%09%09protected void configure() throws Exception %7B%0A%09%09%09%09%09bind(Circular1.class).toInstance(new Circular1());%0A%09%09%09%09%09bind(Circular2.class).toInstance(new Circular2());%0A%09%09%09%09%7D%0A%09%09%09%7D).getInstance(Circular1.class);%0A%09%09%09fail();%0A%09%09%7D catch (SaltaException e) %7B%0A%09%09%09assertTrue(e.getMessage().contains(%22Recursive recipe creation%22));%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

add circular instances test