From 135452228911c06f4b805247436da267787bf0d8 Mon Sep 17 00:00:00 2001 From: Tim Dettrick Date: Tue, 23 Oct 2012 15:36:39 +1000 Subject: [PATCH 1/4] Pegging version for activerecord to fix Travis CI tests. --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 9fce9bc..4e3703b 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ gemspec gem 'jruby-openssl', :platform => :jruby group :test do - gem 'activerecord' + gem 'activerecord', '~> 3.2.8' gem 'activerecord-jdbcsqlite3-adapter', :platform => [:jruby] gem 'libxml-ruby', :platform => [:ruby, :mswin] gem 'rake' From 3ad25b6a5335366c61317490f45dff77db6a053c Mon Sep 17 00:00:00 2001 From: Tim Dettrick Date: Fri, 21 Sep 2012 15:07:40 +1000 Subject: [PATCH 2/4] Making metadataPrefix a required argument. Like it or not, the OAI-PMH spec states that metadataPrefix cannot be left out of GetRecord or ListRecords requests. ie. http://www.openarchives.org/OAI/openarchivesprotocol.html#GetRecord http://www.openarchives.org/OAI/openarchivesprotocol.html#ListRecords --- lib/oai/provider/response.rb | 58 +++++++------- lib/oai/provider/response/get_record.rb | 14 ++-- lib/oai/provider/response/list_records.rb | 9 ++- lib/oai/provider/response/record_response.rb | 28 +++---- test/activerecord_provider/tc_ar_provider.rb | 47 +++++++---- .../tc_ar_sets_provider.rb | 24 ++++-- .../tc_caching_paging_provider.rb | 3 +- .../tc_simple_paging_provider.rb | 3 +- test/provider/tc_exceptions.rb | 50 +++++++----- test/provider/tc_functional_tokens.rb | 17 ++-- test/provider/tc_provider.rb | 71 +++++++++++------ test/provider/tc_resumption_tokens.rb | 12 +-- test/provider/tc_simple_provider.rb | 77 ++++++++++++------- 13 files changed, 250 insertions(+), 163 deletions(-) diff --git a/lib/oai/provider/response.rb b/lib/oai/provider/response.rb index d74bd84..f5bd769 100755 --- a/lib/oai/provider/response.rb +++ b/lib/oai/provider/response.rb @@ -2,29 +2,29 @@ module OAI module Provider module Response - + class Base attr_reader :provider, :options - + class << self attr_reader :valid_options, :default_options, :required_options def valid_parameters(*args) @valid_options ||= [] @valid_options = (@valid_options + args.dup).uniq end - + def default_parameters(options = {}) @default_options ||= {} @default_options.merge! options.dup end - + def required_parameters(*args) valid_parameters(*args) @required_options ||= [] @required_options = (@required_options + args.dup).uniq end - - end + + end def initialize(provider, options = {}) @provider = provider @options = internalize(options) @@ -33,19 +33,19 @@ def initialize(provider, options = {}) def response @builder = Builder::XmlMarkup.new @builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8" - @builder.tag!('OAI-PMH', header) do + @builder.tag!('OAI-PMH', header) do @builder.responseDate Time.now.utc.xmlschema #options parameter has been removed here because with it - #the data won't validate against oai validators. Without, it - #validates. - @builder.request(provider.url) #-- OAI 2.0 Hack - removed request options + #the data won't validate against oai validators. Without, it + #validates. + @builder.request(provider.url) #-- OAI 2.0 Hack - removed request options yield @builder end end private - + def header - { + { 'xmlns' => "http://www.openarchives.org/OAI/2.0/", 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", 'xsi:schemaLocation' => %{http://www.openarchives.org/OAI/2.0/ @@ -55,66 +55,66 @@ def header def extract_identifier(id) id.sub("#{provider.prefix}/", '') end - + def valid? return true if resumption? - + return true if self.class.valid_options.nil? and options.empty? - - # check if the request includes an argument and there are no valid + + # check if the request includes an argument and there are no valid # arguments for that verb (Identify, for example). raise OAI::ArgumentException.new if self.class.valid_options.nil? && !options.empty? - + if self.class.required_options return false unless (self.class.required_options - @options.keys).empty? end return false unless (@options.keys - self.class.valid_options).empty? populate_defaults end - + def populate_defaults self.class.default_options.each do |k,v| @options[k] = v.respond_to?(:call) ? v.call(self) : v if not @options[k] end end - + def resumption? - if @options.keys.include?(:resumption_token) + if @options.keys.include?(:resumption_token) return true if 1 == @options.keys.size raise OAI::ArgumentException.new end end - + # Convert our internal representations back into standard OAI options def externalize(value) value.to_s.gsub(/_[a-z]/) { |m| m.sub("_", '').capitalize } end - + def parse_date(value) return value if value.respond_to?(:strftime) - + Date.parse(value) # This will raise an exception for badly formatted dates Time.parse(value).utc # -- UTC Bug fix hack 8/08 not in core rescue - raise OAI::ArgumentError.new + raise OAI::ArgumentError.new end - + def internalize(hash = {}) internal = {} hash.keys.each do |key| internal[key.to_s.gsub(/([A-Z])/, '_\1').downcase.intern] = hash[key].dup end - + # Convert date formated strings into internal time values # Convert date formated strings in dates. internal[:from] = parse_date(internal[:from]) if internal[:from] internal[:until] = parse_date(internal[:until]) if internal[:until] - + internal end - + end - + end end end diff --git a/lib/oai/provider/response/get_record.rb b/lib/oai/provider/response/get_record.rb index e3571be..d1ecb8d 100755 --- a/lib/oai/provider/response/get_record.rb +++ b/lib/oai/provider/response/get_record.rb @@ -1,8 +1,8 @@ module OAI::Provider::Response - + class GetRecord < RecordResponse - required_parameters :identifier - + required_parameters :identifier, :metadata_prefix + def to_xml id = extract_identifier(options.delete(:identifier)) unless record = provider.model.find(id, options) @@ -11,7 +11,7 @@ def to_xml response do |r| r.GetRecord do - r.record do + r.record do header_for record data_for record unless deleted?(record) about_for record unless deleted?(record) @@ -19,9 +19,9 @@ def to_xml end end end - + end end - - + + diff --git a/lib/oai/provider/response/list_records.rb b/lib/oai/provider/response/list_records.rb index 7576477..61ce465 100755 --- a/lib/oai/provider/response/list_records.rb +++ b/lib/oai/provider/response/list_records.rb @@ -1,7 +1,8 @@ module OAI::Provider::Response class ListRecords < RecordResponse - + required_parameters :metadata_prefix + def to_xml result = provider.model.find(:all, options) # result may be an array of records, or a partial result @@ -27,8 +28,8 @@ def to_xml end end end - + end - + end - + diff --git a/lib/oai/provider/response/record_response.rb b/lib/oai/provider/response/record_response.rb index af7cade..2a798e8 100755 --- a/lib/oai/provider/response/record_response.rb +++ b/lib/oai/provider/response/record_response.rb @@ -2,16 +2,16 @@ module OAI::Provider::Response class RecordResponse < Base def self.inherited(klass) klass.valid_parameters :metadata_prefix, :from, :until, :set - klass.default_parameters :metadata_prefix => "oai_dc", + klass.default_parameters :metadata_prefix => "oai_dc", :from => Proc.new {|x| Time.parse(x.provider.model.earliest.to_s) }, #-- OAI 2.0 hack - UTC :until => Proc.new {|x| Time.parse(x.provider.model.latest.to_s) } #-- OAI 2.0 hack - UTC end - + # emit record header def header_for(record) param = Hash.new param[:status] = 'deleted' if deleted?(record) - @builder.header param do + @builder.header param do @builder.identifier identifier_for(record) @builder.datestamp timestamp_for(record) sets_for(record).each do |set| @@ -33,7 +33,7 @@ def about_for(record) return unless provider.model.respond_to? :about about = provider.model.about(record) - return if about.nil? + return if about.nil? unless about.is_a? Array about = [about] @@ -42,43 +42,43 @@ def about_for(record) about.each do |a| @builder.about do @builder.target! << a - end + end end end - + private - + def identifier_for(record) "#{provider.prefix}/#{record.id}" end - + def timestamp_for(record) record.send(provider.model.timestamp_field).utc.xmlschema end - + def sets_for(record) return [] unless record.respond_to?(:sets) and record.sets record.sets.respond_to?(:each) ? record.sets : [record.sets] end - + def requested_format - format = + format = if options[:metadata_prefix] options[:metadata_prefix] elsif options[:resumption_token] OAI::Provider::ResumptionToken.extract_format(options[:resumption_token]) end raise OAI::FormatException.new unless provider.format_supported?(format) - + format end - + def deleted?(record) return record.deleted? if record.respond_to?(:deleted?) return record.deleted if record.respond_to?(:deleted) return record.deleted_at if record.respond_to?(:deleted_at) false end - + end end diff --git a/test/activerecord_provider/tc_ar_provider.rb b/test/activerecord_provider/tc_ar_provider.rb index 8da2469..f371002 100755 --- a/test/activerecord_provider/tc_ar_provider.rb +++ b/test/activerecord_provider/tc_ar_provider.rb @@ -20,8 +20,11 @@ def test_metadata_formats_for_record end def test_list_records - assert_nothing_raised { REXML::Document.new(@provider.list_records) } - doc = REXML::Document.new(@provider.list_records) + assert_nothing_raised do + REXML::Document.new(@provider.list_records(:metadata_prefix => 'oai_dc')) + end + doc = REXML::Document.new(@provider.list_records( + :metadata_prefix => 'oai_dc')) assert_equal 100, doc.elements['OAI-PMH/ListRecords'].to_a.size end @@ -33,8 +36,12 @@ def test_list_identifiers def test_get_record record_id = DCField.find(:first).id - assert_nothing_raised { REXML::Document.new(@provider.get_record(:identifier => "oai:test/#{record_id}")) } - doc = REXML::Document.new(@provider.get_record(:identifier => "#{record_id}")) + assert_nothing_raised do + REXML::Document.new(@provider.get_record( + :identifier => "oai:test/#{record_id}", :metadata_prefix => 'oai_dc')) + end + doc = REXML::Document.new(@provider.get_record( + :identifier => "#{record_id}", :metadata_prefix => 'oai_dc')) assert_equal "oai:test/#{record_id}", doc.elements['OAI-PMH/GetRecord/record/header/identifier'].text end @@ -42,7 +49,8 @@ def test_deleted record = DCField.find(:first) record.deleted = true; record.save - doc = REXML::Document.new(@provider.get_record(:identifier => "oai:test/#{record.id}")) + doc = REXML::Document.new(@provider.get_record( + :identifier => "oai:test/#{record.id}", :metadata_prefix => 'oai_dc')) assert_equal "oai:test/#{record.id}", doc.elements['OAI-PMH/GetRecord/record/header/identifier'].text assert_equal 'deleted', doc.elements['OAI-PMH/GetRecord/record/header'].attributes["status"] end @@ -57,14 +65,16 @@ def test_from from_param = Time.parse("January 1 2006") doc = REXML::Document.new( - @provider.list_records(:from => from_param) - ) + @provider.list_records( + :metadata_prefix => 'oai_dc', :from => from_param) + ) assert_equal DCField.find(:all, :conditions => ["updated_at >= ?", from_param]).size, doc.elements['OAI-PMH/ListRecords'].size doc = REXML::Document.new( - @provider.list_records(:from => Time.parse("May 30 2005")) - ) + @provider.list_records( + :metadata_prefix => 'oai_dc', :from => Time.parse("May 30 2005")) + ) assert_equal 20, doc.elements['OAI-PMH/ListRecords'].to_a.size end @@ -74,8 +84,9 @@ def test_until "id < #{first_id + 10}") doc = REXML::Document.new( - @provider.list_records(:until => Time.parse("June 1 2005")) - ) + @provider.list_records( + :metadata_prefix => 'oai_dc', :until => Time.parse("June 1 2005")) + ) assert_equal 10, doc.elements['OAI-PMH/ListRecords'].to_a.size end @@ -88,7 +99,9 @@ def test_from_and_until "id < #{first_id + 10}") doc = REXML::Document.new( - @provider.list_records(:from => Time.parse("June 3 2005"), + @provider.list_records( + :metadata_prefix => 'oai_dc', + :from => Time.parse("June 3 2005"), :until => Time.parse("June 16 2005")) ) assert_equal 40, doc.elements['OAI-PMH/ListRecords'].to_a.size @@ -101,12 +114,12 @@ def test_handles_empty_collections test_identify test_metadata_formats # ListIdentifiers and ListRecords should return "noRecordsMatch" error code - assert_raises(OAI::NoMatchException) { + assert_raises(OAI::NoMatchException) do REXML::Document.new(@provider.list_identifiers) - } - assert_raises(OAI::NoMatchException) { - REXML::Document.new(@provider.list_records) - } + end + assert_raises(OAI::NoMatchException) do + REXML::Document.new(@provider.list_records(:metadata_prefix => 'oai_dc')) + end end def setup diff --git a/test/activerecord_provider/tc_ar_sets_provider.rb b/test/activerecord_provider/tc_ar_sets_provider.rb index bd78e8d..eb336e3 100755 --- a/test/activerecord_provider/tc_ar_sets_provider.rb +++ b/test/activerecord_provider/tc_ar_sets_provider.rb @@ -10,17 +10,20 @@ def test_list_sets end def test_set_a - doc = REXML::Document.new(@provider.list_records(:set => "A")) + doc = REXML::Document.new(@provider.list_records( + :metadata_prefix => 'oai_dc', :set => "A")) assert_equal 20, doc.elements['OAI-PMH/ListRecords'].to_a.size end def test_set_b - doc = REXML::Document.new(@provider.list_records(:set => "B")) + doc = REXML::Document.new(@provider.list_records( + :metadata_prefix => 'oai_dc', :set => "B")) assert_equal 10, doc.elements['OAI-PMH/ListRecords'].to_a.size end def test_set_ab - doc = REXML::Document.new(@provider.list_records(:set => "A:B")) + doc = REXML::Document.new(@provider.list_records( + :metadata_prefix => 'oai_dc', :set => "A:B")) assert_equal 10, doc.elements['OAI-PMH/ListRecords'].to_a.size end @@ -31,7 +34,8 @@ def test_record_with_multiple_sets def test_missing_set assert_raise(OAI::NoMatchException) do - doc = REXML::Document.new(@provider.list_records(:set => "D")) + doc = REXML::Document.new(@provider.list_records( + :metadata_prefix => 'oai_dc', :set => "D")) end end @@ -81,23 +85,27 @@ def test_list_sets end def test_set_a - doc = REXML::Document.new(@provider.list_records(:set => "A")) + doc = REXML::Document.new(@provider.list_records( + :metadata_prefix => 'oai_dc', :set => "A")) assert_equal 20, doc.elements['OAI-PMH/ListRecords'].to_a.size end def test_set_b - doc = REXML::Document.new(@provider.list_records(:set => "B")) + doc = REXML::Document.new(@provider.list_records( + :metadata_prefix => 'oai_dc', :set => "B")) assert_equal 10, doc.elements['OAI-PMH/ListRecords'].to_a.size end def test_set_ab - doc = REXML::Document.new(@provider.list_records(:set => "A:B")) + doc = REXML::Document.new(@provider.list_records( + :metadata_prefix => 'oai_dc', :set => "A:B")) assert_equal 10, doc.elements['OAI-PMH/ListRecords'].to_a.size end def test_missing_set assert_raise(OAI::NoMatchException) do - doc = REXML::Document.new(@provider.list_records(:set => "D")) + doc = REXML::Document.new(@provider.list_records( + :metadata_prefix => 'oai_dc', :set => "D")) end end diff --git a/test/activerecord_provider/tc_caching_paging_provider.rb b/test/activerecord_provider/tc_caching_paging_provider.rb index aefca2d..03a180a 100755 --- a/test/activerecord_provider/tc_caching_paging_provider.rb +++ b/test/activerecord_provider/tc_caching_paging_provider.rb @@ -4,7 +4,7 @@ class CachingPagingProviderTest < TransactionalTestCase include REXML def test_full_harvest - doc = Document.new(@provider.list_records) + doc = Document.new(@provider.list_records(:metadata_prefix => 'oai_dc')) assert_not_nil doc.elements["/OAI-PMH/ListRecords/resumptionToken"] assert_equal 26, doc.elements["/OAI-PMH/ListRecords"].size token = doc.elements["/OAI-PMH/ListRecords/resumptionToken"].text @@ -31,6 +31,7 @@ def test_from_and_until # Should return 50 records broken into 2 groups of 25. doc = Document.new( @provider.list_records( + :metadata_prefix => 'oai_dc', :from => Time.parse("September 1 2005"), :until => Time.parse("November 30 2005")) ) diff --git a/test/activerecord_provider/tc_simple_paging_provider.rb b/test/activerecord_provider/tc_simple_paging_provider.rb index 7e0d741..9428564 100755 --- a/test/activerecord_provider/tc_simple_paging_provider.rb +++ b/test/activerecord_provider/tc_simple_paging_provider.rb @@ -4,7 +4,7 @@ class SimpleResumptionProviderTest < TransactionalTestCase include REXML def test_full_harvest - doc = Document.new(@provider.list_records) + doc = Document.new(@provider.list_records(:metadata_prefix => 'oai_dc')) assert_not_nil doc.elements["/OAI-PMH/ListRecords/resumptionToken"] assert_equal 26, doc.elements["/OAI-PMH/ListRecords"].to_a.size token = doc.elements["/OAI-PMH/ListRecords/resumptionToken"].text @@ -33,6 +33,7 @@ def test_from_and_until # Should return 50 records broken into 2 groups of 25. doc = Document.new( @provider.list_records( + :metadata_prefix => 'oai_dc', :from => Time.parse("September 1 2005"), :until => Time.parse("November 30 2005")) ) diff --git a/test/provider/tc_exceptions.rb b/test/provider/tc_exceptions.rb index da7c914..65a2114 100755 --- a/test/provider/tc_exceptions.rb +++ b/test/provider/tc_exceptions.rb @@ -1,7 +1,7 @@ require 'test_helper' class ProviderExceptions < Test::Unit::TestCase - + def setup @provider = ComplexProvider.new end @@ -9,7 +9,7 @@ def setup def test_argument_exception assert_raise(OAI::ArgumentException) do @provider.identify(:identifier => 'invalid_arg') - end + end end def test_resumption_token_exception @@ -26,44 +26,54 @@ def test_resumption_token_exception @provider.list_identifiers(:resumption_token => '\:\\:\/$%^&*!@#!:1') end end - + def test_bad_verb_raises_exception assert @provider.process_request(:verb => 'BadVerb') =~ /badVerb/ assert @provider.process_request(:verb => '\a$#^%!@') =~ /badVerb/ assert @provider.process_request(:verb => 'identity') =~ /badVerb/ assert @provider.process_request(:verb => '!!\\$\$\.+') =~ /badVerb/ end - + def test_bad_format_raises_exception assert_raise(OAI::FormatException) do @provider.get_record(:identifier => 'oai:test/1', :metadata_prefix => 'html') end end - - def test_bad_id_raises_exception - assert_raise(OAI::IdException) do - @provider.get_record(:identifier => 'oai:test/5000') - end - assert_raise(OAI::IdException) do - @provider.get_record(:identifier => 'oai:test/-1') + + def test_missing_format_raises_exception + assert_raise(OAI::ArgumentException) do + @provider.list_records() end - assert_raise(OAI::IdException) do - @provider.get_record(:identifier => 'oai:test/one') + assert_raise(OAI::ArgumentException) do + @provider.get_record(:identifier => 'oai:test/1') end - assert_raise(OAI::IdException) do - @provider.get_record(:identifier => 'oai:test/\\$1\1!') + end + + def test_bad_id_raises_exception + badIdentifiers = [ + 'oai:test/5000', + 'oai:test/-1', + 'oai:test/one', + 'oai:test/\\$1\1!'] + badIdentifiers.each do |id| + assert_raise(OAI::IdException) do + @provider.get_record(:identifier => id, :metadata_prefix => 'oai_dc') + end end end - + def test_no_records_match_dates_that_are_out_of_range assert_raise(OAI::NoMatchException) do - @provider.list_records(:from => Time.parse("November 2 2000"), + @provider.list_records(:metadata_prefix => 'oai_dc', + :from => Time.parse("November 2 2000"), :until => Time.parse("November 1 2000")) end end - + def test_no_records_match_bad_set - assert_raise(OAI::NoMatchException) { @provider.list_records(:set => 'unknown') } + assert_raise(OAI::NoMatchException) do + @provider.list_records(:metadata_prefix => 'oai_dc', :set => 'unknown') + end end - + end diff --git a/test/provider/tc_functional_tokens.rb b/test/provider/tc_functional_tokens.rb index 5eec12b..1034d5d 100755 --- a/test/provider/tc_functional_tokens.rb +++ b/test/provider/tc_functional_tokens.rb @@ -2,14 +2,16 @@ class ResumptionTokenFunctionalTest < Test::Unit::TestCase include REXML - + def setup @provider = ComplexProvider.new end def test_resumption_tokens - assert_nothing_raised { Document.new(@provider.list_records) } - doc = Document.new(@provider.list_records) + assert_nothing_raised do + Document.new(@provider.list_records(:metadata_prefix => 'oai_dc')) + end + doc = Document.new(@provider.list_records(:metadata_prefix => 'oai_dc')) assert_not_nil doc.elements["/OAI-PMH/ListRecords/resumptionToken"] assert_equal 101, doc.elements["/OAI-PMH/ListRecords"].to_a.size token = doc.elements["/OAI-PMH/ListRecords/resumptionToken"].text @@ -20,15 +22,18 @@ def test_resumption_tokens def test_from_and_until_with_resumption_tokens # Should return 300 records broken into 3 groups of 100. - assert_nothing_raised { Document.new(@provider.list_records) } + assert_nothing_raised do + Document.new(@provider.list_records(:metadata_prefix => 'oai_dc')) + end doc = Document.new( @provider.list_records( + :metadata_prefix => 'oai_dc', :from => Time.parse("September 1 2004"), :until => Time.parse("November 30 2004")) ) assert_equal 101, doc.elements["/OAI-PMH/ListRecords"].to_a.size token = doc.elements["/OAI-PMH/ListRecords/resumptionToken"].text - + doc = Document.new(@provider.list_records(:resumption_token => token)) assert_not_nil doc.elements["/OAI-PMH/ListRecords/resumptionToken"] assert_equal 101, doc.elements["/OAI-PMH/ListRecords"].to_a.size @@ -38,5 +43,5 @@ def test_from_and_until_with_resumption_tokens assert_nil doc.elements["/OAI-PMH/ListRecords/resumptionToken"] assert_equal 100, doc.elements["/OAI-PMH/ListRecords"].to_a.size end - + end \ No newline at end of file diff --git a/test/provider/tc_provider.rb b/test/provider/tc_provider.rb index f7cccfc..34170db 100644 --- a/test/provider/tc_provider.rb +++ b/test/provider/tc_provider.rb @@ -13,7 +13,7 @@ def test_additional_description assert_equal "oai:test:13900", doc.elements['OAI-PMH/Identify/description/oai-identifier/sampleIdentifier'].text assert_not_nil doc.elements['OAI-PMH/Identify/my_custom_xml'] end - + def test_list_identifiers_for_correct_xml doc = REXML::Document.new(@mapped_provider.list_identifiers) assert_not_nil doc.elements['OAI-PMH/ListIdentifiers'] @@ -22,55 +22,78 @@ def test_list_identifiers_for_correct_xml assert_not_nil doc.elements['OAI-PMH/ListIdentifiers/header/datestamp'] assert_not_nil doc.elements['OAI-PMH/ListIdentifiers/header/setSpec'] end - + def test_list_records_for_correct_xml - doc = REXML::Document.new(@mapped_provider.list_records) + doc = REXML::Document.new( + @mapped_provider.list_records(:metadata_prefix => 'oai_dc')) assert_not_nil doc.elements['OAI-PMH/ListRecords/record/header'] assert_not_nil doc.elements['OAI-PMH/ListRecords/record/metadata'] end - + def test_mapped_source - assert_nothing_raised { REXML::Document.new(@mapped_provider.list_records) } - doc = REXML::Document.new(@mapped_provider.list_records) + assert_nothing_raised do + REXML::Document.new( + @mapped_provider.list_records(:metadata_prefix => 'oai_dc')) + end + doc = REXML::Document.new( + @mapped_provider.list_records(:metadata_prefix => 'oai_dc')) assert_equal "title_0", doc.elements['OAI-PMH/ListRecords/record/metadata/oai_dc:dc/dc:creator'].text assert_equal "creator_0", doc.elements['OAI-PMH/ListRecords/record/metadata/oai_dc:dc/dc:title'].text assert_equal "tag_0", doc.elements['OAI-PMH/ListRecords/record/metadata/oai_dc:dc/dc:subject'].text end - + def test_from - assert_nothing_raised { REXML::Document.new(@big_provider.list_records) } + assert_nothing_raised do + REXML::Document.new( + @big_provider.list_records(:metadata_prefix => 'oai_dc')) + end doc = REXML::Document.new( - @big_provider.list_records(:from => Time.parse("February 1 2001")) - ) + @big_provider.list_records( + :metadata_prefix => 'oai_dc', + :from => Time.parse("February 1 2001")) + ) assert_equal 100, doc.elements['OAI-PMH/ListRecords'].to_a.size doc = REXML::Document.new( - @big_provider.list_records(:from => Time.parse("January 1 2001")) - ) + @big_provider.list_records( + :metadata_prefix => 'oai_dc', + :from => Time.parse("January 1 2001")) + ) assert_equal 200, doc.elements['OAI-PMH/ListRecords'].to_a.size end - + def test_until - assert_nothing_raised { REXML::Document.new(@big_provider.list_records) } + assert_nothing_raised do + REXML::Document.new( + @big_provider.list_records(:metadata_prefix => 'oai_dc')) + end doc = REXML::Document.new( - @big_provider.list_records(:until => Time.parse("November 1 2000")) - ) + @big_provider.list_records( + :metadata_prefix => 'oai_dc', :until => Time.parse("November 1 2000")) + ) assert_equal 100, doc.elements['OAI-PMH/ListRecords'].to_a.size end - + def test_from_and_until - assert_nothing_raised { REXML::Document.new(@big_provider.list_records) } + assert_nothing_raised do + REXML::Document.new( + @big_provider.list_records(:metadata_prefix => 'oai_dc')) + end doc = REXML::Document.new( - @big_provider.list_records(:from => Time.parse("November 1 2000"), + @big_provider.list_records( + :metadata_prefix => 'oai_dc', + :from => Time.parse("November 1 2000"), :until => Time.parse("November 30 2000")) - ) + ) assert_equal 100, doc.elements['OAI-PMH/ListRecords'].to_a.size doc = REXML::Document.new( - @big_provider.list_records(:from => Time.parse("December 1 2000"), - :until => Time.parse("December 31 2000")) - ) + @big_provider.list_records( + :metadata_prefix => 'oai_dc', + :from => Time.parse("December 1 2000"), + :until => Time.parse("December 31 2000")) + ) assert_equal 100, doc.elements['OAI-PMH/ListRecords'].to_a.size end - + end diff --git a/test/provider/tc_resumption_tokens.rb b/test/provider/tc_resumption_tokens.rb index d8144c4..eb36537 100755 --- a/test/provider/tc_resumption_tokens.rb +++ b/test/provider/tc_resumption_tokens.rb @@ -3,13 +3,13 @@ class ResumptionTokenTest < Test::Unit::TestCase include REXML include OAI::Provider - + def setup @token = ResumptionToken.new( :from => Time.utc(2005,"jan",1,17,0,0), :until => Time.utc(2005,"jan",31,17,0,0), :set => "A", - :metadata_prefix => "oai_dc", + :metadata_prefix => "oai_dc", :last => 1 ) end @@ -18,11 +18,11 @@ def test_resumption_token_options_encoding assert_equal "oai_dc.s(A).f(2005-01-01T17:00:00Z).u(2005-01-31T17:00:00Z)", @token.to_s end - + def test_resumption_token_next_method assert_equal 100, @token.next(100).last end - + def test_resumption_token_to_condition_hash hash = @token.to_conditions_hash assert_equal @token.from, hash[:from] @@ -37,10 +37,10 @@ def test_resumption_token_parsing ) assert_equal @token, new_token end - + def test_resumption_token_to_xml doc = REXML::Document.new(@token.to_xml) assert_equal "#{@token.to_s}:#{@token.last}", doc.elements['/resumptionToken'].text end - + end \ No newline at end of file diff --git a/test/provider/tc_simple_provider.rb b/test/provider/tc_simple_provider.rb index 0718eaa..caf395d 100755 --- a/test/provider/tc_simple_provider.rb +++ b/test/provider/tc_simple_provider.rb @@ -6,18 +6,18 @@ def setup @simple_provider = SimpleProvider.new @model = @simple_provider.class.model end - + def test_identify doc = REXML::Document.new(@simple_provider.identify) assert_equal @simple_provider.class.name, doc.elements["/OAI-PMH/Identify/repositoryName"].text assert_equal SimpleModel.new.earliest.to_s, doc.elements["/OAI-PMH/Identify/earliestDatestamp"].text - + # PC # lambda { REXML::Document.new(@simple_provider.identify(:set => 'A')) } - - + + end def test_list_sets @@ -26,72 +26,97 @@ def test_list_sets assert_equal @model.sets.size, sets.size assert_equal @model.sets[0].name, sets[0].elements["//setName"].text end - + def test_metadata_formats assert_nothing_raised { REXML::Document.new(@simple_provider.list_metadata_formats) } doc = REXML::Document.new(@simple_provider.list_metadata_formats) assert_equal "oai_dc", doc.elements['/OAI-PMH/ListMetadataFormats/metadataFormat/metadataPrefix'].text end - + def test_metadata_formats_for_document assert_nothing_raised { REXML::Document.new(@simple_provider.list_metadata_formats(:identifier => "oai:test/1")) } doc = REXML::Document.new(@simple_provider.list_metadata_formats) assert_equal "oai_dc", doc.elements['/OAI-PMH/ListMetadataFormats/metadataFormat/metadataPrefix'].text end - + def test_list_records_without_constraints - assert_nothing_raised { REXML::Document.new(@simple_provider.list_records) } + assert_nothing_raised { REXML::Document.new(@simple_provider.list_records(:metadata_prefix => 'oai_dc')) } total = @model.find(:all).size - doc = REXML::Document.new(@simple_provider.list_records) + doc = REXML::Document.new(@simple_provider.list_records(:metadata_prefix => 'oai_dc')) assert_equal total, doc.elements['OAI-PMH/ListRecords'].size end - + def test_list_records_with_set_equal_a total = @model.find(:all, :set => 'A').size - doc = REXML::Document.new(@simple_provider.list_records(:set => 'A')) + doc = REXML::Document.new(@simple_provider.list_records(:metadata_prefix => 'oai_dc', :set => 'A')) assert_equal total, doc.elements['OAI-PMH/ListRecords'].size end - + def test_list_record_with_set_equal_ab total = @model.find(:all, :set => 'A:B').size - doc = REXML::Document.new(@simple_provider.list_records(:set => 'A:B')) + doc = REXML::Document.new(@simple_provider.list_records(:metadata_prefix => 'oai_dc', :set => 'A:B')) assert_equal total, doc.elements['OAI-PMH/ListRecords'].size end def test_list_identifiers_without_constraints - assert_nothing_raised { REXML::Document.new(@simple_provider.list_identifiers) } + assert_nothing_raised { REXML::Document.new(@simple_provider.list_identifiers(:metadata_prefix => 'oai_dc')) } total = @model.find(:all).size - doc = REXML::Document.new(@simple_provider.list_identifiers) + doc = REXML::Document.new(@simple_provider.list_identifiers(:metadata_prefix => 'oai_dc')) assert_equal total, doc.elements['OAI-PMH/ListIdentifiers'].to_a.size end - + def test_list_identifiers_with_set_equal_a total = @model.find(:all, :set => 'A').size - doc = REXML::Document.new(@simple_provider.list_identifiers(:set => 'A')) + doc = REXML::Document.new(@simple_provider.list_identifiers(:metadata_prefix => 'oai_dc', :set => 'A')) assert_equal total, doc.elements['OAI-PMH/ListIdentifiers'].to_a.size end - + def test_list_indentifiers_with_set_equal_ab total = @model.find(:all, :set => 'A:B').size - doc = REXML::Document.new(@simple_provider.list_identifiers(:set => 'A:B')) + doc = REXML::Document.new(@simple_provider.list_identifiers(:metadata_prefix => 'oai_dc', :set => 'A:B')) assert_equal total, doc.elements['OAI-PMH/ListIdentifiers'].to_a.size end def test_get_record - assert_nothing_raised { REXML::Document.new(@simple_provider.get_record(:identifier => 'oai:test/1')) } - doc = REXML::Document.new(@simple_provider.get_record(:identifier => 'oai:test/1')) - assert_equal 'oai:test/1', doc.elements['OAI-PMH/GetRecord/record/header/identifier'].text + assert_nothing_raised do + REXML::Document.new( + @simple_provider.get_record( + :identifier => 'oai:test/1', + :metadataPrefix => 'oai_dc' + ) + ) + end + doc = REXML::Document.new( + @simple_provider.get_record( + :identifier => 'oai:test/1', + :metadataPrefix => 'oai_dc' + ) + ) + assert_equal 'oai:test/1', + doc.elements['OAI-PMH/GetRecord/record/header/identifier'].text end - + def test_deleted_record - assert_nothing_raised { REXML::Document.new(@simple_provider.get_record(:identifier => 'oai:test/6')) } - doc = REXML::Document.new(@simple_provider.get_record(:identifier => 'oai:test/5')) + assert_nothing_raised do + REXML::Document.new( + @simple_provider.get_record( + :identifier => 'oai:test/6', + :metadataPrefix => 'oai_dc' + ) + ) + end + doc = REXML::Document.new( + @simple_provider.get_record( + :identifier => 'oai:test/5', + :metadataPrefix => 'oai_dc' + ) + ) assert_equal 'oai:test/5', doc.elements['OAI-PMH/GetRecord/record/header/identifier'].text assert_equal 'deleted', doc.elements['OAI-PMH/GetRecord/record/header'].attributes["status"] end - + end From a86db36778381053f5e53a834297ecf528040453 Mon Sep 17 00:00:00 2001 From: Tim Dettrick Date: Wed, 26 Sep 2012 12:50:33 +1000 Subject: [PATCH 3/4] Making schemaLocation whitespace so simple that any parser can handle it. --- lib/oai/provider/response.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/oai/provider/response.rb b/lib/oai/provider/response.rb index f5bd769..d9ec19a 100755 --- a/lib/oai/provider/response.rb +++ b/lib/oai/provider/response.rb @@ -49,7 +49,7 @@ def header 'xmlns' => "http://www.openarchives.org/OAI/2.0/", 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", 'xsi:schemaLocation' => %{http://www.openarchives.org/OAI/2.0/ - http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd} + http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd}.gsub(/\s+/, ' ') } end def extract_identifier(id) From c4c0c6d870781af36b8b9aac0a3506289ed7ec01 Mon Sep 17 00:00:00 2001 From: Tim Dettrick Date: Wed, 24 Oct 2012 15:57:29 +1000 Subject: [PATCH 4/4] Removing new line in response to issue #25. --- lib/oai/provider/metadata_format/oai_dc.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/oai/provider/metadata_format/oai_dc.rb b/lib/oai/provider/metadata_format/oai_dc.rb index 9416d04..aeed7d6 100755 --- a/lib/oai/provider/metadata_format/oai_dc.rb +++ b/lib/oai/provider/metadata_format/oai_dc.rb @@ -1,9 +1,8 @@ module OAI::Provider::Metadata - # = OAI::Metadata::DublinCore - # + # Simple implementation of the Dublin Core metadata format. class DublinCore < Format - + def initialize @prefix = 'oai_dc' @schema = 'http://www.openarchives.org/OAI/2.0/oai_dc.xsd' @@ -19,9 +18,9 @@ def header_specification 'xmlns:oai_dc' => "http://www.openarchives.org/OAI/2.0/oai_dc/", 'xmlns:dc' => "http://purl.org/dc/elements/1.1/", 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", - 'xsi:schemaLocation' => - %{http://www.openarchives.org/OAI/2.0/oai_dc/ - http://www.openarchives.org/OAI/2.0/oai_dc.xsd} + 'xsi:schemaLocation' => + %{http://www.openarchives.org/OAI/2.0/oai_dc/ + http://www.openarchives.org/OAI/2.0/oai_dc.xsd}.gsub(/\s+/, ' ') } end