Class: JavaMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/java_testing_guff/qdox_extensions.rb

Defined Under Namespace

Classes: EasyMockStubMethodWriter, JMockStubMethodWriter, StubMethodWriter

Instance Method Summary collapse

Instance Method Details

#add_easy_mock_builder_to(generated_class) ⇒ Object



214
215
216
217
218
219
220
221
222
223
# File 'lib/java_testing_guff/qdox_extensions.rb', line 214

def add_easy_mock_builder_to(generated_class)
    EasyMockStubMethodWriter.new(self).add_stub_methods_to(generated_class)
    add_expectation_methods_to(generated_class) {|body|
        if (returns_something?)
            body.line("#{easyMock}.expect(mock.#{name}(#{parameter_list})).andReturn(result);")
        else
            body.line("mock.#{name}(#{parameter_list});")
        end
    }
end

#add_easymock_stub_methods_to(generated_clazz) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/java_testing_guff/qdox_extensions.rb', line 407

def add_easymock_stub_methods_to(generated_clazz)
    if (setter?)
        return
    end

    if (getter?)
        add_easymock_getter_stub_to(generated_clazz)
    else
        method_builder = generated_clazz.add_method("with#{name.camelize}").returns('T')
        with_parameters_and_return do |p|
            p.add_takes_clause_to(method_builder)
        end
        with_exceptions do |e|
            e.add_throws_clause_to(method_builder)
        end

        method_builder.body {|body|
            if (returns_something?)
                body.line("#{easyMock}.expect(mock.#{name}(#{parameter_list})).andStubReturn(result);")
            else
                body.line("mock.#{name}(#{parameter_list});")
                body.line("#{easyMock}.expectLastCall().asStub();")
            end
            body.line('return (T) this;')
        }
    end
end

#add_expectation_methods_to(generated_class) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/java_testing_guff/qdox_extensions.rb', line 252

def add_expectation_methods_to(generated_class)
    if (!getter?)
        method_builder = generated_class.add_method("expecting#{name.camelize}").returns('T')
        with_parameters_and_return do |p|
            p.add_takes_clause_to(method_builder)
        end

        with_exceptions do |e|
            e.add_throws_clause_to(method_builder)
        end

        method_builder.body {|body|
            yield body
            body.line('return (T) this;')
        }
    end

end

#add_jmock_builder_to(generated_class) ⇒ Object



225
226
227
228
229
230
231
232
233
234
# File 'lib/java_testing_guff/qdox_extensions.rb', line 225

def add_jmock_builder_to(generated_class)
    JMockStubMethodWriter.new(self).add_stub_methods_to(generated_class)
    add_expectation_methods_to(generated_class) {|body|
        if (returns_something?)
            body.line("expects(once()).method(\"#{name}\").#{jmock_with_clause}.will(returnValue(result));")
        else
            body.line("expects(once()).method(\"#{name}\").#{jmock_with_clause};")
        end
    }
end

#easyMockObject



474
475
476
# File 'lib/java_testing_guff/qdox_extensions.rb', line 474

def easyMock
    'EasyMock'
end

#getter?Boolean

Returns:

  • (Boolean)


458
459
460
# File 'lib/java_testing_guff/qdox_extensions.rb', line 458

def getter?
    return parameters.length == 0 && returns_something? && (name.starts_with?('get') || name.starts_with?('has') || name.starts_with?('is'))
end

#jmock_with_clauseObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/java_testing_guff/qdox_extensions.rb', line 236

def jmock_with_clause
    if (parameters.length == 0)
        return "withNoArguments()";
    else
        clause=[]
        with_parameters do |p|
            clause << "eq(#{p.name})"
        end
        if (parameters.length > 4)
            return "with(new org.jmock.core.Constraint[]{#{clause.join(',')}})"
        else
            return "with(#{clause.join(',')})"
        end
    end
end

#my_declaration_signatureObject



196
197
198
# File 'lib/java_testing_guff/qdox_extensions.rb', line 196

def my_declaration_signature
    "#{returns.fully_qualified_name} #{name}(#{parameter_types}) #{throws_clause}"
end

#parameter_listObject



435
436
437
438
439
440
441
# File 'lib/java_testing_guff/qdox_extensions.rb', line 435

def parameter_list
    result=[]
    with_parameters do |p|
        result << "#{p.name}"
    end
    result.join(',')
end

#parameter_typesObject



200
201
202
203
204
205
# File 'lib/java_testing_guff/qdox_extensions.rb', line 200

def parameter_types
    types = parameters.collect do |p|
        p.type.fully_qualified_name
    end
    types.join(",")
end

#returns_nothing?Boolean

Returns:

  • (Boolean)


470
471
472
# File 'lib/java_testing_guff/qdox_extensions.rb', line 470

def returns_nothing?
    returns.void?
end

#returns_something?Boolean

Returns:

  • (Boolean)


466
467
468
# File 'lib/java_testing_guff/qdox_extensions.rb', line 466

def returns_something?
    !returns.void?
end

#setter?Boolean

Returns:

  • (Boolean)


462
463
464
# File 'lib/java_testing_guff/qdox_extensions.rb', line 462

def setter?
    return parameters.length == 1 && returns_nothing? && name.starts_with?('set')
end

#throws_clauseObject



207
208
209
210
211
212
# File 'lib/java_testing_guff/qdox_extensions.rb', line 207

def throws_clause
    types=exceptions.collect do |e|
        e.fully_qualified_name
    end
    types.join(",")
end

#with_exceptionsObject



271
272
273
274
275
# File 'lib/java_testing_guff/qdox_extensions.rb', line 271

def with_exceptions
    exceptions.each do |e|
        yield e
    end
end

#with_parametersObject



443
444
445
446
447
# File 'lib/java_testing_guff/qdox_extensions.rb', line 443

def with_parameters
    parameters.each do |p|
        yield p
    end
end

#with_parameters_and_returnObject



449
450
451
452
453
454
455
456
# File 'lib/java_testing_guff/qdox_extensions.rb', line 449

def with_parameters_and_return
    parameters.each do |p|
        yield p
    end
    if (returns_something?)
        yield returns
    end
end