KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > test > TestSimpleSelector


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestSimpleSelector.java,v 1.6 2005/03/17 10:12:12 chris-dollin Exp $
5 */

6
7 package com.hp.hpl.jena.db.test;
8
9 /**
10  *
11  * This tests basic selection operations on the modelRDB.
12  *
13  * To run, you must have a mySQL database operational on
14  * localhost with a database name of "test" and allow use
15  * by a user named "test" with an empty password.
16  *
17  * (Based on an earlier Jena test by bwm and kers.)
18  *
19  * @author csayers
20 */

21
22 import com.hp.hpl.jena.db.IDBConnection;
23 import com.hp.hpl.jena.db.ModelRDB;
24 import com.hp.hpl.jena.rdf.model.*;
25 import com.hp.hpl.jena.vocabulary.*;
26
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30
31 public class TestSimpleSelector extends TestCase
32     {
33         
34     public TestSimpleSelector( String JavaDoc name )
35         { super( name ); }
36     
37     public static TestSuite suite()
38         { return new TestSuite( TestSimpleSelector.class ); }
39  /*
40     public void assertFalse( String name, boolean b )
41         { assertTrue( name, !b ); } */

42         
43     
44     Model model = null;
45     IDBConnection conn = null;
46        
47     protected void setUp() throws java.lang.Exception JavaDoc {
48         
49         conn = TestConnection.makeAndCleanTestConnection();
50         model = ModelRDB.createModel(conn, TestPackage.M_DB);
51         
52         model.createResource()
53              .addProperty(RDF.type, RDFS.Resource)
54              .addProperty(RDFS.label, "foo")
55              .addProperty(RDF.value, "123");
56         model.createResource()
57              .addProperty(RDF.type, RDFS.Resource)
58              .addProperty(RDFS.label, "bar")
59              .addProperty(RDF.value, "123");
60         
61     }
62     
63     protected void tearDown() throws java.lang.Exception JavaDoc {
64         model.close();
65         model = null;
66         conn.cleanDB();
67         conn.close();
68     }
69     
70     public void testAll() {
71         StmtIterator iter = model.listStatements(
72           new SimpleSelector(null, null, (RDFNode) null));
73         int i =0;
74         while (iter.hasNext()) {
75             i++;
76             iter.next();
77         }
78         assertEquals(6, i);
79     }
80     
81     public void testFindProperty() {
82         StmtIterator iter = model.listStatements(
83           new SimpleSelector(null, RDFS.label, (RDFNode) null));
84         int i =0;
85         while (iter.hasNext()) {
86             i++;
87             Statement stmt = iter.nextStatement();
88             assertEquals(RDFS.label, stmt.getPredicate());
89         }
90         assertEquals(2, i);
91     }
92     
93     public void testFindObject() {
94         StmtIterator iter = model.listStatements(
95           new SimpleSelector(null, null, RDFS.Resource));
96         int i =0;
97         while (iter.hasNext()) {
98             i++;
99             Statement stmt = iter.nextStatement();
100             assertEquals(RDFS.Resource, stmt.getObject());
101         }
102         assertEquals(2, i);
103     }
104     
105     public void testFindSubject() {
106         StmtIterator iter = model.listStatements( new SimpleSelector(null, null, RDFS.Resource));
107         assertTrue( iter.hasNext() );
108         Resource subject = iter.nextStatement().getSubject();
109         iter.close();
110         iter = model.listStatements( new SimpleSelector(subject, null, (RDFNode) null));
111         int i =0;
112         while (iter.hasNext()) {
113             i++;
114             Statement stmt = iter.nextStatement();
115             assertEquals(subject, stmt.getSubject());
116         }
117         assertEquals(3, i);
118     }
119     
120     public void testFindPropertyAndObject() {
121         StmtIterator iter = model.listStatements(
122           new SimpleSelector(null, RDF.value, 123));
123         int i =0;
124         while (iter.hasNext()) {
125             i++;
126             Statement stmt = iter.nextStatement();
127             assertEquals(RDF.value, stmt.getPredicate());
128             assertEquals(123, stmt.getInt());
129         }
130         assertEquals(2, i);
131     }
132     
133 }
134         
135
136 /*
137     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
138     All rights reserved.
139
140     Redistribution and use in source and binary forms, with or without
141     modification, are permitted provided that the following conditions
142     are met:
143
144     1. Redistributions of source code must retain the above copyright
145        notice, this list of conditions and the following disclaimer.
146
147     2. Redistributions in binary form must reproduce the above copyright
148        notice, this list of conditions and the following disclaimer in the
149        documentation and/or other materials provided with the distribution.
150
151     3. The name of the author may not be used to endorse or promote products
152        derived from this software without specific prior written permission.
153
154     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
155     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
156     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
157     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
158     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
159     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
160     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
161     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
162     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
163     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
164 */

165
Popular Tags