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: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * JDBCNewConnectionPoolTest.java
043: *
044: * Created on March 21, 2006, 10:35 AM
045: *
046: * To change this template, choose Tools | Template Manager
047: * and open the template in the editor.
048: */
049:
050: package org.netbeans.modules.j2ee.sun.test;
051:
052: import java.io.BufferedReader;
053: import java.io.File;
054: import java.io.InputStreamReader;
055: import java.util.Vector;
056: import org.netbeans.junit.NbTestCase;
057: import org.netbeans.junit.NbTestSuite;
058: import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
059: import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
060: import org.netbeans.modules.j2ee.sun.api.ServerInterface;
061: import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
062: import org.netbeans.modules.j2ee.sun.dd.api.serverresources.Resources;
063: import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils;
064: import org.netbeans.modules.j2ee.sun.ide.sunresources.wizards.ResourceConfigData;
065: import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants;
066: import org.openide.filesystems.FileObject;
067: import org.openide.filesystems.FileUtil;
068:
069: /**
070: *
071: * @author Amanpreet Kaur
072: */
073: public class JDBCNewConnectionPoolTest extends NbTestCase implements
074: WizardConstants {
075:
076: private static String CONNECTION_POOL_NAME = "poolTest";
077:
078: /** Creates a new instance of JDBCNewConnectionPoolResourcesTest */
079: public JDBCNewConnectionPoolTest(String testName) {
080: super (testName);
081: }
082:
083: public void registerConnectionPool() {
084: try {
085: // TODO : retoche migration
086: //Project project = (Project)Util.openProject(new File(Util.WEB_PROJECT_PATH));
087: ResourceConfigData cpdata = new ResourceConfigData();
088: ServerInstance inst = ServerRegistry.getInstance()
089: .getServerInstance(Util._URL);
090: //connection pool setting
091: cpdata.setProperties(new Vector());
092: cpdata.addProperty(__DatabaseVendor, "derby_net");
093: cpdata.addProperty(__User, "app");
094: cpdata.addProperty(__Password, "app");
095: cpdata.addProperty(__ServerName, "localhost");
096: cpdata.addProperty(__DerbyPortNumber, "1527");
097: cpdata.addProperty(__DatabaseName, "sample");
098: cpdata.setString(__Name, CONNECTION_POOL_NAME);
099: cpdata.setString(__ResType, "javax.sql.DataSource");
100: cpdata.setString(__DatasourceClassname,
101: "org.apache.derby.jdbc.ClientDataSource");
102: cpdata.setString(__SteadyPoolSize, "8");
103: cpdata.setString(__MaxPoolSize, "32");
104: cpdata.setString(__MaxWaitTimeInMillis, "60000");
105: cpdata.setString(__PoolResizeQuantity, "2");
106: cpdata.setString(__IdleTimeoutInSeconds, "300");
107: //cpdata.setTargetFileObject(project.getProjectDirectory());
108: File fpf = File.createTempFile("falseProject", "");
109: fpf.delete();
110: FileObject falseProject = FileUtil.createFolder(fpf);
111: falseProject.createFolder("setup");
112: cpdata.setTargetFileObject(falseProject);
113: ResourceUtils.saveConnPoolDatatoXml(cpdata);
114: File resourceObj = FileUtil.toFile(falseProject
115: .getFileObject("sun-resources.xml"));
116: Resources res = ResourceUtils
117: .getResourcesGraph(resourceObj);
118: ServerInterface mejb = ((SunDeploymentManagerInterface) inst
119: .getDeploymentManager()).getManagement();
120: ResourceUtils.register(res.getJdbcConnectionPool(0), mejb,
121: false);
122: resourceObj.delete();
123: falseProject.delete();
124: ///Util.closeProject(Util.WEB_PROJECT_NAME);
125: Util.sleep(5000);
126: String[] connPools = Util.getResourcesNames(
127: "getJdbcConnectionPool", "name", mejb);
128: for (int i = 0; i < connPools.length; i++) {
129: if (connPools[i].equals(CONNECTION_POOL_NAME))
130: return;
131: }
132: throw new Exception("Connection Pool hasn't been created !");
133: } catch (Exception e) {
134: fail(e.getMessage());
135: }
136: }
137:
138: public void unregisterConnectionPool() {
139: try {
140: ServerInstance inst = ServerRegistry.getInstance()
141: .getServerInstance(Util._URL);
142: ServerInterface mejb = ((SunDeploymentManagerInterface) inst
143: .getDeploymentManager()).getManagement();
144: String[] command = new String[] {
145: "delete-jdbc-connection-pool", "--user", "admin",
146: CONNECTION_POOL_NAME };
147: Process p = Util.runAsadmin(command);
148: Util.sleep(Util.SLEEP);
149: BufferedReader error = new BufferedReader(
150: new InputStreamReader(p.getErrorStream()));
151: String errorMess = error.readLine();
152: BufferedReader input = new BufferedReader(
153: new InputStreamReader(p.getInputStream()));
154: String output = input.readLine();
155: if (errorMess != null)
156: throw new Exception(errorMess + "\n" + output);
157: System.out.println(output);
158: Util.closeProject(Util.WEB_PROJECT_NAME);
159: Util.sleep(10000);
160: String[] connPools = Util.getResourcesNames(
161: "getJdbcConnectionPool", "name", mejb);
162: for (int i = 0; i < connPools.length; i++) {
163: if (connPools[i].equals(CONNECTION_POOL_NAME))
164: throw new Exception(
165: "Connection Pool hasn't been removed !");
166: }
167: } catch (Exception e) {
168: fail(e.getMessage());
169: }
170: }
171:
172: public static NbTestSuite suite() {
173: NbTestSuite suite = new NbTestSuite("JDBCNewConnectionPoolTest");
174: suite
175: .addTest(new AddRemoveSjsasInstanceTest(
176: "addSjsasInstance"));
177: suite.addTest(new StartStopServerTest("startServer"));
178: suite.addTest(new JDBCNewConnectionPoolTest(
179: "registerConnectionPool"));
180: suite.addTest(new JDBCNewConnectionPoolTest(
181: "unregisterConnectionPool"));
182: suite.addTest(new StartStopServerTest("stopServer"));
183: suite.addTest(new AddRemoveSjsasInstanceTest(
184: "removeSjsasInstance"));
185: return suite;
186: }
187: }
|