Skip to content

Commit

Permalink
Committed the 'Request routing' scenario for the 'Host a spring web M…
Browse files Browse the repository at this point in the history
…VC application'
  • Loading branch information
Stuart Blair committed Jan 20, 2012
1 parent b552e63 commit 448ffa8
Show file tree
Hide file tree
Showing 33 changed files with 154 additions and 86 deletions.
15 changes: 1 addition & 14 deletions features/host_a_spring_mvc_web_application.feature
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
Feature:Host a spring mvc web application

Scenario: Request routing
Given I have a spring mvc application context location of "mvc-dispatcher-servlet.xml"
Given I host a spring mvc application with context location of "WEB-INF/mvc-dispatcher-servlet.xml"
And the application will respond to get requests for the uri "/hello"
When I host the application
And I GET "/hello"
Then I recieve an HTTPResponse with a 200 code

#Scenario: Invalid application context
#Given I have a spring mvc application context location of ""
#When I host the application
#Then I receive an error message reporting that "" is an invalid application context

#Scenario: Missing the supporting spring mvc classes from the runtime environment
#Given I have a spring mvc application context location of "application-servlet.xml"
#And the application will respond to get requests for the uri "/hello"
#But I don't have Spring MVC's supporting classes on my classpath
#When I host the application
#Then I receive an error message reporting the classes that cannot be found
18 changes: 8 additions & 10 deletions features/step_definitions/fake_dispatcher_steps.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
Given /^I have a spring mvc application context location of "([^"]*)"$/ do |context_location|
set_context_location(context_location)
Given /^I host a spring mvc application with context location of "([^"]*)"$/ do |context_location|
host_application(context_location)
end

Given /^the application will respond to get requests for the uri "([^"]*)"$/ do |uri|
# Because it will - see the reference folder
# Because it will - see the reference_apps/valid_app folder
# Somewhat tricky to do this step with a real J2EE app supplied as reference.
# Need to think of something better here to verify that the J2EE app *is* able to respond to the URI
end

When /^I (GET|PUT|POST|DELETE) "([^"]*)"$/ do |method, uri|
pending # express the regexp above with the code you wish you had
end

Then /^I recieve an HTTPResponse with a (\d+) code$/ do |response_code|
pending # express the regexp above with the code you wish you had
@last_response = send_request(method, uri)
end

When /^I host the application$/ do
host_application
Then /^I recieve an HTTPResponse with a (\d+) code$/ do |status_code|
@last_response.get_status.should eql status_code.to_i
end

Then /^I receive an error message reporting that "([^"]*)" is an invalid application context$/ do |arg1|
Expand Down
7 changes: 4 additions & 3 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
$LOAD_PATH.unshift(File.expand_path('../../../lib', __FILE__))
$LOAD_PATH.unshift(File.expand_path('../../../reference_apps', __FILE__))
require 'java'
$CLASSPATH << File.join(File.dirname(__FILE__), 'reference_apps', 'valid_app', 'target', 'valid_app', 'WEB-INF', 'classes')
Dir.glob("**/lib/*.jar") {|jar| $CLASSPATH << jar}

require 'classpath'
require 'reigns'
java_import 'com._5values.dummy.controller.HelloController'
50 changes: 0 additions & 50 deletions features/support/reference_apps/valid_app/pom.xml

This file was deleted.

9 changes: 5 additions & 4 deletions features/support/world_extension.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module KnowsSpring
def set_context_location(context_location)
@context_location = context_location
def host_application(context_location)
@fake_dispatcher_servlet = ::Reigns::FakeDispatcherServlet.new(context_location)
end

def host_application
@fake_dispatcher_servlet = FakeDispatcherServlet.new(context_location)
def send_request(method, uri)
@fake_dispatcher_servlet.send(method.downcase, uri)
end
end

World(KnowsSpring)
22 changes: 22 additions & 0 deletions lib/reigns/fake_dispatcher_servlet.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
require 'java'

