KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > test > JDBCResourceNewCPTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * JDBCResourceNewCPTest.java
21  *
22  * Created on March 21, 2006, 4:35 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.j2ee.sun.test;
29
30 import java.io.BufferedReader JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.util.Vector JavaDoc;
34 import org.netbeans.junit.NbTestCase;
35 import org.netbeans.junit.NbTestSuite;
36 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
37 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
38 import org.netbeans.modules.j2ee.sun.api.ServerInterface;
39 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
40 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.Resources;
41 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils;
42 import org.netbeans.modules.j2ee.sun.ide.sunresources.resourcesloader.SunResourceDataObject;
43 import org.netbeans.modules.j2ee.sun.ide.sunresources.wizards.ResourceConfigData;
44 import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants;
45 import org.openide.filesystems.FileObject;
46 import org.openide.filesystems.FileUtil;
47
48 /**
49  *
50  * @author Amanpreet Kaur
51  */

52 public class JDBCResourceNewCPTest extends NbTestCase implements WizardConstants{
53     
54     private static String JavaDoc CONNECTION_POOL_NAME = "poolTest";
55     private static String JavaDoc DATA_RESOURCE_NAME="resourceTest";
56     
57     
58     
59     /** Creates a new instance of JDBCResourceNewCPTest */
60     public JDBCResourceNewCPTest(String JavaDoc testName) {
61         
62         super(testName);
63     }
64     
65     public void registerDataResource() {
66         try {
67             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
68             //Project project = (Project)Util.openProject(new File(Util.WEB_PROJECT_PATH));
69
ResourceConfigData dsdata = new ResourceConfigData();
70             ResourceConfigData cpdata = new ResourceConfigData();
71             //connection pool settings
72
cpdata.setProperties(new Vector JavaDoc());
73             cpdata.addProperty(__DatabaseVendor, "derby_net");
74             cpdata.addProperty(__User, "app");
75             cpdata.addProperty(__Password,"app");
76             cpdata.addProperty(__ServerName,"localhost");
77             cpdata.addProperty(__DerbyPortNumber,"1527");
78             cpdata.addProperty(__DatabaseName,"sample");
79             cpdata.setString(__Name, CONNECTION_POOL_NAME);
80             cpdata.setString(__ResType, "javax.sql.DataSource");
81             cpdata.setString(__DatasourceClassname, "org.apache.derby.jdbc.ClientDataSource");
82             cpdata.setString(__SteadyPoolSize, "8");
83             cpdata.setString(__MaxPoolSize, "32");
84             cpdata.setString(__MaxWaitTimeInMillis, "60000");
85             cpdata.setString(__PoolResizeQuantity, "2");
86             cpdata.setString(__IdleTimeoutInSeconds, "300");
87             //datasouce settings
88
dsdata.setString(__JndiName,DATA_RESOURCE_NAME);
89             dsdata.setString(__Enabled, "true");
90             dsdata.setString(__JdbcObjectType, "user");
91             dsdata.setString(__PoolName,CONNECTION_POOL_NAME);
92 // dsdata.setTargetFileObject(project.getProjectDirectory());
93
File JavaDoc fpf = File.createTempFile("falseProject","");
94             fpf.delete();
95             FileObject falseProject = FileUtil.createFolder(fpf);
96             falseProject.createFolder("setup");
97             dsdata.setTargetFileObject(falseProject);
98             dsdata.setTargetFile("resourceTest");
99             cpdata.setTargetFileObject(falseProject);
100             cpdata.setTargetFile("poolTest");
101             ResourceUtils.saveJDBCResourceDatatoXml(dsdata,cpdata);
102             SunResourceDataObject resourceObjds = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/resourceTest.sun-resource"));
103             Resources resds = Util.getResourcesObject(resourceObjds);
104             SunResourceDataObject resourceObjcp = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/poolTest.sun-resource"));
105             Resources rescp = Util.getResourcesObject(resourceObjcp);
106             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
107             ResourceUtils.register(rescp.getJdbcConnectionPool(0), mejb, false);
108             ResourceUtils.register(resds.getJdbcResource(0), mejb, false);
109             resourceObjds.delete();
110             resourceObjcp.delete();
111             Util.closeProject(Util.WEB_PROJECT_NAME);
112             Util.sleep(5000);
113             String JavaDoc[] dataResources =Util.getResourcesNames("getJdbcResource","jndi-name",mejb);
114             for(int i=0;i<dataResources.length;i++) {
115                 if(dataResources[i].equals(DATA_RESOURCE_NAME))
116                     return;
117             }
118             
119             throw new Exception JavaDoc("Data Resource hasn't been created !");
120         } catch(Exception JavaDoc e) {
121             fail(e.getMessage());
122         }
123     }
124     public void unregisterDataResource() {
125         try {
126             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
127             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
128             String JavaDoc[] commandds = new String JavaDoc[] {"delete-jdbc-resource", "--user", "admin", DATA_RESOURCE_NAME};
129             String JavaDoc[] commandcp = new String JavaDoc[] {"delete-jdbc-connection-pool", "--user", "admin", CONNECTION_POOL_NAME};
130             Process JavaDoc p=Util.runAsadmin(commandds);
131             Util.sleep(Util.SLEEP);
132             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
133             String JavaDoc errorMess = error.readLine();
134             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
135             String JavaDoc output=input.readLine();
136             if(errorMess!=null)
137                 throw new Exception JavaDoc(errorMess+"\n"+output);
138             System.out.println(output);
139            Process JavaDoc pc=Util.runAsadmin(commandcp);
140             Util.sleep(Util.SLEEP);
141             BufferedReader JavaDoc errorcp = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(pc.getErrorStream()));
142             errorMess = error.readLine();
143             BufferedReader JavaDoc inputcp = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(pc.getInputStream()));
144             String JavaDoc outputcp=inputcp.readLine();
145             if(errorMess!=null)
146                 throw new Exception JavaDoc(errorMess+"\n"+outputcp);
147             System.out.println(outputcp);
148             Util.closeProject(Util.WEB_PROJECT_NAME);
149             Util.sleep(5000);
150             String JavaDoc[] dataRes = Util.getResourcesNames("getJdbcResource", "jndi-name", mejb);
151             for(int i=0;i<dataRes.length;i++) {
152                 if(dataRes[i].equals(DATA_RESOURCE_NAME))
153                     throw new Exception JavaDoc("Data Resource hasn't been removed !");}
154             String JavaDoc[] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb);
155             for(int i=0;i<connPools.length;i++) {
156                 if(connPools[i].equals(CONNECTION_POOL_NAME))
157                     throw new Exception JavaDoc("Connection Pool hasn't been removed !");
158             }
159         } catch(Exception JavaDoc e) {
160             fail(e.getMessage());
161         }
162     }
163     public static NbTestSuite suite() {
164         NbTestSuite suite = new NbTestSuite("JDBCResourceNewCPTest");
165         suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance"));
166         suite.addTest(new StartStopServerTest("startServer"));
167         suite.addTest(new JDBCResourceNewCPTest("registerDataResource"));
168         suite.addTest(new JDBCResourceNewCPTest("unregisterDataResource"));
169         suite.addTest(new StartStopServerTest("stopServer"));
170         suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance"));
171         return suite;
172     }
173 }
Popular Tags