polonium 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. data/CHANGES +7 -0
  2. data/README +27 -12
  3. data/Rakefile +2 -2
  4. data/init.rb +9 -0
  5. data/lib/polonium/adapters/rspec.rb +19 -13
  6. data/lib/polonium/adapters/test_unit.rb +1 -1
  7. data/lib/polonium/configuration.rb +32 -97
  8. data/lib/polonium/driver.rb +25 -28
  9. data/lib/polonium/dsl/selenium_dsl.rb +67 -8
  10. data/lib/polonium/element.rb +60 -30
  11. data/lib/polonium/page.rb +13 -2
  12. data/lib/polonium/server_runners/external_server_runner.rb +20 -0
  13. data/lib/polonium/server_runners/mongrel_server_runner.rb +63 -0
  14. data/lib/polonium/server_runners/server_runner.rb +36 -0
  15. data/lib/polonium/server_runners/webrick_server_runner.rb +49 -0
  16. data/lib/polonium/test_case.rb +1 -19
  17. data/lib/polonium/wait_for.rb +4 -1
  18. data/lib/polonium.rb +6 -4
  19. data/spec/polonium/configuration_spec.rb +41 -99
  20. data/spec/polonium/driver_spec.rb +112 -62
  21. data/spec/polonium/element_spec.rb +69 -24
  22. data/spec/polonium/server_runners/external_server_runner_spec.rb +33 -0
  23. data/spec/polonium/server_runners/mongrel_server_runner_spec.rb +69 -0
  24. data/spec/polonium/server_runners/server_runner_spec.rb +36 -0
  25. data/spec/polonium/server_runners/webrick_server_runner_spec.rb +121 -0
  26. data/spec/polonium/test_case_spec.rb +538 -649
  27. data/spec/rspec/options_spec.rb +23 -22
  28. data/spec/spec_helper.rb +0 -18
  29. data/spec/spec_suite.rb +1 -1
  30. data/spec/test_unit/testrunnermediator_spec.rb +2 -2
  31. metadata +50 -41
  32. data/lib/polonium/dsl/test_unit_dsl.rb +0 -61
  33. data/lib/polonium/mongrel_selenium_server_runner.rb +0 -37
  34. data/lib/polonium/server_runner.rb +0 -33
  35. data/lib/polonium/webrick_selenium_server_runner.rb +0 -33
  36. data/spec/polonium/mongrel_selenium_server_runner_spec.rb +0 -35
  37. data/spec/polonium/server_runner_spec.rb +0 -42
  38. data/spec/polonium/webrick_selenium_server_runner_spec.rb +0 -117
@@ -1,7 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
2
 
3
3
  module Polonium
4
- describe Driver, :shared => true do
4
+ describe Driver do
5
5
  it_should_behave_like "Selenium"
6
6
  attr_reader :driver, :commands
7
7
  before do
@@ -15,90 +15,140 @@ module Polonium
15
15
  def sample_text
16
16
  "test text"
17
17
  end
18
- end
19
18
 
20
- describe Driver, "#initialize" do
21
- it_should_behave_like "Polonium::Driver"
22
-
23
- it "initializes with defaults" do
24
- driver.server_host.should == "localhost"
25
- driver.server_port.should == 4444
26
- driver.browser_start_command.should == "*iexplore"
27
- driver.browser_url.should == "localhost:3000"
28
- driver.timeout_in_milliseconds.should == 30000
29
- end
19
+ describe "#initialize" do
20
+ it "initializes with defaults" do
21
+ driver.server_host.should == "localhost"
22
+ driver.server_port.should == 4444
23
+ driver.browser_start_command.should == "*iexplore"
24
+ driver.browser_url.should == "localhost:3000"
25
+ driver.timeout_in_milliseconds.should == 30000
26
+ end
30
27
 
31
- it "should start" do
32
- mock(driver).do_command.
33
- with("getNewBrowserSession", ["*iexplore", "localhost:3000"]).returns(" 12345")
28
+ it "should start" do
29
+ mock(driver).do_command.
30
+ with("getNewBrowserSession", ["*iexplore", "localhost:3000"]).returns(" 12345")
34
31
 
