KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > test > TestTriple


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

6
7 package com.hp.hpl.jena.graph.test;
8
9 /**
10     @author bwm out of kers, then kers updates
11 */

12
13 import com.hp.hpl.jena.graph.*;
14 import com.hp.hpl.jena.graph.impl.LiteralLabel;
15 import com.hp.hpl.jena.rdf.model.AnonId;
16 import com.hp.hpl.jena.shared.*;
17
18 import junit.framework.*;
19
20 public class TestTriple extends GraphTestBase
21     {
22         
23     public TestTriple(String JavaDoc name)
24         { super( name ); }
25     
26     public static TestSuite suite()
27         { return new TestSuite( TestTriple.class ); }
28                 
29     private static final String JavaDoc U = "http://some.domain.name/magic/spells.incant";
30     private static final String JavaDoc N = "Alice";
31     private static final LiteralLabel L = new LiteralLabel( "ashes are burning", "en", false );
32         
33     public void testTripleEquals() {
34         try {
35             Node.cache(false);
36             
37             // create some nodes to test
38
AnonId id = new AnonId();
39             LiteralLabel L2 = new LiteralLabel(id.toString(), "", false);
40             String JavaDoc U2 = id.toString();
41             String JavaDoc N2 = id.toString();
42             
43             Node[] nodes = new Node[] {
44               Node.ANY,
45               Node.createAnon(id), Node.createAnon(),
46               Node.createLiteral(L), Node.createLiteral(L2),
47               Node.createURI(U), Node.createURI(U2),
48               Node.createVariable(N), Node.createVariable(N2)
49             };
50             
51             Triple[] triples =
52                new Triple [nodes.length * nodes.length * nodes.length];
53             for (int i=0; i<nodes.length; i++) {
54                 for (int j=0; j<nodes.length; j++) {
55                     for (int k=0; k<nodes.length; k++) {
56                         triples[i*nodes.length*nodes.length +
57                                 j*nodes.length +
58                                 k] = new Triple(nodes[i], nodes[j], nodes[k]);
59                     }
60                 }
61             }
62             
63             // set up the expected results matrix
64
// a expected[i][j] is true if triples[i] equals triples[j]
65
// triples are expected to be equals if there components are equal
66
boolean[][] expected = new boolean[triples.length][triples.length];
67             for (int i1=0; i1<nodes.length; i1++) {
68                 for (int j1=0; j1<nodes.length; j1++) {
69                     for (int k1=0; k1<nodes.length; k1++) {
70                         for (int i2=0; i2<nodes.length; i2++) {
71                             for (int j2=0; j2<nodes.length; j2++) {
72                                 for (int k2=0; k2<nodes.length; k2++) {
73                                     expected[i1*nodes.length*nodes.length +
74                                              j1*nodes.length +
75                                              k1]
76                                             [i2*nodes.length*nodes.length +
77                                              j2*nodes.length +
78                                              k2] =
79                                              nodes[i1].equals(nodes[i2]) &&
80                                              nodes[j1].equals(nodes[j2]) &&
81                                              nodes[k1].equals(nodes[k2]);
82                                 }
83                             }
84                         }
85                     }
86                 }
87             }
88
89             assertEquals("triple, null", triples[0].equals(null), false);
90             assertDiffer("triple, string", triples[0], "string");
91             
92             // now compare each triple with each other triple
93
for (int i=0; i<triples.length; i++) {
94                 for (int j=0; j<triples.length; j++) {
95                     if (expected[i][j]) {
96                         assertEquals("triples " + i + ", " + j ,
97                                                        triples[i], triples[j]);
98                     } else {
99                         assertDiffer("triples" + i + ", " + j,
100                                                        triples[i], triples[j]);
101                     }
102                 }
103             }
104                 
105         } finally {
106             Node.cache(true);
107         }
108     }
109     
110     public void testTripleCreate()
111         {
112         Node S = Node.create( "s" ), P = Node.create( "p" ), O = Node.create( "o" );
113         assertEquals( new Triple( S, P, O ), Triple.create( S, P, O ) );
114         }
115         
116     public void testTripleCreateFromString()
117         {
118         Node S = Node.create( "a" ), P = Node.create( "_P" ), O = Node.create( "?c" );
119         assertEquals( new Triple( S, P, O ), Triple.create( "a _P ?c") );
120         }
121         
122     /**
123         Test that triple-creation respects prefixes, assuming that node creation
124         does.
125     */

126     public void testTriplePrefixes()
127         {
128         Node S = Node.create( "rdf:alpha" ), P = Node.create( "dc:creator" );
129         Node O = Node.create( "spoo:notmapped" );
130         Triple t = Triple.create( "rdf:alpha dc:creator spoo:notmapped" );
131         assertEquals( new Triple( S, P, O ), t );
132         }
133         
134     public void testTripleCreationMapped()
135         {
136         PrefixMapping pm = PrefixMapping.Factory.create()
137             .setNsPrefix( "a", "ftp://foo/" )
138             .setNsPrefix( "b", "http://spoo/" )
139             ;
140         Triple wanted = Triple.create( "ftp://foo/x http://spoo/y c:z" );
141         Triple got = Triple.create( pm, "a:x b:y c:z" );
142         assertEquals( wanted, got );
143         }
144         
145     public void testPlainTripleMatches()
146         {
147         testMatches( "S P O" );
148         testMatches( "_S _P _O" );
149         testMatches( "1 2 3" );
150         }
151         
152     public void testAnyTripleMatches()
153         {
154         testMatches( "?? P O", "Z P O" );
155         testMatches( "S ?? O", "S Q O" );
156         testMatches( "S P ??", "S P oh" );
157         testMatches( "?? ?? ??", "X Y Z" );
158         testMatches( "?? ?? ??", "X Y 1" );
159         testMatches( "?? ?? ??", "_X Y Z" );
160         testMatches( "?? ?? ??", "X _Y Z" );
161         }
162         
163     private void testMatches( String JavaDoc triple )
164         { testMatches( triple, triple ); }
165         
166     private void testMatches( String JavaDoc pattern, String JavaDoc triple )
167         { assertTrue( Triple.create( pattern ).matches( Triple.create( triple ) ) ); }
168         
169     public void testPlainTripleDoesntMatch()
170         {
171         testMatchFails( "S P O", "Z P O" );
172         testMatchFails( "S P O", "S Q O" );
173         testMatchFails( "S P O", "S P oh" );
174         }
175         
176     public void testAnyTripleDoesntMatch()
177         {
178         testMatchFails( "?? P O", "S P oh" );
179         testMatchFails( "S ?? O", "Z R O" );
180         testMatchFails( "S P ??", "Z P oh" );
181         }
182         
183     public void testMatchFails( String JavaDoc pattern, String JavaDoc triple )
184         { assertFalse( Triple.create( pattern ).matches( Triple.create( triple ) ) ); }
185         
186     public void testMatchesNodes()
187         {
188         assertTrue( Triple.create( "S P O" ).matches( node("S" ), node( "P" ), node( "O" ) ) );
189         assertTrue( Triple.create( "?? P O" ).matches( node("Z" ), node( "P" ), node( "O" ) ) );
190         assertTrue( Triple.create( "S ?? O" ).matches( node("S" ), node( "Q" ), node( "O" ) ) );
191         assertTrue( Triple.create( "S P ??" ).matches( node("S" ), node( "P" ), node( "I" ) ) );
192     /* */
193         assertFalse( Triple.create( "S P O" ).matches( node("Z" ), node( "P" ), node( "O" ) ) );
194         assertFalse( Triple.create( "S P O" ).matches( node("S" ), node( "Q" ), node( "O" ) ) );
195         assertFalse( Triple.create( "S P O" ).matches( node("Z" ), node( "P" ), node( "I" ) ) );
196         }
197         
198     public void testElementMatches()
199         {
200         assertTrue( Triple.create( "S P O" ).subjectMatches( node( "S" ) ) );
201         assertTrue( Triple.create( "S P O" ).predicateMatches( node( "P" ) ) );
202         assertTrue( Triple.create( "S P O" ).objectMatches( node( "O" ) ) );
203     /* */
204         assertFalse( Triple.create( "S P O" ).subjectMatches( node( "Z" ) ) );
205         assertFalse( Triple.create( "S P O" ).predicateMatches( node( "Q" ) ) );
206         assertFalse( Triple.create( "S P O" ).objectMatches( node( "I" ) ) );
207     /* */
208         assertTrue( Triple.create( "?? P O" ).subjectMatches( node( "SUB" ) ) );
209         assertTrue( Triple.create( "S ?? O" ).predicateMatches( node( "PRED" ) ) );
210         assertTrue( Triple.create( "S P ??" ).objectMatches( node( "OBJ" ) ) );
211         }
212         
213     public void testConcrete()
214         {
215         assertTrue( Triple.create( "S P O" ).isConcrete() );
216         assertTrue( Triple.create( "S P 11").isConcrete() );
217         assertTrue( Triple.create( "S P _X").isConcrete() );
218         assertTrue( Triple.create( "S _P 11").isConcrete() );
219         assertTrue( Triple.create( "_S _P _O").isConcrete() );
220         assertTrue( Triple.create( "10 11 12").isConcrete() );
221         assertTrue( Triple.create( "S P 11").isConcrete() );
222         assertFalse( Triple.create( "?? P 11").isConcrete() );
223         assertFalse( Triple.create( "S ?? 11").isConcrete() );
224         assertFalse( Triple.create( "S P ??").isConcrete() );
225         assertFalse( Triple.create( "?S P 11").isConcrete() );
226         assertFalse( Triple.create( "S ?P 11").isConcrete() );
227         assertFalse( Triple.create( "S P ?O").isConcrete() );
228         }
229         
230     /**
231         Primarily to make sure that literals get quoted and stuff comes out in some
232         kind of coherent order.
233     */

234     public void testTripleToStringOrdering()
235         {
236         Triple t1 = Triple.create( "subject predicate object" );
237         assertTrue( "subject must be present", t1.toString().indexOf( "subject" ) >= 0 );
238         assertTrue( "subject must preceed predicate", t1.toString().indexOf( "subject" ) < t1.toString().indexOf( "predicate" ) );
239         assertTrue( "predicate must preceed object", t1.toString().indexOf( "predicate" ) < t1.toString().indexOf( "object" ) );
240         }
241         
242     public void testTripleToStringQuoting()
243         {
244         Triple t1 = Triple.create( "subject predicate 'object'" );
245         assertTrue( t1.toString().indexOf( "\"object\"") > 0 );
246         }
247         
248     public void testTripleToStringWithPrefixing()
249         {
250         PrefixMapping pm = PrefixMapping.Factory.create();
251         pm.setNsPrefix( "spoo", "eg://domain.dom/spoo#" );
252         Triple t1 = Triple.create( "eg://domain.dom/spoo#a b c" );
253         assertEquals( "spoo:a @eh:/b eh:/c", t1.toString( pm ) );
254         }
255
256     }
257 /*
258     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
259     All rights reserved.
260
261     Redistribution and use in source and binary forms, with or without
262     modification, are permitted provided that the following conditions
263     are met:
264
265     1. Redistributions of source code must retain the above copyright
266        notice, this list of conditions and the following disclaimer.
267
268     2. Redistributions in binary form must reproduce the above copyright
269        notice, this list of conditions and the following disclaimer in the
270        documentation and/or other materials provided with the distribution.
271
272     3. The name of the author may not be used to endorse or promote products
273        derived from this software without specific prior written permission.
274
275     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
276     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
277     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
278     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
279     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
280     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
281     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
282     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
283     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
284     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
285 */

286
Popular Tags