1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
package parser
import (
"bufio"
"bytes"
xmlreader "github.com/spdx/gordf/rdfloader/xmlreader"
"io"
"testing"
)
func xmlreaderFromString(fileContent string) xmlreader.XMLReader {
return xmlreader.XMLReaderFromFileObject(bufio.NewReader(io.Reader(bytes.NewReader([]byte(fileContent)))))
}
func TestTriple_Hash(t *testing.T) {
testTriple := Triple{
Subject: &Node{BLANK, ""},
Predicate: &Node{BLANK, ""},
Object: &Node{BLANK, ""},
}
expectedHash := "{(BNODE, ); (BNODE, ); (BNODE, )}"
if expectedHash != testTriple.Hash() {
t.Errorf("expected %v, found %v", expectedHash, testTriple.Hash())
}
}
func TestNew(t *testing.T) {
// testing if the initialized parameters are okay.
newParser := New()
// Triples should be initially empty
if len(newParser.Triples) > 0 {
t.Errorf("Initialized parser shouldn't have any triple")
}
}
func TestParser_Parse(t *testing.T) {
// TestCase 1: only root tag in the document.
func() {
emptyValidRDF := `
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
</rdf:RDF>`
rdfParser := New()
xmlReader := xmlreaderFromString(emptyValidRDF)
rootBlock, err := xmlReader.Read()
if err != nil {
return
}
err = rdfParser.Parse(rootBlock)
// there shouldn't be any error parsing the content
if err != nil {
t.Errorf("unexpected error parsing the document: %v", err)
}
// there shouldn't be any triple in the parsed document.
if len(rdfParser.Triples) != 0 {
t.Errorf("empty document must have no triples. Found %v", rdfParser.Triples)
}
}()
// TestCase 2:
// empty rdf with prolog
func() {
emptyRDFWithProlog := `<? xml version="1.0" ?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
</rdf:RDF>`
xmlReader := xmlreaderFromString(emptyRDFWithProlog)
rootBlock, err := xmlReader.Read()
if err != nil {
return
}
rdfParser := New()
err = rdfParser.Parse(rootBlock)
// there shouldn't be any error parsing the content
if err != nil {
t.Errorf("unexpected error parsing the document: %v", err)
}
// there shouldn't be any triple in the parsed document and
// the prolog is not counted in the triples
if len(rdfParser.Triples) != 0 {
t.Errorf("empty document must have no triples. Found %v", rdfParser.Triples)
}
}()
// TestCase 3:
// Invalid RDF with stray characters before closing tag.
func() {
invalidRDF := "......<rdf:RDF>"
xmlReader := xmlreaderFromString(invalidRDF)
rootBlock, err := xmlReader.Read()
if err != nil {
return
}
rdfParser := New()
err = rdfParser.Parse(rootBlock)
if err == nil {
t.Errorf("should've raised an error stating stray characters found")
}
}()
// TestCase 4:
// Valid RDF with single valid triple.
func() {
twoTripleRDF := `
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:example="https://www.sample.com/example">
<rdf:Description>
<example:Tag> Name </example:Tag>
</rdf:Description>
</rdf:RDF>`
xmlReader := xmlreaderFromString(twoTripleRDF)
rootBlock, err := xmlReader.Read()
if err != nil {
return
}
rdfParser := New()
err = rdfParser.Parse(rootBlock)
if err != nil {
t.Errorf("error parsing a valid rdf file. Error: %v", err)
}
if len(rdfParser.Triples) != 2 {
t.Errorf("expected rdfParser to have exactly two triples. %v triples found", len(rdfParser.Triples))
}
}()
// TestCase 5: extra tag in the rdf.
func() {
extraTagRDF := `
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:example="https://www.sample.com/example">
<rdf:Description>
<example:Tag> Name </example:Tag>
</rdf:Description>
<example:extraTag>
</rdf:RDF>`
xmlReader := xmlreaderFromString(extraTagRDF)
rootBlock, err := xmlReader.Read()
if err != nil {
return
}
rdfParser := New()
err = rdfParser.Parse(rootBlock)
if err == nil {
t.Errorf("expected an EOF error")
}
}()
// TestCase 6: mismatch tag
func() {
invalidRDF := `
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
</rdf:rdf>`
xmlReader := xmlreaderFromString(invalidRDF)
rootBlock, err := xmlReader.Read()
if err != nil {
return
}
rdfParser := New()
err = rdfParser.Parse(rootBlock)
if err == nil {
t.Errorf("expected an error stating opening and closing tags are not same")
}
}()
}
func Test_parseHeaderBlock(t *testing.T) {
// parseHeaderBlock returns all the schema definitions in the input rootBlock.
// TestCase 1: empty root block without any children or attributes.
// should return a map with only rdf namespace declaration
rootBlock := xmlreader.Block{
OpeningTag: xmlreader.Tag{
SchemaName: "",
Name: "",
Attrs: nil,
},
Value: "",
Children: nil,
}
schemaDefinition, err := parseHeaderBlock(rootBlock)
if err != nil {
t.Errorf("unexpected error : %v", err)
return
}
if n := len(schemaDefinition); n != 1 {
t.Errorf("expected schemaDefinition to have exactly one default namespace declaration of rdf. found %d declarations", n)
}
// check if the "rdf" uri is correct
ref := schemaDefinition["rdf"]
if ref.String() != RDFNS {
t.Errorf("default schema uri for rdf should be %s, found %s", RDFNS, ref.String())
}
// TestCase 2: root block with invalid schema name attribute and no children
rootBlock = xmlreader.Block{
OpeningTag: xmlreader.Tag{
SchemaName: "",
Name: "",
Attrs: []xmlreader.Attribute{
{
Name: "rdf",
SchemaName: "xmlns",
Value: "invalid uri",
},
},
},
Value: "",
Children: nil,
}
_, err = parseHeaderBlock(rootBlock)
if err == nil {
t.Error("should've raised an error saying invalid uri received")
}
// TestCase 3: redefined "rdf" attribute must not be over-written by the default namespace.
newRDFNS := "https://www.sample.com/rdf#"
rootBlock = xmlreader.Block{
OpeningTag: xmlreader.Tag{
SchemaName: "",
Name: "",
Attrs: []xmlreader.Attribute{
{
Name: "rdf",
SchemaName: "xmlns",
Value: newRDFNS,
},
},
},
Value: "",
Children: nil,
}
schemaDefinition, err = parseHeaderBlock(rootBlock)
uriRef := schemaDefinition["rdf"]
if uriRef.String() != newRDFNS {
t.Errorf("expected uri: %s, found %s", newRDFNS, uriRef.String())
}
// TestCase 3: Valid Case: exactly one namespace declaration which is not rdf.
doapNS := `http://usefulinc.com/ns/doap#`
rootBlock = xmlreader.Block{
OpeningTag: xmlreader.Tag{
SchemaName: "",
Name: "",
Attrs: []xmlreader.Attribute{
{
Name: "doap",
SchemaName: "xmlns",
Value: doapNS,
},
},
},
Value: "",
Children: nil,
}
schemaDefinition, err = parseHeaderBlock(rootBlock)
if len(schemaDefinition) != 2 {
t.Errorf("after parsing rootblock with one namespace " +
"declaration(not xmlns:rdf), the schema-definition " +
"should've two elements")
}
rdfURI := schemaDefinition["rdf"]
doapURI := schemaDefinition["doap"]
if rdfURI.String() != RDFNS || doapURI.String() != doapNS {
t.Errorf("invalid schema definition: %v", schemaDefinition)
}
}
|