35
- driver.start
36
- driver.instance_variable_get(:@session_id).should == "12345"
32
+ driver.start
33
+ driver.instance_variable_get(:@session_id).should == "12345"
34
+ end
37
35
  end
38
- end
39
-
40
- describe Driver, "#inner_html_js" do
41
- it_should_behave_like "Polonium::Driver"
42
36
 
43
- it "returns findElement command in js" do
44
- driver.inner_html_js(sample_locator).should ==
45
- %Q|this.page().findElement("#{sample_locator}").innerHTML|
37
+ describe "#inner_html_js" do
38
+ it "returns findElement command in js" do
39
+ driver.inner_html_js(sample_locator).should ==
40
+ %Q|this.page().findElement("#{sample_locator}").innerHTML|
41
+ end
46
42
  end
47
- end
48
43
 
49
- describe Driver, "#wait_for_element_to_contain" do
50
- it_should_behave_like "Polonium::Driver"
44
+ describe "#open and #open_and_wait" do
45
+ it "opens page and waits for it to load" do
46
+ mock(driver).do_command("open", ["http://localhost:4000"])
47
+ mock(driver).do_command("waitForPageToLoad", [driver.default_timeout]) {result}
48
+ mock(driver).do_command("getTitle", []) {result("Some Title")}
51
49
 
52
- it "when finding text within time limit, passes" do
53
- is_element_present_results = [false, true]
54
- mock(driver).do_command('isElementPresent', [sample_locator]).times(2) do
55
- result(is_element_present_results.shift)
56
- end
57
- mock(driver).do_command('getEval', [driver.inner_html_js(sample_locator)]) do
58
- result(sample_text)
50
+ driver.open("http://localhost:4000")
59
51
  end
60
52
 
61
- driver.wait_for_element_to_contain(sample_locator, sample_text)
53
+ it "aliases #open_and_wait to #open" do
54
+ driver.method(:open_and_wait).should == driver.method(:open)
55
+ end
62
56
  end
63
57
 
64
- it "when element not found in time, fails" do
65
- mock(driver).do_command('isElementPresent', [sample_locator]).times(4) do
66
- result(false)
58
+ describe "#type" do
59
+ it "types when element is present and types" do
60
+ is_element_present_results = [false, true]
61
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).twice do
62
+ result(is_element_present_results.shift)
63
+ end
64
+ mock(driver).do_command("type", ["id=foobar", "The Text"]) do
65
+ result()
66
+ end
67
+
68
+ driver.type "id=foobar", "The Text"
67
69
  end
68
70
 
69
- proc do
70
- driver.wait_for_element_to_contain(sample_locator, "")
71
- end.should raise_error(Test::Unit::AssertionFailedError, "Timeout exceeded (after 5 sec)")
72
- end
71
+ it "fails when element is not present" do
72
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).times(4) do
73
+ result(false)
74
+ end
75
+ dont_allow(driver).do_command("type", ["id=foobar", "The Text"])
73
76
 
74
- it "when text does not match in time, fails" do
75
- is_element_present_results = [false, true, true, true]
76
- stub(driver).do_command('isElementPresent', [sample_locator]) do
77
- result(is_element_present_results.shift)
77
+ proc {
78
+ driver.type "id=foobar", "The Text"
79
+ }.should raise_error(Test::Unit::AssertionFailedError)
78
80
  end
79
- stub(driver).do_command('getEval', [driver.inner_html_js(sample_locator)]) do
80
- result(sample_text)
81
+ end
82
+
83
+ describe "#click" do
84
+ it "click when element is present and types" do
85
+ is_element_present_results = [false, true]
86
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).twice do
87
+ result(is_element_present_results.shift)
88
+ end
89
+ mock(driver).do_command("click", ["id=foobar"]) {result}
90
+
91
+ driver.click "id=foobar"
81
92
  end
82
93
 
83
- proc do
84
- driver.wait_for_element_to_contain(sample_locator, "wrong text", nil, 1)
85
- end.should raise_error(Test::Unit::AssertionFailedError, "Timeout exceeded (after 1 sec)")
94
+ it "fails when element is not present" do
95
+ is_element_present_results = [false, false, false, false]
96
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).times(4) do
97
+ result(is_element_present_results.shift)
98
+ end
99
+ dont_allow(driver).do_command("click", [])
100
+
101
+ proc {
102
+ driver.click "id=foobar"
103
+ }.should raise_error(Test::Unit::AssertionFailedError)
104
+ end
86
105
  end
