package org.jboss.weld.tests.interceptors.bridgemethods.ejb;

import javax.ejb.EJBException;
import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.weld.tests.category.Integration;
import org.jboss.weld.tests.interceptors.bridgemethods.common.BaseService;
import org.jboss.weld.tests.interceptors.bridgemethods.common.SomeInterceptor;
import org.jboss.weld.tests.interceptors.bridgemethods.common.SpecialService;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;


/**
 *
 */
@Category(Integration.class)
@RunWith(Arquillian.class)
@Ignore("Enable once we upgrade AS7 version to 7.2.x -- this is fixed there")
public class EJBBridgeMethodTest {

    @Deployment
    public static Archive<?> deploy() {
        return ShrinkWrap.create(BeanArchive.class)
                .intercept(SomeInterceptor.class)
                .addPackage(BaseService.class.getPackage())
                .addPackage(EJBBridgeMethodTest.class.getPackage());
    }

    @Inject
    private SpecialService specialService;

    @Inject
    private BaseService<String> stringBaseService;

    @Inject
    private BaseService baseService;

    @Before
    public void setUp() throws Exception {
        SomeInterceptor.invocationCount = 0;
    }

    @Test
    public void testSpecialService() {
        specialService.doSomething("foo");
        assertEquals(1, SomeInterceptor.invocationCount);
    }

    @Test
    public void testStringBaseService() {
        stringBaseService.doSomething("foo");
        assertEquals(1, SomeInterceptor.invocationCount);
    }

    @SuppressWarnings("unchecked")
    @Test
    public void testBaseService() {
        baseService.doSomething("foo");
        assertEquals(1, SomeInterceptor.invocationCount);
    }

    @SuppressWarnings("unchecked")
    @Ignore
    @Test
    public void testBaseServiceWithInvalidArgumentType() {
        try {
            baseService.doSomething(Boolean.TRUE);
            fail("Expected ClassCastException");
        } catch (ClassCastException e) {
        } catch (EJBException e) {
            if (!(e.getCause() instanceof ClassCastException)) {
                throw e;
            }
        }
        assertEquals("ClassCastException should be thrown before interceptor is invoked", 0, SomeInterceptor.invocationCount);
    }

    @Test
    public void testSpecialServiceDoSomethingElse() {
        specialService.returnSomething();
        assertEquals(1, SomeInterceptor.invocationCount);
    }

    @Test
    public void testBaseServiceDoSomethingElse() {
        baseService.returnSomething();
        assertEquals(1, SomeInterceptor.invocationCount);
    }

}

@@ -55,16 +55,99 @@
 s.ejb;%0A%0A
+import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.fail;%0A%0A
 import j
@@ -877,235 +877,74 @@
 h;%0A%0A
-import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.fail;%0A%0A%0A/**%0A *%0A */%0A@Category(Integration.class)%0A@RunWith(Arquillian.class)%0A@Ignore(%22Enable once we upgrade AS7 version to 7.2.x -- this is fixed there%22
+%0A/**%0A *%0A */%0A@Category(Integration.class)%0A@RunWith(Arquillian.class
 )%0Apu
@@ -1396,36 +1396,160 @@
 ;%0A%0A    @
-Inject%0A    private B
+SuppressWarnings(%22rawtypes%22)%0A    private BaseService baseService;%0A%0A    @Inject%0A    public void init(BaseService%3C?%3E baseService) %7B%0A        this.b
 aseServi
@@ -1542,32 +1542,34 @@
 this.baseService
+ =
  baseService;%0A%0A 
@@ -1561,24 +1561,30 @@
 baseService;
+%0A    %7D
 %0A%0A    @Befor

Enable and fix test for WELD-1162 Interceptors not invoked when EJB local interface extends a generic interface