KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdql > test > TestManifestList


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6  
7 package com.hp.hpl.jena.rdql.test;
8
9 import java.util.* ;
10 import com.hp.hpl.jena.rdf.model.* ;
11 import com.hp.hpl.jena.vocabulary.RDF ;
12 import com.hp.hpl.jena.util.FileManager;
13 import com.hp.hpl.jena.vocabulary.TestManifest;
14 import junit.framework.* ;
15 import org.apache.commons.logging.*;
16
17
18 /** Wrapper class for handling models containg a test manifest according to the
19     vocabulary <code>@link{http://www.w3.org/2001/sw/DataAccess/result-set}#</code>
20     (originally <code>@link{http://jena.hpl.hp.com/2003/03/test-manifest}#</code>).
21   
22     The test manifest framework takes a description file or model and provides a
23     set of convenience operations on the manifest. Entries are in order they are
24     in the lists in the manifest but if the manifest model conatins multiple lists,
25     i.e. theer are muitple manifest objects or a manifest object has mutiple lists,
26     then the order between the lists is not determined.
27   
28     @author Andy Seaborne
29     @version $Id: TestManifestList.java,v 1.3 2005/02/21 12:16:09 andy_seaborne Exp $
30 */

31 class TestManifestList
32 {
33     
34     Model manifest = null ;
35     
36     public TestManifestList(String JavaDoc filename)
37     {
38         manifest = FileManager.get().loadModel(filename) ;
39     }
40     
41     public TestManifestList(Model m)
42     {
43         manifest = m ;
44     }
45
46     public Model getModel() { return manifest ; }
47
48     public TestIterator iterator() { return new TestIterator() ; }
49
50     /** Iterator over all the manifest entries found in the model - returns TestManifestItems
51      *
52      *
53      * @author Andy Seaborne
54      */

55
56     public class TestIterator implements Iterator
57     {
58         // Build the test set in memory.
59
// Means we can cleanly close iterators.
60
List entries ;
61         Iterator iterator ;
62
63         /** Create the iterator object */
64         TestIterator()
65         {
66             init() ;
67         }
68
69         /** Initialise the datatstructures from the model - only needed
70          * if the model has changed since the internal datastructures were
71          * calculated.
72          */

73         public void init()
74         {
75             entries = new ArrayList();
76
77             StmtIterator manifestStmts =
78                 manifest.listStatements(null, RDF.type, TestManifest.Manifest);
79
80             for (; manifestStmts.hasNext();)
81             {
82                 Statement manifestItemStmt = manifestStmts.nextStatement();
83                 Resource manifestRes = manifestItemStmt.getSubject();
84                 StmtIterator listIter = manifestRes.listProperties(TestManifest.entries);
85                 for (; listIter.hasNext();)
86                 {
87                     Resource listItem = listIter.nextStatement().getResource();
88
89                     for (; !listItem.equals(RDF.nil);)
90                     {
91
92                         Resource entry = listItem.getRequiredProperty(RDF.first).getResource();
93                         TestItem item = new TestItem(entry) ;
94                         
95                         entries.add(item);
96                         // Move on
97
listItem = listItem.getRequiredProperty(RDF.rest).getResource();
98                     }
99                 }
100                 listIter.close();
101             }
102             manifestStmts.close();
103             reset() ;
104         }
105
106         /** Start again. Iterator is reset - model is not processed again.
107          */

108         public void reset() { iterator = entries.iterator(); }
109         
110         /**
111          * @see java.util.Iterator#hasNext()
112          */

113         public boolean hasNext()
114         {
115             return iterator.hasNext();
116         }
117
118         /**
119          * @see java.util.Iterator#next()
120          */

121         public Object JavaDoc next()
122         {
123             return iterator.next();
124         }
125
126         public TestItem nextItem()
127         {
128             Object JavaDoc obj = iterator.next();
129             if ( ! ( obj instanceof TestItem ) )
130             {
131                 LogFactory.getLog(TestManifestList.class)
132                     .fatal("obj is "+obj.getClass().getName()) ;
133                 return null ;
134             }
135             return (TestItem)obj ;
136         }
137
138         /**
139          * @see java.util.Iterator#remove()
140          */

141         public void remove()
142         {
143             throw new UnsupportedOperationException JavaDoc(this.getClass().getName());
144         }
145     }
146     
147     // -------- Support for a "map" programming style
148

149     /** Action to perform on each entry of the manifest file to create the test */
150     public interface ActionProc
151     {
152         /** Called on each test described.
153          * @param testItem The RDF Resource for the whole test case
154          */

155         public void map1(TestItem testItem) ;
156     }
157     
158
159     /** Apply an ActionProc to every item in the manifest to
160      * create a JUnit test suite.
161      *
162      * @param actionProc
163      */

164
165     public void apply(ActionProc actionProc)
166     {
167         TestSuite suite = new TestSuite() ;
168         TestIterator iter = (TestIterator)iterator() ;
169         for ( ; iter.hasNext() ; )
170         {
171             TestItem item = (TestItem)iter.next() ;
172             actionProc.map1(item) ;
173         }
174     }
175 }
176
177
178 /*
179  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
180  * All rights reserved.
181  *
182  * Redistribution and use in source and binary forms, with or without
183  * modification, are permitted provided that the following conditions
184  * are met:
185  * 1. Redistributions of source code must retain the above copyright
186  * notice, this list of conditions and the following disclaimer.
187  * 2. Redistributions in binary form must reproduce the above copyright
188  * notice, this list of conditions and the following disclaimer in the
189  * documentation and/or other materials provided with the distribution.
190  * 3. The name of the author may not be used to endorse or promote products
191  * derived from this software without specific prior written permission.
192  *
193  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
194  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
195  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
196  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
198  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
199  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
201  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
202  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
203  */

204
205
Popular Tags