java_testing_guff 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-01-29
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Mike Hogan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,27 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ config/hoe.rb
7
+ config/requirements.rb
8
+ lib/java_testing_guff.rb
9
+ lib/java_testing_guff-0.0.1.jar
10
+ lib/java_testing_guff/guff_extensions.rb
11
+ lib/java_testing_guff/qdox_extensions.rb
12
+ lib/java_testing_guff/version.rb
13
+ log/debug.log
14
+ script/destroy
15
+ script/generate
16
+ script/txt2html
17
+ setup.rb
18
+ tasks/deployment.rake
19
+ tasks/environment.rake
20
+ tasks/website.rake
21
+ test/test_helper.rb
22
+ test/test_java_testing_guff.rb
23
+ website/index.html
24
+ website/index.txt
25
+ website/javascripts/rounded_corners_lite.inc.js
26
+ website/stylesheets/screen.css
27
+ website/template.rhtml
data/README.txt ADDED
@@ -0,0 +1 @@
1
+ README
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/config/hoe.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'java_testing_guff/version'
2
+
3
+ AUTHOR = 'Mike Hogan' # can also be an array of Authors
4
+ EMAIL = "me@mikehogan.net"
5
+ DESCRIPTION = "Generates java source code that provides fluent interfaces to configure mock objects in particular states, or real objects in particular states. Intent is to make writing tests easier. Requires JRuby and QDox. "
6
+ GEM_NAME = 'java_testing_guff' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'jbuilder-guff' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "m081072"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+
31
+ REV = nil
32
+ # UNCOMMENT IF REQUIRED:
33
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34
+ VERS = JavaTestingGuff::VERSION::STRING + (REV ? ".#{REV}" : "")
35
+ RDOC_OPTS = ['--quiet', '--title', 'java_testing_guff documentation',
36
+ "--opname", "index.html",
37
+ "--line-numbers",
38
+ "--main", "README",
39
+ "--inline-source"]
40
+
41
+ class Hoe
42
+ def extra_deps
43
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44
+ @extra_deps
45
+ end
46
+ end
47
+
48
+ # Generate all the Rake tasks
49
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
50
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
51
+ p.author = AUTHOR
52
+ p.description = DESCRIPTION
53
+ p.email = EMAIL
54
+ p.summary = DESCRIPTION
55
+ p.url = HOMEPATH
56
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
57
+ p.test_globs = ["test/**/test_*.rb"]
58
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
59
+
60
+ # == Optional
61
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
62
+ p.extra_deps = [ ['guff','>= 0.0.3'] ] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
63
+
64
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
65
+
66
+ end
67
+
68
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
69
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
70
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
71
+ hoe.rsync_args = '-av --delete --ignore-errors'
72
+
73
+
@@ -0,0 +1,18 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
16
+
17
+ require 'java_testing_guff'
18
+
Binary file
@@ -0,0 +1,169 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'java_testing_guff/qdox_extensions'
3
+ require 'java_testing_guff/guff_extensions'
4
+
5
+ module JavaTestingGuff
6
+ class GuffGenerator
7
+ def initialize(javadocBuilder)
8
+ @javadocBuilder = javadocBuilder
9
+ end
10
+
11
+ def save_in(path)
12
+ @save_path=path
13
+ self
14
+ end
15
+
16
+ def use_package(p)
17
+ @packageName=p
18
+ self
19
+ end
20
+
21
+ def generate_guff_foreach(type_names)
22
+ type_names.each do |type_name|
23
+ currentClass=@javadocBuilder.getClassByName(type_name)
24
+ source = Guff::JavaSource::SourceFile.new.package(@packageName)
25
+ generate_specific_guff(source, currentClass)
26
+ source.save_in(@save_path)
27
+ end
28
+ end
29
+
30
+ def generate_selector_foreach(type_names)
31
+ source = Guff::JavaSource::SourceFile.new.package(@packageName)
32
+ source.begin_class(builder_selector_name) {|generated_selector|
33
+ add_specific_selector_stuff(generated_selector)
34
+ type_names.each do |type_name|
35
+ currentClass=@javadocBuilder.getClassByName(type_name)
36
+ generated_selector.add_method(currentClass.selector_method_name).returns(builder_name(currentClass)).body {|body|
37
+ body.line("return new #{builder_name(currentClass)}(#{constructor_arguments_for_builder});")
38
+ }
39
+ end
40
+ }
41
+ source.save_in(@save_path)
42
+ end
43
+
44
+ def add_specific_selector_stuff(generated_selector)
45
+
46
+ end
47
+
48
+ def constructor_arguments_for_builder
49
+ ""
50
+ end
51
+ end
52
+
53
+ class EasyMockGuffGenerator < GuffGenerator
54
+ def builder_name(currentClass)
55
+ "Generated#{currentClass.name}MockBuilder"
56
+ end
57
+
58
+ def builder_selector_name
59
+ "GeneratedMockBuilderSelector"
60
+ end
61
+
62
+ def generate_specific_guff(source, currentClass)
63
+ generated_name = builder_name(currentClass)
64
+ source.import('org.easymock.EasyMock')
65
+ source.begin_class(generated_name) {|generated_class|
66
+ generated_class.genericized_using("<T extends #{generated_name}>")
67
+ generated_class.add_field('mock', currentClass.fullyQualifiedName).final.protected
68
+
69
+ generated_class.add_constructor.takes("creator", "org.rubyforge.javatestingguff.MockCreator").body {|body|
70
+ body.line("mock = creator.create(#{currentClass.fully_qualified_name}.class);")
71
+ }
72
+
73
+ generated_class.add_method("build").returns(currentClass.fully_qualified_name).body {|body|
74
+ body.line('return mock;')
75
+ }
76
+
77
+ generated_class.add_method("apply").returns('T').takes("stuff", "org.rubyforge.javatestingguff.Command").body {|body|
78
+ body.line("stuff.execute(this);")
79
+ body.line("return (T) this;")
80
+ }
81
+ currentClass.with_self_and_supers {|clzz|
82
+ clzz.add_easy_mock_methods_to(generated_class)
83
+ }
84
+ }
85
+ end
86
+ end
87
+
88
+ class JMockGuffGenerator < GuffGenerator
89
+ def builder_name(currentClass)
90
+ "Generated#{currentClass.name}JMockBuilder"
91
+ end
92
+
93
+ def builder_selector_name
94
+ "GeneratedJMockBuilderSelector"
95
+ end
96
+
97
+ def constructor_arguments_for_builder
98
+ "testcase"
99
+ end
100
+
101
+ def add_specific_selector_stuff(generated_selector)
102
+ generated_selector.add_field("testcase","org.jmock.cglib.MockObjectTestCase").private.final
103
+ generated_selector.add_constructor.for_field("testcase")
104
+ end
105
+
106
+ def generate_specific_guff(source, currentClass)
107
+ generated_name = builder_name(currentClass)
108
+ source.begin_class(generated_name) {|generated_class|
109
+ generated_class.extends('org.rubyforge.javatestingguff.AbstractJMockObjectBuilder')
110
+ generated_class.genericized_using("<T extends #{generated_name}>")
111
+
112
+
113
+ generated_class.add_constructor.takes("testCase", "org.jmock.cglib.MockObjectTestCase").body {|body|
114
+ body.line("super(testCase,#{currentClass.fully_qualified_name}.class);")
115
+ }
116
+
117
+ generated_class.add_method("build").returns(currentClass.fully_qualified_name).body {|body|
118
+ body.line("return (#{currentClass.fully_qualified_name}) mock.proxy();")
119
+ }
120
+
121
+ generated_class.add_method("apply").returns('T').takes("stuff", "org.rubyforge.javatestingguff.Command").body {|body|
122
+ body.line("stuff.execute(this);")
123
+ body.line("return (T) this;")
124
+ }
125
+ currentClass.with_self_and_supers {|clzz|
126
+ clzz.add_jmock_methods_to(generated_class)
127
+ }
128
+ }
129
+ end
130
+ end
131
+
132
+
133
+ class RealBuilderGuffGenerator < GuffGenerator
134
+ def builder_name(currentClass)
135
+ "Generated#{currentClass.name}Builder"
136
+ end
137
+
138
+ def builder_selector_name
139
+ "GeneratedBuilderSelector"
140
+ end
141
+
142
+ def generate_specific_guff(source, currentClass)
143
+ generated_name = builder_name(currentClass)
144
+ source.begin_class(generated_name) {|generated_class|
145
+ generated_class.genericized_using("<T extends #{generated_name}>")
146
+
147
+ generated_class.add_method("build").returns(currentClass.fully_qualified_name).body {|body|
148
+ variable_name = currentClass.add_simplest_instantiation_line_to(body);
149
+ currentClass.with_self_and_supers {|self_or_super|
150
+ self_or_super.add_reflective_field_assignments_to(variable_name, body)
151
+ }
152
+ body.line("return #{variable_name};")
153
+ }
154
+
155
+ currentClass.with_self_and_supers {|self_or_super|
156
+ self_or_super.add_field_definitions_to(generated_class)
157
+ }
158
+
159
+ currentClass.with_self_and_supers {|self_or_super|
160
+ self_or_super.add_setter_method_definitions_to('with', generated_class)
161
+ }
162
+ generated_class.add_method("apply").returns('T').takes("stuff", "org.rubyforge.javatestingguff.Command").body {|body|
163
+ body.line("stuff.execute(this);")
164
+ body.line("return (T) this;")
165
+ }
166
+ }
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,20 @@
1
+ module VisitMemory
2
+ def mark_visited(method)
3
+ methods_visited << method.my_declaration_signature
4
+ end
5
+
6
+ def already_visited?(method)
7
+ methods_visited.include?(method.my_declaration_signature)
8
+ end
9
+ def methods_visited
10
+ @methods_visited ||= []
11
+ end
12
+ end
13
+
14
+ module Guff
15
+ module JavaSource
16
+ class Class
17
+ include VisitMemory
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,477 @@
1
+ require 'java'
2
+ require 'rubygems'
3
+ require 'guff'
4
+ require 'active_support'
5
+ require 'forwardable'
6
+ include_class 'com.thoughtworks.qdox.JavaDocBuilder'
7
+ include_class 'com.thoughtworks.qdox.model.JavaMethod'
8
+ include_class 'com.thoughtworks.qdox.model.JavaParameter'
9
+ include_class 'com.thoughtworks.qdox.model.JavaClass'
10
+ include_class 'com.thoughtworks.qdox.model.JavaField'
11
+ include_class 'com.thoughtworks.qdox.model.Type'
12
+ include_class('java.io.File') {|package, name| "J#{name}" }
13
+
14
+ class JavaField
15
+ def add_definition_to(generated_class)
16
+ generated_class.add_field(name, type.fully_qualified_name).protected
17
+ end
18
+
19
+ def add_setter_method_definitions_to(method_name_prefix, generated_class)
20
+ method_builder = generated_class.add_method("#{method_name_prefix}#{name.camelize}").returns('T').takes(name, type.fully_qualified_name)
21
+ method_builder.body {|body|
22
+ body.line("this.#{name} = #{name};")
23
+ body.line('return (T) this;')
24
+ }
25
+ end
26
+
27
+ def add_reflective_assignments_to(variable_name, body)
28
+ body.line("org.rubyforge.javatestingguff.Reflect.setField(#{variable_name},\"#{name}\",#{name});")
29
+ end
30
+ end
31
+
32
+ class JavaClass
33
+ def self.camelize_lower(s)
34
+ s.first.downcase + s.camelize()[1..-1]
35
+ end
36
+
37
+ def selector_method_name
38
+ JavaClass::camelize_lower(name)
39
+ end
40
+
41
+ def with_self_and_supers(&proc)
42
+ proc.call(self)
43
+ if (interface?)
44
+ implemented_interfaces.each do |interface|
45
+ interface.with_self_and_supers &proc
46
+ end
47
+ elsif (super_class.fully_qualified_name != "java.lang.Object")
48
+ super_java_class.with_self_and_supers &proc
49
+ end
50
+ end
51
+
52
+ def add_easy_mock_methods_to(generated_class)
53
+ mockable_methods.each do |method|
54
+ if (!generated_class.already_visited?(method))
55
+ method.add_easy_mock_builder_to(generated_class)
56
+ generated_class.mark_visited(method)
57
+ end
58
+ end
59
+ end
60
+
61
+ def add_jmock_methods_to(generated_class)
62
+ mockable_methods.each do |method|
63
+ if (!generated_class.already_visited?(method))
64
+ method.add_jmock_builder_to(generated_class)
65
+ generated_class.mark_visited(method)
66
+ end
67
+ end
68
+ end
69
+
70
+ def mockable_methods
71
+ methods.select {|m|
72
+ m.public? && !m.static? && !m.constructor?
73
+ }
74
+ end
75
+
76
+ def add_field_definitions_to(generated_class)
77
+ instance_fields.each do |field|
78
+ field.add_definition_to(generated_class)
79
+ end
80
+ end
81
+
82
+ def instance_fields
83
+ fields.select {|field|
84
+ !field.static
85
+ }
86
+ end
87
+
88
+ def add_setter_method_definitions_to(method_name_prefix, generated_class)
89
+ instance_fields.each do |field|
90
+ field.add_setter_method_definitions_to(method_name_prefix, generated_class)
91
+ end
92
+ end
93
+
94
+ def add_simplest_instantiation_line_to(body)
95
+ variable_name=JavaClass.camelize_lower(name)
96
+ body.line("#{fully_qualified_name} #{variable_name} = #{smallest_constructor_call};")
97
+ variable_name
98
+ end
99
+
100
+ def add_reflective_field_assignments_to(variable_name, body)
101
+ instance_fields.each do |field|
102
+ field.add_reflective_assignments_to(variable_name, body)
103
+ end
104
+ end
105
+
106
+ def constructors
107
+ methods.select {|method|
108
+ method.constructor? && method.public?
109
+ }
110
+ end
111
+
112
+ def smallest_constructor_call
113
+ sorted_constructors=constructors.sort {|a, b|
114
+ a.parameters.length <=> b.parameters.length
115
+ }
116
+ args=[]
117
+ if (!sorted_constructors.empty?)
118
+ smallest_constructor=sorted_constructors.first
119
+ smallest_constructor.parameters.each do |parameter|
120
+ args << parameter.any_old_permitted_value
121
+ end
122
+ end
123
+ "new #{fully_qualified_name}(#{args.join(',')})"
124
+ end
125
+ end
126
+
127
+ class Type
128
+ def boolean?
129
+ one_of?("boolean", 'java.lang.boolean')
130
+ end
131
+
132
+ def one_of?(*possibles)
133
+ possibles.include?(to_string.downcase)
134
+ end
135
+
136
+ def whole_number?
137
+ one_of?("long", 'java.lang.long', 'int', 'java.lang.integer')
138
+ end
139
+
140
+ def partial_number?
141
+ one_of?("float", 'java.lang.float', 'double', 'java.lang.double')
142
+ end
143
+
144
+ def fully_qualified_name
145
+ result = java_class.fully_qualified_name.gsub("$", ".")
146
+ if (result.size == 1 && result == result.upcase)
147
+ result = "Object"
148
+ end
149
+ result
150
+ end
151
+
152
+ def fully_qualified_type
153
+ fully_qualified_name
154
+ end
155
+
156
+ def add_takes_clause_to(method_builder)
157
+ method_builder.takes(name, fully_qualified_name)
158
+ end
159
+
160
+ def name
161
+ "result"
162
+ end
163
+
164
+ def add_throws_clause_to(method_builder)
165
+ method_builder.throws(fully_qualified_name)
166
+ end
167
+
168
+ def any_old_permitted_value
169
+ if (boolean?)
170
+ return "true"
171
+ elsif (whole_number?)
172
+ return "0"
173
+ elsif (partial_number?)
174
+ return "0.0"
175
+ else
176
+ return "null"
177
+ end
178
+ end
179
+ end
180
+
181
+ class JavaParameter
182
+ def add_takes_clause_to(method_builder)
183
+ method_builder.takes(name, fully_qualified_type)
184
+ end
185
+
186
+ def fully_qualified_type
187
+ type.fully_qualified_name
188
+ end
189
+
190
+ def any_old_permitted_value
191
+ type.any_old_permitted_value
192
+ end
193
+ end
194
+
195
+ class JavaMethod
196
+ def my_declaration_signature
197
+ "#{returns.fully_qualified_name} #{name}(#{parameter_types}) #{throws_clause}"
198
+ end
199
+
200
+ def parameter_types
201
+ types = parameters.collect do |p|
202
+ p.type.fully_qualified_name
203
+ end
204
+ types.join(",")
205
+ end
206
+
207
+ def throws_clause
208
+ types=exceptions.collect do |e|
209
+ e.fully_qualified_name
210
+ end
211
+ types.join(",")
212
+ end
213
+
214
+ def add_easy_mock_builder_to(generated_class)
215
+ EasyMockStubMethodWriter.new(self).add_stub_methods_to(generated_class)
216
+ add_expectation_methods_to(generated_class) {|body|
217
+ if (returns_something?)
218
+ body.line("#{easyMock}.expect(mock.#{name}(#{parameter_list})).andReturn(result);")
219
+ else
220
+ body.line("mock.#{name}(#{parameter_list});")
221
+ end
222
+ }
223
+ end
224
+
225
+ def add_jmock_builder_to(generated_class)
226
+ JMockStubMethodWriter.new(self).add_stub_methods_to(generated_class)
227
+ add_expectation_methods_to(generated_class) {|body|
228
+ if (returns_something?)
229
+ body.line("expects(once()).method(\"#{name}\").#{jmock_with_clause}.will(returnValue(result));")
230
+ else
231
+ body.line("expects(once()).method(\"#{name}\").#{jmock_with_clause};")
232
+ end
233
+ }
234
+ end
235
+
236
+ def jmock_with_clause
237
+ if (parameters.length == 0)
238
+ return "withNoArguments()";
239
+ else
240
+ clause=[]
241
+ with_parameters do |p|
242
+ clause << "eq(#{p.name})"
243
+ end
244
+ if (parameters.length > 4)
245
+ return "with(new org.jmock.core.Constraint[]{#{clause.join(',')}})"
246
+ else
247
+ return "with(#{clause.join(',')})"
248
+ end
249
+ end
250
+ end
251
+
252
+ def add_expectation_methods_to(generated_class)
253
+ if (!getter?)
254
+ method_builder = generated_class.add_method("expecting#{name.camelize}").returns('T')
255
+ with_parameters_and_return do |p|
256
+ p.add_takes_clause_to(method_builder)
257
+ end
258
+
259
+ with_exceptions do |e|
260
+ e.add_throws_clause_to(method_builder)
261
+ end
262
+
263
+ method_builder.body {|body|
264
+ yield body
265
+ body.line('return (T) this;')
266
+ }
267
+ end
268
+
269
+ end
270
+
271
+ def with_exceptions
272
+ exceptions.each do |e|
273
+ yield e
274
+ end
275
+ end
276
+
277
+ class StubMethodWriter
278
+ extend Forwardable
279
+
280
+ def_delegator :underlying_method, :setter?
281
+ def_delegator :underlying_method, :getter?
282
+ def_delegator :underlying_method, :name
283
+ def_delegator :underlying_method, :returns
284
+ def_delegator :underlying_method, :with_parameters_and_return
285
+ def_delegator :underlying_method, :with_exceptions
286
+ def_delegator :underlying_method, :returns_something?
287
+ def_delegator :underlying_method, :parameter_list
288
+ def_delegator :underlying_method, :jmock_with_clause
289
+ def_delegator :underlying_method, :easyMock
290
+
291
+ def initialize(underlying_method)
292
+ @underlying_method=underlying_method
293
+ end
294
+
295
+ def underlying_method
296
+ @underlying_method
297
+ end
298
+
299
+ def add_stub_methods_to(generated_clazz)
300
+ if (setter?)
301
+ return
302
+ end
303
+
304
+ if (getter?)
305
+ add_getter_stub_to(generated_clazz)
306
+ else
307
+ method_builder = generated_clazz.add_method("with#{name.camelize}").returns('T')
308
+ with_parameters_and_return do |p|
309
+ p.add_takes_clause_to(method_builder)
310
+ end
311
+ with_exceptions do |e|
312
+ e.add_throws_clause_to(method_builder)
313
+ end
314
+ add_nongetter_stub_to(method_builder)
315
+ end
316
+ end
317
+
318
+ def add_getter_stub_to(generated_clazz)
319
+ gotten_field_name = JavaClass.camelize_lower(name.sub(/^get/, "").sub(/^has/, "").sub(/^is/, ""))
320
+ if (returns.boolean?)
321
+ generated_method = generated_clazz.add_method("#{JavaClass.camelize_lower(gotten_field_name)}").returns('T')
322
+ append_throws_clause_if_necessary(generated_method)
323
+ generated_method.body {|body|
324
+ add_boolean_getter_stub_positive_case_to(body)
325
+ body.line('return (T) this;')
326
+ }
327
+ generated_method = generated_clazz.add_method("not#{gotten_field_name.camelize}").returns('T')
328
+ append_throws_clause_if_necessary(generated_method)
329
+ generated_method.body {|body|
330
+ add_boolean_getter_stub_negative_case_to(body)
331
+ body.line('return (T) this;')
332
+ }
333
+ else
334
+ method_builder = generated_clazz.add_method("with#{gotten_field_name.camelize}").returns('T').takes(gotten_field_name, returns.fully_qualified_name)
335
+ append_throws_clause_if_necessary(method_builder)
336
+ method_builder.body {|body|
337
+ add_nonboolean_getter_stub_to(body, gotten_field_name)
338
+ body.line('return (T) this;')
339
+ }
340
+ end
341
+ end
342
+
343
+ def append_throws_clause_if_necessary(generated_method)
344
+
345
+ end
346
+
347
+ end
348
+
349
+ class EasyMockStubMethodWriter < StubMethodWriter
350
+ def append_throws_clause_if_necessary(generated_method)
351
+ with_exceptions do |e|
352
+ e.add_throws_clause_to(generated_method)
353
+ end
354
+ end
355
+
356
+ def add_boolean_getter_stub_positive_case_to(body)
357
+ body.line("#{easyMock}.expect(mock.#{name}()).andStubReturn(true);")
358
+ end
359
+
360
+ def add_boolean_getter_stub_negative_case_to(body)
361
+ body.line("#{easyMock}.expect(mock.#{name}()).andStubReturn(false);")
362
+ end
363
+
364
+ def add_nonboolean_getter_stub_to(body, gotten_field_name)
365
+ body.line("#{easyMock}.expect(mock.#{name}()).andStubReturn(#{gotten_field_name});")
366
+ end
367
+
368
+ def add_nongetter_stub_to(method_builder)
369
+ method_builder.body {|body|
370
+ if (returns_something?)
371
+ body.line("#{easyMock}.expect(mock.#{name}(#{parameter_list})).andStubReturn(result);")
372
+ else
373
+ body.line("mock.#{name}(#{parameter_list});")
374
+ body.line("#{easyMock}.expectLastCall().asStub();")
375
+ end
376
+ body.line('return (T) this;')
377
+ }
378
+
379
+ end
380
+ end
381
+
382
+ class JMockStubMethodWriter < StubMethodWriter
383
+ def add_boolean_getter_stub_positive_case_to(body)
384
+ body.line("stubs().method(\"#{name}\").will(returnValue(true));")
385
+ end
386
+
387
+ def add_boolean_getter_stub_negative_case_to(body)
388
+ body.line("stubs().method(\"#{name}\").will(returnValue(false));")
389
+ end
390
+
391
+ def add_nonboolean_getter_stub_to(body, gotten_field_name)
392
+ body.line("stubs().method(\"#{name}\").will(returnValue(#{gotten_field_name}));")
393
+ end
394
+
395
+ def add_nongetter_stub_to(method_builder)
396
+ method_builder.body {|body|
397
+ if (returns_something?)
398
+ body.line("stubs().method(\"#{name}\").#{jmock_with_clause}.will(returnValue(result));")
399
+ else
400
+ body.line("stubs().method(\"#{name}\").#{jmock_with_clause};")
401
+ end
402
+ body.line('return (T) this;')
403
+ }
404
+ end
405
+ end
406
+
407
+ def add_easymock_stub_methods_to(generated_clazz)
408
+ if (setter?)
409
+ return
410
+ end
411
+
412
+ if (getter?)
413
+ add_easymock_getter_stub_to(generated_clazz)
414
+ else
415
+ method_builder = generated_clazz.add_method("with#{name.camelize}").returns('T')
416
+ with_parameters_and_return do |p|
417
+ p.add_takes_clause_to(method_builder)
418
+ end
419
+ with_exceptions do |e|
420
+ e.add_throws_clause_to(method_builder)
421
+ end
422
+
423
+ method_builder.body {|body|
424
+ if (returns_something?)
425
+ body.line("#{easyMock}.expect(mock.#{name}(#{parameter_list})).andStubReturn(result);")
426
+ else
427
+ body.line("mock.#{name}(#{parameter_list});")
428
+ body.line("#{easyMock}.expectLastCall().asStub();")
429
+ end
430
+ body.line('return (T) this;')
431
+ }
432
+ end
433
+ end
434
+
435
+ def parameter_list
436
+ result=[]
437
+ with_parameters do |p|
438
+ result << "#{p.name}"
439
+ end
440
+ result.join(',')
441
+ end
442
+
443
+ def with_parameters
444
+ parameters.each do |p|
445
+ yield p
446
+ end
447
+ end
448
+
449
+ def with_parameters_and_return
450
+ parameters.each do |p|
451
+ yield p
452
+ end
453
+ if (returns_something?)
454
+ yield returns
455
+ end
456
+ end
457
+
458
+ def getter?
459
+ return parameters.length == 0 && returns_something? && (name.starts_with?('get') || name.starts_with?('has') || name.starts_with?('is'))
460
+ end
461
+
462
+ def setter?
463
+ return parameters.length == 1 && returns_nothing? && name.starts_with?('set')
464
+ end
465
+
466
+ def returns_something?
467
+ !returns.void?
468
+ end
469
+
470
+ def returns_nothing?
471
+ returns.void?
472
+ end
473
+
474
+ def easyMock
475
+ 'EasyMock'
476
+ end
477
+ end