001: /*
002: (c) Copyright 2008 Hewlett-Packard Development Company, LP
003: All rights reserved.
004: $Id: TestRDBAssemblerContents.java,v 1.1 2008/01/03 15:19:09 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.db.test;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.assembler.*;
012: import com.hp.hpl.jena.assembler.assemblers.AssemblerBase;
013: import com.hp.hpl.jena.assembler.test.AssemblerTestBase;
014: import com.hp.hpl.jena.db.*;
015: import com.hp.hpl.jena.rdf.model.*;
016:
017: public class TestRDBAssemblerContents extends AssemblerTestBase {
018: public TestRDBAssemblerContents(String name) {
019: super (name);
020: }
021:
022: private static final String url = ModelFactoryBase.guessDBURL();
023: private static final String user = ModelFactoryBase.guessDBUser();
024: private static final String password = ModelFactoryBase
025: .guessDBPassword();
026: private static final String type = ModelFactoryBase.guessDBType();
027: private static final String driver = ModelFactoryBase
028: .guessDBDriver();
029:
030: static {
031: try {
032: Class.forName(driver);
033: } catch (Exception e) {
034: throw new RuntimeException(e);
035: }
036: }
037:
038: public void testCreatesEmptyModel() {
039: ConnectionDescription cd = new ConnectionDescription("db", url,
040: user, password, type);
041: Resource root = resourceInModel("db rdf:type ja:RDBModel; db ja:connection C; db ja:modelName 'CreatesEmptyModel'");
042: Assembler ca = new NamedObjectAssembler(resource("C"), cd);
043: Model assembled = (Model) Assembler.rdbModel.open(ca, root,
044: Mode.ANY);
045: assertIsoModels(modelWithStatements(""), assembled);
046: assembled.close();
047: }
048:
049: public void testCreatesInitialisedModel() {
050: ConnectionDescription cd = new ConnectionDescription("db", url,
051: user, password, type);
052: Resource root = resourceInModel("db rdf:type ja:RDBModel; db ja:connection C; db ja:modelName 'CreatesInitialisedModel'; db ja:quotedContent X; X rdf:type T");
053: Assembler ca = new AssistantAssembler(Assembler.content).with(
054: resource("C"), cd);
055: Model assembled = (Model) Assembler.rdbModel.open(ca, root,
056: Mode.ANY);
057: assertIsoModels(modelWithStatements("X rdf:type T"), assembled);
058: assembled.close();
059: }
060:
061: public void testOpensAndInitialisesModel() {
062: ConnectionDescription cd = new ConnectionDescription("db", url,
063: user, password, type);
064: Resource root = resourceInModel("db rdf:type ja:RDBModel; db ja:connection C; db ja:modelName 'OpensAndInitialisesModel'; db ja:quotedContent X; X rdf:type T");
065: Assembler ca = new AssistantAssembler(Assembler.content).with(
066: resource("C"), cd);
067: Model assembled = (Model) Assembler.rdbModel.open(ca, root,
068: Mode.ANY);
069: assertIsoModels(modelWithStatements("X rdf:type T"), assembled);
070: assembled.close();
071: }
072:
073: public void testCreatesAndInitialisesModel() {
074: ConnectionDescription cd = new ConnectionDescription("db", url,
075: user, password, type);
076: ensureAbsent(cd, "CreatesAndInitialisesModel");
077: Resource root = resourceInModel("db rdf:type ja:RDBModel; db ja:connection C; db ja:modelName 'CreatesAndInitialisesModel'; db ja:initialContent Q; Q ja:quotedContent X; X rdf:type T");
078: Assembler ca = new AssistantAssembler(Assembler.content).with(
079: resource("C"), cd);
080: Model assembled = (Model) Assembler.rdbModel.open(ca, root,
081: Mode.ANY);
082: assertIsoModels(modelWithStatements("X rdf:type T"), assembled);
083: assembled.close();
084: }
085:
086: public void testOpensAndDoesNotInitialiseModel() {
087: ConnectionDescription cd = new ConnectionDescription("db", url,
088: user, password, type);
089: ensurePresent(cd, "OpensAndDoesNotInitialiseModel");
090: Resource root = resourceInModel("db rdf:type ja:RDBModel; db ja:connection C; db ja:modelName 'OpensAndDoesNotInitialiseModel'; db ja:initialContent Q; Q ja:quotedContent X; X rdf:type T");
091: Assembler ca = new AssistantAssembler(Assembler.content).with(
092: resource("C"), cd);
093: Model assembled = (Model) Assembler.rdbModel.open(ca, root,
094: Mode.ANY);
095: assertIsoModels(modelWithStatements(""), assembled);
096: assembled.close();
097: }
098:
099: private void ensurePresent(ConnectionDescription cd,
100: String modelName) {
101: IDBConnection ic = cd.getConnection();
102: if (!ic.containsModel(modelName))
103: ModelRDB.createModel(ic, modelName).close();
104: }
105:
106: private void ensureAbsent(ConnectionDescription cd, String modelName) {
107: IDBConnection ic = cd.getConnection();
108: if (ic.containsModel(modelName))
109: ModelRDB.open(ic, modelName).remove();
110: }
111:
112: static class AssistantAssembler extends AssemblerBase {
113: protected final Assembler general;
114: protected final Map map = new HashMap();
115:
116: public AssistantAssembler(Assembler general) {
117: this .general = general;
118: }
119:
120: public AssistantAssembler with(Resource name, Object value) {
121: map.put(name, value);
122: return this ;
123: }
124:
125: public Object open(Assembler a, Resource root, Mode mode) {
126: Object fromMap = map.get(root);
127: return fromMap == null ? general.open(a, root, mode)
128: : fromMap;
129: }
130:
131: }
132: }
133:
134: /*
135: * (c) Copyright 2005, 2006, 2007 Hewlett-Packard Development Company, LP
136: * All rights reserved.
137: *
138: * Redistribution and use in source and binary forms, with or without
139: * modification, are permitted provided that the following conditions
140: * are met:
141: * 1. Redistributions of source code must retain the above copyright
142: * notice, this list of conditions and the following disclaimer.
143: * 2. Redistributions in binary form must reproduce the above copyright
144: * notice, this list of conditions and the following disclaimer in the
145: * documentation and/or other materials provided with the distribution.
146: * 3. The name of the author may not be used to endorse or promote products
147: * derived from this software without specific prior written permission.
148: *
149: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
150: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
151: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
152: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
153: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
154: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
155: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
156: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
157: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
158: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
159: */
|