From e3b9594e882939c75226a07c69ba1d8ca69fb9bf Mon Sep 17 00:00:00 2001 From: Fritz Thielemann Date: Tue, 10 Nov 2009 18:50:45 +0100 Subject: [PATCH 1/3] initial config params handling --- README | 22 +++++++++++++++++ init.rb | 27 +++++++++++++++++++++ lib/steam.rb | 21 ++++++++-------- lib/steam/browser/html_unit/connection.rb | 2 +- lib/steam/browser/html_unit/drb/service.rb | 2 +- lib/steam/browser/html_unit/web_response.rb | 4 +-- lib/steam/configuration.rb | 27 +++++++++++++++++++++ lib/steam/java.rb | 2 +- lib/steam/request.rb | 6 ++--- lib/steam/session/rails.rb | 2 +- 10 files changed, 96 insertions(+), 19 deletions(-) create mode 100644 lib/steam/configuration.rb diff --git a/README b/README index 974e8d6..3966f50 100644 --- a/README +++ b/README @@ -29,6 +29,28 @@ After that, installing Steam for a Rails project should be as simple as typing Note: Since the HtmlUnit and all the JARs it depends on come packaged with Steam, the download might take a while. +Configuration +============= + +You can control some parameters in init.rb for fitting your needs: + + Steam.configure do |config| + #:javaloadparams: "-Xms256M -Xmx2048M" + config.java_load_params = "-Xmx2048M" + # your test server running steam + config.server_name = "localhost" + # the port you start your test server + config.server_port = "3000" + # the url scheme for rack + config.rack_url_scheme = "http" + # defaut charset + config.charset = "utf-8" + # DRB uri + config.drb_uri = "druby://127.0.0.1:9000" + # for which server the session request the env + config.request_env_for = "http://localhost" + end + Usage ===== diff --git a/init.rb b/init.rb index 8bc53ef..6ec7af6 100644 --- a/init.rb +++ b/init.rb @@ -8,3 +8,30 @@ # ActionController::Dispatcher.middleware.instance_eval do # use Steam::Reloader # end + +Steam.configure do |config| + # jvm load params - lib/steam/java.rb + #:javaloadparams: "-Xms256M -Xmx2048M" + config.java_load_params = "-Xmx2048M" + + # common params - lib/steam/request.rb + config.server_name = "localhost" + config.server_port = "3000" + config.rack_url_scheme = "http" + + # char params - lib/steam/browser/html_unit/connection.rb + web_response.rb + config.charset = "utf-8" + + # drb params - lib/steam/browser/html_unit/drb/service.rb + config.drb_uri = "druby://127.0.0.1:9000" + + # JS params - lib/steam/browser/html_unit/html_unit.rb + #@client.waitForBackgroundJavaScript(5000) # FIXME should probably use some block syntax + + # env params - lib/steam/connection/patron.rb + #headers = {} # FIXME ... request.header, cookies? + #options = { :data => '' } # FIXME ... body + + # env params - lib/steam/session/rails.rb + config.request_env_for = "http://localhost" +end diff --git a/lib/steam.rb b/lib/steam.rb index 7757595..7102156 100644 --- a/lib/steam.rb +++ b/lib/steam.rb @@ -2,16 +2,17 @@ require 'rack' module Steam - autoload :Browser, 'steam/browser' - autoload :Connection, 'steam/connection' - autoload :Dom, 'steam/dom' - autoload :Forker, 'steam/forker' - autoload :Java, 'steam/java' - autoload :Locators, 'steam/locators' - autoload :Matchers, 'steam/matchers' - autoload :Reloader, 'steam/reloader' - autoload :Request, 'steam/request' - autoload :Session, 'steam/session' + autoload :Browser, 'steam/browser' + autoload :Connection, 'steam/connection' + autoload :Dom, 'steam/dom' + autoload :Forker, 'steam/forker' + autoload :Java, 'steam/java' + autoload :Locators, 'steam/locators' + autoload :Matchers, 'steam/matchers' + autoload :Reloader, 'steam/reloader' + autoload :Request, 'steam/request' + autoload :Session, 'steam/session' + autoload :Configuration, 'steam/configuration' # autoload :TestCase, 'steam/test_case' # autoload :Utils, 'steam/utils' end \ No newline at end of file diff --git a/lib/steam/browser/html_unit/connection.rb b/lib/steam/browser/html_unit/connection.rb index adc1c60..835fd96 100644 --- a/lib/steam/browser/html_unit/connection.rb +++ b/lib/steam/browser/html_unit/connection.rb @@ -45,7 +45,7 @@ def set_response(url, response) body = response.body.join status = response.status message = Rack::Utils::HTTP_STATUS_CODES[status.to_i] - charset = 'utf-8' # FIXME + charset = Steam.config.charset headers = response.header.map { |key, value| Java::NameValuePair.new(key, value) } headers = Java::Arrays.asList(headers) content_type = response.content_type diff --git a/lib/steam/browser/html_unit/drb/service.rb b/lib/steam/browser/html_unit/drb/service.rb index 26f0a3c..66c2490 100644 --- a/lib/steam/browser/html_unit/drb/service.rb +++ b/lib/steam/browser/html_unit/drb/service.rb @@ -7,7 +7,7 @@ module Drb module Service class << self def uri - 'druby://127.0.0.1:9000' + Steam.config.drb_uri end def daemonize(connection = nil, options = {}) diff --git a/lib/steam/browser/html_unit/web_response.rb b/lib/steam/browser/html_unit/web_response.rb index fd64839..af348f4 100644 --- a/lib/steam/browser/html_unit/web_response.rb +++ b/lib/steam/browser/html_unit/web_response.rb @@ -66,14 +66,14 @@ def getContentType end def getContentCharset - @content_charset ||= 'utf-8' # FIXME + @content_charset ||= Steam.config.charset rescue Exception => e puts e.message e.backtrace.each { |line| puts line } end def getContentCharsetOrNull - @content_charset_or_null ||= 'utf-8' # FIXME + @content_charset_or_null ||= Steam.config.charset rescue Exception => e puts e.message e.backtrace.each { |line| puts line } diff --git a/lib/steam/configuration.rb b/lib/steam/configuration.rb new file mode 100644 index 0000000..c99a04f --- /dev/null +++ b/lib/steam/configuration.rb @@ -0,0 +1,27 @@ +module Steam + + def self.config + @@config ||= Configuration.new + end + + def self.configure(&block) + raise "#configure must be sent a block" unless block_given? + yield config + end + + class Configuration + + attr_accessor_with_default :java_load_params, "-Xmx2048M" + attr_accessor_with_default :server_name, "localhost" + attr_accessor_with_default :server_port, "3000" + attr_accessor_with_default :rack_url_scheme, "http" + attr_accessor_with_default :charset, "utf-8" + attr_accessor_with_default :drb_uri, "druby://127.0.0.1:9000" + attr_accessor_with_default :request_env_for, "http://localhost" + + def initialize + end + + end + +end diff --git a/lib/steam/java.rb b/lib/steam/java.rb index c4998d7..b31d351 100644 --- a/lib/steam/java.rb +++ b/lib/steam/java.rb @@ -32,7 +32,7 @@ def import_common! def set_classpath! path = File.expand_path(File.dirname(__FILE__) + "/../htmlunit/") - Rjb::load(Dir["#{path}/*.jar"].join(':'), ['-Xmx2048M']) + Rjb::load(Dir["#{path}/*.jar"].join(':'), Steam.config.java_load_params) end end end diff --git a/lib/steam/request.rb b/lib/steam/request.rb index f30f314..75d8262 100644 --- a/lib/steam/request.rb +++ b/lib/steam/request.rb @@ -21,11 +21,11 @@ def env_for(uri = '', opts = {}) env.merge!( 'REQUEST_METHOD' => opts[:method] || 'GET', - 'SERVER_NAME' => 'localhost', - 'SERVER_PORT' => '3000', + 'SERVER_NAME' => Steam.config.server_name, + 'SERVER_PORT' => Steam.config.server_port, 'QUERY_STRING' => uri.query.to_s, 'PATH_INFO' => (!uri.path || uri.path.empty?) ? '/' : uri.path, - 'rack.url_scheme' => 'http', + 'rack.url_scheme' => Steam.config.rack_url_scheme, 'SCRIPT_NAME' => opts[:script_name] || '', 'rack.errors' => StringIO.new, 'rack.input' => input.is_a?(String) ? StringIO.new(input) : input, diff --git a/lib/steam/session/rails.rb b/lib/steam/session/rails.rb index 4f8379e..8ec9ed8 100644 --- a/lib/steam/session/rails.rb +++ b/lib/steam/session/rails.rb @@ -26,7 +26,7 @@ def url_for(options) # FIXME remove ActionController::Request dependency def generic_url_rewriter - env = Request.env_for('http://localhost') + env = Request.env_for(Steam.config.request_env_for) UrlRewriter.new(ActionController::Request.new(env), {}) end end From 6579675742b175edfe7094041f108b5c5973eec7 Mon Sep 17 00:00:00 2001 From: Fritz Thielemann Date: Wed, 11 Nov 2009 16:30:59 +0100 Subject: [PATCH 2/3] reworked --- init.rb | 4 ++-- lib/steam.rb | 3 ++- lib/steam/configuration.rb | 14 +++++++------- lib/steam/java.rb | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/init.rb b/init.rb index 6ec7af6..89f5548 100644 --- a/init.rb +++ b/init.rb @@ -11,8 +11,8 @@ Steam.configure do |config| # jvm load params - lib/steam/java.rb - #:javaloadparams: "-Xms256M -Xmx2048M" - config.java_load_params = "-Xmx2048M" + #:javaloadparams: "'-Xms256M', '-Xmx2048M'" + config.java_load_params = "-Xmx1024M" # common params - lib/steam/request.rb config.server_name = "localhost" diff --git a/lib/steam.rb b/lib/steam.rb index 7102156..bead2cc 100644 --- a/lib/steam.rb +++ b/lib/steam.rb @@ -12,7 +12,8 @@ module Steam autoload :Reloader, 'steam/reloader' autoload :Request, 'steam/request' autoload :Session, 'steam/session' - autoload :Configuration, 'steam/configuration' +# autoload :Configuration, 'steam/configuration' + require 'steam/configuration' # autoload :TestCase, 'steam/test_case' # autoload :Utils, 'steam/utils' end \ No newline at end of file diff --git a/lib/steam/configuration.rb b/lib/steam/configuration.rb index c99a04f..d10ed8b 100644 --- a/lib/steam/configuration.rb +++ b/lib/steam/configuration.rb @@ -11,13 +11,13 @@ def self.configure(&block) class Configuration - attr_accessor_with_default :java_load_params, "-Xmx2048M" - attr_accessor_with_default :server_name, "localhost" - attr_accessor_with_default :server_port, "3000" - attr_accessor_with_default :rack_url_scheme, "http" - attr_accessor_with_default :charset, "utf-8" - attr_accessor_with_default :drb_uri, "druby://127.0.0.1:9000" - attr_accessor_with_default :request_env_for, "http://localhost" + attr_accessor :java_load_params + attr_accessor :server_name + attr_accessor :server_port + attr_accessor :rack_url_scheme + attr_accessor :charset + attr_accessor :drb_uri + attr_accessor :request_env_for def initialize end diff --git a/lib/steam/java.rb b/lib/steam/java.rb index b31d351..a6fd89a 100644 --- a/lib/steam/java.rb +++ b/lib/steam/java.rb @@ -32,7 +32,7 @@ def import_common! def set_classpath! path = File.expand_path(File.dirname(__FILE__) + "/../htmlunit/") - Rjb::load(Dir["#{path}/*.jar"].join(':'), Steam.config.java_load_params) + Rjb::load(Dir["#{path}/*.jar"].join(':'), Steam.config.java_load_params.to_a) end end end From d529867c0cbee63d023715ead9c5ea7a3c005f21 Mon Sep 17 00:00:00 2001 From: Fritz Thielemann Date: Mon, 14 Feb 2011 17:47:30 +0100 Subject: [PATCH 3/3] minor changes to get rid of to_a (prep for ruby 1.9) --- Gemfile | 4 ++-- README.textile | 3 +-- lib/steam.rb | 2 +- lib/steam/java.rb | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index 1393f1b..b005c64 100644 --- a/Gemfile +++ b/Gemfile @@ -1,11 +1,11 @@ source :rubygems gem 'rack' -gem 'rjb' +gem 'rjb', :platforms => :ruby # to prevent some errors with precompiled versions while using ruby 1.9 and rvm gem 'locator', :path => '~/Development/projects/locator' group :test do gem 'mocha' - gem 'ruby-debug' + #gem 'ruby-debug' <-- because of diff versions for ruby 1.8 and 1.9 recommend to take out of repo file gem 'test_declarative' end diff --git a/README.textile b/README.textile index 1d4e979..86c5800 100644 --- a/README.textile +++ b/README.textile @@ -32,7 +32,6 @@ Since some Mac OS X 10.6 update removed the Java header files that are necessary Also, if you are on Mac OS X, use `sudo` and get an error saying that "JAVA_HOME is not set" then you need to export the JAVA_HOME variable for RJB. See here for two solutions: "Installing RJB on Mac OS X":http://www.elctech.com/articles/sudo-java_home-and-mac-os-x. The visudo way worked for us. Don't forget to add yourself to the sudoers file, though. - h2. Configuration @@ -41,7 +40,7 @@ You should not need to configure anything. If you do need though have a look at E.g. in order to tweak the Java load params you can -pre. Steam.config[:java_load_params] = "-Xmx2048M" +pre. Steam.config[:java_load_params] = ["-Xmx2048M"] If you want to test your application through Cucumber features then you need to setup your Cucumber environment and steps accordingly. You can find an example for a Cucumber setup here: "env.rb":http://github.com/svenfuchs/steam/blob/master/example/cucumber/env.rb. diff --git a/lib/steam.rb b/lib/steam.rb index 8f6ef7d..8ec5b18 100644 --- a/lib/steam.rb +++ b/lib/steam.rb @@ -23,7 +23,7 @@ def config :server_port => '3000', :url_scheme => 'http', :charset => 'utf-8', - :java_load_params => '-Xmx1024M', + :java_load_params => ['-Xmx1024M'], :drb_uri => 'druby://127.0.0.1:9000', :html_unit => { :java_path => File.expand_path("../../vendor/htmlunit-2.6/", __FILE__), diff --git a/lib/steam/java.rb b/lib/steam/java.rb index 26c1816..b3a1e36 100644 --- a/lib/steam/java.rb +++ b/lib/steam/java.rb @@ -60,7 +60,7 @@ def load_from(path) end def load(paths) - Rjb::load(paths, Steam.config[:java_load_params].to_a) + Rjb::load(paths, Steam.config[:java_load_params]) end def logger(classifier)