001: package org.openrdf.query.algebra.evaluation.impl;
002:
003: import junit.framework.TestCase;
004:
005: import org.openrdf.model.BNode;
006: import org.openrdf.model.Literal;
007: import org.openrdf.model.URI;
008: import org.openrdf.model.ValueFactory;
009: import org.openrdf.query.QueryLanguage;
010: import org.openrdf.query.TupleQuery;
011: import org.openrdf.query.TupleQueryResult;
012: import org.openrdf.repository.Repository;
013: import org.openrdf.repository.RepositoryConnection;
014: import org.openrdf.repository.RepositoryException;
015: import org.openrdf.repository.sail.SailRepository;
016: import org.openrdf.sail.memory.MemoryStore;
017:
018: public class SparqlRegexTest extends TestCase {
019: public String queryInline = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
020: + "SELECT ?name ?mbox\n"
021: + " WHERE { ?x foaf:name ?name ;\n"
022: + " foaf:mbox ?mbox .\n"
023: + " FILTER regex(str(?mbox), \"@Work.example\", \"i\") }";
024:
025: public String queryBinding = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
026: + "SELECT ?name ?mbox\n"
027: + " WHERE { ?x foaf:name ?name ;\n"
028: + " foaf:mbox ?mbox .\n"
029: + " FILTER regex(str(?mbox), ?pattern) }";
030:
031: public String queryBindingFlags = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
032: + "SELECT ?name ?mbox\n"
033: + " WHERE { ?x foaf:name ?name ;\n"
034: + " foaf:mbox ?mbox .\n"
035: + " FILTER regex(str(?mbox), ?pattern, ?flags) }";
036:
037: public String queryExpr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
038: + "SELECT ?name ?mbox\n"
039: + " WHERE { ?x foaf:name ?name ;\n"
040: + " foaf:mbox ?mbox .\n"
041: + " ?y <http://example.org/ns#pattern> ?pattern .\n"
042: + " ?y <http://example.org/ns#flags> ?flags .\n"
043: + " FILTER regex(str(?mbox), ?pattern, ?flags) }";
044:
045: private Repository repository;
046:
047: private RepositoryConnection conn;
048:
049: private ValueFactory vf;
050:
051: private Literal hunt;
052:
053: public void testInline() throws Exception {
054: TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL,
055: queryInline);
056: TupleQueryResult result = query.evaluate();
057: assertEquals(hunt, result.next().getValue("name"));
058: assertFalse(result.hasNext());
059: result.close();
060: }
061:
062: public void testBinding() throws Exception {
063: TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL,
064: queryBinding);
065: query.setBinding("pattern", vf.createLiteral("@work.example"));
066: TupleQueryResult result = query.evaluate();
067: assertEquals(hunt, result.next().getValue("name"));
068: assertFalse(result.hasNext());
069: result.close();
070: }
071:
072: public void testBindingFlags() throws Exception {
073: TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL,
074: queryBindingFlags);
075: query.setBinding("pattern", vf.createLiteral("@Work.example"));
076: query.setBinding("flags", vf.createLiteral("i"));
077: TupleQueryResult result = query.evaluate();
078: assertEquals(hunt, result.next().getValue("name"));
079: assertFalse(result.hasNext());
080: result.close();
081: }
082:
083: public void testExpr() throws Exception {
084: URI pattern = vf.createURI("http://example.org/ns#", "pattern");
085: URI flags = vf.createURI("http://example.org/ns#", "flags");
086: BNode bnode = vf.createBNode();
087: conn.add(bnode, pattern, vf.createLiteral("@Work.example"));
088: conn.add(bnode, flags, vf.createLiteral("i"));
089: TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL,
090: queryExpr);
091: TupleQueryResult result = query.evaluate();
092: assertEquals(hunt, result.next().getValue("name"));
093: assertFalse(result.hasNext());
094: result.close();
095: }
096:
097: @Override
098: protected void setUp() throws Exception {
099: repository = new SailRepository(new MemoryStore());
100: repository.initialize();
101: vf = repository.getValueFactory();
102: hunt = vf.createLiteral("James Leigh Hunt");
103: createUser("james", "James Leigh", "james@leigh");
104: createUser("megan", "Megan Leigh", "megan@leigh");
105: createUser("hunt", "James Leigh Hunt", "james@work.example");
106: conn = repository.getConnection();
107: }
108:
109: @Override
110: protected void tearDown() throws Exception {
111: conn.close();
112: repository.shutDown();
113: }
114:
115: private void createUser(String id, String name, String email)
116: throws RepositoryException {
117: RepositoryConnection conn = repository.getConnection();
118: URI subj = vf.createURI("http://example.org/ns#", id);
119: URI foafName = vf.createURI("http://xmlns.com/foaf/0.1/",
120: "name");
121: URI foafMbox = vf.createURI("http://xmlns.com/foaf/0.1/",
122: "mbox");
123: conn.add(subj, foafName, vf.createLiteral(name));
124: conn.add(subj, foafMbox, vf.createURI("mailto:", email));
125: conn.close();
126: }
127: }
|