Skip to content

Commit

Permalink
improvements in speed of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
armoucar committed Jul 28, 2012
2 parents c10de7a + 0787c9f commit d2e88bd
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 73 deletions.
30 changes: 13 additions & 17 deletions lib/vraptor-scaffold/generators/app/app_generator.rb
Expand Up @@ -34,8 +34,8 @@ class AppGenerator < VraptorScaffold::Base
class_option :orm, :default => "jpa", :aliases => "-o",
:desc => "Object-relational mapping (options: #{ORMS.join(', ')})"

class_option :jquery, :aliases => "-j", :default => "latest version",
:desc => "jQuery version"
class_option :skip_jquery, :type => :boolean, :aliases => "-j",
:desc => "Skip jQuery download file"

class_option :skip_eclipse, :type => :boolean, :aliases => "-E",
:desc => "Skip Eclipse files"
Expand Down Expand Up @@ -148,7 +148,10 @@ def create_webapp
def create_javascripts
javascripts = File.join Configuration::WEB_APP, "javascripts"
create_file File.join javascripts, "application.js"
add_file (File.join javascripts, "jquery.min.js"), get_jquery.body
unless options[:skip_jquery]
jquery = get_jquery
add_file (File.join javascripts, "jquery.min.js"), jquery.body if jquery
end
end

def configure_scaffold_properties
Expand All @@ -157,7 +160,7 @@ def configure_scaffold_properties

def configure_template_engine
templates = {"jsp" => JspTemplateEngine, "ftl" => FreemarkerTemplateEngine}
templates[options[:template_engine]].new(project_path).configure if templates[options[:template_engine]]
templates[options[:template_engine]].new(project_path, @options).configure if templates[options[:template_engine]]
end

def create_test
Expand Down Expand Up @@ -218,24 +221,17 @@ def validate
puts "The project #{project_path} already exist"
Kernel::exit
end

if options[:jquery] != 'latest version'
case get_jquery
when Net::HTTPClientError, Net::HTTPServerError
download_url = "http://docs.jquery.com/Downloading_jQuery"
puts "jQuery version #{options[:jquery]} was not found. Please visit the download page to see the versions available #{download_url}."
Kernel::exit
end
end
end

def jquery_uri
jquery_version = "1" #this mean get latest version
jquery_version = options[:jquery] if options[:jquery] != 'latest version'
"/ajax/libs/jquery/#{jquery_version}/jquery.min.js"
"/ajax/libs/jquery/1/jquery.min.js"
end

def get_jquery
VraptorScaffold::HttpRequest.open_session("ajax.googleapis.com").get jquery_uri
begin
VraptorScaffold::HttpRequest.open_session("ajax.googleapis.com").get jquery_uri;
rescue
Kernel.puts "Was not possible to download jQuery."
end
end
end
Expand Up @@ -4,24 +4,26 @@ def self.source_root
File.join(File.dirname(__FILE__), "templates", "freemarker")
end

def initialize(project_path)
def initialize(project_path, options)
super
self.destination_root=(project_path)
@project_path = project_path
@options = options
end

def configure
directory("macros", File.join(Configuration::WEB_APP, "macros"))
template("../decorators.erb", File.join(Configuration::WEB_INF, "decorators.xml"))
copy_file("main.ftl", File.join(Configuration::WEB_INF, decorators_path, "main.ftl"))
template("main.ftl.erb", File.join(Configuration::WEB_INF, decorators_path, "main.ftl"))
#copy_file("main.ftl", File.join(Configuration::WEB_INF, decorators_path, "main.ftl"))
directory("infrastructure", infra_path)
append_freemarker_servlet
end

def extension
"ftl"
end

def decorators_path
File.join "views", "decorators"
end
Expand All @@ -36,9 +38,9 @@ def append_freemarker_servlet
file = File.join(Configuration::WEB_INF, "web.xml")
template = File.join FreemarkerTemplateEngine.source_root, "freemarker-web.xml"
inject_into_file(file, File.read(template), :before => "</web-app>", :verbose => false)
end
end

def infra_path
File.join Configuration::MAIN_SRC, package.gsub(".", File::Separator), "infrastructure"
end
end
end
11 changes: 6 additions & 5 deletions lib/vraptor-scaffold/generators/app/jsp_template_engine.rb
Expand Up @@ -4,30 +4,31 @@ def self.source_root
File.join(File.dirname(__FILE__), "templates", "jsp")
end

def initialize(project_path)
def initialize(project_path, options)
super
self.destination_root=(project_path)
@options = options
end