87
- end
88
106
 
89
- describe Driver, "#open and #open_and_wait" do
90
- it_should_behave_like "Polonium::Driver"
107
+ describe "#select" do
108
+ it "types when element is present and types" do
109
+ is_element_present_results = [false, true]
110
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).twice do
111
+ result is_element_present_results.shift
112
+ end
113
+ mock(driver).do_command("select", ["id=foobar", "value=3"]) {result}
91
114
 
92
- it "opens page and waits for it to load" do
93
- mock(driver).do_command("open", ["http://localhost:4000"])
94
- mock(driver).do_command("waitForPageToLoad", [driver.default_timeout]) {result}
95
- mock(driver).do_command("getTitle", []) {result("Some Title")}
115
+ driver.select "id=foobar", "value=3"
116
+ end
117
+
118
+ it "fails when element is not present" do
119
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).times(4) do
120
+ result false
121
+ end
122
+ dont_allow(driver).do_command("select", ["id=foobar", "value=3"])
96
123
 
97
- driver.open("http://localhost:4000")
124
+ proc {
125
+ driver.select "id=foobar", "value=3"
126
+ }.should raise_error(Test::Unit::AssertionFailedError)
127
+ end
98
128
  end
99
129
 
100
- it "aliases #open_and_wait to #open" do
101
- driver.method(:open_and_wait).should == driver.method(:open)
130
+ describe "#click" do
131
+ it "click when element is present and types" do
132
+ is_element_present_results = [false, true]
133
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).twice do
134
+ result is_element_present_results.shift
135
+ end
136
+ mock(driver).do_command("click", ["id=foobar"]) {result}
137
+
138
+ driver.click "id=foobar"
139
+ end
140
+
141
+ it "fails when element is not present" do
142
+ is_element_present_results = [false, false, false, false]
143
+ mock(driver).is_element_present("id=foobar").times(4) do
144
+ is_element_present_results.shift
145
+ end
146
+ dont_allow(driver).do_command("click", ["id=foobar"])
147
+
148
+ proc {
149
+ driver.click "id=foobar"
150
+ }.should raise_error(Test::Unit::AssertionFailedError)
151
+ end
102
152
  end
103
- end
153
+ end
104
154
  end
@@ -109,7 +109,7 @@ module Polonium
109
109
 
110
110
  describe "#assert_value" do
111
111
  it "passes when element is present and value is expected value" do
112
- element_ticks = [false, false, false, true]
112
+ element_ticks = [true, true, true, true]
113
113
  mock(driver).is_element_present(element_locator) do
114
114
  element_ticks.shift
115
115
  end.times(4)
@@ -121,7 +121,7 @@ module Polonium
121
121
  end
122
122
 
123
123
  it "fails when element is present and not expected value" do
124
- mock(driver).is_element_present(element_locator) {true}
124
+ stub(driver).is_element_present(element_locator) {true}
125
125
  stub(driver).get_value(element_locator) {"jane"}
126
126
  proc do
127
127
  element.assert_value("joe")
@@ -149,14 +149,8 @@ module Polonium
149
149
  end
150
150
 
151
151
  describe "#assert_attribute" do
152
- attr_reader :element_locator
153
-
154
- before do
155
- element_locator = "id=foobar"
156
- end
157
-
158
152
  it "passes when element is present and value is expected value" do
159
- element_ticks = [false, false, false, true]
153
+ element_ticks = [true, true, true, true]
160
154
  mock(driver).is_element_present(element_locator) do
161
155
  element_ticks.shift
162
156
  end.times(4)
@@ -186,13 +180,36 @@ module Polonium
186
180
  end
187
181
  end
188
182
 
