1 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 22 public class TestModelSpecFactory extends ModelTestBase 23 { 24 public TestModelSpecFactory( String name ) 25 { super( name ); } 26 27 public static TestSuite suite() 28 { return new TestSuite( TestModelSpecFactory.class ); } 29 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 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 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 } 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 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 149 public void testCreateCreatorDeferred() 150 { String 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 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 193 protected Model fullModel( String statements ) 194 { return ModelSpecFactory.withSchema( modelWithStatements( statements ) ); } 195 } 196 197 198 | Popular Tags |