def configure
template("../decorators.erb", File.join(Configuration::WEB_INF, "decorators.xml"))
copy_file("main.jsp", File.join(Configuration::WEB_INF, decorators_path, "main.jsp"))
template("main.jsp.erb", File.join(Configuration::WEB_INF, decorators_path, "main.jsp"))
copy_file("prelude.jspf", File.join(Configuration::WEB_INF, "jsp", "prelude.jspf"))
append_prelude_config
end

def extension
"jsp"
end

def decorators_path
File.join "jsp", "decorators"
end

private
def append_prelude_config
file = File.join(Configuration::WEB_INF, "web.xml")
template = File.join JspTemplateEngine.source_root, "jsp-web.xml"
inject_into_file(file, File.read(template), :before => "</web-app>", :verbose => false)
end
end
end
@@ -0,0 +1,18 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<@html.css "application"/>
<title>Vraptor Scaffold</title>
${head}
</head>

<body>
${body}
<%
jquery_file = File.join(destination_root, Configuration::WEB_APP, "javascripts", "jquery.min.js")
if (File.exists?(jquery_file))
%><@html.js "jquery.min"/>
<% end %><@html.js "application"/>
</body>
</html>
20 changes: 20 additions & 0 deletions lib/vraptor-scaffold/generators/app/templates/jsp/main.jsp.erb
@@ -0,0 +1,20 @@
<% sitemesh_directive = "<%@ taglib uri=\"http://www.opensymphony.com/sitemesh/decorator\" prefix=\"decorator\" %\>" %><%= sitemesh_directive %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><decorator:title default="Vraptor Scaffold"/></title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/stylesheets/application.css"/>
<decorator:head/>
</head>
<body>
<decorator:body/>
<%
jquery_file = File.join(destination_root, Configuration::WEB_APP, "javascripts", "jquery.min.js")
if (File.exists?(jquery_file))
%><script type="text/javascript" src="${pageContext.request.contextPath}/javascripts/jquery.min.js"></script>
<% end %><script type="text/javascript" src="${pageContext.request.contextPath}/javascripts/application.js"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -25,3 +25,10 @@ def exists_and_identical?(source, created)
c2 = File.open(source).read.gsub( /\r/m, "" )
c.should be == c2
end

def mock_http_request
http_request = mock(Net::HTTP)
http_request.stub!(:get).with(kind_of(String)).and_return(http_request)
http_request.stub!(:body).and_return("corpo :)")
VraptorScaffold::HttpRequest.stub!(:open_session).with(kind_of(String)).and_return(http_request)
end
76 changes: 49 additions & 27 deletions spec/vraptor-scaffold/generators/app/app_generator_spec.rb
Expand Up @@ -5,7 +5,7 @@
context "build new application" do
before(:all) do
@project_path = "src/vraptor-scaffold"
AppGenerator.new(@project_path, ["-b=mvn", "-r=repository", "-m=domain", "-c=control"]).invoke_all
AppGenerator.new(@project_path, ["-b=mvn", "-r=repository", "-m=domain", "-c=control", "--skip_jquery"]).invoke_all
end

after(:all) do
Expand All @@ -18,7 +18,7 @@

it "should be invalid when project name already exist" do
Kernel.should_receive(:exit)
AppGenerator.new(@project_path)
AppGenerator.new(@project_path, ["--skip_jquery"])
end

it "should create pom" do
Expand Down Expand Up @@ -91,7 +91,7 @@
@project_path = "src/vraptor-scaffold-hibernate"
@main_resources = "#{@project_path}/#{Configuration::MAIN_RESOURCES}"
@meta_inf = "#{@main_resources}/META-INF"
AppGenerator.new(@project_path, ["--orm=hibernate"]).invoke_all
AppGenerator.new(@project_path, ["--orm=hibernate", "--skip_jquery"]).invoke_all
end

after(:all) do
Expand Down Expand Up @@ -182,11 +182,6 @@
exists_and_identical?(source, destination)
end

it "should create jquery js" do
destination = "#{@webapp}/javascripts/jquery.min.js"
File.exists?(destination).should be_true
end

it "should create application js" do
destination = "#{@webapp}/javascripts/application.js"
File.exists?(destination).should be_true
Expand Down Expand Up @@ -234,7 +229,7 @@

before(:all) do
@project_path = "src/vraptor-scaffold"
AppGenerator.new(@project_path, ["-p=br.com.caelum"]).invoke_all
AppGenerator.new(@project_path, ["-p=br.com.caelum", "--skip_jquery"]).invoke_all
@main_java = "#{@project_path}/#{Configuration::MAIN_SRC}/br/com/caelum"
@test_java = "#{@project_path}/#{Configuration::TEST_SRC}/br/com/caelum"
end
Expand Down Expand Up @@ -265,24 +260,24 @@

