Browse code

- Integrate arquillian - first test case for JavaArchive - container: Weld EE embedded - MS: JEE-Testapp-01

Change-Id: I1eeaf9ee11f471d5a32a2c195210b426ac087020

Hafid Haddouti authored on 26/04/2014 19:46:52
Showing 8 changed files
... ...
@@ -21,12 +21,12 @@
21 21
 			</arguments>
22 22
 		</buildCommand>
23 23
 		<buildCommand>
24
-			<name>org.eclipse.m2e.core.maven2Builder</name>
24
+			<name>org.eclipse.wst.validation.validationbuilder</name>
25 25
 			<arguments>
26 26
 			</arguments>
27 27
 		</buildCommand>
28 28
 		<buildCommand>
29
-			<name>org.eclipse.wst.validation.validationbuilder</name>
29
+			<name>org.eclipse.m2e.core.maven2Builder</name>
30 30
 			<arguments>
31 31
 			</arguments>
32 32
 		</buildCommand>
... ...
@@ -1,3 +1,4 @@
1 1
 eclipse.preferences.version=1
2 2
 encoding//src/main/java=UTF-8
3
+encoding//src/test/java=UTF-8
3 4
 encoding/<project>=UTF-8
... ...
@@ -1,76 +1,141 @@
1 1
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
-    <modelVersion>4.0.0</modelVersion>
2
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
+	<modelVersion>4.0.0</modelVersion>
4 4
 
5
-    <groupId>com.haddouti.pg</groupId>
6
-    <artifactId>testapp-jee6</artifactId>
7
-    <version>0.0.1-SNAPSHOT</version>
8
-    <packaging>war</packaging>
5
+	<groupId>com.haddouti.pg</groupId>
6
+	<artifactId>testapp-jee6</artifactId>
7
+	<version>0.0.1-SNAPSHOT</version>
8
+	<packaging>war</packaging>
9 9
 
10
-    <name>testapp-jee6</name>
10
+	<name>testapp-jee6</name>
11 11
 
12
-    <properties>
13
-        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
14
-        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15
-    </properties>
12
+	<properties>
13
+		<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
14
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16 15
 
17
-    <dependencies>
18
-        <dependency>
19
-            <groupId>javax</groupId>
20
-            <artifactId>javaee-web-api</artifactId>
21
-            <version>6.0</version>
16
+		<version.junit>4.8.1</version.junit>
17
+		<version.arquillian>1.1.4.Final</version.arquillian>
18
+	</properties>
19
+
20
+	<dependencyManagement>
21
+		<dependencies>
22
+			<dependency>
23
+				<groupId>junit</groupId>
24
+				<artifactId>junit</artifactId>
25
+				<version>${version.junit}</version>
26
+				<scope>test</scope>
27
+			</dependency>
28
+			<dependency>
29
+				<groupId>org.jboss.arquillian</groupId>
30
+				<artifactId>arquillian-bom</artifactId>
31
+				<version>${version.arquillian}</version>
32
+				<scope>import</scope>
33
+				<type>pom</type>
34
+			</dependency>
35
+		</dependencies>
36
+	</dependencyManagement>
37
+
38
+
39
+	<build>
40
+		<plugins>
41
+			<plugin>
42
+				<groupId>org.apache.maven.plugins</groupId>
43
+				<artifactId>maven-compiler-plugin</artifactId>
44
+				<version>2.3.2</version>
45
+				<configuration>
46
+					<source>1.6</source>
47
+					<target>1.6</target>
48
+					<compilerArguments>
49
+						<endorseddirs>${endorsed.dir}</endorseddirs>
50
+					</compilerArguments>
51
+				</configuration>
52
+			</plugin>
53
+			<!-- Updating the Surefire plugin to resolve a bug with clearing the context -->
54
+			<plugin>
55
+				<artifactId>maven-surefire-plugin</artifactId>
56
+				<version>2.12</version>
57
+			</plugin>
58
+			<plugin>
59
+				<groupId>org.apache.maven.plugins</groupId>
60
+				<artifactId>maven-war-plugin</artifactId>
61
+				<version>2.1.1</version>
62
+				<configuration>
63
+					<failOnMissingWebXml>false</failOnMissingWebXml>
64
+				</configuration>
65
+			</plugin>
66
+			<plugin>
67
+				<groupId>org.apache.maven.plugins</groupId>
68
+				<artifactId>maven-dependency-plugin</artifactId>
69
+				<version>2.1</version>
70
+				<executions>
71
+					<execution>
72
+						<phase>validate</phase>
73
+						<goals>
74
+							<goal>copy</goal>
75
+						</goals>
76
+						<configuration>
77
+							<outputDirectory>${endorsed.dir}</outputDirectory>
78
+							<silent>true</silent>
79
+							<artifactItems>
80
+								<artifactItem>
81
+									<groupId>javax</groupId>
82
+									<artifactId>javaee-endorsed-api</artifactId>
83
+									<version>6.0</version>
84
+									<type>jar</type>
85
+								</artifactItem>
86
+							</artifactItems>
87
+						</configuration>
88
+					</execution>
89
+				</executions>
90
+			</plugin>
91
+		</plugins>
92
+	</build>
93
+	
94
+	
95
+	<dependencies>
96
+		<!-- JEE Spec -->
97
+		<dependency>
98
+            <groupId>org.jboss.spec</groupId>
99
+            <artifactId>jboss-javaee-6.0</artifactId>
100
+            <version>1.0.0.Final</version>
101
+            <type>pom</type>
22 102
             <scope>provided</scope>
