KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JSFModuleTest.java
21  *
22  * Created on May 15, 2006, 10:29 AM
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
31 import java.io.BufferedReader JavaDoc;
32 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
33 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
34
35 import org.netbeans.junit.NbTestCase;
36 import org.netbeans.junit.NbTestSuite;
37 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
38 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
39 import org.netbeans.spi.project.ActionProvider;
40 import org.netbeans.api.project.Project;
41 import java.io.File JavaDoc;
42 import java.io.FileOutputStream JavaDoc;
43 import java.io.InputStreamReader JavaDoc;
44 import java.io.OutputStream JavaDoc;
45 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
46 import org.netbeans.modules.j2ee.sun.ide.j2ee.VerifierImpl;
47 import org.openide.util.RequestProcessor;
48 import org.openide.util.Task;
49
50
51 /**
52  *
53  * @author Amanpreet
54  */

55 public class JSFModuleTest extends NbTestCase {
56     
57     private final int SLEEP = 10000;
58     
59     public JSFModuleTest(String JavaDoc testName) {
60         super(testName);
61     }
62     
63     public void deployJSFModule() {
64         try {
65             Util.deployModule(ModuleType.WAR, Util.JSF_PROJECT_PATH, Util.JSF_PROJECT_NAME);
66         } catch(Exception JavaDoc e) {
67             fail(e.getMessage());
68         }
69     }
70     //checks the http error code for web url of the project
71
public void executeJSFModule() {
72         try{
73             
74             int errorcode=Util.executeWebModule(ModuleType.WAR, Util.JSF_PROJECT_NAME);
75             if(errorcode!=200)
76                 throw new Exception JavaDoc("Execution of project failed with errorcode "+errorcode);
77         }catch(Exception JavaDoc e){
78             fail(e.getMessage());
79         }
80     }
81     public void disableJSFModule(){
82         try{
83             ServerInstance si = ServerRegistry.getInstance().getServerInstance(Util._URL);
84             String JavaDoc[] command = new String JavaDoc[]{"disable", Util.JSF_PROJECT_NAME};
85             Process JavaDoc p=Util.runAsadmin(command);
86             Util.sleep(Util.SLEEP);
87             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
88             String JavaDoc errorMess = error.readLine();
89             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
90             String JavaDoc output=input.readLine();
91             if(errorMess!=null)
92                 throw new Exception JavaDoc(errorMess+"\n"+output);
93             System.out.println(output);
94             if(Util.getModuleID(ModuleType.WAR, Util.JSF_PROJECT_NAME, si,true)!=null)
95                 throw new Exception JavaDoc("Disable of application failed.");
96         }catch(Exception JavaDoc e){
97             fail(e.getMessage());
98         }
99         
100     }
101     public void enableJSFModule(){
102         try{
103             ServerInstance si = ServerRegistry.getInstance().getServerInstance(Util._URL);
104             String JavaDoc[] command = new String JavaDoc[]{"enable", Util.JSF_PROJECT_NAME};
105             Process JavaDoc p=Util.runAsadmin(command);
106             Util.sleep(Util.SLEEP);
107             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
108             String JavaDoc errorMess = error.readLine();
109             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
110             String JavaDoc output=input.readLine();
111             if(errorMess!=null)
112                 throw new Exception JavaDoc(errorMess+"\n"+output);
113             System.out.println(output);
114             executeJSFModule();
115         }catch(Exception JavaDoc e){
116             fail(e.getMessage());
117         }
118     }
119     public void undeployJSFModule() {
120         try {
121             ServerInstance si = ServerRegistry.getInstance().getServerInstance(Util._URL);
122             TargetModuleID JavaDoc moduleID = Util.getModuleID(ModuleType.WAR, Util.JSF_PROJECT_NAME, si,false);
123             
124             if(moduleID == null)
125                 return;
126             
127             Util.undeployModule(ModuleType.WAR, Util.JSF_PROJECT_PATH, Util.JSF_PROJECT_NAME, moduleID);
128         } catch(Exception JavaDoc e) {
129             fail(e.getMessage());
130         }
131     }
132     //only runs the verifier, no failures reported- need more work
133
public void verifyJSFModule() {
134         try{
135             File JavaDoc f =new File JavaDoc(Util.JSF_PROJECT_PATH + Util._SEP + "verifier_results");
136             Project project = (Project)Util.openProject(new File JavaDoc(Util.JSF_PROJECT_PATH));
137             ActionProvider ap=(ActionProvider)project.getLookup().lookup(ActionProvider.class);
138              ap.invokeAction("verify", project.getLookup());
139             Util.sleep(10*Util.SLEEP);
140             Util.closeProject(Util.JSF_PROJECT_NAME);
141             Util.sleep(Util.SLEEP);
142             
143         } catch(Exception JavaDoc e){
144             fail(e.getMessage());
145         }
146         
147         
148     }
149     public void updateModule(){
150         try{
151             File JavaDoc javaFile = new File JavaDoc(Util.JSF_PROJECT_PATH+Util._SEP+"src"+Util._SEP+"java"+Util._SEP+"org"+Util._SEP+"demo"+Util._SEP+"Customer.java");
152             File JavaDoc warFile = new File JavaDoc(Util.JSF_PROJECT_PATH+Util._SEP+"dist"+Util._SEP+Util.JSF_PROJECT_NAME+".war");
153             Long JavaDoc initialDate=warFile.lastModified();
154             javaFile.setLastModified(new java.util.Date JavaDoc().getTime());
155             deployJSFModule();
156             if(warFile.lastModified()!= initialDate)
157                 System.out.println("war updated");
158             else
159                 throw new Exception JavaDoc("war was not updated despite java code being modified");
160         }catch(Exception JavaDoc e){
161             fail(e.getMessage());
162         }
163         
164     }
165 //checks the run api
166
public void runJSFModule(){
167         try{
168             Project project = (Project)Util.openProject(new File JavaDoc(Util.JSF_PROJECT_PATH));
169             ActionProvider ap = (ActionProvider)project.getLookup().lookup(ActionProvider.class);
170             J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
171             final ServerInstance si = ServerRegistry.getInstance().getServerInstance(Util._URL);
172             
173             Runnable JavaDoc startCondition = new Runnable JavaDoc() {
174                 public void run() {
175                     while(!si.isConnected()) {
176                         try {
177                             Thread.sleep(5000);
178                         } catch(Exception JavaDoc e) {}
179                     }
180                 }
181             };
182             
183             Runnable JavaDoc runCondition = new Runnable JavaDoc() {
184                 public void run() {
185                     while(Util.getModuleID(ModuleType.WAR, Util.JSF_PROJECT_NAME, si,true) == null) {
186                         try {
187                             
188                             Thread.sleep(5000);
189                         } catch(Exception JavaDoc e) {}
190                     }
191                 }
192             };
193             
194             Task t = RequestProcessor.getDefault().create(startCondition);
195             ap.invokeAction(ap.COMMAND_RUN, project.getLookup());
196             t.run();
197             if(!t.waitFinished(300000))
198                 throw new Exception JavaDoc("Server start timeout");
199             
200             Util.sleep(Util.SLEEP);
201             
202             t = RequestProcessor.getDefault().create(runCondition);
203             t.run();
204             if(!t.waitFinished(300000))
205                 throw new Exception JavaDoc("WEB Application execution timeout");
206             
207             Util. sleep(Util.SLEEP);
208             
209             Util.closeProject(Util.JSF_PROJECT_NAME);
210             
211             Util.sleep(Util.SLEEP);
212             executeJSFModule();
213             
214         }catch(Exception JavaDoc e){
215             fail(e.getMessage());
216         }
217         
218         
219     }
220     
221     public static NbTestSuite suite() {
222         NbTestSuite suite = new NbTestSuite("JSFModuleTest");
223         // TODO : Retouche migration
224
suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance"));
225         suite.addTest(new StartStopServerTest("startServer"));
226         suite.addTest(new JSFModuleTest("deployJSFModule"));
227         suite.addTest(new JSFModuleTest("executeJSFModule"));
228         suite.addTest(new JSFModuleTest("updateModule"));
229         //suite.addTest(new JSFModuleTest("verifyJSFModule"));
230
suite.addTest(new JSFModuleTest("disableJSFModule"));
231         suite.addTest(new JSFModuleTest("enableJSFModule"));
232         suite.addTest(new StartStopServerTest("restartServer"));
233         suite.addTest(new JSFModuleTest("executeJSFModule"));
234         suite.addTest(new JSFModuleTest("runJSFModule"));
235         suite.addTest(new JSFModuleTest("undeployJSFModule"));
236         suite.addTest(new StartStopServerTest("stopServer"));
237         suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance"));
238         return suite;
239     }
240 }
241
Popular Tags