001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.visualweb.dataconnectivity.test;
040:
041: import java.io.File;
042: import java.io.IOException;
043: import java.lang.reflect.InvocationHandler;
044: import java.lang.reflect.Method;
045: import java.lang.reflect.Proxy;
046: import java.net.MalformedURLException;
047: import java.net.URL;
048: import java.sql.Connection;
049: import java.sql.DatabaseMetaData;
050: import java.sql.Driver;
051: import java.sql.DriverPropertyInfo;
052: import java.sql.SQLException;
053: import java.util.Properties;
054: import java.util.logging.Logger;
055: import org.netbeans.api.db.explorer.JDBCDriver;
056: import org.netbeans.api.project.Project;
057: import org.netbeans.junit.NbTestCase;
058: import org.netbeans.modules.db.explorer.DbDriverManager;
059: import org.netbeans.modules.visualweb.api.j2ee.common.RequestedJdbcResource;
060: import org.netbeans.modules.visualweb.dataconnectivity.explorer.RowSetBeanCreateInfoSet;
061: import org.netbeans.modules.visualweb.dataconnectivity.model.DataSourceInfo;
062: import org.netbeans.modules.visualweb.dataconnectivity.test.utils.SetupProject;
063:
064: /**
065: *
066: * @author JohnBaker
067: */
068: public class DatasourceBeanCreateInfoSetTest extends NbTestCase {
069:
070: private static String DRIVER_PROPERTY = "db.driverclass";
071: private static String URL_PROPERTY = "db.url";
072: private static String USERNAME_PROPERTY = "db.username";
073: private static String PASSWORD_PROPERTY = "db.password";
074: private static String DBDIR_PROPERTY = "db.dir";
075: private static String DBNAME_PROPERTY = "db.name";
076:
077: protected static String dblocation;
078: private Project project;
079: private RequestedJdbcResource jdbcResource;
080: private Properties props;
081: private Connection conn;
082: private String schemaName;
083: private DatabaseMetaData metaData;
084: private RowSetBeanCreateInfoSet rowSetBeanCreateInfoSet;
085: private JDBCDriver jdbcDriver;
086:
087: private static final Logger LOGGER = Logger
088: .getLogger(DatasourceBeanCreateInfoSetTest.class.getName());
089:
090: public DatasourceBeanCreateInfoSetTest(String testName) {
091: super (testName);
092: }
093:
094: @Override
095: protected void setUp() throws Exception {
096: super .setUp();
097: project = openProject();
098: props = new Properties();
099: props.setProperty(DRIVER_PROPERTY,
100: "org.apache.derby.jdbc.EmbeddedDriver");
101: props.setProperty(URL_PROPERTY,
102: "jdbc:derby://localhost:1527/travel");
103: props.setProperty(USERNAME_PROPERTY, "travel");
104: props.setProperty(PASSWORD_PROPERTY, "travel");
105: props.setProperty(DBNAME_PROPERTY, "ApacheDerby");
106:
107: testCreateJdbcResource();
108: testCreateRowSetInfoSet();
109: testCreateDataSource();
110: }
111:
112: // TODO add test methods here. The name must begin with 'test'. For example:
113: // public void testHello() {}
114: public Project openProject() throws IOException {
115: project = SetupProject.setup(getWorkDir());
116: return project;
117: }
118:
119: public void testCreateJdbcResource() {
120: jdbcResource = new RequestedJdbcResource("jdbc/VIR",
121: "org.apache.derby.jdbc.ClientDriver",
122: "jdbc:derby://localhost:1527/vir", "vir", "vir");
123:
124: assertNotNull("jdbcResource should not be null", jdbcResource);
125: }
126:
127: @SuppressWarnings("empty-statement")
128: public void testDatasourceBeanCreateInfoSet() {
129: DatabaseMetaData metaData = null;
130: try {
131:
132: jdbcDriver = createDummyJDBCDriver(getDataDir());
133:
134: // Register driver
135: Driver d = new DriverImpl(props.getProperty(URL_PROPERTY));
136: DbDriverManager.getDefault().registerDriver(d);
137:
138: conn = DbDriverManager.getDefault().getConnection(
139: props.getProperty(URL_PROPERTY), props, jdbcDriver);
140: System.out.println("driver = " + jdbcDriver);
141: schemaName = "TestSchema";
142:
143: } catch (SQLException sqe) {
144: sqe.printStackTrace();
145:
146: } catch (MalformedURLException mue) {
147: mue.printStackTrace();
148: }
149: }
150:
151: public void testCreateRowSetInfoSet() {
152: // Create infoset for storing a data source
153: try {
154: metaData = (conn == null) ? null : conn.getMetaData();
155: String tableName = ((schemaName == null) || (schemaName
156: .equals(""))) ? "RowSetBeanCreateInfoSet"
157: : schemaName + "." + "RowSetBeanCreateInfoSet";
158: rowSetBeanCreateInfoSet = new RowSetBeanCreateInfoSet(
159: "TestSchema", metaData);
160: assertNotNull("RowSetBeanCreateInfoSet failed creation",
161: rowSetBeanCreateInfoSet);
162: } catch (SQLException sqe) {
163: sqe.printStackTrace();
164: }
165: }
166:
167: public void testCreateDataSource() {
168: try {
169: jdbcDriver = createDummyJDBCDriver(getDataDir());
170:
171: // Register driver
172: Driver d = new DriverImpl(props.getProperty(URL_PROPERTY));
173: DbDriverManager.getDefault().registerDriver(d);
174:
175: conn = DbDriverManager.getDefault().getConnection(
176: props.getProperty(URL_PROPERTY), props, jdbcDriver);
177: System.out.println("driver = " + jdbcDriver);
178: schemaName = "TestSchema";
179:
180: metaData = (conn == null) ? null : conn.getMetaData();
181: String tableName = ((schemaName == null) || (schemaName
182: .equals(""))) ? "RowSetBeanCreateInfoSet"
183: : schemaName + "." + "RowSetBeanCreateInfoSet";
184: rowSetBeanCreateInfoSet = new RowSetBeanCreateInfoSet(
185: "TestSchema", metaData);
186: assertNotNull("RowSetBeanCreateInfoSet failed creation",
187: rowSetBeanCreateInfoSet);
188: // Create data source
189: DataSourceInfo dsi = new DataSourceInfo(
190: "TestDataSourceName", jdbcDriver.getName(), props
191: .getProperty(URL_PROPERTY), null, props
192: .getProperty(USERNAME_PROPERTY), props
193: .getProperty(PASSWORD_PROPERTY));
194: rowSetBeanCreateInfoSet.setDataSourceInfo(dsi);
195: assertNotNull(
196: "RowSetBeanCreateInfoSet, data source not created",
197: rowSetBeanCreateInfoSet.getDataSourceInfo(dsi));
198: } catch (SQLException sqe) {
199: sqe.printStackTrace();
200: } catch (MalformedURLException mue) {
201: mue.printStackTrace();
202: }
203: }
204:
205: private static JDBCDriver createDummyJDBCDriver(File dataDir)
206: throws MalformedURLException {
207: URL url = dataDir.toURL();
208: return JDBCDriver.create("test_driver",
209: "DbDriverManagerTest DummyDriver", "DummyDriver",
210: new URL[] { url });
211: }
212:
213: protected void tearDown() throws Exception {
214: super .tearDown();
215: }
216:
217: public static final class DriverImpl implements Driver {
218:
219: public static final String DEFAULT_URL = "jdbc:DbDriverManagerTest";
220: private String url;
221:
222: public DriverImpl() {
223: this (DEFAULT_URL);
224: }
225:
226: public DriverImpl(String url) {
227: this .url = url;
228: }
229:
230: public DriverPropertyInfo[] getPropertyInfo(String url,
231: Properties info) throws SQLException {
232: return new DriverPropertyInfo[0];
233: }
234:
235: public Connection connect(String url, Properties info)
236: throws SQLException {
237: return (Connection) Proxy.newProxyInstance(DriverImpl.class
238: .getClassLoader(),
239: new Class[] { ConnectionEx.class },
240: new InvocationHandler() {
241: public Object invoke(Object proxy, Method m,
242: Object[] args) {
243: String methodName = m.getName();
244: if (methodName.equals("getDriver")) {
245: return DriverImpl.this ;
246: } else if (methodName.equals("hashCode")) {
247: Integer i = new Integer(System
248: .identityHashCode(proxy));
249: return i;
250: } else if (methodName.equals("equals")) {
251: return Boolean
252: .valueOf(proxy == args[0]);
253: }
254: return null;
255: }
256: });
257: }
258:
259: public boolean acceptsURL(String url) throws SQLException {
260: return (this .url.equals(url));
261: }
262:
263: public boolean jdbcCompliant() {
264: return true;
265: }
266:
267: public int getMinorVersion() {
268: return 0;
269: }
270:
271: public int getMajorVersion() {
272: return 0;
273: }
274: }
275:
276: private static interface ConnectionEx extends Connection {
277:
278: public Driver getDriver();
279: }
280: }
|