23 103
         </dependency>
24
-    </dependencies>
25 104
 
26
-    <build>
27
-        <plugins>
28
-            <plugin>
29
-                <groupId>org.apache.maven.plugins</groupId>
30
-                <artifactId>maven-compiler-plugin</artifactId>
31
-                <version>2.3.2</version>
32
-                <configuration>
33
-                    <source>1.6</source>
34
-                    <target>1.6</target>
35
-                    <compilerArguments>
36
-                        <endorseddirs>${endorsed.dir}</endorseddirs>
37
-                    </compilerArguments>
38
-                </configuration>
39
-            </plugin>
40
-            <plugin>
41
-                <groupId>org.apache.maven.plugins</groupId>
42
-                <artifactId>maven-war-plugin</artifactId>
43
-                <version>2.1.1</version>
44
-                <configuration>
45
-                    <failOnMissingWebXml>false</failOnMissingWebXml>
46
-                </configuration>
47
-            </plugin>
48
-            <plugin>
49
-                <groupId>org.apache.maven.plugins</groupId>
50
-                <artifactId>maven-dependency-plugin</artifactId>
51
-                <version>2.1</version>
52
-                <executions>
53
-                    <execution>
54
-                        <phase>validate</phase>
55
-                        <goals>
56
-                            <goal>copy</goal>
57
-                        </goals>
58
-                        <configuration>
59
-                            <outputDirectory>${endorsed.dir}</outputDirectory>
60
-                            <silent>true</silent>
61
-                            <artifactItems>
62
-                                <artifactItem>
63
-                                    <groupId>javax</groupId>
64
-                                    <artifactId>javaee-endorsed-api</artifactId>
65
-                                    <version>6.0</version>
66
-                                    <type>jar</type>
67
-                                </artifactItem>
68
-                            </artifactItems>
69
-                        </configuration>
70
-                    </execution>
71
-                </executions>
72
-            </plugin>
73
-        </plugins>
74
-    </build>
105
+		<!-- Testing: JUnit, Arquillian -->
106
+		<dependency>
107
+			<groupId>junit</groupId>
108
+			<artifactId>junit</artifactId>
109
+			<scope>test</scope>
110
+		</dependency>
111
+		<dependency>
112
+			<groupId>org.jboss.arquillian.junit</groupId>
113
+			<artifactId>arquillian-junit-container</artifactId>
114
+			<scope>test</scope>
115
+		</dependency>
116
+
117
+		<!-- Weld EE container -->
118
+		<dependency>
119
+			<groupId>org.jboss.arquillian.container</groupId>
120
+			<artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
121
+			<version>1.0.0.CR3</version>
122
+			<scope>test</scope>
123
+		</dependency>
124
+		<dependency>
125
+			<groupId>org.jboss.weld</groupId>
126
+			<artifactId>weld-core</artifactId>
127
+			<version>1.1.5.Final</version>
128
+			<scope>test</scope>
129
+		</dependency>
130
+		<dependency>
131
+			<groupId>org.slf4j</groupId>
132
+			<artifactId>slf4j-simple</artifactId>
133
+			<version>1.6.4</version>
134
+			<scope>test</scope>
135
+		</dependency>
136
+		
137
+		
138
+		
139
+	</dependencies>
75 140
 
76 141
 </project>
... ...
@@ -1,17 +1,27 @@
1 1
 package com.haddouti.pg.jee6;
