Skip to content

Commit

Permalink
WELD-1823 a null value should be returned for the injection point if …
Browse files Browse the repository at this point in the history
…it is not being injected into any injection point
  • Loading branch information
hasys authored and jharting committed Jan 6, 2015
1 parent 364b024 commit 7878aad
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, 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.injectionPoint.weld1823;

import java.util.Set;

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

/*
*@author Emily Jiang
*/
public class CDIBean {

CDIBean2 beanManagerInstance;

public CDIBean(BeanManager manager){
Set<Bean<?>> beanList = manager.getBeans(CDIBean2.class);
if (beanList!=null && !beanList.isEmpty()){
Bean<?> bean = beanList.iterator().next();
CreationalContext<?> context = manager.createCreationalContext(bean);
beanManagerInstance = (CDIBean2) manager.getReference(bean, CDIBean2.class, context);
}
}

public String getData() {
return beanManagerInstance.getData();
}

public void setData(String data) {
beanManagerInstance.setData(data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, 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.injectionPoint.weld1823;

/*
*@author Emily Jiang
*/
public class CDIBean2 {
private String data;

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, 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.injectionPoint.weld1823;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

/*
*@author Emily Jiang
*/
public class CDIBean2Producer {

@Produces
public CDIBean2 produce(InjectionPoint ip) {

if (ip != null) {
throw new RuntimeException("InjectionPoint exists when producing CDIBean2: " + ip.toString());
}
return new CDIBean2();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, 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.injectionPoint.weld1823;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;

/*
*@author Emily Jiang
*/
public class CDIBeanProducer {

@Inject
private BeanManager manager;

@Produces
public CDIBean produce(InjectionPoint ip) {

if (ip == null) {
throw new RuntimeException("InjectionPoint is NULL when producing CDIBean");
}
return new CDIBean(manager);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, 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.injectionPoint.weld1823;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
*@author Emily Jiang
*/
@WebServlet("/ProducerNullIPServlet")
public class TestServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@SuppressWarnings("unused")
@Inject
private CDIBean bean;

@Inject
private BeanManager manager;

@SuppressWarnings("unused")
@Inject
private Instance<CDIBean2> anotherBeanInstance;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

PrintWriter out = resp.getWriter();

Set<Bean<?>> beanList = manager.getBeans(CDIBean2.class);
if (beanList != null && !beanList.isEmpty()) {
Bean<?> bean = beanList.iterator().next();
CreationalContext<?> context = manager.createCreationalContext(bean);
CDIBean2 beanManagerInstance = (CDIBean2) manager.getReference(bean, CDIBean2.class, context);
beanManagerInstance.setData("Test2");
}
out.println("Test Sucessful!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, 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.injectionPoint.weld1823;

import static junit.framework.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.weld.tests.category.Integration;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

/*
*@author Kirill Gaevskii
*/
@RunWith(Arquillian.class)
@Category(Integration.class)
@Ignore("WELD-1823")
public class Weld1823 {

@Deployment(testable = false)
public static Archive<?> deploy() {
return ShrinkWrap.create(WebArchive.class).addPackage(CDIBean.class.getPackage())
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
}

@Test
public void testNullValueForInjectionPoint(@ArquillianResource(TestServlet.class) URL baseURL) throws IOException {
final URL url = new URL(baseURL, "ProducerNullIPServlet");

BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
assertEquals(reader.readLine(), "Test Sucessful!");
}
}

0 comments on commit 7878aad

Please sign in to comment.