Skip to content

Commit 1efc18b

Browse files
committed
Merge pull request #178 from kubamarchwicki/spock
Adding Spock testing framework with sample test
2 parents fecc8f4 + 069af04 commit 1efc18b

File tree

8 files changed

+316
-329
lines changed

8 files changed

+316
-329
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ With your help we can improve this set of samples, learn from each other and gro
4444

4545
There is just a bunch of things you should keep in mind before sending a pull request, so we can easily get all the new things incorporated into the master branch.
4646

47+
Standard tests are jUnit based - for example [this commit](servlet/servlet-filters/src/test/java/org/javaee7/servlet/filters/FilterServletTest.java). Test classes naming must comply with surefire naming standards `**/*Test.java`, `**/*Test*.java` or `**/*TestCase.java`.
48+
49+
However, if you fancy something new, hip and fashionable we also accept Spock specifications - [like here](/servlet/servlet-filters/src/test/groovy/org/javaee7/servlet/filters/FilterServletSpecification.groovy). The spec files are included in the maven test phase if and only if you follow Spock naming convention and give your `Specification` suffix the magic will happen.
50+
4751
### Some coding principles ###
4852

4953
* When creating new source file do not put (or copy) any license header, as we use top-level license (MIT) for each and every file in this repository.

pom.xml

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
<maven.min.version>3.0.0</maven.min.version>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<org.jboss.arquillian.version>1.1.1.Final</org.jboss.arquillian.version>
16+
<org.jboss.arquillian.spock>1.0.0.Beta2</org.jboss.arquillian.spock>
1617
<org.wildfly>8.0.0.CR1</org.wildfly>
1718
<!-- Plugin versions -->
1819
<plugin.enforcer.version>1.3.1</plugin.enforcer.version>
1920
<maven.test.skip>false</maven.test.skip>
2021
<hamcrest.version>1.3</hamcrest.version>
22+
<spock.version>0.7-groovy-2.0</spock.version>
23+
<groovy.version>2.1.5</groovy.version>
2124
</properties>
2225
<dependencyManagement>
2326
<dependencies>
@@ -72,6 +75,12 @@
7275
<artifactId>arquillian-junit-container</artifactId>
7376
<scope>test</scope>
7477
</dependency>
78+
<dependency>
79+
<groupId>org.jboss.arquillian.spock</groupId>
80+
<artifactId>arquillian-spock-container</artifactId>
81+
<version>${org.jboss.arquillian.spock}</version>
82+
<scope>test</scope>
83+
</dependency>
7584
<dependency>
7685
<groupId>org.jboss.arquillian.protocol</groupId>
7786
<artifactId>arquillian-protocol-servlet</artifactId>
@@ -83,6 +92,18 @@
8392
<scope>test</scope>
8493
<type>jar</type>
8594
</dependency>
95+
<dependency>
96+
<groupId>org.spockframework</groupId>
97+
<artifactId>spock-core</artifactId>
98+
<version>${spock.version}</version>
99+
<scope>test</scope>
100+
</dependency>
101+
<dependency>
102+
<groupId>org.codehaus.groovy</groupId>
103+
<artifactId>groovy-all</artifactId>
104+
<version>${groovy.version}</version>
105+
<scope>test</scope>
106+
</dependency>
86107
<dependency>
87108
<groupId>xmlunit</groupId>
88109
<artifactId>xmlunit</artifactId>
@@ -170,7 +191,54 @@
170191
<configuration>
171192
<source>${java.min.version}</source>
172193
<target>${java.min.version}</target>
194+
<compilerId>groovy-eclipse-compiler</compilerId>
173195
</configuration>
196+
<dependencies>
197+
<dependency>
198+
<groupId>org.codehaus.groovy</groupId>
199+
<artifactId>groovy-eclipse-compiler</artifactId>
200+
<version>2.8.0-01</version>
201+
</dependency>
202+
<dependency>
203+
<groupId>org.codehaus.groovy</groupId>
204+
<artifactId>groovy-eclipse-batch</artifactId>
205+
<version>2.1.5-03</version>
206+
</dependency>
207+
</dependencies>
208+
</plugin>
209+
<plugin>
210+
<groupId>org.apache.maven.plugins</groupId>
211+
<artifactId>maven-surefire-plugin</artifactId>
212+
<version>2.16</version>
213+
<executions>
214+
<execution>
215+
<id>default-test</id>
216+
<phase>test</phase>
217+
<goals>
218+
<goal>test</goal>
219+
</goals>
220+
<configuration>
221+
<classpathDependencyExcludes>
222+
<classpathDependencyExcludes>org.jboss.arquillian.spock:arquillian-spock-container</classpathDependencyExcludes>
223+
</classpathDependencyExcludes>
224+
</configuration>
225+
</execution>
226+
<execution>
227+
<id>spock-test</id>
228+
<phase>test</phase>
229+
<goals>
230+
<goal>test</goal>
231+
</goals>
232+
<configuration>
233+
<includes>
234+
<include>**/*Specification.java</include>
235+
</includes>
236+
<classpathDependencyExcludes>
237+
<classpathDependencyExcludes>org.jboss.arquillian.junit:arquillian-junit-container</classpathDependencyExcludes>
238+
</classpathDependencyExcludes>
239+
</configuration>
240+
</execution>
241+
</executions>
174242
</plugin>
175243
<plugin>
176244
<groupId>org.apache.maven.plugins</groupId>
@@ -206,6 +274,7 @@
206274
</plugin>
207275
</plugins>
208276
<pluginManagement>
277+
209278
<plugins>
210279
<plugin>
211280
<groupId>org.apache.maven.plugins</groupId>
@@ -474,7 +543,7 @@
474543
</build>
475544
</profile>
476545
</profiles>
477-
546+
478547
<reporting>
479548
<plugins>
480549
<plugin>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.javaee7.servlet.filters;
2+
3+
import javax.servlet.http.HttpServletResponse;
4+
import javax.servlet.http.HttpServletResponseWrapper;
5+
import java.io.CharArrayWriter;
6+
import java.io.PrintWriter;
7+
8+
public class CharResponseWrapper extends HttpServletResponseWrapper {
9+
private CharArrayWriter output;
10+
11+
public String toString() {
12+
return output.toString();
13+
}
14+
15+
public CharResponseWrapper(HttpServletResponse response) {
16+
super(response);
17+
output = new CharArrayWriter();
18+
}
19+
20+
public PrintWriter getWriter() {
21+
return new PrintWriter(output);
22+
}
23+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.servlet.filters;
41+
42+
import javax.servlet.Filter;
43+
import javax.servlet.FilterChain;
44+
import javax.servlet.FilterConfig;
45+
import javax.servlet.ServletException;
46+
import javax.servlet.ServletRequest;
47+
import javax.servlet.ServletResponse;
48+
import javax.servlet.annotation.WebFilter;
49+
import javax.servlet.http.HttpServletResponse;
50+
import java.io.IOException;
51+
import java.io.PrintWriter;
52+
53+
/**
54+
* @author Arun Gupta
55+
*/
56+
@WebFilter(filterName = "FooBarFilter", urlPatterns = {"/filtered/*"})
57+
public class FooBarFilter implements Filter {
58+
59+
private FilterConfig filterConfig;
60+
61+
private void doBeforeProcessing(ServletRequest request, ServletResponse response)
62+
throws IOException, ServletException {
63+
try (PrintWriter out = response.getWriter()) {
64+
out.print("foo--");
65+
out.flush();
66+
}
67+
}
68+
69+
private void doAfterProcessing(ServletRequest request, ServletResponse response)
70+
throws IOException, ServletException {
71+
try (PrintWriter out = response.getWriter()) {
72+
out.print("--bar");
73+
out.flush();
74+
}
75+
}
76+
77+
@Override
78+
public void doFilter(ServletRequest request, ServletResponse response,
79+
FilterChain chain)
80+
throws IOException, ServletException {
81+
PrintWriter out = response.getWriter();
82+
CharResponseWrapper wrappedResponse = new CharResponseWrapper(
83+
(HttpServletResponse)response);
84+
85+
doBeforeProcessing(request, wrappedResponse);
86+
chain.doFilter(request, wrappedResponse);
87+
doAfterProcessing(request, wrappedResponse);
88+
89+
out.write(wrappedResponse.toString());
90+
}
91+
92+
@Override
93+
public void destroy() {
94+
}
95+
96+
@Override
97+
public void init(FilterConfig filterConfig) {
98+
this.filterConfig = filterConfig;
99+
}
100+
101+
}

0 commit comments

Comments
 (0)