2 2
 
3 3
 import javax.annotation.PostConstruct;
4
+import javax.ejb.EJB;
4 5
 import javax.ejb.Stateless;
5 6
 
7
+import com.haddouti.pg.jee6.service.OneStatelessBean;
8
+
6 9
 /**
7 10
  * This is service
8 11
  */
9 12
 @Stateless
10 13
 public class OneService {
11 14
 
15
+	@EJB
16
+	private OneStatelessBean bean;
17
+	
12 18
 	@PostConstruct
13 19
 	public void init() {
14 20
 		// ToDo 
15 21
 	}
16 22
 	
23
+	public String process() {
24
+		// lot of magic
25
+		return bean.process();
26
+	}
17 27
 }
18 28
new file mode 100644
... ...
@@ -0,0 +1,21 @@
1
+package com.haddouti.pg.jee6.service;
2
+
3
+import javax.ejb.Stateless;
4
+
5
+/**
6
+ * Session Bean implementation class OneStatelessBean
7
+ */
8
+@Stateless
9
+public class OneStatelessBean {
10
+
11
+    /**
12
+     * Default constructor. 
13
+     */
14
+    public OneStatelessBean() {
15
+    }
16
+
17
+    public String process() {
18
+    	// lot of magic
19
+    	return this.getClass().getSimpleName() + this.hashCode();
20
+    }
21
+}
0 22
new file mode 100644
... ...
@@ -0,0 +1,8 @@
1
+package com.haddouti.pg.jee6.util;
2
+
3
+public class OneUtils {
4
+
5
+	public long getCurrentMillis() {
6
+		return System.currentTimeMillis();
7
+	}
8
+}
0 9
new file mode 100644
... ...
@@ -0,0 +1,38 @@
1
+package com.haddouti.pg.jee6;
2
+
3
+import javax.ejb.EJB;
4
+
5
+import junit.framework.Assert;
6
+
7
+import org.jboss.arquillian.container.test.api.Deployment;
8
+import org.jboss.arquillian.junit.Arquillian;
9
+import org.jboss.shrinkwrap.api.ShrinkWrap;
10
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
11
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
12
+import org.junit.Test;
13
+import org.junit.runner.RunWith;
14
+
15
+import com.haddouti.pg.jee6.service.OneStatelessBean;
16
+
17
+@RunWith(Arquillian.class)
18
+public class OneServiceJarTest {
19
+
20
+	@EJB
21
+	OneService oneService;
22
+	
23
+	@Deployment
24
+	public static JavaArchive createDeployment() {
25
+		return ShrinkWrap.create(JavaArchive.class)
26
+				.addClass(OneStatelessBean.class)
27
+				.addClass(OneService.class)
28
+				.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
29
+	}
30
+	
31
+	
32
+	
33
+	@Test
34
+	public void testOne() {
35
+		System.out.println(oneService);
36
+		Assert.assertNotNull(oneService);
37
+	}
38
+}
0 39
new file mode 100644
... ...
@@ -0,0 +1,46 @@
1
+package com.haddouti.pg.jee6.util;
2
+
3
+import javax.inject.Inject;
4
+
5
+import junit.framework.Assert;
6
+
7
+import org.jboss.arquillian.container.test.api.Deployment;
8
+import org.jboss.arquillian.junit.Arquillian;
9
+import org.jboss.shrinkwrap.api.ShrinkWrap;
10
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
11
+import org.junit.Test;
12
+import org.junit.runner.RunWith;
13
+
14
+/**
15
+ * Arquillian test unit
16
+ * <ul>
17
+ *  <li>JavaArchive</li>
18
+ *  <li>Test normal java classes</li>
19
+ *  <li>Inject them</li>
20
+ * </ul>
21
+ *
22
+ */
23
+@RunWith(Arquillian.class)
24
+public class OneUtilsTest {
25
+
26
+	@Inject
27
+	private OneUtils oneUtils;
28
+	
29
+	
30
+	@Deployment
31
+	public static JavaArchive createDeployment() {
32
+		final JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
33
+				.addClass(OneUtils.class);
34
+
35
+		// Print out the archive
36
+		System.out.println(jar.toString(true));
37
+		return jar;
38
+	}
39
+	
40
+	@Test
41
+	public void testUtilGetCurrentMillis() {
42
+		
43
+		Assert.assertNotNull(oneUtils);
44
+		System.out.println(oneUtils.getCurrentMillis());
45
+	}
46
+}