KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > test > TestFileGraph


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestFileGraph.java,v 1.14 2005/03/10 14:35:34 chris-dollin Exp $
5 */

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 /**
19     Test FileGraph by seeing if we can make some file graphs and then read
20     them back.
21
22     @author hedgehog
23 */

24
25 public class TestFileGraph extends GraphTestBase
26     {
27     public TestFileGraph( String JavaDoc name )
28         { super( name ); }
29
30     // TODO want a wider variety of cases, now we've discovered how to abstract.
31
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     /**
124         Test that the graph encoded as the test-string content can be
125         written out to a temporary file generated from the prefix and suffix,
126         and then read back correctly. The temporary files are marked as
127         delete-on-exit to try and avoid cluttering the user's filespace ...
128     */

129     private static class Case extends TestFileGraph
130         {
131         String JavaDoc content;
132         String JavaDoc prefix;
133         String JavaDoc suffix;
134
135         Case( String JavaDoc content, String JavaDoc prefix, String JavaDoc 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 /*
159     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
160     All rights reserved.
161
162     Redistribution and use in source and binary forms, with or without
163     modification, are permitted provided that the following conditions
164     are met:
165
166     1. Redistributions of source code must retain the above copyright
167        notice, this list of conditions and the following disclaimer.
168
169     2. Redistributions in binary form must reproduce the above copyright
170        notice, this list of conditions and the following disclaimer in the
171        documentation and/or other materials provided with the distribution.
172
173     3. The name of the author may not be used to endorse or promote products
174        derived from this software without specific prior written permission.
175
176     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
177     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
178     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
179     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
180     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
181     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
182     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
183     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
184     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
185     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
186 */

187
Popular Tags