KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > test > TestTransactions


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestTransactions.java,v 1.8 2005/02/21 12:03:34 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.db.test;
8
9 /**
10  *
11  * This tests basic operations on the modelRDB.
12  *
13  * It adds/removes statements of different types and verifys
14  * that the correct statements exist at the correct times.
15  *
16  * To run, you must have a mySQL database operational on
17  * localhost with a database name of "test" and allow use
18  * by a user named "test" with an empty password.
19  *
20  * @author hkuno
21 */

22
23 import junit.framework.*;
24
25 import com.hp.hpl.jena.db.IDBConnection;
26 import com.hp.hpl.jena.db.ModelRDB;
27 import com.hp.hpl.jena.db.impl.DriverRDB;
28 import com.hp.hpl.jena.db.impl.Driver_MySQL;
29 import com.hp.hpl.jena.rdf.model.*;
30
31 public class TestTransactions extends TestCase
32     {
33         
34     public TestTransactions( String JavaDoc name )
35         { super( name ); }
36     
37     public static TestSuite suite()
38         { return new TestSuite( TestTransactions.class ); }
39      
40     ModelRDB model = null;
41     Model dbProperties = null;
42     IDBConnection conn = null;
43     DriverRDB m_driver = null;
44     
45     protected void setUp() throws java.lang.Exception JavaDoc {
46         
47         conn = TestConnection.makeAndCleanTestConnection();
48         dbProperties = conn.getDatabaseProperties();
49         model = ModelRDB.createModel(conn);
50         m_driver = new Driver_MySQL();
51         m_driver.setConnection(conn);
52     }
53     
54     protected void tearDown() throws java.lang.Exception JavaDoc {
55         model.close();
56         model = null;
57         conn.cleanDB();
58         conn.close();
59         conn = null;
60     }
61
62     private void addCommit(Statement stmt) {
63         model.remove(stmt);
64         model.begin();
65         model.add(stmt);
66         model.commit();
67         assertTrue( model.contains(stmt) );
68     }
69     
70     private void addAbort(Statement stmt) {
71             model.remove(stmt);
72             // try {
73
model.begin();
74                 model.add(stmt);
75                 model.abort();
76             // } catch(Exception e) {
77
// throw new JenaException( e ); // System.out.println("addAbort caught exception: " + e);
78
// }
79
assertTrue(!model.contains(stmt) );
80         }
81         
82     public void testAddCommitURI() {
83         Resource s = model.createResource("test#subject");
84         Property p = model.createProperty("test#predicate");
85         Resource o = model.createResource("test#object");
86         
87         addCommit( model.createStatement(s,p,o));
88     }
89     
90     public void testAddAbortURI() {
91             Resource s = model.createResource("test#subject");
92             Property p = model.createProperty("test#predicate");
93             Resource o = model.createResource("test#object");
94         
95             addAbort( model.createStatement(s,p,o));
96         }
97     
98     public void testAddCommitLiteral() {
99         Resource s = model.createResource("test#subject");
100         Property p = model.createProperty("test#predicate");
101         Literal l = model.createLiteral("testLiteral");
102         
103         addCommit( model.createStatement(s,p,l));
104     }
105     
106     
107     public void testAddCommitHugeLiteral() {
108         String JavaDoc base = "This is a huge string that repeats.";
109         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
110         while(buffer.length() < 4000 )
111             buffer.append(base);
112         Resource s = model.createResource("test#subject");
113         Property p = model.createProperty("test#predicate");
114         Literal l = model.createLiteral(buffer.toString());
115         
116         addCommit( model.createStatement(s,p,l));
117     }
118     
119     public void testAddAbortHugeLiteral() {
120             String JavaDoc base = "This is a huge string that repeats.";
121             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
122             while(buffer.length() < 4000 )
123                 buffer.append(base);
124             Resource s = model.createResource("test#subject");
125             Property p = model.createProperty("test#predicate");
126             Literal l = model.createLiteral(buffer.toString());
127         
128             addAbort( model.createStatement(s,p,l));
129         }
130     
131     public void testAddCommitDatatype() {
132         Resource s = model.createResource("test#subject");
133         Property p = model.createProperty("test#predicate");
134         Literal l = model.createTypedLiteral("stringType");
135         
136         addCommit( model.createStatement(s,p,l));
137     }
138     
139     public void testAddAbortDatatype() {
140             Resource s = model.createResource("test#subject");
141             Property p = model.createProperty("test#predicate");
142             Literal l = model.createTypedLiteral("stringType");
143         
144             addAbort( model.createStatement(s,p,l));
145         }
146
147
148     public void testAddAbortHugeDatatype() {
149         String JavaDoc base = "This is a huge string that repeats.";
150         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
151         while(buffer.length() < 4000 )
152             buffer.append(base);
153         Resource s = model.createResource("test#subject");
154         Property p = model.createProperty("test#predicate");
155         Literal l2 = model.createTypedLiteral(buffer.toString());
156         
157         addAbort( model.createStatement(s,p,l2));
158     }
159     
160     public void testAddCommitHugeDatatype() {
161             String JavaDoc base = "This is a huge string that repeats.";
162             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
163             while(buffer.length() < 4000 )
164                 buffer.append(base);
165             Resource s = model.createResource("test#subject");
166             Property p = model.createProperty("test#predicate");
167             Literal l2 = model.createTypedLiteral(buffer.toString());
168         
169             addCommit( model.createStatement(s,p,l2));
170         }
171     
172    public void testAddCommitBNode() {
173         Resource s = model.createResource();
174         Property p = model.createProperty("test#predicate");
175         Resource o = model.createResource();
176                 
177         addCommit( model.createStatement(s,p,o));
178    }
179    
180    public void testAddAbortBNode() {
181            Resource s = model.createResource();
182            Property p = model.createProperty("test#predicate");
183            Resource o = model.createResource();
184                 
185            addAbort( model.createStatement(s,p,o));
186       }
187
188
189 }
190         
191
192 /*
193     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
194     All rights reserved.
195
196     Redistribution and use in source and binary forms, with or without
197     modification, are permitted provided that the following conditions
198     are met:
199
200     1. Redistributions of source code must retain the above copyright
201        notice, this list of conditions and the following disclaimer.
202
203     2. Redistributions in binary form must reproduce the above copyright
204        notice, this list of conditions and the following disclaimer in the
205        documentation and/or other materials provided with the distribution.
206
207     3. The name of the author may not be used to endorse or promote products
208        derived from this software without specific prior written permission.
209
210     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
211     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
212     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
213     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
214     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
215     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
216     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
217     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
218     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
219     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
220 */

221
Popular Tags