1 6 7 package com.hp.hpl.jena.graph.test; 8 9 import com.hp.hpl.jena.graph.*; 10 import com.hp.hpl.jena.graph.impl.*; 11 import com.hp.hpl.jena.rdf.model.*; 12 import com.hp.hpl.jena.util.FileUtils; 13 14 import java.io.*; 15 16 import junit.framework.*; 17 18 24 25 public class TestFileGraph extends GraphTestBase 26 { 27 public TestFileGraph( String name ) 28 { super( name ); } 29 30 public static TestSuite suite() 32 { 33 TestSuite result = new TestSuite( TestFileGraph.class ); 34 result.addTest( new Case( "x /R y", "xxxA", ".rdf" ) ); 35 result.addTest( new Case( "x /R y", "xxxB", ".n3" ) ); 36 result.addTest( new Case( "x /R y", "xxxC", ".nt" ) ); 37 result.addTest( new Case( "x /R y; p /R q", "xxxD", ".rdf" ) ); 38 result.addTest( new Case( "x /R y; p /R q", "xxxE", ".n3" ) ); 39 result.addTest( new Case( "x /R y; p /R q", "xxxF", ".nt" ) ); 40 result.addTest( new Case( "http://domain/S ftp:ftp/P O", "xxxG", ".rdf" ) ); 41 result.addTest( new Case( "http://domain/S ftp:ftp/P O", "xxxH", ".nt" ) ); 42 result.addTest( new Case( "http://domain/S ftp:ftp/P O", "xxxI", ".n3" ) ); 43 return result; 44 } 45 46 public void testPlausibleGraphname() 47 { 48 assertTrue( FileGraph.isPlausibleGraphName( "agnessi.rdf" ) ); 49 assertTrue( FileGraph.isPlausibleGraphName( "parabola.nt" ) ); 50 assertTrue( FileGraph.isPlausibleGraphName( "hyperbola.n3" ) ); 51 assertTrue( FileGraph.isPlausibleGraphName( "chris.dollin.n3" ) ); 52 assertTrue( FileGraph.isPlausibleGraphName( "hedgehog.spine.end.rdf" ) ); 53 } 54 55 public void testisPlausibleUppercaseGraphname() 56 { 57 assertTrue( FileGraph.isPlausibleGraphName( "LOUDER.RDF" ) ); 58 assertTrue( FileGraph.isPlausibleGraphName( "BRIDGE.NT" ) ); 59 assertTrue( FileGraph.isPlausibleGraphName( "NOTN2.N3" ) ); 60 assertTrue( FileGraph.isPlausibleGraphName( "chris.dollin.N3" ) ); 61 assertTrue( FileGraph.isPlausibleGraphName( "hedgehog.spine.end.RDF" ) ); 62 } 63 64 public void testImPlausibleGraphName() 65 { 66 assertFalse( FileGraph.isPlausibleGraphName( "undecorated" ) ); 67 assertFalse( FileGraph.isPlausibleGraphName( "danger.exe" ) ); 68 assertFalse( FileGraph.isPlausibleGraphName( "pretty.jpg" ) ); 69 assertFalse( FileGraph.isPlausibleGraphName( "FileGraph.java" ) ); 70 assertFalse( FileGraph.isPlausibleGraphName( "infix.rdf.c" ) ); 71 } 72 73 public void testTransactionCommit() 74 { 75 Graph initial = graphWith( "initial hasValue 42; also hasURI hello" ); 76 Graph extra = graphWith( "extra hasValue 17; also hasURI world" ); 77 File foo = FileUtils.tempFileName( "fileGraph", ".n3" ); 78 Graph g = new FileGraph( foo, true, true ); 79 g.getBulkUpdateHandler().add( initial ); 80 g.getTransactionHandler().begin(); 81 g.getBulkUpdateHandler().add( extra ); 82 g.getTransactionHandler().commit(); 83 Graph union = graphWith( "" ); 84 union.getBulkUpdateHandler().add( initial ); 85 union.getBulkUpdateHandler().add( extra ); 86 assertIsomorphic( union, g ); 87 Model inFile = ModelFactory.createDefaultModel(); 88 inFile.read( "file:///" + foo, "N3" ); 89 assertIsomorphic( union, inFile.getGraph() ); 90 } 91 92 public void testTransactionAbort() 93 { 94 Graph initial = graphWith( "initial hasValue 42; also hasURI hello" ); 95 Graph extra = graphWith( "extra hasValue 17; also hasURI world" ); 96 File foo = FileUtils.tempFileName( "fileGraph", ".n3" ); 97 Graph g = new FileGraph( foo, true, true ); 98 g.getBulkUpdateHandler().add( initial ); 99 g.getTransactionHandler().begin(); 100 g.getBulkUpdateHandler().add( extra ); 101 g.getTransactionHandler().abort(); 102 assertIsomorphic( initial, g ); 103 } 104 105 public void testTransactionCommitThenAbort() 106 { 107 Graph initial = graphWith( "A pings B; B pings C" ); 108 Graph extra = graphWith( "C pingedBy B; fileGraph rdf:type Graph" ); 109 File foo = FileUtils.tempFileName( "fileGraph", ".n3" ); 110 Graph g = new FileGraph( foo, true, true ); 111 g.getTransactionHandler().begin(); 112 g.getBulkUpdateHandler().add( initial ); 113 g.getTransactionHandler().commit(); 114 g.getTransactionHandler().begin(); 115 g.getBulkUpdateHandler().add( extra ); 116 g.getTransactionHandler().abort(); 117 assertIsomorphic( initial, g ); 118 Model inFile = ModelFactory.createDefaultModel(); 119 inFile.read( "file:///" + foo, "N3" ); 120 assertIsomorphic( initial, inFile.getGraph() ); 121 } 122 123 129 private static class Case extends TestFileGraph 130 { 131 String content; 132 String prefix; 133 String suffix; 134 135 Case( String content, String prefix, String suffix ) 136 { 137 super( "Case: " + content + " in " + prefix + "*" + suffix ); 138 this.content = content; 139 this.prefix = prefix; 140 this.suffix = suffix; 141 } 142 143 public void runTest() 144 { 145 File foo = FileUtils.tempFileName( prefix, suffix ); 146 Graph original = graphWith( content ); 147 Graph g = new FileGraph( foo, true, true ); 148 g.getBulkUpdateHandler().add( original ); 149 g.close(); 150 Graph g2 = new FileGraph( foo, false, true ); 151 assertIsomorphic( original, g2 ); 152 g2.close(); 153 } 154 } 155 156 } 157 158 187 | Popular Tags |