From: Steve F. <sm...@us...> - 2002-08-31 10:55:25
|
Update of /cvsroot/mockobjects/no-stone-unturned/src/ruby In directory usw-pr-cvs1:/tmp/cvs-serv26200/src/ruby Added Files: tasks.txt server.rb test_addrservlet.rb addrservlet.rb Log Message: Restructured source code under single src directory --- NEW FILE: tasks.txt --- Stories 1) anyone can search the entries in the book. 2) anyone can add their name and email address to the book. 3) anyone can remove an entry from the book. Tasks 1a) accept a name and find no result DONE Points of Interest: - no need to implement any interfaces when defining mocks 1b) accept a name and return a result from a hard-coded collection. 1b.1) what about the servlet receiving no name? DONE Points of Interest: - using blocks to handle expectations REFACTORING 1c) Retrieve the entries from a file, specified as a servlet property. Values are held in memory. --- NEW FILE: server.rb --- #!/usr/bin/ruby require 'webrick' require 'addrservlet' server = WEBrick::HTTPServer.new( :Port => 2000 ) server.mount( "/address", AddressBookServlet, "Nat Pryce" => "nat...@so...", "Steve Freeman" => "st...@so...", "Jeff Martin" => "cus...@so..." ) trap("INT") { server.shutdown } server.start --- NEW FILE: test_addrservlet.rb --- require 'test/unit' require 'webrick' require 'addrservlet' class MockRequest def _setup_query_string( query ) @query_string = WEBrick::HTTPUtils::escape_form(query) end attr_reader :query_string end class MockResponse include Test::Unit::Assertions def initialize @content_type_set = false @body_set = false @body_expectation = nil end def []=( header, value ) assert_equal( "content-type", header ) assert_match( /^text\/.*/, value ) @content_type_set = true end def _expect_body( &proc ) @body_expectation = proc end def body=( value ) @body_expectation.call(value) if @body_expectation @body_set = true end def _verify assert( "content-type", @content_type_set ) assert( "body", @body_expectation && @body_set ) end end class AddressBookServletTest < Test::Unit::TestCase NAME1 = "First Last" ADDR1 = "ADDRESS" def set_up @request = MockRequest.new @response = MockResponse.new @servlet = AddressBookServlet.new( {}, NAME1 => ADDR1 ) end def test_no_address_found @request._setup_query_string( "UNKNOWN NAME" ) @response._expect_body { |text| assert_match( /no address found/, text ) } @servlet.do_GET( @request, @response ) @response._verify end def test_no_address_found_when_no_name @response._expect_body { |text| assert_match( /no address found/, text ) } @servlet.do_GET( @request, @response ) @response._verify end def test_address_found @request._setup_query_string( NAME1 ) @response._expect_body { |text| assert_match( /#{ADDR1}/, text ) } @servlet.do_GET( @request, @response ) @response._verify end end --- NEW FILE: addrservlet.rb --- require 'webrick' class AddressBookServlet < WEBrick::HTTPServlet::AbstractServlet def initialize( config, address_book ) super @address_book = address_book end def do_GET( request, response ) response['content-type'] = 'text/plain' query = request.query_string if query == nil respond_no_match( response ) else name = WEBrick::HTTPUtils::unescape_form( query ) find_address( name, response ) end end def find_address( name, response ) if @address_book.has_key?(name) respond_address( response, @address_book[name] ) else respond_no_match( response ) end end def respond_address( response, address ) response.body = address end def respond_no_match( response ) response.body = "no address found" end end |