KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > test > TestSimpleListStatements


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestSimpleListStatements.java,v 1.12 2005/03/14 16:01:54 chris-dollin Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model.test;
8
9 /**
10     @author bwm out of kers
11 */

12
13 import com.hp.hpl.jena.rdf.model.*;
14 import com.hp.hpl.jena.vocabulary.*;
15
16
17 import junit.framework.*;
18
19 public class TestSimpleListStatements extends ModelTestBase
20     {
21         
22     public TestSimpleListStatements( String JavaDoc name )
23         { super( name ); }
24     
25     public static TestSuite suite()
26         { return new TestSuite( TestSimpleListStatements.class ); }
27     
28     Model model = null;
29     
30     static boolean booleanValue = true;
31     static char charValue = 'c';
32     static long longValue = 456;
33     static float floatValue = 5.67F;
34     static double doubleValue = 6.78;
35     static String JavaDoc stringValue ="stringValue";
36     static String JavaDoc langValue = "en";
37         
38     protected void setUp() throws java.lang.Exception JavaDoc {
39         
40         model = ModelFactory.createDefaultModel();
41         model.createResource("http://example.org/boolean")
42              .addProperty(RDF.value, booleanValue);
43         model.createResource("http://example.org/char")
44              .addProperty(RDF.value, charValue);
45         model.createResource("http://example.org/long")
46              .addProperty(RDF.value, longValue);
47         model.createResource("http://example.org/float")
48              .addProperty(RDF.value, floatValue);
49         model.createResource("http://example.org/double")
50              .addProperty(RDF.value, doubleValue);
51         model.createResource("http://example.org/string")
52              .addProperty(RDF.value, stringValue);
53         model.createResource("http://example.org/langString")
54              .addProperty(RDF.value, stringValue, langValue);
55         
56     }
57     
58     protected void tearDown() throws java.lang.Exception JavaDoc {
59         model.close();
60         model = null;
61     }
62     
63     public void testBoolean() {
64         StmtIterator iter = model.listStatements(null, null, booleanValue);
65         int i =0;
66         while (iter.hasNext()) {
67             i++;
68             assertEquals(iter.nextStatement().getSubject().getURI(),
69                          "http://example.org/boolean");
70         }
71         assertEquals(1, i);
72     }
73     
74     public void testChar() {
75         StmtIterator iter = model.listStatements(null, null, charValue);
76         int i =0;
77         while (iter.hasNext()) {
78             i++;
79             assertEquals(iter.nextStatement().getSubject().getURI(),
80                          "http://example.org/char");
81         }
82         assertEquals(1, i);
83     }
84     
85     public void testLong() {
86         StmtIterator iter = model.listStatements(null, null, longValue);
87         int i =0;
88         while (iter.hasNext()) {
89             i++;
90             assertEquals(iter.nextStatement().getSubject().getURI(),
91                          "http://example.org/long");
92         }
93         assertEquals(1, i);
94     }
95     
96     public void testFloat() {
97         StmtIterator iter = model.listStatements(null, null, floatValue);
98         int i =0;
99         while (iter.hasNext()) {
100             i++;
101             assertEquals(iter.nextStatement().getSubject().getURI(),
102                          "http://example.org/float");
103         }
104         assertEquals(1, i);
105     }
106     
107     public void testDouble() {
108         StmtIterator iter = model.listStatements(null, null, doubleValue);
109         int i =0;
110         while (iter.hasNext()) {
111             i++;
112             assertEquals(iter.nextStatement().getSubject().getURI(),
113                          "http://example.org/double");
114         }
115         assertEquals(1, i);
116     }
117     
118     public void testString() {
119         StmtIterator iter = model.listStatements(null, null, stringValue);
120         int i =0;
121         while (iter.hasNext()) {
122             i++;
123             assertEquals(iter.nextStatement().getSubject().getURI(),
124                          "http://example.org/string");
125         }
126         assertEquals(1, i);
127     }
128     
129     public void testLangString() {
130         StmtIterator iter = model.listStatements(null, null,
131                                                            stringValue, langValue);
132         int i =0;
133         while (iter.hasNext()) {
134             i++;
135             assertEquals(iter.nextStatement().getSubject().getURI(),
136                          "http://example.org/langString");
137         }
138         assertEquals(1, i);
139     }
140         
141     
142     public void testAll() {
143         StmtIterator iter = model.listStatements(null, null, (RDFNode) null);
144         int i =0;
145         while (iter.hasNext()) {
146             i++;
147             iter.next();
148         }
149         assertEquals(7, i);
150     }
151
152
153     public Model modelWithStatements( StmtIterator it )
154         {
155         Model m = ModelFactory.createDefaultModel();
156         while (it.hasNext()) m.add( it.nextStatement() );
157         return m;
158         }
159         
160     public void checkReturns( String JavaDoc things, StmtIterator it )
161         {
162         Model wanted = modelWithStatements( things );
163         Model got = modelWithStatements( it );
164         if (wanted.isIsomorphicWith( got ) == false)
165             fail( "wanted " + wanted + " got " + got );
166         }
167         
168     public void testListStatementsSPO()
169         {
170         Model m = ModelFactory.createDefaultModel();
171         Resource A = resource( m, "A" ), X = resource( m, "X" );
172         Property P = property( m, "P" ), P1 = property( m, "P1" );
173         RDFNode O = resource( m, "O" ), Y = resource( m, "Y" );
174         String JavaDoc S1 = "S P O; S1 P O; S2 P O";
175         String JavaDoc S2 = "A P1 B; A P1 B; A P1 C";
176         String JavaDoc S3 = "X P1 Y; X P2 Y; X P3 Y";
177         modelAdd( m, S1 );
178         modelAdd( m, S2 );
179         modelAdd( m, S3 );
180         checkReturns( S1, m.listStatements( null, P, O ) );
181         checkReturns( S2, m.listStatements( A, P1, (RDFNode) null ) );
182         checkReturns( S3, m.listStatements( X, null, Y ) );
183         m.close();
184         }
185         
186     public void testListStatementsClever()
187         {
188         Model m = ModelFactory.createDefaultModel();
189         modelAdd( m, "S P O; S P O2; S P2 O; S2 P O" );
190         Selector sel = new SimpleSelector( null, null, (RDFNode) null )
191             {
192             public boolean test( Statement st )
193                 { return
194                         st.getSubject().toString().length()
195                         + st.getPredicate().toString().length()
196                         + st.getObject().toString().length()
197                         == 15; /* eh:/S + eh:/P + eh:/O */
198                 }
199                 
200             public boolean isSimple()
201                 { return false; }
202             };
203         checkReturns( "S P O", m.listStatements( sel ) );
204         }
205 }
206         
207
208 /*
209     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
210     All rights reserved.
211
212     Redistribution and use in source and binary forms, with or without
213     modification, are permitted provided that the following conditions
214     are met:
215
216     1. Redistributions of source code must retain the above copyright
217        notice, this list of conditions and the following disclaimer.
218
219     2. Redistributions in binary form must reproduce the above copyright
220        notice, this list of conditions and the following disclaimer in the
221        documentation and/or other materials provided with the distribution.
222
223     3. The name of the author may not be used to endorse or promote products
224        derived from this software without specific prior written permission.
225
226     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
227     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
228     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
229     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
230     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
231     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
233     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
234     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
235     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
236 */

237
Popular Tags