183
+ describe "#assert_attribute_does_not_contain" do
184
+ it "passes when element is present and value does not contain the illegal value" do
185
+ stub(driver).is_element_present(element_locator) {true}
186
+ stub(driver).get_attribute("#{element_locator}@theattribute") { "jane" }
187
+ element.assert_attribute_does_not_contain('theattribute', "joe")
188
+ end
189
+
190
+ it "passes when element is present and value does contain the illegal value" do
191
+ stub(driver).is_element_present(element_locator) {true}
192
+ stub(driver).get_attribute("#{element_locator}@theattribute") { "jane" }
193
+ proc do
194
+ element.assert_attribute_does_not_contain('theattribute', "jane")
195
+ end.should raise_error(Test::Unit::AssertionFailedError)
196
+ end
197
+
198
+ it "fails when element is not present" do
199
+ stub(driver).is_element_present(element_locator) {false}
200
+ proc do
201
+ element.assert_attribute_does_not_contain('theattribute', "jane")
202
+ end.should raise_error(Test::Unit::AssertionFailedError)
203
+ end
204
+ end
205
+
189
206
  describe "#assert_selected" do
190
207
  before do
191
208
  @element_locator = "id=foobar"
192
209
  end
193
210
 
194
211
  it "passes when element is present and value is expected value" do
195
- element_ticks = [false, false, false, true]
212
+ element_ticks = [true, true, true, true]
196
213
  mock(driver).is_element_present(element_locator) do
197
214
  element_ticks.shift
198
215
  end.times(4)
@@ -229,7 +246,7 @@ module Polonium
229
246
  end
230
247
 
231
248
  it "passes when element exists and is visible" do
232
- element_ticks = [false, false, false, true]
249
+ element_ticks = [true, true, true, true]
233
250
  mock(driver).is_element_present(element_locator) do
234
251
  element_ticks.shift
235
252
  end.times(4)
@@ -262,7 +279,7 @@ module Polonium
262
279
  end
263
280
 
264
281
  it "passes when element exists and is not visible" do
265
- element_ticks = [false, false, false, true]
282
+ element_ticks = [true, true, true, true]
266
283
  mock(driver).is_element_present(element_locator) do
267
284
  element_ticks.shift
268
285
  end.times(4)
@@ -295,7 +312,7 @@ module Polonium
295
312
  end
296
313
 
297
314
  it "passes when element is present and value is expected value" do
298
- element_ticks = [false, false, false, true]
315
+ element_ticks = [true, true, true, true]
299
316
  mock(driver).is_element_present(element_locator) do
300
317
  element_ticks.shift
301
318
  end.times(4)
@@ -328,7 +345,7 @@ module Polonium
328
345
  end
329
346
 
330
347
  it "passes when element is present and value is expected value" do
331
- element_ticks = [false, false, false, true]
348
+ element_ticks = [true, true, true, true]
332
349
  mock(driver).is_element_present(element_locator) do
333
350
  element_ticks.shift
334
351
  end.times(4)
@@ -361,7 +378,7 @@ module Polonium
361
378
  end
362
379
 
363
380
  it "passes when element is present and value is expected value" do
364
- element_ticks = [false, false, false, true]
381
+ element_ticks = [true, true, true, true]
365
382
  mock(driver).is_element_present(element_locator) do
366
383
  element_ticks.shift
367
384
  end.times(4)
@@ -400,7 +417,7 @@ module Polonium
400
417
 
401
418
  describe "when passed a String" do
402
419
  it "passes when element is present and the element contains text" do
403
- element_ticks = [false, false, false, true]
420
+ element_ticks = [true, true, true, true]
404
421
  mock(driver).is_element_present(element_locator) do
405
422
  element_ticks.shift
406
423
  end.times(4)
@@ -422,7 +439,7 @@ module Polonium
422
439
 
423
440
  describe "when passed a Regexp" do
424
441
  it "passes when element is present and the element contains text that matches the regexp" do
425
- element_ticks = [false, false, false, true]
442
+ element_ticks = [true, true, true, true]
426
443
  mock(driver).is_element_present(element_locator) do
427
444
  element_ticks.shift
428
445
  end.times(4)
@@ -467,7 +484,7 @@ module Polonium
467
484
 
468
485
  describe "when passed a String" do
469
486
  it "passes when element is present and the element does not contain text" do
