| 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 { $|scip-perl cpan perl-project 0.0.0 main/`$|`. = 1; printscip-perl cpan perl-project 0.0.0 main/print(). "1..7\n"; } |
| 27 | END {printscip-perl cpan perl-project 0.0.0 main/print(). "not ok 1\n" unless $loadedscip-perl cpan perl-project 0.0.0 main/$loaded.;} |
| 28 | use RDF::Redlandscip-perl cpan perl-project 0.0.0 RDF/Redland/; |
| 29 | $loadedscip-perl cpan perl-project 0.0.0 main/$loaded. = 1; |
| 30 | printscip-perl cpan perl-project 0.0.0 main/print(). "ok 1\n"; |
| 31 | |
| 32 | ######################### End of black magic. |
| 33 | |
| 34 | use strictscip-perl cpan perl-project 0.0.0 strict/; |
| 35 | |
| 36 | my $test=2; |
| 37 | |
| 38 | # Test the RDF::Redland module interfaces |
| 39 | |
| 40 | $RDF::Redland::Debugscip-perl cpan perl-project 0.0.0 RDF/Redland/$Debug.=1 if $ENVscip-perl cpan perl-project 0.0.0 main/`%ENV`.{'TEST_VERBOSE'}; |
| 41 | |
| 42 | my $storage=new RDF::Redland::Storage("hashes", "test", "new='yes',hash-type='memory',dir='.'"); |
| 43 | if(!$storage) { |
| 44 | warnscip-perl cpan perl-project 0.0.0 main/warn(). "new RDF::Redland::Storage failed\n"; |
| 45 | printscip-perl cpan perl-project 0.0.0 main/print(). "not ok $test\n"; |
| 46 | last; |
| 47 | } |
| 48 | printscip-perl cpan perl-project 0.0.0 main/print(). "ok $test\n"; |
| 49 | $test++; |
| 50 | |
| 51 | my $model=new RDF::Redland::Model($storage, ""); |
| 52 | if(!$model) { |
| 53 | warnscip-perl cpan perl-project 0.0.0 main/warn(). "new RDF::Redland::Model failed\n"; |
| 54 | printscip-perl cpan perl-project 0.0.0 main/print(). "not ok $test\n"; |
| 55 | last; |
| 56 | } |
| 57 | printscip-perl cpan perl-project 0.0.0 main/print(). "ok $test\n"; |
| 58 | $test++; |
| 59 | |
| 60 | my $statement=RDF::Redland::Statement->new_from_nodesscip-perl cpan perl-project 0.0.0 RDF/Redland/Statement/new_from_nodes().(RDF::Redland::Node->new_from_uri_stringscip-perl cpan perl-project 0.0.0 RDF/Redland/Node/new_from_uri_string().("http://www.dajobe.org/"), |
| 61 | RDF::Redland::Node->new_from_uri_stringscip-perl cpan perl-project 0.0.0 RDF/Redland/Node/new_from_uri_string().("http://purl.org/dc/elements/1.1/creator"), |
| 62 | RDF::Redland::Node->new_from_literalscip-perl cpan perl-project 0.0.0 RDF/Redland/Node/new_from_literal().("Dave Beckett", "", 0)); |
| 63 | if(!$statement) { |
| 64 | warnscip-perl cpan perl-project 0.0.0 main/warn(). "new RDF::Redland::Statement->new_from_nodes failed\n"; |
| 65 | printscip-perl cpan perl-project 0.0.0 main/print(). "not ok $test\n"; |
| 66 | last; |
| 67 | } |
| 68 | printscip-perl cpan perl-project 0.0.0 main/print(). "ok $test\n"; |
| 69 | $test++; |
| 70 | |
| 71 | $model->add_statementscip-perl cpan perl-project 0.0.0 main/add_statement().($statement); |
| 72 | $statement=undef; |
| 73 | |
| 74 | # Match against an empty statement - find everything |
| 75 | $statement=RDF::Redland::Statement->new_from_nodesscip-perl cpan perl-project 0.0.0 RDF/Redland/Statement/new_from_nodes().(undef,undef,undef); |
| 76 | my $stream=$model->find_statementsscip-perl cpan perl-project 0.0.0 main/find_statements().($statement); |
| 77 | my $failed=0; |
| 78 | while(!$stream->endscip-perl cpan perl-project 0.0.0 main/end().) { |
| 79 | my $s=$stream->currentscip-perl cpan perl-project 0.0.0 main/current().->as_stringscip-perl cpan perl-project 0.0.0 main/as_string().; |
| 80 | if(!lengthscip-perl cpan perl-project 0.0.0 main/length(). $s) { |
| 81 | warnscip-perl cpan perl-project 0.0.0 main/warn(). "RDF::Redland::Statement->as_string failed\n"; |
| 82 | printscip-perl cpan perl-project 0.0.0 main/print(). "not ok $test\n"; |
| 83 | $failed=1; |
| 84 | last; |
| 85 | } |
| 86 | warnscip-perl cpan perl-project 0.0.0 main/warn(). "found statement: $s\n" if $RDF::Redland::Debugscip-perl cpan perl-project 0.0.0 RDF/Redland/$Debug.; |
| 87 | $stream->nextscip-perl cpan perl-project 0.0.0 main/next().; |
| 88 | } |
| 89 | last if $failed; |
| 90 | |
| 91 | my $source_node=RDF::Redland::Node->new_from_uri_stringscip-perl cpan perl-project 0.0.0 RDF/Redland/Node/new_from_uri_string().("http://www.dajobe.org/"); |
| 92 | my $target_node=RDF::Redland::Node->new_from_uri_stringscip-perl cpan perl-project 0.0.0 RDF/Redland/Node/new_from_uri_string().("http://purl.org/dc/elements/1.1/creator"); |
| 93 | |
| 94 | my $iterator=$model->targets_iteratorscip-perl cpan perl-project 0.0.0 main/targets_iterator().($source_node,$target_node); |
| 95 | $failed=0; |
| 96 | while(!$iterator->endscip-perl cpan perl-project 0.0.0 main/end().) { |
| 97 | my $n=$iterator->currentscip-perl cpan perl-project 0.0.0 main/current().->as_stringscip-perl cpan perl-project 0.0.0 main/as_string().; |
| 98 | if(!lengthscip-perl cpan perl-project 0.0.0 main/length(). $n) { |
| 99 | warnscip-perl cpan perl-project 0.0.0 main/warn(). "RDF::Redland::Node->as_string failed\n"; |
| 100 | printscip-perl cpan perl-project 0.0.0 main/print(). "not ok $test\n"; |
| 101 | $failed=1; |
| 102 | last; |
| 103 | } |
| 104 | warnscip-perl cpan perl-project 0.0.0 main/warn(). "found node: $n\n" if $RDF::Redland::Debugscip-perl cpan perl-project 0.0.0 RDF/Redland/$Debug.; |
| 105 | $iterator->nextscip-perl cpan perl-project 0.0.0 main/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_stringscip-perl cpan perl-project 0.0.0 RDF/Redland/Node/new_from_uri_string().("http://example.org/subject"); |
| 114 | my $pred=RDF::Redland::Node->new_from_uri_stringscip-perl cpan perl-project 0.0.0 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_statementscip-perl cpan perl-project 0.0.0 main/add_typed_literal_statement().($sub, $pred, |
| 117 | "Literal content", "en-GB", $dt_uri); |
| 118 | $sub=undef; |
| 119 | $pred=undef; |
| 120 | |
| 121 | printscip-perl cpan perl-project 0.0.0 main/print(). "ok $test\n"; |
| 122 | $test++; |
| 123 | |
| 124 | |
| 125 | my $serializer=new RDF::Redland::Serializer(); |
| 126 | $serializer->serialize_model_to_filescip-perl cpan perl-project 0.0.0 main/serialize_model_to_file().("test-out.rdf", undef, $model); |
| 127 | |
| 128 | printscip-perl cpan perl-project 0.0.0 main/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 | printscip-perl cpan perl-project 0.0.0 main/print(). "ok $test\n"; |
| 143 | $test++; |
| 144 | |
| 145 | exitscip-perl cpan perl-project 0.0.0 main/exit(). 0; |