Skip to content

Commit

Permalink
added example/jacocoInstantiateTasks
Browse files Browse the repository at this point in the history
  • Loading branch information
akhikhl committed May 26, 2014
1 parent 75e753a commit ffa3bdd
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 0 deletions.
98 changes: 98 additions & 0 deletions examples/jacocoInstantiateTasks/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
buildscript {
repositories {
mavenLocal()
jcenter()
}

dependencies {
classpath 'org.akhikhl.gretty:gretty9-plugin:0.0.21'
}
}

apply plugin: 'gretty9'
apply from: rootProject.file('integrationTests.gradle')
apply plugin: 'jacoco'

import org.akhikhl.gretty.*

dependencies {
// not needed by gretty, we used it for template processing within webapp
compile 'org.apache.velocity:velocity:1.7'
testCompile 'ch.qos.logback:logback-classic:1.1.2'
}

jacoco {
toolVersion = '+'
}

gretty {
scanInterval = 1
fastReload = true
}

task('myIntegrationTest', type: Test, dependsOn: 'test') {

finalizedBy { tasks.integrationTestClientReport }

outputs.upToDateWhen { false }
include '**/*IT.*'
doFirst {
systemProperty 'geb.build.reportsDir', reporting.file('geb')
}

jacoco {
append = false
destinationFile = new File(project.buildDir, 'jacoco/integrationTest_client.exec')
// excludes.add 'hellogretty.RequestResponseIT'
}
}

task('myBeforeIntegrationTest', type: JettyBeforeIntegrationTestTask) {

integrationTestTask 'myIntegrationTest'

jacoco {
append = false
destinationFile = new File(project.buildDir, 'jacoco/integrationTest_server.exec')
}
}

task('myAfterIntegrationTest', type: JettyAfterIntegrationTestTask) {

finalizedBy { tasks.integrationTestServerReport }

integrationTestTask 'myIntegrationTest'
}

task('integrationTestClientReport', type: JacocoReport) {

executionData project.tasks.myIntegrationTest

sourceDirectories = project.files(project.sourceSets.test.allSource.srcDirs)
classDirectories = project.sourceSets.test.output

def reportDir = project.reporting.file("jacoco/integrationTest_client/html")
reports {
html.destination = reportDir
}
doLast {
System.out.println "Jacoco report for client created: file://${reportDir.toURI().path}"
}
}

task('integrationTestServerReport', type: JacocoReport) {

executionData tasks.myBeforeIntegrationTest

sourceDirectories = project.files(project.sourceSets.main.allSource.srcDirs)
classDirectories = project.sourceSets.main.output

def reportDir = project.reporting.file("jacoco/integrationTest_server/html")
reports {
html.destination = reportDir
}
doLast {
System.out.println "Jacoco report for server created: file://${reportDir.toURI().path}"
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package hellogretty;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

public class ExampleServlet extends HttpServlet {

private static final long serialVersionUID = -6506276378398106663L;

private VelocityEngine ve = new VelocityEngine();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Template template = ve.getTemplate("/hellogretty/templates/servletpage.html", "UTF-8");
VelocityContext context = new VelocityContext();
context.put("today", new java.util.Date());
try (PrintWriter out = response.getWriter()) {
template.merge(context, out);
out.flush();
}
}

@Override
public void init() throws ServletException {
super.init();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>Hello-world page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Abel:400,600" rel="stylesheet">
<link href="css/default.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="margin-base-vertical">Hello, world!</h1>
<p>This is dynamic HTML page generated by servlet.</p>
<p>Today is $today.</p>
<p><a href="index.html">Click here</a> to see static page.</p>
</div>
</div>
</div>
</body>
</html>
21 changes: 21 additions & 0 deletions examples/jacocoInstantiateTasks/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<servlet>
<display-name>ExampleServlet</display-name>
<servlet-name>ExampleServlet</servlet-name>
<servlet-class>hellogretty.ExampleServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>ExampleServlet</servlet-name>
<url-pattern>/dynamic</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
15 changes: 15 additions & 0 deletions examples/jacocoInstantiateTasks/src/main/webapp/css/default.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
body {
padding-top: 20px;
font-size: 18px;
font-family: "Abel", serif;
}

h1 {
font-family: "Abel", Arial, sans-serif;
font-weight: 400;
font-size: 40px;
}

.margin-base-vertical {
margin: 40px 0;
}
22 changes: 22 additions & 0 deletions examples/jacocoInstantiateTasks/src/main/webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<title>Hello-world page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Abel:400,600" rel="stylesheet">
<link href="css/default.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="margin-base-vertical">Hello, world!</h1>
<p>This is static HTML page.</p>
<p><a href="dynamic">Click here</a> to see dynamic page generated by servlet.</p>
</div>
</div>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package hellogretty

import geb.spock.GebReportingSpec
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class RequestResponseIT extends GebReportingSpec {

private static final Logger log = LoggerFactory.getLogger(RequestResponseIT)

private static String baseURI

void setupSpec() {
baseURI = System.getProperty('gretty.baseURI')
}

def 'should get expected static page'() {
when:
go "${baseURI}/index.html"
then:
$('h1').text() == 'Hello, world!'
$('p', 0).text() == /This is static HTML page./
}

def 'should get expected response from servlet'() {
when:
go "${baseURI}/dynamic"
then:
$('h1').text() == 'Hello, world!'
$('p', 0).text() == /This is dynamic HTML page generated by servlet./
}
}

0 comments on commit ffa3bdd

Please sign in to comment.