470
- element_ticks = [false, false, false, true]
487
+ element_ticks = [true, true, true, true]
471
488
  mock(driver).is_element_present(element_locator) do
472
489
  element_ticks.shift
473
490
  end.times(4)
@@ -489,7 +506,7 @@ module Polonium
489
506
 
490
507
  describe "when passed a Regexp" do
491
508
  it "passes when element is present and the element does not contain text that matches the Regexp" do
492
- element_ticks = [false, false, false, true]
509
+ element_ticks = [true, true, true, true]
493
510
  mock(driver).is_element_present(element_locator) do
494
511
  element_ticks.shift
495
512
  end.times(4)
@@ -524,7 +541,7 @@ module Polonium
524
541
  end
525
542
 
526
543
  it "passes when element is present and value is expected value" do
527
- element_ticks = [false, false, false, true]
544
+ element_ticks = [true, true, true, true]
528
545
  mock(driver).is_element_present(@element_locator) do
529
546
  element_ticks.shift
530
547
  end.times(4)
@@ -558,7 +575,7 @@ module Polonium
558
575
  end
559
576
 
560
577
  it "passes when element is present and passed in text and Regexp matches are in order" do
561
- element_ticks = [false, false, false, true]
578
+ element_ticks = [true, true, true, true]
562
579
  mock(driver).is_element_present(@element_locator) do
563
580
  element_ticks.shift
564
581
  end.times(4)
@@ -589,12 +606,40 @@ module Polonium
589
606
  end.should raise_error(Test::Unit::AssertionFailedError)
590
607
  end
591
608
  end
609
+
610
+ describe "#assert_number_of_children" do
611
+ before do
612
+ @element_locator = "id=foobar"
613
+ @evaled_js = "this.page().findElement('#{@element_locator}').childNodes.length"
614
+ end
615
+
616
+ it "passes when element is present and it contains the correct number of (direct) children" do
617
+ stub(driver).is_element_present(@element_locator) {true}
618
+ stub(driver).get_eval(@evaled_js) { 3 }
619
+ element.assert_number_of_children(3)
620
+ end
621
+
622
+ it "fails when element is present and it contains the wrong number of (direct) children" do
623
+ stub(driver).is_element_present(@element_locator) {true}
624
+ stub(driver).get_eval(@evaled_js) { 999 }
625
+ proc do
626
+ element.assert_number_of_children(3)
627
+ end.should raise_error(Test::Unit::AssertionFailedError)
628
+ end
629
+
630
+ it "fails when element is not present" do
631
+ stub(driver).is_element_present(@element_locator) {false}
632
+ proc do
633
+ element.assert_number_of_children 3
634
+ end.should raise_error(Test::Unit::AssertionFailedError)
635
+ end
636
+ end
592
637
 
593
638
  describe "#method_missing" do
594
639
  it "delegates command to the driver" do
595
- element.methods.should_not include('click')
596
- mock(driver).click(@element_locator)
597
- element.click
640
+ element.methods.should_not include('foobar')
641
+ mock(driver).foobar(@element_locator)
642
+ element.foobar
598
643
  end
599
644
  end
