KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2     (c) Copyright 2005, Hewlett-Packard Development Company, LP
3     All rights reserved.
4     [See end of file]
5 */

6
7 package com.hp.hpl.jena.rdf.model.test;
8
9 import junit.framework.*;
10
11 import com.hp.hpl.jena.rdf.model.*;
12 import com.hp.hpl.jena.rdf.model.impl.*;
13 import com.hp.hpl.jena.rdf.model.impl.ModelSpecFactory;
14 import com.hp.hpl.jena.shared.*;
15
16
17 /**
18     Test cases for ModelSpecFactory to ensure it returns a ModelSpec
19     plausibly created by the associated ModelSpecCreator via the registry.
20     @author kers
21 */

22 public class TestModelSpecFactory extends ModelTestBase
23     {
24     public TestModelSpecFactory( String JavaDoc name )
25         { super( name ); }
26     
27     public static TestSuite suite()
28         { return new TestSuite( TestModelSpecFactory.class ); }
29 // { TestSuite result = new TestSuite();
30
// result.addTest( new TestModelSpecFactory( "testDefaultCreate" ) );
31
// return result; }
32

33     public void testFindUniqueRootByType()
34         {
35         Model m = modelWithStatements( "eh:x rdf:type eh:T; eh:y rdf:type eh:U" );
36         assertEquals( resource( "eh:x" ), ModelSpecFactory.findRootByType( m, resource( "eh:T" ) ) );
37         }
38     
39     public void testFindMissingRootByType()
40         {
41         Model m = modelWithStatements( "eh:y rdf:type eh:U" );
42         Resource type = resource( "eh:T" );
43         try
44             { ModelSpecFactory.findRootByType( m, type );
45             fail( "should trap missing root" ); }
46         catch (BadDescriptionNoRootException e)
47             { assertEquals( type, e.type );
48             assertSame( m, e.badModel ); }
49         }
50     
51     public void testFindMultipleRootByType()
52         {
53         Model m = modelWithStatements( "eh:x rdf:type eh:T; eh:y rdf:type eh:T" );
54         Resource type = resource( "eh:T" );
55         try
56             { ModelSpecFactory.findRootByType( m, type );
57             fail( "should trap multiple roots" ); }
58         catch (BadDescriptionMultipleRootsException e)
59             { assertEquals( type, e.type );
60             assertSame( m, e.badModel ); }
61         }
62     
63     public void testFindSpecificTypeTrivial()
64         {
65         Model m = fullModel( "eh:root rdf:type eh:T" );
66         Resource T = resource( "eh:T" ), root = m.createResource( "eh:root" );
67         assertEquals( T, ModelSpecFactory.findSpecificType( root, T ) );
68         }
69     
70     public void testFindSpecificTypeWithIrrelevantOtherType()
71         {
72         Model m = fullModel( "eh:root rdf:type eh:T; eh:root rdf:type eh:Other" );
73         Resource T = resource( "eh:T" ), root = m.createResource( "eh:root" );
74         assertEquals( T, ModelSpecFactory.findSpecificType( root, T ) );
75         }
76     
77 // public void testFindSpecificTypeWithSubtypes()
78
// {
79
// Model m = fullModel
80
// ( "eh:root rdf:type eh:V; eh:V rdfs:subClassOf eh:U; eh:U rdfs:subClassOf eh:T" );
81
// Resource root = m.createResource( "eh:root" );
82
// assertEquals( resource( "eh:V" ), ModelSpecFactory.findSpecificType( root, resource( "eh:T") ) );
83
// }
84

85     public void testFactoryReturnsAModelSpec()
86         {
87         Model m = modelWithStatements( "eh:Root rdf:type jms:PlainModelSpec" );
88         Resource r = m.createResource( "eh:Root" );
89         ModelSpec s = ModelSpecFactory.createSpec( m, r );
90         }
91     
92     public void testModelAccessible()
93         {
94         Model m = modelWithStatements( "eh:Root rdf:type jms:PlainModelSpec" );
95         Resource r = m.createResource( "eh:Root" );
96         ModelSpec g = ModelSpecFactory.createSpec( m, r );
97 // assertIsoModels( trim( r ), trim( g.getRoot() ) );
98
// assertIsoModels( trim( r ), g.getDescription() );
99
assertNotNull( g.getDescription() );
100         }
101
102     public void testFactoryNoRoot()
103         {
104         Model m = modelWithStatements( "" );
105         try { ModelSpecFactory.createSpec( m ); }
106         catch (BadDescriptionException e){ pass(); }
107         }
108     
109     public void testGEMSmultipleRoots()
110         {
111         Model m = modelWithStatements( "eh:Root rdf:type jms:ModelSpec; eh:Fake rdf:type jms:PlainModelSpec" );
112         try { ModelSpecFactory.createSpec( m ); }
113         catch (BadDescriptionException e) { pass(); }
114         }
115     
116     public void testDefaultCreate()
117         {
118         Model m = modelWithStatements( "eh:Root rdf:type jms:PlainModelSpec" );
119         ModelSpec s = ModelSpecFactory.createSpec( m );
120         Model x = s.createModel();
121         assertNotNull( x );
122         }
123     
124     public void testMatchingModelSpec()
125         {
126         Model m = modelWithStatements( "eh:Root rdf:type jms:PlainModelSpec" );
127         ModelSpec s = ModelSpecFactory.createSpec( m );
128         // TODO assertTrue( s instanceof BaseModelSpecImpl );
129
}
130     
131     public void testLoadsCorrectModelSpec()
132         {
133         Model m = modelWithStatements( "eh:Root rdf:type eh:MockSpec; eh:MockSpec rdfs:subClassOf jms:ModelSpec" );
134         ModelSpec s = ModelSpecFactory.createSpec( ModelSpecCreatorRegistry.registryWith( resource( "eh:MockSpec" ), createMock ), m );
135         assertTrue( s instanceof MockModelSpec );
136         }
137     
138     public void testCreateCreator()
139         {
140         String JavaDoc className = "com.hp.hpl.jena.rdf.model.test.MockModelSpec";
141         ModelSpecCreator c = new ModelSpecCreatorByClassname( className );
142         ModelSpec s = c.create( resource( "root" ), modelWithStatements( "" ) );
143         assertEquals( className, s.getClass().getName() );
144         }
145     
146     /**
147         The class loading should be deferred until an instance is required.
148     */

149     public void testCreateCreatorDeferred()
150         { String JavaDoc className = "lets.go.gathering.nuts.in.May";
151         ModelSpecCreator c = new ModelSpecCreatorByClassname( className ); }
152     
153     public void testRegistryDetectsMissingConfig()
154         {
155         try
156             {
157             new ModelSpecCreatorRegistry( "there/is/no/such/config.file" );
158             fail( "there should be no such configuration file found" );
159             }
160         catch (NotFoundException e)
161             { pass(); }
162         }
163     
164     public void testRegistryIgnoresMissingConfig()
165         {
166         new ModelSpecCreatorRegistry( "there/is/no/such/config.file", true );
167         }
168     
169     /**
170         Note: this relies on the contents of testing/modelspecs/modelspec-config.n3;
171         mayhap there's a less fragile way of making the test.
172     */

173     public void testRegistryDetectSuppliedConfig()
174         {
175         Model model = modelWithStatements( "" );
176         Resource root = model.createResource( "eh:pseudo-modelspec-type" );
177         ModelSpec s = new ModelSpecCreatorRegistry( "testing/modelspecs/modelspec-config.n3" )
178             .getCreator( resource( "eh:pseudo-modelspec-type" ) )
179             .create( root, model );
180         assertTrue( s instanceof MockModelSpec );
181         }
182     
183     protected ModelSpecCreator createMock = new ModelSpecCreator()
184         {
185         public ModelSpec create( Resource root, Model desc )
186             { return new MockModelSpec( root, desc ); }
187         };
188     
189     /**
190         Answer a model which is the RDFS closure of the statements encoded in
191         the string <code>statements</code>.
192     */

193     protected Model fullModel( String JavaDoc statements )
194         { return ModelSpecFactory.withSchema( modelWithStatements( statements ) ); }
195     }
196
197
198 /*
199  * (c) Copyright 2005 Hewlett-Packard Development Company, LP
200  * All rights reserved.
201  *
202  * Redistribution and use in source and binary forms, with or without
203  * modification, are permitted provided that the following conditions
204  * are met:
205  * 1. Redistributions of source code must retain the above copyright
206  * notice, this list of conditions and the following disclaimer.
207  * 2. Redistributions in binary form must reproduce the above copyright
208  * notice, this list of conditions and the following disclaimer in the
209  * documentation and/or other materials provided with the distribution.
210  * 3. The name of the author may not be used to endorse or promote products
211  * derived from this software without specific prior written permission.
212  *
213  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
214  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
215  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
216  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
217  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
218  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
219  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
220  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
222  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
223 */
Popular Tags