march_hare_mock 0.0.1-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 47bc8f4f939e1588df2c6efe1ca1d2fd04714794
4
+ data.tar.gz: c2c7e54b16911fce3eb01c0f00306eed92c6ab3d
5
+ SHA512:
6
+ metadata.gz: 842e0e563a8e87bd978893a94d4f8fa8f851a598353635afd3d9d7a43c459b882066af327a52337374c8f4f39cc832fd148ca2203b1b7b56b638c20fd41bb61a
7
+ data.tar.gz: 9a9369f6daba6b318bf5c4a93c1051a4f753e497e134531724b7e1ee0b561f16d106b622c146a49e77367e400de75fcf8f5268c9c7a2d4a3ccc7ed516feda257
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .rspec
16
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in march_hare_mock.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Chris Blackburn
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # MarchHareMock
2
+
3
+ **Version: 0.0.1**
4
+
5
+ MarchHareMock is a simple mocking library for those using JRuby and the MarchHare gem for RabbitMQ messaging. Inspired by the [BunnyMock](https://github.com/midwire/bunny_mock) gem.
6
+
7
+ MarchHareMock does not cover all of the methods of MarchHare. It currently only mocks the behavior we have required for our immediate needs, which is mainly creating and binding queues and exchanges, and publishing/subscribing messages.
8
+
9
+ Feel free to fork it to add more behavior mocking and send me a pull request, just "Don't Mock the March Hare":
10
+
11
+ **Warning:** This March Hare may mock ***you***!
12
+ ![Don't Mock the March Hare!](http://animals.ekstrax.com/wp-content/uploads/2014/05/Funny-Rabbit-Pictures-25.jpg "Don't Mock the March Hare!")
13
+
14
+ ## Installation
15
+
16
+ Add the following to your application's Gemfile:
17
+
18
+ ```ruby
19
+ group :test do
20
+ gem 'march_hare_mock'
21
+ # ... other gems as necessary
22
+ end
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ bundle install
28
+
29
+ Or install it into your global gemspace as:
30
+
31
+ gem install march_hare_mock
32
+
33
+ ## Usage
34
+
35
+ Since this project is intended as a simple way to help test your collaboration with MarchHare/RabbitMQ, it does not really opereate as a real queue, but it _does_ support receiving messages into a queue, and reading them out. The main thing to be aware of is that the `MarchHareMock::Queue#subscribe` method does not block waiting for messages, consumes all queued messages, and returns when there are no more messages. This differs from the behavior of the real MarchHare.
36
+
37
+ See the first "integration" test case in `spec/lib/march_hare_mock_spec.rb` for a quick example of how to use MarchHareMock.
38
+
39
+ ## Authors
40
+
41
+ * Chris Blackburn - https://midwire.github.io
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/[my-github-username]/march_hare_mock/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
50
+
51
+ # License
52
+
53
+ This code is licensed under [The MIT License](http://opensource.org/licenses/MIT)
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require "midwire_common/rake_tasks"
5
+ rescue StandardError => e
6
+ puts ">>> Could not load 'midwire_common/rake_tasks': #{e}"
7
+ exit
8
+ end
9
+
10
+ require 'rspec/core/rake_task'
11
+ RSpec::Core::RakeTask.new('spec')
12
+ task :default => :spec
@@ -0,0 +1,141 @@
1
+ # require "march_hare_mock/version"
2
+
3
+ module MarchHareMock
4
+
5
+ class MarchHare
6
+ def self.connect(*args)
7
+ Session.connect(*args)
8
+ end
9
+ end
10
+
11
+ class Session
12
+ def self.connect(*args)
13
+ self.new
14
+ end
15
+
16
+ def start
17
+ nil
18
+ end
19
+
20
+ def create_channel
21
+ MarchHareMock::Channel.new
22
+ end
23
+
24
+ def close
25
+ nil
26
+ end
27
+ end
28
+
29
+ class Channel
30
+ def queue(name, *args)
31
+ MarchHareMock::Queue.new(name, *args)
32
+ end
33
+
34
+ def default_exchange
35
+ MarchHareMock::Exchange.new
36
+ end
37
+
38
+ def close
39
+ nil
40
+ end
41
+ end
42
+
43
+ class Queue
44
+ attr_accessor :name, :attrs, :messages, :delivery_count
45
+
46
+ def initialize(name, attrs = {})
47
+ self.name = name
48
+ self.attrs = attrs.dup
49
+ self.messages = []
50
+ self.delivery_count = 0
51
+ end
52
+
53
+ def bind(exchange, *args)
54
+ exchange.queues << self
55
+ end
56
+
57
+ # Note that this doesn't block waiting for messages like the real world.
58
+ def subscribe(*args, &block)
59
+ while message = messages.shift
60
+ self.delivery_count += 1
61
+ yield({:payload => message})
62
+ end
63
+ end
64
+
65
+ def default_consumer
66
+ BunnyMock::Consumer.new(self.delivery_count)
67
+ end
68
+
69
+ # NOTE: This is NOT a method that is supported on real Bunny queues.
70
+ # This is a custom method to get us a deep copy of
71
+ # all the messages currently in the queue. This is provided
72
+ # to aid in testing a system where it is not practical for the
73
+ # test to subscribe to the queue and read the messages, but we
74
+ # need to verify that certain messages have been published.
75
+ def snapshot_messages
76
+ Marshal.load(Marshal.dump(messages))
77
+ end
78
+
79
+ def method_missing(method, *args)
80
+ method_name = method.to_s
81
+ is_predicate = false
82
+ if method_name =~ /^(.*)\?$/
83
+ key = $1.to_sym
84
+ is_predicate = true
85
+ else
86
+ key = method.to_sym
87
+ end
88
+
89
+ if attrs.has_key? key
90
+ value = attrs[key]
91
+ is_predicate ? !!value : value
92
+ else
93
+ super
94
+ end
95
+ end
96
+ end
97
+
98
+ class Exchange
99
+ attr_accessor :name, :attrs, :queues
100
+
101
+ def initialize(name = 'default.exchange', attrs = {})
102
+ self.name = name
103
+ self.attrs = attrs.dup
104
+ self.queues = []
105
+ end
106
+
107
+ def publish(msg, msg_attrs = {})
108
+ queues.each { |q| q.messages << msg }
109
+ end
110
+
111
+ def bound_to?(queue_name)
112
+ queues.any?{|q| q.name == queue_name}
113
+ end
114
+
115
+ def method_missing(method, *args)
116
+ method_name = method.to_s
117
+ is_predicate = false
118
+ if method_name =~ /^(.*)\?$/
119
+ key = $1.to_sym
120
+ is_predicate = true
121
+ else
122
+ key = method.to_sym
123
+ end
124
+
125
+ if attrs.has_key? key
126
+ value = attrs[key]
127
+ is_predicate ? !!value : value
128
+ else
129
+ super
130
+ end
131
+ end
132
+ end
133
+
134
+ class Consumer
135
+ attr_accessor :message_count
136
+ def initialize(c)
137
+ self.message_count = c
138
+ end
139
+ end
140
+
141
+ end
@@ -0,0 +1,3 @@
1
+ module MarchHareMock
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'march_hare_mock/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'march_hare_mock'
8
+ spec.version = MarchHareMock::VERSION
9
+ spec.authors = ['Chris Blackburn']
10
+ spec.email = ['chris@midwiretech.com']
11
+ spec.summary = 'MarchHare/RabbitMQ mock class in JRuby. Useful for
12
+ mocking MarchHare usage in test code.'
13
+ spec.description = spec.summary
14
+ spec.homepage = ''
15
+ spec.license = 'MIT'
16
+ spec.platform = 'java'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'simplecov'
26
+ spec.add_development_dependency 'midwire_common', '~> 0.1.11'
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'rspec', '~> 3.1'
29
+ end
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe MarchHareMock do
4
+
5
+ TEST_STRING = 'bogus'
6
+
7
+ context 'MarchHare' do
8
+ context '#connect' do
9
+ it 'returns a Session' do
10
+ sess = MarchHareMock::MarchHare.connect
11
+ expect(sess).to be_a(MarchHareMock::Session)
12
+ end
13
+ end
14
+ end
15
+
16
+ context 'Session' do
17
+ let(:session) { MarchHareMock::Session.connect }
18
+
19
+ context '#connect' do
20
+ it 'returns a Session instance' do
21
+ expect(session).to be_a(MarchHareMock::Session)
22
+ end
23
+ end
24
+
25
+ context '.create_channel' do
26
+ it 'returns a Channel instance' do
27
+ expect(session.create_channel).to be_a(MarchHareMock::Channel)
28
+ end
29
+ end
30
+
31
+ context 'empty methods' do
32
+ %w(start close).each do |method|
33
+ context ".#{method}" do
34
+ it 'returns nil' do
35
+ expect(session.send(method)).to eq(nil)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ context 'Channel' do
43
+ let(:channel) { MarchHareMock::Channel.new }
44
+
45
+ context '.queue' do
46
+ it 'returns a Queue instance' do
47
+ expect(channel.queue(TEST_STRING)).to be_a(MarchHareMock::Queue)
48
+ end
49
+
50
+ it 'returns a Queue instance with the given name' do
51
+ q = channel.queue(TEST_STRING)
52
+ expect(q.name).to eq(TEST_STRING)
53
+ end
54
+ end
55
+
56
+ context '.default_exchange' do
57
+ it 'returns an Exchange instance' do
58
+ expect(channel.default_exchange).to be_a(MarchHareMock::Exchange)
59
+ end
60
+ end
61
+
62
+ context 'empty methods' do
63
+ %w(close).each do |method|
64
+ context ".#{method}" do
65
+ it 'returns nil' do
66
+ expect(channel.send(method)).to eq(nil)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ context 'Queue' do
74
+ let(:queue) { MarchHareMock::Queue.new(TEST_STRING) }
75
+
76
+ context '.new' do
77
+ it 'returns a Queue instance' do
78
+ expect(queue).to be_a(MarchHareMock::Queue)
79
+ end
80
+
81
+ it 'returns a Queue with the given name' do
82
+ expect(queue.name).to eq(TEST_STRING)
83
+ end
84
+
85
+ it 'returns a Queue with the given attributes' do
86
+ attrs = { asdf: 'asdf' }
87
+ q = MarchHareMock::Queue.new(TEST_STRING, attrs)
88
+ expect(q.attrs).to eq(attrs)
89
+ end
90
+
91
+ it 'initializes messages' do
92
+ expect(queue.messages).to eq([])
93
+ end
94
+
95
+ it 'initializes delivery_count' do
96
+ expect(queue.delivery_count).to eq(0)
97
+ end
98
+ end
99
+
100
+ context '.bind' do
101
+ it 'returns a collection of exchange queues including self' do
102
+ exchange = MarchHareMock::Exchange.new
103
+ queues = queue.bind(exchange)
104
+ expect(queues).to be_a(Array)
105
+ expect(queues).to include(queue)
106
+ end
107
+ end
108
+ end
109
+
110
+ end
@@ -0,0 +1,80 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/_spec.rb$/"
4
+ add_filter "/vendor/"
5
+ end
6
+
7
+ require 'pry'
8
+ require_relative '../lib/march_hare_mock'
9
+
10
+ root = File.expand_path('../', File.dirname(__FILE__))
11
+ Dir[File.join(root, 'spec/support/**/*.rb')].each { |f| require f }
12
+
13
+ RSpec.configure do |config|
14
+ # rspec-expectations config goes here. You can use an alternate
15
+ # assertion/expectation library such as wrong or the stdlib/minitest
16
+ # assertions if you prefer.
17
+ config.expect_with :rspec do |expectations|
18
+ # This option will default to `true` in RSpec 4. It makes the `description`
19
+ # and `failure_message` of custom matchers include text for helper methods
20
+ # defined using `chain`, e.g.:
21
+ # be_bigger_than(2).and_smaller_than(4).description
22
+ # # => "be bigger than 2 and smaller than 4"
23
+ # ...rather than:
24
+ # # => "be bigger than 2"
25
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
26
+ end
27
+
28
+ # rspec-mocks config goes here. You can use an alternate test double
29
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
30
+ config.mock_with :rspec do |mocks|
31
+ # Prevents you from mocking or stubbing a method that does not exist on
32
+ # a real object. This is generally recommended, and will default to
33
+ # `true` in RSpec 4.
34
+ mocks.verify_partial_doubles = true
35
+ end
36
+
37
+ # The settings below are suggested to provide a good initial experience
38
+ # with RSpec, but feel free to customize to your heart's content.
39
+ # These two settings work together to allow you to limit a spec run
40
+ # to individual examples or groups you care about by tagging them with
41
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
42
+ # get run.
43
+ config.filter_run :focus
44
+ config.run_all_when_everything_filtered = true
45
+
46
+ # Limits the available syntax to the non-monkey patched syntax that is
47
+ # recommended.
48
+ # For more details, see:
49
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
50
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
51
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
52
+ config.disable_monkey_patching!
53
+
54
+ # Many RSpec users commonly either run the entire suite or an individual
55
+ # file, and it's useful to allow more verbose output when running an
56
+ # individual spec file.
57
+ if config.files_to_run.one?
58
+ # Use the documentation formatter for detailed output,
59
+ # unless a formatter has already been configured
60
+ # (e.g. via a command-line flag).
61
+ config.default_formatter = 'doc'
62
+ end
63
+
64
+ # Print the 10 slowest examples and example groups at the
65
+ # end of the spec run, to help surface which specs are running
66
+ # particularly slow.
67
+ config.profile_examples = 3
68
+
69
+ # Run specs in random order to surface order dependencies. If you find an
70
+ # order dependency and want to debug it, you can fix the order by providing
71
+ # the seed, which is printed after each run.
72
+ # --seed 1234
73
+ config.order = :random
74
+
75
+ # Seed global randomization in this process using the `--seed` CLI option.
76
+ # Setting this allows you to use `--seed` to deterministically reproduce
77
+ # test failures related to randomization by passing the same `--seed` value
78
+ # as the one that triggered the failure.
79
+ Kernel.srand config.seed
80
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: march_hare_mock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: java
6
+ authors:
7
+ - Chris Blackburn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: '1.7'
25
+ prerelease: false
26
+ type: :development
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '10.0'
39
+ prerelease: false
40
+ type: :development
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ prerelease: false
54
+ type: :development
55
+ - !ruby/object:Gem::Dependency
56
+ name: midwire_common
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.11
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 0.1.11
67
+ prerelease: false
68
+ type: :development
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ prerelease: false
82
+ type: :development
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '3.1'
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: '3.1'
95
+ prerelease: false
96
+ type: :development
97
+ description: MarchHare/RabbitMQ mock class in JRuby. Useful for mocking MarchHare usage in test code.
98
+ email:
99
+ - chris@midwiretech.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/march_hare_mock.rb
110
+ - lib/march_hare_mock/version.rb
111
+ - march_hare_mock.gemspec
112
+ - spec/lib/march_hare_mock_spec.rb
113
+ - spec/spec_helper.rb
114
+ homepage: ''
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.1.9
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: MarchHare/RabbitMQ mock class in JRuby. Useful for mocking MarchHare usage in test code.
138
+ test_files:
139
+ - spec/lib/march_hare_mock_spec.rb
140
+ - spec/spec_helper.rb