it "should configure freemarker template engine" do
template = mock(FreemarkerTemplateEngine)
FreemarkerTemplateEngine.stub!(:new).with(@project_path).and_return(template)
FreemarkerTemplateEngine.stub!(:new).with(@project_path, anything()).and_return(template)
template.should_receive(:configure)
AppGenerator.new(@project_path, ["--template-engine=ftl"]).invoke_all
AppGenerator.new(@project_path, ["--template-engine=ftl", "--skip_jquery"]).invoke_all
end

it "should configure jsp template engine" do
template = mock(JspTemplateEngine)
JspTemplateEngine.stub!(:new).with(@project_path).and_return(template)
JspTemplateEngine.stub!(:new).with(@project_path, anything()).and_return(template)
template.should_receive(:configure)
AppGenerator.new(@project_path).invoke_all
AppGenerator.new(@project_path, ["--skip_jquery"]).invoke_all
end
end

context "configuring ant application" do

before(:all) do
@project_path = "vraptor-scaffold"
AppGenerator.new(@project_path, ["-b=ant"]).invoke_all
AppGenerator.new(@project_path, ["-b=ant", "--skip_jquery"]).invoke_all
end

after(:all) do
Expand Down Expand Up @@ -341,7 +336,7 @@
context "skip eclipse configuration files" do
before(:all) do
@project_path = "vraptor-scaffold"
AppGenerator.new(@project_path, ["--skip-eclipse"]).invoke_all
AppGenerator.new(@project_path, ["--skip-eclipse", "--skip_jquery"]).invoke_all
end

after(:all) do
Expand All @@ -353,11 +348,43 @@
end
end

context "no skip jquery download file" do
before(:all) do
@project_path = "vraptor-scaffold"

mock_http_request()

AppGenerator.new(@project_path).invoke_all
end

after(:all) do
FileUtils.remove_dir(@project_path)
end

it "should exist jquery.min.js file" do
javascripts = File.join @project_path, Configuration::WEB_APP, "javascripts", "jquery.min.js"
File.exist?(javascripts).should be_true
end

it "should show a message for http problems" do
http_request = mock(Net::HTTP)
http_request.stub!(:get).with(kind_of(String)).and_throw(Net::HTTPError)
VraptorScaffold::HttpRequest.stub!(:open_session).with(kind_of(String)).and_return(http_request)

Kernel.should_receive(:puts).with("Was not possible to download jQuery.")

local_project_path = "project_without_jquery_by_http_error"
AppGenerator.new(local_project_path).invoke_all

FileUtils.remove_dir(local_project_path)
end
end

context "configuring gradle application" do

before(:all) do
@project_path = "vraptor-scaffold"
AppGenerator.new(@project_path, ["-b=gradle"]).invoke_all
AppGenerator.new(@project_path, ["-b=gradle", "--skip_jquery"]).invoke_all
end

after(:all) do
Expand Down Expand Up @@ -424,34 +451,29 @@

it "should be invalid when build tool is not supported" do
Kernel.should_receive(:exit)
AppGenerator.new(@project_path, ["-b=maven"])
AppGenerator.new(@project_path, ["-b=maven", "--skip_jquery"])
end

it "should be invalid when template engine is not supported" do
Kernel.should_receive(:exit)
AppGenerator.new(@project_path, ["-e=velocity"])
AppGenerator.new(@project_path, ["-e=velocity", "--skip_jquery"])
end

it "should be invalid when orm mapping is not supported" do
Kernel.should_receive(:exit)
AppGenerator.new(@project_path, ["-o=toplink"])
end

it "should be invalid when jquery version does not exist" do
Kernel.should_receive(:exit)
AppGenerator.new(@project_path, ["-j=1.x"])
AppGenerator.new(@project_path, ["-o=toplink", "--skip_jquery"])
end

it "should be invalid when gae and heroku are selected" do
Kernel.should_receive(:exit)
AppGenerator.new(@project_path, ["-g", "-h"])
AppGenerator.new(@project_path, ["-g", "-h", "--skip_jquery"])
end
end

context "heroku app" do
before(:all) do
@project_path = "heroku"
AppGenerator.new(@project_path, ["--heroku"]).invoke_all
AppGenerator.new(@project_path, ["--heroku", "--skip_jquery"]).invoke_all
end

after(:all) do
Expand All @@ -476,7 +498,7 @@
context "gae app" do
before(:all) do
@project_path = "gae"
AppGenerator.new(@project_path, ["--gae"]).invoke_all
AppGenerator.new(@project_path, ["--gae", "--skip_jquery"]).invoke_all
@main_java = "#{@project_path}/#{Configuration::MAIN_SRC}"
@app = "#{@main_java}/app"
end
Expand Down

0 comments on commit d2e88bd

Please sign in to comment.