600
645
  end
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ module Polonium
4
+ module ServerRunners
5
+ describe ExternalServerRunner do
6
+ attr_reader :configuration, :rails_env, :rails_root, :runner, :start_server_cmd, :stop_server_cmd
7
+ before do
8
+ @configuration = Configuration.new
9
+ @rails_env = configuration.rails_env = 'test'
10
+ @rails_root = configuration.rails_root = File.dirname(__FILE__)
11
+ @start_server_cmd = "cd #{rails_root}; script/server -e #{rails_env} -p #{configuration.internal_app_server_port} -c #{rails_root}"
12
+ @stop_server_cmd = "ps ax | grep 'script/server -e #{rails_env}' | sed /grep/d | awk '{print $1}' | xargs kill -9 2>/dev/null"
13
+
14
+ @runner = ExternalServerRunner.new(configuration)
15
+ end
16
+
17
+ describe "#start" do
18
+ it "stops the server, then starts an external rails server" do
19
+ mock(runner).system(stop_server_cmd).ordered
20
+ mock(runner).system(start_server_cmd).ordered
21
+ runner.start
22
+ end
23
+ end
24
+
25
+ describe "#stop" do
26
+ it "stops the server" do
27
+ mock(runner).system(stop_server_cmd).ordered
28
+ runner.stop
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,69 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ module Polonium
4
+ module ServerRunners
5
+ describe MongrelServerRunner do
6
+ describe "#start_server" do
7
+ attr_reader :configuration
8
+ before do
9
+ @configuration = Configuration.new
10
+ end
11
+
12
+ it "initializes server and runs app_server_initialization callback" do
13
+ runner = MongrelServerRunner.new(configuration)
14
+
15
+ fake_rails = "fake rails"
16
+ mongrel_configurator = nil
17
+ mock.proxy(runner).create_mongrel_configurator do |mongrel_configurator|
18
+ mongrel_configurator = mongrel_configurator
19
+ stub(configuration).create_mongrel_configurator {mongrel_configurator}
20
+ mock(mongrel_configurator).run
21
+ stub(mongrel_configurator).log
22
+ mock(mongrel_configurator).join
23
+ mock(mongrel_configurator).rails {fake_rails}
24
+ mock(mongrel_configurator).uri("/", {:handler => fake_rails})
25
+ mock(mongrel_configurator).load_plugins
26
+ mock(mongrel_configurator).listener.yields(mongrel_configurator)
27
+ mongrel_configurator
28
+ end
29
+
30
+ callback_mongrel = nil
31
+ configuration.app_server_initialization = proc do |mongrel|
32
+ callback_mongrel = mongrel
33
+ end
34
+ stub(runner).defaults do
35
+ ; {:environment => ""};
36
+ end
37
+ mock(Thread).start.yields
38
+
39
+ runner.start
40
+ callback_mongrel.should == mongrel_configurator
41
+ end
42
+
43
+ describe "#create_mongrel_configurator" do
44
+ it "creates Mongrel configurator" do
45
+ configuration.internal_app_server_host = "localhost"
46
+ configuration.internal_app_server_port = 4000
47
+ configuration.rails_env = "test"
48
+ configuration.rails_root = rails_root = File.dirname(__FILE__)
49
+
50
+ runner = MongrelServerRunner.new(configuration)
51
+ configurator = runner.send(:create_mongrel_configurator)
52
+ configurator.defaults[:host].should == "localhost"
53
+ configurator.defaults[:port].should == 4000
54
+ configurator.defaults[:cwd].should == configuration.rails_root
55
+ configurator.defaults[:log_file].should == "#{configuration.rails_root}/log/mongrel.log"
56
+ configurator.defaults[:pid_file].should == "#{configuration.rails_root}/log/mongrel.pid"
57
+ configurator.defaults[:environment].should == "test"
58
+ configurator.defaults[:docroot].should == "#{rails_root}/public"
59
+ configurator.defaults[:mime_map].should be_nil
60
+ configurator.defaults[:daemon].should == false
61
+ configurator.defaults[:debug].should == false
62
+ configurator.defaults[:includes].should == ["mongrel"]
63
+ configurator.defaults[:config_script].should be_nil
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ module Polonium
4
+ module ServerRunners
5
+ describe ServerRunner do
6
+ before(:each) do
7
+ @runner = ServerRunner.new(Configuration.new)
8
+ class << @runner
9
+ public :start_server, :stop_server
10
+ end
11
+ end
12
+
13
+ it "should initialize started? to be false" do
14
+ @runner.started?.should == false
15
+ end
16
+
17
+ it "start method should start new thread and set started" do
18
+ mock(@runner).start_server
19
+ stub(@runner).stop_server
20
+ @runner.start
21
+ end
22
+
23
+ it "stop method should set started? to false" do
24
+ def @runner.stop_server;
25
+ end
26
+ @runner.instance_eval {@started = true}
27
+ @runner.stop
28
+ @runner.started?.should == false
29
+ end
30
+
31
+ it "start_server method should raise a NotImplementedError by default" do
32
+ proc {@runner.start_server}.should raise_error(NotImplementedError)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,121 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ module Polonium
4
+ module ServerRunners
5
+ describe WebrickServerRunner do
6
+ attr_reader :configuration, :mock_server
7
+ before(:each) do
8
+ Object.const_set(:RAILS_ROOT, "foobar")
9
+ end
10
+
11
+ after(:each) do
12
+ Object.instance_eval {remove_const :RAILS_ROOT}
13
+ end
14
+
15
+ it "start method should initialize the HttpServer with parameters" do
16
+ runner = create_runner_that_is_stubbed_so_start_method_works
17
+ runner.start
18
+ end
19
+
20
+ it "start method should mount and start the server" do
21
+ runner = create_runner_that_is_stubbed_so_start_method_works
22
+ runner.start
23
+ end
24
+
25
+ it "start method should require environment when rails_root is not set" do
26
+ runner = create_runner_that_is_stubbed_so_start_method_works
27
+ requires = []
28
+ stub(runner).require {|val| requires << val}
29
+
30
+ runner.start
31
+ requires.any? {|r| r =~ /\/config\/environment/}.should == true
32
+ end
33
+
34
+ it "start method should trap server.shutdown" do
35
+ runner = create_runner_that_is_stubbed_so_start_method_works
36
+
37
+ (
38
+ class << runner;
39
+ self;
40
+ end).class_eval {attr_reader :trap_signal_name}
41
+ def runner.trap(signal_name, &block)
42
+ @trap_signal_name = signal_name
43
+ block.call
44
+ end
45
+ mock(mock_server).shutdown
46
+
47
+ runner.start
48
+ runner.trap_signal_name.should == "INT"
49
+ end
50
+
51
+ it "should shutdown webrick server" do
52
+ runner = create_runner_that_is_stubbed_so_start_method_works
53
+ runner.start
54
+ mock(mock_server).shutdown
55
+ runner.stop
56
+ end
57
+
58
+ def create_runner_that_is_stubbed_so_start_method_works
59
+ configuration = Polonium::Configuration.new
60
+ runner = WebrickServerRunner.new(configuration)
61
+ class << runner;
62
+ public :start_server;
63
+ end
64
+
65
+ stub(runner).require
66
+
67
+ configuration.internal_app_server_port = 4000
68
+ configuration.internal_app_server_host = "localhost"
69
+ configuration.rails_env = "test"
70
+
71
+ @mock_server = "mock_server"
72
+ stub(WEBrick::HTTPServer).new {mock_server}
73
+
74
+ stub(mock_server).mount('/')
75
+ mock(mock_server) do |s|
76
+ s.mount(
77
+ '/',
78
+ DispatchServlet,
79
+ {
80
+ :port => configuration.internal_app_server_port,
81
+ :ip => configuration.internal_app_server_host,
82
+ :environment => configuration.rails_env,
83
+ :server_root => File.expand_path("#{configuration.rails_root}/public/"),
84
+ :server_type => WEBrick::SimpleServer,
85
+ :charset => "UTF-8",
86
+ :mime_types => WEBrick::HTTPUtils::DefaultMimeTypes,
87
+ :working_directory => File.expand_path(configuration.rails_root.to_s)
88
+ }
89
+ )
90
+ s.start
91
+ end
92
+
93
+ mock(Thread).start.yields
94
+
95
+ return runner
96
+ end
97
+
98
+ describe "#create_webrick_server" do
99
+ it "creates webrick http server" do
100
+ configuration = Polonium::Configuration.new
101
+ configuration.internal_app_server_port = 4000
102
+ configuration.internal_app_server_host = "localhost"
103
+
104
+ mock_logger = "logger"
105
+ mock(configuration).new_logger {mock_logger}
106
+ mock(WEBrick::HTTPServer).new({
107
+ :Port => 4000,
108
+ :BindAddress => "localhost",
109
+ :ServerType => WEBrick::SimpleServer,
110
+ :MimeTypes => WEBrick::HTTPUtils::DefaultMimeTypes,
111
+ :Logger => mock_logger,
112
+ :AccessLog => []
113
+ })
114
+ runner = WebrickServerRunner.new(configuration)
115
+ server = runner.send(:create_webrick_server)
116
+ end
117
+ end
118
+
119
+ end
120
+ end
121
+ end