001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestConnectionAssembler.java,v 1.8 2008/01/02 12:05:57 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.test;
008:
009: import java.util.StringTokenizer;
010:
011: import com.hp.hpl.jena.assembler.*;
012: import com.hp.hpl.jena.assembler.assemblers.ConnectionAssembler;
013: import com.hp.hpl.jena.assembler.exceptions.CannotLoadClassException;
014: import com.hp.hpl.jena.rdf.model.Resource;
015: import com.hp.hpl.jena.shared.JenaException;
016:
017: public class TestConnectionAssembler extends AssemblerTestBase {
018: public TestConnectionAssembler(String name) {
019: super (name);
020: }
021:
022: protected Class getAssemblerClass() {
023: return ConnectionAssembler.class;
024: }
025:
026: public void testConnectionAssemblerType() {
027: testDemandsMinimalType(new ConnectionAssembler(), JA.Connection);
028: }
029:
030: public void testConnectionVocabulary() {
031: assertRange(JA.Connection, JA.connection);
032: assertDomain(JA.Connection, JA.dbClass);
033: assertDomain(JA.Connection, JA.dbUser);
034: assertDomain(JA.Connection, JA.dbPassword);
035: assertDomain(JA.Connection, JA.dbType);
036: assertDomain(JA.Connection, JA.dbURL);
037: assertDomain(JA.Connection, JA.dbClassProperty);
038: assertDomain(JA.Connection, JA.dbUserProperty);
039: assertDomain(JA.Connection, JA.dbPasswordProperty);
040: assertDomain(JA.Connection, JA.dbTypeProperty);
041: assertDomain(JA.Connection, JA.dbURLProperty);
042: }
043:
044: public void testConnectionDescriptionFailsOnMissingURL() {
045: ConnectionDescription c = new ConnectionDescription(
046: "eh:/subject", null, null, null, "myType");
047: try {
048: c.getConnection();
049: fail("should trap null URL");
050: } catch (JenaException e) {
051: assertTrue(e
052: .getMessage()
053: .endsWith(
054: "cannot be opened because no dbURL or dbType was specified"));
055: }
056: }
057:
058: public void testConnectionDescriptionFailsOnMissingType() {
059: ConnectionDescription c = new ConnectionDescription(
060: "eh:/subject", "URL", null, null, null);
061: try {
062: c.getConnection();
063: fail("should trap null type");
064: } catch (JenaException e) {
065: assertTrue(e
066: .getMessage()
067: .endsWith(
068: "cannot be opened because no dbURL or dbType was specified"));
069: }
070: }
071:
072: public void testConnectionInitDefaults() {
073: Resource init = resourceInModel("x ja:dbUser 'USER'; x ja:dbPassword 'PASS'; x ja:dbURL URL:url; x ja:dbType 'TYPE'");
074: ConnectionAssembler c = new ConnectionAssembler(init);
075: assertEquals("USER", c.defaultUser);
076: assertEquals("PASS", c.defaultPassword);
077: assertEquals("URL:url", c.defaultURL);
078: assertEquals("TYPE", c.defaultType);
079: }
080:
081: public void testCannotLoadClass() {
082: Assembler a = new ConnectionAssembler();
083: Resource root = resourceInModel("x rdf:type ja:Connection; x ja:dbClass 'no.such.class'");
084: try {
085: a.open(root);
086: fail("should catch class load failure");
087: } catch (CannotLoadClassException e) {
088: assertEquals(resource("x"), e.getRoot());
089: assertEquals("no.such.class", e.getClassName());
090: assertInstanceOf(ClassNotFoundException.class, e.getCause());
091: }
092: }
093:
094: public void testDefaultUser() {
095: Resource init = resourceInModel("x ja:dbUser 'test'");
096: Resource root = resourceInModel("x rdf:type JA.Connection");
097: assertEquals("test", new ConnectionAssembler(init)
098: .getUser(root));
099: }
100:
101: public void testDefaultPassword() {
102: Resource init = resourceInModel("x ja:dbPassword 'byzantium'");
103: Resource root = resourceInModel("x rdf:type JA.Connection");
104: assertEquals("byzantium", new ConnectionAssembler(init)
105: .getPassword(root));
106: }
107:
108: public void testDefaultURL() {
109: Resource init = resourceInModel("x ja:dbURL URL:database");
110: Resource root = resourceInModel("x rdf:type ja:Connection");
111: assertEquals("URL:database", new ConnectionAssembler(init)
112: .getURL(root));
113: }
114:
115: public void testDefaultType() {
116: Resource init = resourceInModel("x ja:dbType 'bodacious'");
117: Resource root = resourceInModel("x rdf:type ja:Connection");
118: assertEquals("bodacious", new ConnectionAssembler(init)
119: .getType(root));
120: }
121:
122: public void testFullySpecifiedConnection() {
123: Resource root = resourceInModel("x rdf:type ja:Connection; x ja:dbUser 'test'; x ja:dbPassword ''; x ja:dbURL jdbc:mysql://localhost/test; x ja:dbType 'MySQL'");
124: assertEquals("test", new ConnectionAssembler().getUser(root));
125: assertEquals("", new ConnectionAssembler().getPassword(root));
126: assertEquals("jdbc:mysql://localhost/test",
127: new ConnectionAssembler().getURL(root));
128: assertEquals("MySQL", new ConnectionAssembler().getType(root));
129: }
130:
131: public void testTrapsNonStringObjects() {
132: testTrapsNonStringObjects("ja:dbClass", "aResource");
133: testTrapsNonStringObjects("ja:dbClass", "17");
134: testTrapsNonStringObjects("ja:dbClass", "'tag'de");
135: testTrapsNonStringObjects("ja:dbClassProperty", "aResource");
136: testTrapsNonStringObjects("ja:dbClassProperty", "17");
137: testTrapsNonStringObjects("ja:dbClassProperty", "'tag'de");
138: }
139:
140: private void testTrapsNonStringObjects(String property, String value) {
141: Resource root = resourceInModel("x rdf:type ja:Connection; x <property> <value>"
142: .replaceAll("<property>", property).replaceAll(
143: "<value>", value));
144: try {
145: new ConnectionAssembler().open(root);
146: fail("should trap bad object " + value + " for property "
147: + property);
148: } catch (BadObjectException e) {
149: assertEquals(resource("x"), e.getRoot());
150: assertEquals(rdfNode(empty, value), e.getObject());
151: }
152: }
153:
154: public void testOpenConnectionWIthLabels() {
155: Resource root = resourceInModel("x rdf:type ja:Connection; x ja:dbUser 'X'; x ja:dbPassword 'P'; x ja:dbURL U:RL; x ja:dbType 'T'");
156: final ConnectionDescription fake = ConnectionDescription
157: .create("eh:/x", "DD", "TT", "UU", "PP");
158: CheckingConnectionAssembler x = new CheckingConnectionAssembler(
159: fake, "eh:/x U:RL X P T");
160: assertSame(fake, x.open(root));
161: assertTrue("mock createConnection should have been called",
162: x.called);
163: }
164:
165: public void testConnection() {
166: Assembler a = new ConnectionAssembler();
167: Resource root = resourceInModel("x rdf:type ja:Connection; x ja:dbUser 'test'; x ja:dbPassword ''; x ja:dbURL jdbc:mysql://localhost/test; x ja:dbType 'MySQL'");
168: Object x = a.open(root);
169: assertInstanceOf(ConnectionDescription.class, x);
170: ConnectionDescription d = (ConnectionDescription) x;
171: assertEquals("test", d.dbUser);
172: assertEquals("", d.dbPassword);
173: assertEquals("MySQL", d.dbType);
174: assertEquals("jdbc:mysql://localhost/test", d.dbURL);
175: }
176:
177: public void testIndirectURLConnection() {
178: System.setProperty("test.url", "bbb");
179: Resource root = resourceInModel("x rdf:type ja:Connection; x ja:dbURLProperty 'test.url'");
180: Assembler a = new ConnectionAssembler();
181: ConnectionDescription d = (ConnectionDescription) a.open(root);
182: assertEquals("bbb", d.dbURL);
183: }
184:
185: public void testIndirectUserConnection() {
186: System.setProperty("test.user", "blenkinsop");
187: Resource root = resourceInModel("x rdf:type ja:Connection; x ja:dbUserProperty 'test.user'");
188: Assembler a = new ConnectionAssembler();
189: ConnectionDescription d = (ConnectionDescription) a.open(root);
190: assertEquals("blenkinsop", d.dbUser);
191: }
192:
193: public void testIndirectPasswordConnection() {
194: System.setProperty("test.password", "Top/Secret");
195: Resource root = resourceInModel("x rdf:type ja:Connection; x ja:dbPasswordProperty 'test.password'");
196: Assembler a = new ConnectionAssembler();
197: ConnectionDescription d = (ConnectionDescription) a.open(root);
198: assertEquals("Top/Secret", d.dbPassword);
199: }
200:
201: public void testIndirectTypeConnection() {
202: System.setProperty("test.type", "HisSQL");
203: Resource root = resourceInModel("x rdf:type ja:Connection; x ja:dbTypeProperty 'test.type'");
204: Assembler a = new ConnectionAssembler();
205: ConnectionDescription d = (ConnectionDescription) a.open(root);
206: assertEquals("HisSQL", d.dbType);
207: }
208:
209: private static final class CheckingConnectionAssembler extends
210: ConnectionAssembler {
211: private final ConnectionDescription result;
212: private final String expectSubject;
213: private final String expectURL;
214: private final String expectUser;
215: private final String expectPassword;
216: private final String expectType;
217:
218: private boolean called;
219:
220: private CheckingConnectionAssembler(
221: ConnectionDescription result, String expected) {
222: super ();
223: StringTokenizer st = new StringTokenizer(expected);
224: expectSubject = st.nextToken();
225: expectURL = st.nextToken();
226: expectUser = st.nextToken();
227: expectPassword = st.nextToken();
228: expectType = st.nextToken();
229: this .result = result;
230: }
231:
232: public ConnectionDescription createConnection(String subject,
233: String url, String type, String user, String pass) {
234: assertEquals(expectSubject, subject);
235: assertEquals(expectURL, url);
236: assertEquals(expectUser, user);
237: assertEquals(expectPassword, pass);
238: assertEquals(expectType, type);
239: called = true;
240: return result;
241: }
242: }
243: }
244:
245: /*
246: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
247: * All rights reserved.
248: *
249: * Redistribution and use in source and binary forms, with or without
250: * modification, are permitted provided that the following conditions
251: * are met:
252: * 1. Redistributions of source code must retain the above copyright
253: * notice, this list of conditions and the following disclaimer.
254: * 2. Redistributions in binary form must reproduce the above copyright
255: * notice, this list of conditions and the following disclaimer in the
256: * documentation and/or other materials provided with the distribution.
257: * 3. The name of the author may not be used to endorse or promote products
258: * derived from this software without specific prior written permission.
259: *
260: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
261: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
262: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
263: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
264: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
265: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
266: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
267: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
269: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
270: */
|