| 1 | # -*- Mode: Perl -*- |
| 2 | # |
| 3 | # test2.t - Redland perl test 2 - general RDF::Redland coverage |
| 4 | # |
| 5 | # Copyright (C) 2000-2003 David Beckett - http://www.dajobe.org/ |
| 6 | # Copyright (C) 2000-2003 University of Bristol - http://www.bristol.ac.uk/ |
| 7 | # |
| 8 | # This package is Free Software and part of Redland http://librdf.org/ |
| 9 | # |
| 10 | # It is licensed under the following three licenses as alternatives: |
| 11 | # 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version |
| 12 | # 2. GNU General Public License (GPL) V2 or any newer version |
| 13 | # 3. Apache License, V2.0 or any newer version |
| 14 | # |
| 15 | # You may not use this file except in compliance with at least one of |
| 16 | # the above three licenses. |
| 17 | # |
| 18 | # See LICENSE.html or LICENSE.txt at the top of this package for the |
| 19 | # full license terms. |
| 20 | # |
| 21 | # |
| 22 | # |
| 23 | |
| 24 | ######################### We start with some black magic to print on failure. |
| 25 | |
| 26 | BEGIN { $| = 1; print "1..7\n"; } |
| 27 | END {print "not ok 1\n" unless $loaded;} |
| 28 | use RDF::Redland; |
| 29 | $loaded = 1; |
| 30 | print "ok 1\n"; |
| 31 | |
| 32 | ######################### End of black magic. |
| 33 | |
| 34 | use strict; |
| 35 | |
| 36 | my $test=2; |
| 37 | |
| 38 | # Test the RDF::Redland module interfaces |
| 39 | |
| 40 | $RDF::Redland::Debug=1 if $ENV{'TEST_VERBOSE'}; |
| 41 | |
| 42 | my $storage=new RDF::Redland::Storage("hashes", "test", "new='yes',hash-type='memory',dir='.'"); |
| 43 | if(!$storage) { |
| 44 | warn "new RDF::Redland::Storage failed\n"; |
| 45 | print "not ok $test\n"; |
| 46 | last; |
| 47 | } |
| 48 | print "ok $test\n"; |
| 49 | $test++; |
| 50 | |
| 51 | my $model=new RDF::Redland::Model($storage, ""); |
| 52 | if(!$model) { |
| 53 | warn "new RDF::Redland::Model failed\n"; |
| 54 | print "not ok $test\n"; |
| 55 | last; |
| 56 | } |
| 57 | print "ok $test\n"; |
| 58 | $test++; |
| 59 | |
| 60 | my $statement=RDF::Redland::Statement->new_from_nodes(RDF::Redland::Node->new_from_uri_string("http://www.dajobe.org/"), |
| 61 | RDF::Redland::Node->new_from_uri_string("http://purl.org/dc/elements/1.1/creator"), |
| 62 | RDF::Redland::Node->new_from_literal("Dave Beckett", "", 0)); |
| 63 | if(!$statement) { |
| 64 | warn "new RDF::Redland::Statement->new_from_nodes failed\n"; |
| 65 | print "not ok $test\n"; |
| 66 | last; |
| 67 | } |
| 68 | print "ok $test\n"; |
| 69 | $test++; |
| 70 | |
| 71 | $model->add_statement($statement); |
| 72 | $statement=undef; |
| 73 | |
| 74 | # Match against an empty statement - find everything |
| 75 | $statement=RDF::Redland::Statement->new_from_nodes(undef,undef,undef); |
| 76 | my $stream=$model->find_statements($statement); |
| 77 | my $failed=0; |
| 78 | while(!$stream->end) { |
| 79 | my $s=$stream->current->as_string; |
| 80 | if(!length $s) { |
| 81 | warn "RDF::Redland::Statement->as_string failed\n"; |
| 82 | print "not ok $test\n"; |
| 83 | $failed=1; |
| 84 | last; |
| 85 | } |
| 86 | warn "found statement: $s\n" if $RDF::Redland::Debug; |
| 87 | $stream->next; |
| 88 | } |
| 89 | last if $failed; |
| 90 | |
| 91 | my $source_node=RDF::Redland::Node->new_from_uri_string("http://www.dajobe.org/"); |
| 92 | my $target_node=RDF::Redland::Node->new_from_uri_string("http://purl.org/dc/elements/1.1/creator"); |
| 93 | |
| 94 | my $iterator=$model->targets_iterator($source_node,$target_node); |
| 95 | $failed=0; |
| 96 | while(!$iterator->end) { |
| 97 | my $n=$iterator->current->as_string; |
| 98 | if(!length $n) { |
| 99 | warn "RDF::Redland::Node->as_string failed\n"; |
| 100 | print "not ok $test\n"; |
| 101 | $failed=1; |
| 102 | last; |
| 103 | } |
| 104 | warn "found node: $n\n" if $RDF::Redland::Debug; |
| 105 | $iterator->next; |
| 106 | } |
| 107 | $iterator=undef; |
| 108 | $source_node=undef; |
| 109 | $target_node=undef; |
| 110 | last if $failed; |
| 111 | |
| 112 | |
| 113 | my $sub=RDF::Redland::Node->new_from_uri_string("http://example.org/subject"); |
| 114 | my $pred=RDF::Redland::Node->new_from_uri_string("http://example.org/predicate"); |
| 115 | my $dt_uri=new RDF::Redland::URI("http://example.org/datatype"); |
| 116 | $model->add_typed_literal_statement($sub, $pred, |
| 117 | "Literal content", "en-GB", $dt_uri); |
| 118 | $sub=undef; |
| 119 | $pred=undef; |
| 120 | |
| 121 | print "ok $test\n"; |
| 122 | $test++; |
| 123 | |
| 124 | |
| 125 | my $serializer=new RDF::Redland::Serializer(); |
| 126 | $serializer->serialize_model_to_file("test-out.rdf", undef, $model); |
| 127 | |
| 128 | print "ok $test\n"; |
| 129 | $test++; |
| 130 | |
| 131 | |
| 132 | # This happens automatically at the end of scope, but can be forced. |
| 133 | # However the stream must be closed before the object that generated |
| 134 | # it is destroyed. |
| 135 | $stream=undef; |
| 136 | |
| 137 | # These happen automatically but can be forced; model always has |
| 138 | # to be closed first, so it keeps a reference to storage around - sneaky! |
| 139 | $storage=undef; |
| 140 | $model=undef; |
| 141 | |
| 142 | print "ok $test\n"; |
| 143 | $test++; |
| 144 | |
| 145 | exit 0; |