require 'test/unit' require 'rdf/redland' class TestParser < Test::Unit::TestCase include Redland def setup world = $world @foaf = Namespace.new("http://xmlns.com/foaf/0.1/") @faa = Namespace.new("http://http://www.faa.gov/ontology/people#") @data_rdf_string =< Pat Richardson NISC Program Manager Pat Richardson 202-646-2352 Jack Nager Director of NAS Implementation Program Jack Nager Dominic Sisneros Electronic Engineer Dominic Sisneros EOF @data_triples_string=< . _:r1081452401r1 "Pat Richardson" . _:r1081452401r1 "NISC Program Manager" . _:r1081452401r1 "Pat" . _:r1081452401r1 "Richardson" . _:r1081452401r1 . _:r1081452401r1 "202-646-2352" . _:r1081452401r1 . _:r1081452401r2 . _:r1081452401r2 "Jack Nager" . _:r1081452401r2 "Director of NAS Implementation Program" . _:r1081452401r2 "Jack" . _:r1081452401r2 "Nager" . _:r1081452401r2 . _:r1081452401r3 . _:r1081452401r3 "Dominic Sisneros" . _:r1081452401r3 "Electronic Engineer" . _:r1081452401r3 "Dominic" . _:r1081452401r3 "Sisneros" . _:r1081452401r3 . TRIPLES end def test_parse model = Model.new() foaf = Namespace.new("http://xmlns.com/foaf/0.1/") faa = Namespace.new("http://http://www.faa.gov/ontology/people#") parser = Parser.new() parser.parse_into_model(model,"file:./ical.rdf") #model.triples(){|s,p,o| puts "#{s}:#{p}:#{o}"} end def test_stream parser = Parser.new() model = Model.new() parser.parse_as_stream("file:./ical.rdf"){|s| model.add_statement(s)} end def test_parser_with_contexts() store = HashStore.new('bdb','thestore') model = Model.new(store) context = Node.new(Uri.new('http://www.faa.gov')) parser = Parser.new() parser.parse_into_model(model,"file:./ical.rdf",nil,context) contexts = model.contexts assert_equal(1,contexts.size) assert_equal(contexts[0],Node.new(Uri.new('http://www.faa.gov'))) end def check_foaf_model(model) jack = model.subject(@foaf['mbox'],Node.new(:uri_string=>'mailto:jack.nager@bogus.com')) assert_equal('Jack',model.object(jack,@foaf['firstName']).to_s) assert_equal('Nager',model.object(jack,@foaf['surname']).to_s) assert_equal('Director of NAS Implementation Program', model.object(jack,@foaf['title']).to_s) end def test_parse_string_into_model model = Model.new() parser = Parser.new() parser.parse_string_into_model(model,@data_rdf_string,'http://xml.com') check_foaf_model(model) end def test_parse_string_into_model_from_triples model = Model.new() parser = Parser.ntriples() parser.parse_string_into_model(model,@data_triples_string,'http://xml.com') check_foaf_model(model) end def test_parse_string_as_stream model = Model.new() parser = Parser.new() parser.parse_string_as_stream(@data_rdf_string,'http://xml.com'){|s| model.add_statement(s)} check_foaf_model(model) end def smush_tester(model,dom) assert_equal('Electronic Engineer',model.object(dom,@foaf['title']).to_s) assert_equal('Sisneros',model.object(dom,@foaf['surname']).to_s) end def no_test_string_smush model = Model.new() dom = BNode.new('dom') model.add(dom,@foaf['mbox'],Node.new(:uri_string=>'mailto:dominic@bogus.com')) model.add(dom,@foaf['name'],'Dominic Sisneros') #puts "Dom title before = #{model.object(dom,@foaf['title'])}" assert_nil(model.object(dom,@foaf['title'])) parser = Parser.ntriples() parser.add_ident(@foaf['mbox']) parser.smush_string(@data_triples_string,model) smush_tester(model,dom) end def test_file_smush() #temp_model = Model.new() #parser = Parser.new() #parser.parse_string_into_model(temp_model,@data_rdf_string,"http://www.faa.gov") #serializer = Serializer.new() #serializer.to_file('triples.rdf',temp_model) model = Model.new() dom = BNode.new('dom') model.add(dom,@foaf['mbox'],Node.new(:uri_string=>'mailto:dominic@bogus.com')) model.add(dom,@foaf['name'],'Dominic Sisneros') parser = Parser.new() parser.add_ident(@foaf['mbox']) parser.smush_file(model,'file:./triples.rdf') smush_tester(model,dom) #model.statements{|s| puts s} end end