You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ge...@apache.org on 2009/01/16 23:19:33 UTC
svn commit: r735160 - in
/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test:
servlet/TestContext.java unittests/xml/definition/XMLDefinitionTest.java
xml/definition/CtParameter.java xml/definition/TstBeanConstructor.java
Author: gerdogdu
Date: Fri Jan 16 14:19:33 2009
New Revision: 735160
URL: http://svn.apache.org/viewvc?rev=735160&view=rev
Log:
Tests for the XML constructor.
Added:
incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/CtParameter.java (with props)
incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/TstBeanConstructor.java (with props)
Modified:
incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/servlet/TestContext.java
incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/xml/definition/XMLDefinitionTest.java
Modified: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/servlet/TestContext.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/servlet/TestContext.java?rev=735160&r1=735159&r2=735160&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/servlet/TestContext.java (original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/servlet/TestContext.java Fri Jan 16 14:19:33 2009
@@ -29,6 +29,8 @@
import javax.webbeans.manager.Context;
import javax.webbeans.manager.Manager;
+import junit.framework.Assert;
+
import org.apache.log4j.Logger;
import org.apache.webbeans.component.AbstractComponent;
import org.apache.webbeans.component.ComponentImpl;
@@ -59,8 +61,10 @@
import org.apache.webbeans.test.sterotype.StereoWithRequestScope;
import org.apache.webbeans.test.sterotype.StereoWithSessionScope;
import org.apache.webbeans.test.sterotype.StereoWithSessionScope2;
+import org.apache.webbeans.test.unittests.xml.XMLTest;
import org.apache.webbeans.util.WebBeansUtil;
import org.apache.webbeans.xml.WebBeansXMLConfigurator;
+import org.apache.webbeans.xml.XMLUtil;
import org.dom4j.Element;
/**
@@ -314,7 +318,65 @@
return bean;
}
+
+ /**
+ * Protected helper function which loads a WebBean definition from the given xmlResourcePath.
+ * This will first do a Class lookup and take his annotations as a base, later overlaying
+ * it with the definitions from the given XML.
+ *
+ * @param xmlResourcePath
+ * @return XMLComponentImpl<?> with the WebBean definition
+ */
+ protected XMLComponentImpl<?> getWebBeanFromXml(String xmlResourcePath)
+ {
+ InputStream stream = XMLTest.class.getClassLoader().getResourceAsStream(xmlResourcePath);
+ Assert.assertNotNull(stream);
+
+ Element rootElement = XMLUtil.getRootElement(stream);
+ Element beanElement = (Element) rootElement.elements().get(0);
+
+ Class<?> clazz = XMLUtil.getElementJavaType(beanElement);
+
+ XMLComponentImpl<?> def = defineXMLSimpleWebBeans(clazz, beanElement);
+
+ return def;
+ }
+
+ /**
+ * Private helper function which loads a WebBean definition from the given xmlResourcePath.
+ * This will first do a Class lookup and take his annotations as a base, later overlaying
+ * it with the definitions from the given XML.
+ *
+ *
+ * @param xmlResourcePath
+ * @return XMLComponentImpl<?> with the WebBean definition
+ */
+ @SuppressWarnings("unchecked")
+ protected AbstractComponent<?> getWebBeanFromXml(String xmlResourcePath, Class<?> desiredClazz)
+ {
+ InputStream stream = XMLTest.class.getClassLoader().getResourceAsStream(xmlResourcePath);
+ Assert.assertNotNull(stream);
+ Element rootElement = XMLUtil.getRootElement(stream);
+
+ for (Element beanElement : (List<Element>) rootElement.elements())
+ {
+ Class<?> clazz = XMLUtil.getElementJavaType(beanElement);
+
+ defineXMLSimpleWebBeans(clazz, beanElement);
+ }
+
+ for (AbstractComponent<?> def : getComponents())
+ {
+ if (def.getReturnType().equals(desiredClazz))
+ {
+ return def;
+ }
+ }
+
+ return null;
+ }
+
/**
* Defines simple webbeans interceptor.
*
Modified: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/xml/definition/XMLDefinitionTest.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/xml/definition/XMLDefinitionTest.java?rev=735160&r1=735159&r2=735160&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/xml/definition/XMLDefinitionTest.java (original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/xml/definition/XMLDefinitionTest.java Fri Jan 16 14:19:33 2009
@@ -13,21 +13,19 @@
*/
package org.apache.webbeans.test.unittests.xml.definition;
-import java.io.InputStream;
-
import junit.framework.Assert;
+import org.apache.webbeans.component.AbstractComponent;
import org.apache.webbeans.component.xml.XMLComponentImpl;
+import org.apache.webbeans.context.ContextFactory;
import org.apache.webbeans.test.servlet.TestContext;
-import org.apache.webbeans.test.unittests.xml.XMLTest;
import org.apache.webbeans.test.xml.definition.Definition1;
import org.apache.webbeans.test.xml.definition.Definition2;
import org.apache.webbeans.test.xml.definition.MockAsynchronousCreditCardPaymentProcessor;
import org.apache.webbeans.test.xml.definition.PaymentProcessor;
import org.apache.webbeans.test.xml.definition.SystemConfig;
+import org.apache.webbeans.test.xml.definition.TstBeanConstructor;
import org.apache.webbeans.test.xml.definition.TstBeanUnnamed;
-import org.apache.webbeans.xml.XMLUtil;
-import org.dom4j.Element;
import org.junit.Before;
import org.junit.Test;
@@ -47,6 +45,8 @@
@Test
public void testDefinition1()
{
+ clear();
+
XMLComponentImpl<?> compDef = getWebBeanFromXml("org/apache/webbeans/test/xml/definition/definition1.xml");
Assert.assertEquals("definition1", compDef.getName());
@@ -61,6 +61,8 @@
@Test
public void testDefinition2()
{
+ clear();
+
XMLComponentImpl<?> compDef = getWebBeanFromXml("org/apache/webbeans/test/xml/definition/definition2.xml");
Object instance = compDef.create();
@@ -72,6 +74,8 @@
@Test
public void testDefinition3()
{
+ clear();
+
XMLComponentImpl<?> compDef = getWebBeanFromXml("org/apache/webbeans/test/xml/definition/definition3.xml");
// we have to additionally define the PaymentProcessor and SystemConfig
@@ -98,6 +102,8 @@
@Test
public void testWebBeanUnnamed()
{
+ clear();
+
XMLComponentImpl<?> compDef = getWebBeanFromXml("org/apache/webbeans/test/xml/definition/testBeanUnnamed.xml");
// an unnamed bean must not have a name
@@ -107,38 +113,40 @@
Assert.assertNotNull(instance);
Assert.assertTrue(instance instanceof TstBeanUnnamed);
}
-
- /**
- * Private helper function which loads a WebBean definition from the given xmlResourcePath.
- * This will first do a Class lookup and take his annotations as a base, later overlaying
- * it with the definitions from the given XML.
- *
- * @param xmlResourcePath
- * @return XMLComponentImpl<?> with the WebBean definition
- */
- private XMLComponentImpl<?> getWebBeanFromXml(String xmlResourcePath)
+ @Test
+ public void testConstructorInjection1()
{
- InputStream stream = XMLTest.class.getClassLoader().getResourceAsStream(xmlResourcePath);
- Assert.assertNotNull(stream);
-
clear();
-
- Element rootElement = XMLUtil.getRootElement(stream);
- Element beanElement = (Element) rootElement.elements().get(0);
-
- Class<?> clazz = XMLUtil.getElementJavaType(beanElement);
-
- defineXMLSimpleWebBeans(clazz, beanElement);
-
- Assert.assertEquals(1, getComponents().size());
- Object def = getComponents().get(0);
-
- Assert.assertNotNull(def);
- Assert.assertTrue(def instanceof XMLComponentImpl);
+ AbstractComponent<?> compDef = getWebBeanFromXml("org/apache/webbeans/test/xml/definition/testBeanConstructor1.xml",
+ TstBeanConstructor.class);
- return (XMLComponentImpl<?>) def;
+ Object instance = compDef.create();
+ Assert.assertNotNull(instance);
+ Assert.assertTrue(instance instanceof TstBeanConstructor);
+
+ TstBeanConstructor tbc = (TstBeanConstructor) instance;
+ Assert.assertEquals(4200, tbc.getVal1());
+ Assert.assertEquals(13 , tbc.getVal2());
}
+
+ @Test
+ public void testConstructorInjection2()
+ {
+ clear();
+
+ ContextFactory.initRequestContext(null);
+
+ AbstractComponent<?> compDef = getWebBeanFromXml("org/apache/webbeans/test/xml/definition/testBeanConstructor2.xml",
+ TstBeanConstructor.class);
+ Object instance = compDef.create();
+ Assert.assertNotNull(instance);
+ Assert.assertTrue(instance instanceof TstBeanConstructor);
+
+ TstBeanConstructor tbc = (TstBeanConstructor) instance;
+ Assert.assertEquals(0, tbc.getVal1());
+ Assert.assertEquals(40 , tbc.getVal2());
+ }
}
Added: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/CtParameter.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/CtParameter.java?rev=735160&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/CtParameter.java (added)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/CtParameter.java Fri Jan 16 14:19:33 2009
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.webbeans.test.xml.definition;
+
+/**
+ * parameter class for testing constructor injection
+ */
+public class CtParameter
+{
+ public int getValue()
+ {
+ return 42;
+ }
+}
\ No newline at end of file
Propchange: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/CtParameter.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/TstBeanConstructor.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/TstBeanConstructor.java?rev=735160&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/TstBeanConstructor.java (added)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/TstBeanConstructor.java Fri Jan 16 14:19:33 2009
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You 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.apache.webbeans.test.xml.definition;
+
+
+
+/**
+ * This test WebBean has a constructor which should be injected via XML
+ */
+public class TstBeanConstructor
+{
+ private int val1 = 0;
+ private int val2 = 0;
+
+ public TstBeanConstructor()
+ {
+
+ }
+
+ public TstBeanConstructor(CtParameter ctParam)
+ {
+ this.val1 = ctParam.getValue() * 100;
+ }
+
+ public TstBeanConstructor(Integer ctParam)
+ {
+ this.val1 = ctParam.intValue() * 100;
+ }
+
+ public TstBeanConstructor(int ctParam, int multiplier)
+ {
+ this.val1 = ctParam * multiplier;
+ }
+
+ public int getVal1()
+ {
+ return val1;
+ }
+
+
+ public void setVal1(int val1)
+ {
+ this.val1 = val1;
+ }
+
+ public int getVal2()
+ {
+ return val2;
+ }
+
+ public void setVal2()
+ {
+ this.val2 = 40;
+ }
+}
Propchange: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/definition/TstBeanConstructor.java
------------------------------------------------------------------------------
svn:eol-style = native