java_import 'javax.servlet.http.HttpServlet'
java_import 'org.springframework.web.servlet.DispatcherServlet'
java_import 'org.springframework.mock.web.MockServletConfig'
java_import 'org.springframework.mock.web.MockHttpServletRequest'
java_import 'org.springframework.mock.web.MockHttpServletResponse'


module Reigns
class FakeDispatcherServlet
def initialize(context_location)
@servlet = DispatcherServlet.new
config = MockServletConfig.new("resources")
config.add_init_parameter("contextConfigLocation", context_location)
@servlet.init(config)
end

def get(uri)
request = MockHttpServletRequest.new('GET', uri)
response = MockHttpServletResponse.new
@servlet.service(request, response);
return response
end
end
end
3 changes: 3 additions & 0 deletions reference_apps/classpath.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$CLASSPATH << File.join(File.dirname(__FILE__), 'valid_app', 'target', 'valid_app')
$CLASSPATH << File.join(File.dirname(__FILE__), 'valid_app', 'target', 'valid_app', 'WEB-INF', 'classes')
Dir.glob("**/lib/*.jar") {|jar| $CLASSPATH << jar}
63 changes: 63 additions & 0 deletions reference_apps/valid_app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com._5values.dummy</groupId>
<artifactId>valid_app</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Dummy 'valid' website for use in feature testing the Reigns gem</name>

<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>

<dependencies>

<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- Servlet implementation dependency -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.23</version>
</dependency>

</dependencies>

<build>
<finalName>valid_app</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven
#Wed Jan 11 03:20:42 PST 2012
#Thu Jan 19 13:23:42 PST 2012
version=1.0-SNAPSHOT
groupId=com._5values.dummy
artifactId=valid_app
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<string>WEB-INF/lib/spring-expression-3.0.5.RELEASE.jar</string>
<string>WEB-INF/lib/spring-webmvc-3.0.5.RELEASE.jar</string>
<string>WEB-INF/lib/spring-context-support-3.0.5.RELEASE.jar</string>
<string>WEB-INF/lib/spring-test-3.0.5.RELEASE.jar</string>
<string>WEB-INF/lib/tomcat-servlet-api-7.0.23.jar</string>
</pathsSet>
</path-set>
</entry>
Expand Down Expand Up @@ -59,5 +61,29 @@
</dependency>
<targetFileName>spring-webmvc-3.0.5.RELEASE.jar</targetFileName>
</org.apache.maven.plugin.war.util.DependencyInfo>
<org.apache.maven.plugin.war.util.DependencyInfo>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.0.5.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
<exclusions/>
<optional>false</optional>
</dependency>
<targetFileName>spring-test-3.0.5.RELEASE.jar</targetFileName>
</org.apache.maven.plugin.war.util.DependencyInfo>
<org.apache.maven.plugin.war.util.DependencyInfo>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.23</version>
<type>jar</type>
<scope>compile</scope>
<exclusions/>
<optional>false</optional>
</dependency>
<targetFileName>tomcat-servlet-api-7.0.23.jar</targetFileName>
</org.apache.maven.plugin.war.util.DependencyInfo>
</dependenciesInfo>
</webapp-structure>
21 changes: 17 additions & 4 deletions spec/fake_dispatcher_servlet_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
require 'spec_helper'

module Reigns
describe FakeDispatcherServlet do
describe "#initialize" do
context "when initialized with a valid context configuration location" do
pending it "should do something -erk - back to the cucumber features I think!" do
describe FakeDispatcherServlet do
describe "#get" do

before (:each) do
@fake_dispatcher_servlet = FakeDispatcherServlet.new("WEB-INF/mvc-dispatcher-servlet.xml")
end

context "to a resource that exists" do
it "returns an HTTPResponse with a 200 code" do
@fake_dispatcher_servlet.get("/hello").get_status.should eql 200
end
end

context "to a resource that doesn't exist" do
it "returns an HTTPResponse with a 404 code" do
@fake_dispatcher_servlet.get("/goodbye").get_status.should eql 404
end
end

end
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$:.unshift(File.join(File.dirname(__FILE__), '..', 'reference_apps'))
require 'java'
require 'classpath'
require 'reigns'

0 comments on commit 448ffa8

Please sign in to comment.