torquebox-configure 2.0.0.beta1-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,482 @@
1
+ require 'options_macros'
2
+ require 'torquebox/configuration/global'
3
+
4
+ include TorqueBox::Configuration
5
+
6
+ describe "TorqueBox.configure using the GlobalConfiguration" do
7
+ before(:each) do
8
+ Thread.current[:torquebox_config] = TorqueBox::Configuration::GlobalConfiguration.new
9
+ Thread.current[:torquebox_config_entry_map] = TorqueBox::Configuration::GlobalConfiguration::ENTRY_MAP
10
+ end
11
+
12
+ shared_examples_for "options" do
13
+ before(:each) do
14
+ validator = mock('Validator')
15
+ validator.stub(:valid?).and_return(true)
16
+ TorqueBox::Configuration::Validator.stub(:new).and_return(validator)
17
+ end
18
+
19
+ it "should parse a hash" do
20
+ config = TorqueBox.configure do |cfg|
21
+ cfg.send( @method, {'foo' => 'bar'} )
22
+ end
23
+
24
+ config['<root>'][@method].should == {'foo' => 'bar'}
25
+ end
26
+
27
+ it "should parse a block" do
28
+ config = TorqueBox.configure do |cfg|
29
+ cfg.send( @method ) { foo 'bar'}
30
+ end
31
+ config['<root>'][@method].should == { :foo => 'bar' }
32
+ end
33
+ end
34
+
35
+ shared_examples_for "a thing with options" do
36
+ before(:each) do
37
+ validator = mock('Validator')
38
+ validator.stub(:valid?).and_return(true)
39
+ TorqueBox::Configuration::Validator.stub(:new).and_return(validator)
40
+ end
41
+
42
+ def assert_options(config, key, options)
43
+ if @discrete
44
+ config['<root>'][@method].should == [[key, options]]
45
+ else
46
+ config['<root>'][@method][key].should == options
47
+ end
48
+
49
+ end
50
+
51
+ it "should parse a string and a hash" do
52
+ config = TorqueBox.configure do |cfg|
53
+ cfg.send( @method, 'a string', {'foo' => 'bar'} )
54
+ end
55
+
56
+ assert_options( config, 'a string', {'foo' => 'bar'} )
57
+ end
58
+
59
+ it "should parse a symbol and a hash" do
60
+ config = TorqueBox.configure do |cfg|
61
+ cfg.send( @method, :a_sym, {'foo' => 'bar'} )
62
+ end
63
+
64
+ assert_options( config, 'a_sym', {'foo' => 'bar'} )
65
+ end
66
+
67
+ it "should parse a constant and a hash" do
68
+ config = TorqueBox.configure do |cfg|
69
+ cfg.instance_eval "#{@method} AConstant, {'foo' => 'bar'}"
70
+ end
71
+ assert_options( config, 'AConstant', {'foo' => 'bar'} )
72
+ end
73
+
74
+ it "should parse a thing and a block" do
75
+ config = TorqueBox.configure do |cfg|
76
+ cfg.send( @method, 'a thing' ) { foo 'bar' }
77
+ end
78
+
79
+ assert_options( config, 'a thing', { :foo => 'bar' } )
80
+ end
81
+
82
+ end
83
+
84
+ describe '#authentication' do
85
+ before(:each) { @method = 'authentication' }
86
+ it_should_behave_like 'a thing with options'
87
+
88
+ it_should_not_allow_invalid_options { authentication 'a-name', :foo => :bar }
89
+ it_should_allow_valid_options { authentication 'a-name', :domain => :pizza }
90
+ end
91
+
92
+ describe "#credential" do
93
+ it "should only be valid inside authentication" do
94
+ lambda {
95
+ TorqueBox.configure { credential 'ham', 'biscuit' }
96
+ }.should raise_error(TorqueBox::Configuration::ConfigurationError)
97
+
98
+ lambda {
99
+ TorqueBox.configure do
100
+ authentication :default, :domain => 'ham' do
101
+ credential 'ham', 'biscuit'
102
+ end
103
+ end
104
+ }.should_not raise_error(TorqueBox::Configuration::ConfigurationError)
105
+ end
106
+
107
+ it "should nest under its parent" do
108
+ config = TorqueBox.configure do
109
+ authentication :default, :domain => 'ham' do
110
+ credential 'ham', 'biscuit'
111
+ end
112
+ end
113
+ config['<root>']['authentication']['default']['credential'].should == [['ham', 'biscuit']]
114
+ end
115
+
116
+ it "should nest under its parent properly when called more than once" do
117
+ config = TorqueBox.configure do
118
+ authentication :default, :domain => 'ham' do
119
+ credential 'ham', 'biscuit'
120
+ credential 'biscuit', 'gravy'
121
+ end
122
+ end
123
+ config['<root>']['authentication']['default']['credential'].should == [['ham', 'biscuit'], ['biscuit', 'gravy']]
124
+ end
125
+ end
126
+
127
+ describe '#environment' do
128
+ before(:each) { @method = 'environment' }
129
+ it_should_behave_like 'options'
130
+
131
+ it "should be additive" do
132
+ config = TorqueBox.configure do
133
+ environment 'foo' => 'bar'
134
+ environment 'bar' => 'baz'
135
+ end
136
+
137
+ config['<root>']['environment'].should == { 'foo' => 'bar', 'bar' => 'baz' }
138
+ end
139
+ end
140
+
141
+ describe '#ruby' do
142
+ before(:each) { @method = 'ruby' }
143
+ it_should_behave_like 'options'
144
+
145
+ it_should_not_allow_invalid_options { ruby :foo => :bar }
146
+ it_should_allow_valid_options { ruby :version => '1.9', :compile_mode => :jit, :debug => true, :interactive => true }
147
+
148
+ it_should_not_allow_invalid_option_values { ruby :version => '2' }
149
+ it_should_not_allow_invalid_option_values { ruby :compile_mode => :bacon }
150
+ it_should_not_allow_invalid_option_values { ruby :debug => :sure }
151
+ it_should_not_allow_invalid_option_values { ruby :interactive => :sure }
152
+ it_should_allow_valid_option_values { ruby :version => '1.8', :compile_mode => :jit }
153
+ end
154
+
155
+ describe '#web' do
156
+ before(:each) { @method = 'web' }
157
+ it_should_behave_like 'options'
158
+
159
+ it_should_not_allow_invalid_options { web :foo => :bar }
160
+ it_should_allow_valid_options { web :context => '', :host => '', :rackup => '', :static => '' }
161
+ end
162
+
163
+ describe '#pool' do
164
+ before(:each) { @method = 'pool' }
165
+ it_should_behave_like 'a thing with options'
166
+
167
+ it_should_not_allow_invalid_options { pool 'a-name', :foo => :bar }
168
+ it_should_allow_valid_options { pool 'a-name', :type => :shared, :min => '', :max => '' }
169
+
170
+ it_should_not_allow_invalid_option_values { pool 'a-name', :type => :bacon }
171
+ it_should_allow_valid_option_values { pool 'a-name', :type => :shared }
172
+ end
173
+
174
+ %w{ queue topic }.each do |method|
175
+ describe "##{method}" do
176
+ before(:each) { @method = method }
177
+ it_should_behave_like 'a thing with options'
178
+
179
+ it_should_not_allow_invalid_options {send(method, 'a-name', :foo => :bar) }
180
+ it_should_allow_valid_options { send(method, 'a-name', :create => false, :durable => true, :remote_host => '') }
181
+
182
+ it_should_not_allow_invalid_option_values { send(method, 'a-name', :create => :yep) }
183
+ it_should_allow_valid_option_values { send(method, 'a-name', :create => true) }
184
+ end
185
+ end
186
+
187
+ describe "#processor" do
188
+ it "should only be valid inside a queue or topic" do
189
+ lambda {
190
+ TorqueBox.configure { processor 'Foo' }
191
+ }.should raise_error(TorqueBox::Configuration::ConfigurationError)
192
+
193
+ lambda {
194
+ TorqueBox.configure do
195
+ queue 'a-queue' do
196
+ processor 'Foo'
197
+ end
198
+ end
199
+ }.should_not raise_error(TorqueBox::Configuration::ConfigurationError)
200
+
201
+ lambda {
202
+ TorqueBox.configure do
203
+ topic 'a-topic' do
204
+ processor 'Foo'
205
+ end
206
+ end
207
+ }.should_not raise_error(TorqueBox::Configuration::ConfigurationError)
208
+
209
+ end
210
+
211
+ it "should nest under its parent" do
212
+ config = TorqueBox.configure do
213
+ topic 'a-topic' do
214
+ processor 'Foo'
215
+ end
216
+ end
217
+ config['<root>']['topic']['a-topic']['processor'].should == [['Foo', {}]]
218
+ end
219
+
220
+
221
+ it_should_not_allow_invalid_options do
222
+ topic 'a-topic' do
223
+ processor 'AClass', :foo => :bar
224
+ end
225
+ end
226
+
227
+ it_should_allow_valid_options do
228
+ topic 'a-topic' do
229
+ processor 'AClass', :concurrency => 1, :config => '', :filter => '', :name => '', :singleton=>true
230
+ end
231
+ end
232
+
233
+ end
234
+
235
+ describe "#options_for" do
236
+ before(:each) { @method = 'options_for' }
237
+
238
+ it_should_behave_like 'a thing with options'
239
+
240
+ it_should_not_allow_invalid_options { options_for 'a-name', :foo => :bar }
241
+ it_should_allow_valid_options { options_for 'a-name', :concurrency => 1, :disabled => true }
242
+
243
+ it_should_not_allow_invalid_option_values { options_for 'a-name', :disabled => :bacon }
244
+ it_should_allow_valid_option_values { options_for 'a-name', :disabled => false }
245
+ end
246
+
247
+ describe "#service" do
248
+ before(:each) do
249
+ @method = 'service'
250
+ @discrete = true
251
+ end
252
+
253
+ it_should_behave_like 'a thing with options'
254
+
255
+ it_should_not_allow_invalid_options { service 'AClass', :foo => :bar }
256
+ it_should_allow_valid_options { service 'AClass', :config => '', :singleton => true, :name => '' }
257
+
258
+ it_should_not_allow_invalid_option_values { service 'AClass', :singleton => :bacon }
259
+ it_should_allow_valid_option_values { service 'AClass', :singleton => false }
260
+
261
+ it "should allow multiple services using the same class" do
262
+ config = TorqueBox.configure do
263
+ service 'AService'
264
+ service 'AService'
265
+ end
266
+
267
+ config['<root>']['service'].should == [['AService', { }], ['AService', { }]]
268
+ end
269
+ end
270
+
271
+ describe '#stomp' do
272
+ before(:each) { @method = 'stomp' }
273
+ it_should_behave_like 'options'
274
+
275
+ it_should_not_allow_invalid_options { stomp :foo => :bar }
276
+ it_should_allow_valid_options { stomp :host => 'example.com' }
277
+ end
278
+
279
+
280
+ describe "#stomplet" do
281
+ before(:each) do
282
+ @method = 'stomplet'
283
+ @discrete = true
284
+ end
285
+
286
+ it_should_behave_like 'a thing with options'
287
+
288
+ it_should_not_allow_invalid_options { stomplet 'AClass', :route => '/foo', :foo => :bar }
289
+ it_should_allow_valid_options { stomplet 'AClass', :config => '', :route => '/foo', :name => '' }
290
+
291
+ it "should allow multiple stomplets using the same class" do
292
+ config = TorqueBox.configure do
293
+ stomplet 'AStomplet', :route => '/x'
294
+ stomplet 'AStomplet', :route => '/y'
295
+ end
296
+
297
+ config['<root>']['stomplet'].should == [['AStomplet', {:route => '/x' }], ['AStomplet', { :route => '/y' }]]
298
+ end
299
+ end
300
+
301
+ describe "#job" do
302
+ before(:each) do
303
+ @method = 'job'
304
+ @discrete = true
305
+ end
306
+
307
+ it_should_behave_like 'a thing with options'
308
+
309
+ it_should_not_allow_invalid_options { job 'AClass', :foo => :bar }
310
+ it_should_allow_valid_options { job 'AClass', :config => '', :singleton => true, :name => '' , :cron => '234'}
311
+
312
+ it_should_not_allow_invalid_option_values { job 'AClass', :singleton => :bacon }
313
+ it_should_allow_valid_option_values { job 'AClass', :singleton => false, :cron => '234' }
314
+
315
+ it "should allow multiple jobs using the same class" do
316
+ config = TorqueBox.configure do
317
+ job 'AJob', :cron => '1234'
318
+ job 'AJob', :cron => '1234'
319
+ end
320
+
321
+ config['<root>']['job'].should == [['AJob', { :cron => '1234' }], ['AJob', { :cron => '1234' }]]
322
+ end
323
+
324
+ it "should allow cron to be set in a block" do
325
+ lambda {
326
+ TorqueBox.configure do
327
+ job 'AJob' do
328
+ cron '123'
329
+ end
330
+ end
331
+ }.should_not raise_error(TorqueBox::Configuration::ConfigurationError)
332
+ end
333
+
334
+ it "should raise if cron doesn't get set in a block" do
335
+ lambda {
336
+ TorqueBox.configure do
337
+ job 'AJob' do
338
+ end
339
+ end
340
+ }.should raise_error(TorqueBox::Configuration::ConfigurationError)
341
+ end
342
+
343
+ end
344
+
345
+ describe "#to_metadata_hash" do
346
+ before(:each) do
347
+ @config = GlobalConfiguration.new
348
+ @config['<root>'] = {
349
+ 'authentication' => {
350
+ 'ham' => {
351
+ :domain => :gravy,
352
+ 'credential' => [['ham', 'biscuit'], ['biscuit', 'gravy']]
353
+ }
354
+ },
355
+ 'environment' => { :ham => 'biscuit' },
356
+ 'job' => [ [ FakeConstant.new( 'AJob' ), {
357
+ :cron => 'cronspec',
358
+ :name => 'a-job',
359
+ :config => { :foo => :bar }
360
+ } ],
361
+ [ FakeConstant.new( 'AnotherJob' ), {
362
+ :cron => 'cronspec',
363
+ :config => { :foo => :bar }
364
+ } ],
365
+ [ FakeConstant.new( 'AnotherJob' ), {
366
+ :cron => 'cronspec',
367
+ :config => { :foo => :bar }
368
+ } ] ],
369
+ 'options_for' => {
370
+ FakeConstant.new( 'Backgroundable' ) => { :concurrency => 42 },
371
+ 'messaging' => { :default_message_encoding => :biscuit }
372
+ },
373
+ 'pool' => {
374
+ 'web' => { :type => :shared },
375
+ 'foo' => { :type => :bounded, :min => 1, :max => 2 } },
376
+ 'queue' => {
377
+ 'a-queue' => {
378
+ :create => false,
379
+ 'processor' => [ [ FakeConstant.new( 'AProcessor' ),
380
+ {
381
+ :name => 'a-proc',
382
+ :config => { :foo => :bar } } ] ]
383
+ },
384
+ 'another-queue' => {},
385
+ },
386
+ 'ruby' => { :version => '1.9' },
387
+ 'service' => [ [ FakeConstant.new( 'AService' ), {
388
+ :name => 'a-service',
389
+ :config => { :foo => :bar } } ],
390
+ [ FakeConstant.new( 'AnotherService' ), {
391
+ :config => { :foo => :bar } } ] ],
392
+ 'stomp' => { :host => 'hambiscuit.com' },
393
+ 'stomplet' => [ [ FakeConstant.new( 'AStomplet' ), {
394
+ :route => '/a',
395
+ :name => 'a-stomplet',
396
+ :config => { :foo => :bar } } ],
397
+ [ FakeConstant.new( 'AnotherStomplet' ), {
398
+ :route => '/b',
399
+ :config => { :foo => :bar } } ] ],
400
+ 'topic' => { 'a-topic' => { :durable => true } },
401
+ 'web' => { :context => '/bacon' }
402
+ }
403
+
404
+ @metadata = @config.to_metadata_hash
405
+ end
406
+
407
+ it "should properly setup authentication" do
408
+ @metadata['auth']['ham'].should == { 'domain' => :gravy, 'credentials' => { 'ham' => 'biscuit', 'biscuit' => 'gravy' } }
409
+ end
410
+
411
+ it "should properly set the environment" do
412
+ @metadata['environment'].should == { 'ham' => 'biscuit' }
413
+ end
414
+
415
+ it "should properly set a job" do
416
+ job = @metadata['jobs']['a-job']
417
+ job.should_not be_nil
418
+ job['job'].should == 'AJob'
419
+ job['cron'].should == 'cronspec'
420
+ end
421
+
422
+ it "should properly handle jobs for the same class without names" do
423
+ @metadata['jobs']['AnotherJob'].should_not be_nil
424
+ @metadata['jobs']['AnotherJob-1'].should_not be_nil
425
+ end
426
+
427
+
428
+ it "should properly set messaging options from options_for" do
429
+ @metadata['messaging']['default_message_encoding'].should == 'biscuit'
430
+ end
431
+
432
+ it "should properly set task options from options_for" do
433
+ @metadata['tasks']['Backgroundable']['concurrency'].should == 42
434
+ end
435
+
436
+ it "should properly set pooling" do
437
+ @metadata['pooling']['web'].should == 'shared'
438
+ foo = @metadata['pooling']['foo']
439
+ foo.should_not be_nil
440
+ foo['min'].should == 1
441
+ foo['max'].should == 2
442
+ end
443
+
444
+ it "should properly set a processor" do
445
+ @metadata['messaging']['a-queue']['AProcessor'].should == { "name" => 'a-proc', "config" => { 'foo' => :bar } }
446
+ end
447
+
448
+ it "should properly set a queue" do
449
+ @metadata['queues']['another-queue'].should == { }
450
+ end
451
+
452
+ it "should not set a queue marked as create => false" do
453
+ @metadata['queues']['a-queue'].should be_nil
454
+ end
455
+
456
+ it "should properly set ruby runtime options" do
457
+ @metadata['ruby']['version'].should == '1.9'
458
+ end
459
+
460
+ it "should properly set stomp options" do
461
+ @metadata['stomp']['host'].should == 'hambiscuit.com'
462
+ end
463
+
464
+ it "should properly set a service" do
465
+ @metadata['services']['a-service'].should == { 'service' => 'AService', 'config' => { 'foo' => :bar } }
466
+ end
467
+
468
+ it "should properly set a stomplet" do
469
+ @metadata['stomp']['stomplets']['a-stomplet'].should == { 'class' => 'AStomplet', 'route' => '/a', 'config' => { 'foo' => :bar } }
470
+ end
471
+
472
+ it "should properly set a topic" do
473
+ @metadata['topics']['a-topic'].should == { 'durable' => true }
474
+ end
475
+
476
+ it "should properly set web" do
477
+ @metadata['web']['context'].should == '/bacon'
478
+ end
479
+ end
480
+ end
481
+
482
+
@@ -0,0 +1,39 @@
1
+ module OptionsMacros
2
+ def it_should_allow_valid_options(&block)
3
+ it "should allow valid options" do
4
+ lambda {
5
+ TorqueBox.configure &block
6
+ }.should_not raise_error(TorqueBox::Configuration::ConfigurationError)
7
+ end
8
+ end
9
+
10
+ def it_should_not_allow_invalid_options(&block)
11
+ it "should not allow invalid options" do
12
+ lambda {
13
+ TorqueBox.configure &block
14
+ }.should raise_error(TorqueBox::Configuration::ConfigurationError)
15
+ end
16
+ end
17
+
18
+ def it_should_allow_valid_option_values(&block)
19
+ it "should allow valid option values" do
20
+ lambda {
21
+ TorqueBox.configure &block
22
+ }.should_not raise_error(TorqueBox::Configuration::ConfigurationError)
23
+ end
24
+ end
25
+
26
+ def it_should_not_allow_invalid_option_values(&block)
27
+ it "should not allow valid option values" do
28
+ lambda {
29
+ TorqueBox.configure &block
30
+ }.should raise_error(TorqueBox::Configuration::ConfigurationError)
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ RSpec.configure do |config|
37
+ config.extend(OptionsMacros)
38
+ end
39
+
@@ -0,0 +1,36 @@
1
+ require 'torquebox/configuration/validator'
2
+
3
+ include TorqueBox::Configuration
4
+
5
+ describe TorqueBox::Configuration::Validator do
6
+
7
+ context "required options" do
8
+ it "should be valid if all of the required options are there" do
9
+ Validator.new({ :required => [:foo, :bar]}, 'name', {:foo => :x, :bar => :y}).should be_valid
10
+ end
11
+
12
+ it "should not be valid if not all of the required options are there" do
13
+ Validator.new({ :required => [:foo, :bar]}, 'name', {:foo => :x}).should_not be_valid
14
+ end
15
+ end
16
+
17
+ context "allowed options" do
18
+ it "should be valid if no non-allowed options are there" do
19
+ Validator.new({ :optional => [:foo, :bar]}, 'name', {:foo => :x}).should be_valid
20
+ end
21
+
22
+ it "should not be valid if any non-allowed options are there" do
23
+ Validator.new({ :optional => [:foo, :bar]}, 'name', {:cheese => :x}).should_not be_valid
24
+ end
25
+ end
26
+
27
+ context "allowed values" do
28
+ it "should be valid if an allowed value is used" do
29
+ Validator.new({ :optional => [{ :foo => ['gouda', 'havarti']}, :bar]}, 'name', {:foo => 'gouda'}).should be_valid
30
+ end
31
+
32
+ it "should be invalid if an non-allowed value is used" do
33
+ Validator.new({ :optional => [{ :foo => ['gouda', 'havarti']}, :bar]}, 'name', {:foo => 'swiss'}).should_not be_valid
34
+ end
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: torquebox-configure
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: 6
5
+ version: 2.0.0.beta1
6
+ platform: java
7
+ authors:
8
+ - The TorqueBox Team
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-12-02 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: blankslate
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.1.2.4
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - "="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.7.0
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: ""
38
+ email:
39
+ - torquebox-dev@torquebox.org
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - licenses/lgpl-2.1.txt
48
+ - lib/torquebox-configure.jar
49
+ - lib/torquebox-configure.rb
50
+ - lib/gem_hook.rb
51
+ - lib/torquebox/configuration.rb
52
+ - lib/torquebox/configure.rb
53
+ - lib/torquebox/configuration/global.rb
54
+ - lib/torquebox/configuration/validator.rb
55
+ - spec/global_spec.rb
56
+ - spec/options_macros.rb
57
+ - spec/validator_spec.rb
58
+ homepage: http://www.torquebox.org/torquebox-gems-parent/torquebox-configure/
59
+ licenses:
60
+ - lgpl
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">"
76
+ - !ruby/object:Gem::Version
77
+ version: 1.3.1
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.9
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: TorqueBox Configure Gem
85
+ test_files:
86
+ - spec/global_spec.rb
87
+ - spec/validator_spec.rb