Skip to content

Commit

Permalink
Added tests for WELD-557
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting committed Jun 24, 2010
1 parent 2735ca1 commit 2949053
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 0 deletions.
@@ -0,0 +1,22 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.extensions.injectionTarget;

interface Aircraft
{
boolean isFlying();
}
@@ -0,0 +1,34 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.extensions.injectionTarget;

import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.inject.Inject;

@Decorator
class AircraftDecorator implements Aircraft
{
@Inject @Delegate
private Aircraft delegate;

public boolean isFlying()
{
return ! delegate.isFlying();
}

}
@@ -0,0 +1,36 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.extensions.injectionTarget;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.ProcessInjectionTarget;

public class InjectionTargetExtension implements Extension
{
public void foo(@Observes ProcessInjectionTarget<Spitfire> event)
{
InjectionTarget<Spitfire> it = event.getInjectionTarget();
event.setInjectionTarget(decorate(it));
}

private <T> InjectionTarget<T> decorate(InjectionTarget<T> delegate)
{
return new InjectionTargetWrapper<T>(delegate);
}
}
@@ -0,0 +1,71 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.extensions.injectionTarget;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;

import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.testharness.impl.packaging.IntegrationTest;
import org.jboss.testharness.impl.packaging.Packaging;
import org.jboss.testharness.impl.packaging.PackagingType;
import org.jboss.testharness.impl.packaging.jsr299.BeansXml;
import org.jboss.testharness.impl.packaging.jsr299.Extension;
import org.jboss.weld.test.AbstractWeldTest;
import org.testng.annotations.Test;

@Artifact
@IntegrationTest
@BeansXml("beans.xml")
@Packaging(PackagingType.EAR)
@Extension("javax.enterprise.inject.spi.Extension")
public class InjectionTargetTest extends AbstractWeldTest
{
@Test(description="WELD-557")
public void testActualInstanceAndNotProxyPassedToInject()
{
InjectionTargetWrapper.clear();
Spitfire aircraft = getReference(Spitfire.class);
aircraft.isFlying();
assert aircraft.isTheSameInstance(InjectionTargetWrapper.injectInstance);
}

@Test(description="WELD-557")
public void testActualInstanceAndNotProxyPassedToPostConstruct()
{
InjectionTargetWrapper.clear();
Spitfire aircraft = getReference(Spitfire.class);
aircraft.isFlying();
assert aircraft.isTheSameInstance(InjectionTargetWrapper.postConstructInstance);
}

@Test(description="WELD-557")
public void testActualInstanceAndNotProxyPassedToPreDestroy()
{
// prepare instance
InjectionTargetWrapper.clear();
Bean<Spitfire> bean = getBean(Spitfire.class);
CreationalContext<Spitfire> ctx = getCurrentManager().createCreationalContext(bean);
Spitfire aircraft = (Spitfire) getCurrentManager().getReference(bean, Spitfire.class, ctx);
// invoke business method
aircraft.isFlying();
// destroy instance
bean.destroy(aircraft, ctx);

assert aircraft.isTheSameInstance(InjectionTargetWrapper.preDestroyInstance);
}
}
@@ -0,0 +1,76 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.extensions.injectionTarget;

import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;

class InjectionTargetWrapper<T> implements InjectionTarget<T>
{
private InjectionTarget<T> delegate;
public static Object injectInstance;
public static Object postConstructInstance;
public static Object preDestroyInstance;

public static void clear()
{
injectInstance = null;
postConstructInstance = null;
preDestroyInstance = null;
}

public InjectionTargetWrapper(InjectionTarget<T> delegate)
{
this.delegate = delegate;
}

public void inject(T instance, CreationalContext<T> ctx)
{
injectInstance = instance;
delegate.inject(instance, ctx);
}

public void postConstruct(T instance)
{
postConstructInstance = instance;
delegate.postConstruct(instance);
}

public void preDestroy(T instance)
{
preDestroyInstance = instance;
delegate.preDestroy(instance);
}

public void dispose(T instance)
{
delegate.dispose(instance);
}

public Set<InjectionPoint> getInjectionPoints()
{
return delegate.getInjectionPoints();
}

public T produce(CreationalContext<T> ctx)
{
return delegate.produce(ctx);
}
}
@@ -0,0 +1,33 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.extensions.injectionTarget;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Documented;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import javax.interceptor.InterceptorBinding;

@InterceptorBinding
@Retention(RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@interface Secured
{
}
@@ -0,0 +1,32 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.extensions.injectionTarget;

import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
@Secured
class SecurityInterceptor
{
@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception
{
return ! (Boolean) ctx.proceed();
}
}
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.extensions.injectionTarget;

import javax.enterprise.context.RequestScoped;

@RequestScoped
class Spitfire implements Aircraft
{
@Secured
public boolean interceptedMethod()
{
return false;
}

public boolean isFlying()
{
return false;
}

/*
* Cannot use equals() since behavior of all Object class methods
* except for toString() is not defined on the client proxy
*/
public boolean isTheSameInstance(Object object)
{
return this == object;
}
}
@@ -0,0 +1,8 @@
<beans>
<interceptors>
<class>org.jboss.weld.tests.extensions.injectionTarget.SecurityInterceptor</class>
</interceptors>
<decorators>
<class>org.jboss.weld.tests.extensions.injectionTarget.AircraftDecorator</class>
</decorators>
</beans>
@@ -0,0 +1 @@
org.jboss.weld.tests.extensions.injectionTarget.InjectionTargetExtension

0 comments on commit 2949053

Please sign in to comment.