KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > query > test > TestSimpleTripleSorter


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestSimpleTripleSorter.java,v 1.8 2005/02/21 11:52:33 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.query.test;
8
9 import com.hp.hpl.jena.graph.test.*;
10 import com.hp.hpl.jena.graph.query.*;
11 import com.hp.hpl.jena.graph.*;
12
13 import java.util.*;
14 import junit.framework.*;
15
16 /**
17     @author kers
18 */

19 public class TestSimpleTripleSorter extends GraphTestBase
20     {
21     public TestSimpleTripleSorter(String JavaDoc name)
22         {super(name); }
23
24     public static TestSuite suite()
25         { return new TestSuite( TestSimpleTripleSorter.class ); }
26         
27     private TripleSorter sorter = new SimpleTripleSorter();
28         
29     /**
30         Test that the empty triple array sorts into an empty triple array
31     */

32     public void testEmpty()
33         {
34         Triple [] triples = new Triple [] {};
35         assertEquals( 0, sorter.sort( triples ).length );
36         }
37        
38     /**
39         Test that a singleton triple array sorts into that same singleton array
40         for various different search-styles of triples
41     */

42     public void testSingle()
43         {
44         testSingle( "S P O" );
45         testSingle( "S ?P O" );
46         testSingle( "S P ?O" );
47         testSingle( "?S ?P O" );
48         testSingle( "?S P ?O" );
49         testSingle( "S ?P ?O" );
50         testSingle( "?S ?P ?O" );
51         testSingle( "?? P O" );
52         testSingle( "S ?? O" );
53         testSingle( "S P ??O" );
54         testSingle( "?? ?? O" );
55         testSingle( "?? P ??" );
56         testSingle( "S ?? ??" );
57         testSingle( "?? ?? ??" );
58         }
59         
60     public void testSingle(String JavaDoc ts )
61         {
62         Triple t = Triple.create( ts );
63         assertEquals( Arrays.asList( new Triple[] {t} ), Arrays.asList( sorter.sort( new Triple[] {t} ) ) );
64         }
65         
66     /**
67         Test that concrete nodes get sorted to the beginning of the result
68     */

69     public void testConcreteFirst()
70         {
71         testReordersTo( "S P O; ?s ?p ?o", "S P O; ?s ?p ?o" );
72         testReordersTo( "S P O; ?s ?p ?o", "?s ?p ?o; S P O" );
73         testReordersTo( "S P O; ?s ?p ?o; ?a ?b ?c", "?s ?p ?o; ?a ?b ?c; S P O" );
74         testReordersTo( "S P O; ?s ?p ?o; ?a ?b ?c", "?s ?p ?o; S P O; ?a ?b ?c" );
75         }
76         
77     /**
78         Test that bound variables get sorted nearer the beginning than unbound ones
79     */

80     public void testBoundFirst()
81         {
82         testReordersTo( "?s R a; ?s ?p ?o", "?s ?p ?o; ?s R a" );
83         testReordersTo( "?s R a; ?s ?p b;", "?s ?p b; ?s R a" );
84         testReordersTo( "?a P b; ?c Q d; ?a P ?c", "?a P b; ?a P ?c; ?c Q d" );
85         }
86         
87     /**
88         Test that ANY is heavier than one variable but lighter than two
89     */

90     public void testANY()
91         {
92         testReordersTo( "?? C d; ?a X ?b", "?a X ?b; ?? C d" );
93         testReordersTo( "?a B c; ?? D e", "?? D e; ?a B c" );
94         }
95        
96     /**
97         Test that binding a variable makes it lighter than an unbound variable
98     */

99     public void testInteraction()
100         {
101         testReordersTo( "?a P b; ?a Q ?b; ?b R ?c", "?b R ?c; ?a Q ?b; ?a P b" );
102         }
103         
104     /**
105         Test that a triple that binds more things gets sorted earlier than a equally-light
106         triple that binds fewer things
107     */

108     public void testSortByMass()
109         {
110         testReordersTo( "?b c d; ?a b c; ?b ?c d; ?a ?b ?d", "?a b c; ?b c d; ?b ?c d; ?a ?b ?d" );
111         }
112         
113     /**
114         Utility: test that the triple array described by <code>original</code> gets reordered
115         to the triple array described by <code>desired</code>.
116     */

117     public void testReordersTo( String JavaDoc desired, String JavaDoc original )
118         {
119         Triple [] o = tripleArray( original ), d = tripleArray( desired );
120         assertEquals( Arrays.asList( d ), Arrays.asList( sorter.sort( o ) ) );
121         }
122     }
123
124
125 /*
126     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
127     All rights reserved.
128
129     Redistribution and use in source and binary forms, with or without
130     modification, are permitted provided that the following conditions
131     are met:
132
133     1. Redistributions of source code must retain the above copyright
134        notice, this list of conditions and the following disclaimer.
135
136     2. Redistributions in binary form must reproduce the above copyright
137        notice, this list of conditions and the following disclaimer in the
138        documentation and/or other materials provided with the distribution.
139
140     3. The name of the author may not be used to endorse or promote products
141        derived from this software without specific prior written permission.
142
143     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
144     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
145     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
146     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
147     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
148     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
149     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
150     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
151     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
152     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
153 */
Popular Tags