1 19 20 package org.netbeans.test.j2ee.lib; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStreamReader ; 29 import java.net.URL ; 30 import java.nio.channels.FileChannel ; 31 import java.text.SimpleDateFormat ; 32 import java.util.Date ; 33 import java.util.Locale ; 34 import junit.framework.AssertionFailedError; 35 import org.netbeans.jellytools.Bundle; 36 import org.netbeans.jellytools.EditorOperator; 37 import org.netbeans.jellytools.EditorWindowOperator; 38 import org.netbeans.jellytools.MainWindowOperator; 39 import org.netbeans.jellytools.NbDialogOperator; 40 import org.netbeans.jellytools.OutputTabOperator; 41 import org.netbeans.jellytools.ProjectsTabOperator; 42 import org.netbeans.jellytools.RuntimeTabOperator; 43 import org.netbeans.jellytools.actions.Action; 44 import org.netbeans.jellytools.actions.ActionNoBlock; 45 import org.netbeans.jellytools.nodes.Node; 46 import org.netbeans.jellytools.nodes.ProjectRootNode; 47 import org.netbeans.jemmy.EventTool; 48 import org.netbeans.jemmy.TimeoutExpiredException; 49 import org.netbeans.jemmy.operators.JButtonOperator; 50 import org.netbeans.jemmy.operators.JComboBoxOperator; 51 import org.netbeans.jemmy.operators.JLabelOperator; 52 import org.netbeans.jemmy.operators.JTabbedPaneOperator; 53 import org.netbeans.jemmy.operators.JTextFieldOperator; 54 import org.netbeans.jemmy.operators.JTreeOperator; 55 import org.netbeans.jemmy.operators.Operator; 56 import org.netbeans.junit.NbTestCase; 57 58 62 public class Utils { 63 64 private NbTestCase nbtestcase; 65 private static final String LIBRARY_BUNDLE = "org.netbeans.modules.project.libraries.ui.Bundle"; 66 67 public Utils(NbTestCase nbtestcase) { 68 this.nbtestcase = nbtestcase; 69 } 70 71 public static String getTimeIndex() { 72 return new SimpleDateFormat ("HHmmssS",Locale.US).format(new Date ()); 73 } 74 75 public static void addAppServer() { 76 String path = System.getProperty("j2ee.appserver.path"); 77 if (path == null) { 78 throw new RuntimeException ("Cannot setup appserver, property j2ee.appserver.path is not set."); 79 } 80 String username = System.getProperty("j2ee.appserver.username","admin"); 81 String password = System.getProperty("j2ee.appserver.password","adminadmin"); 82 83 Node node = new Node(new RuntimeTabOperator().getRootNode(),Bundle.getStringTrimmed("org.netbeans.modules.j2ee.deployment.impl.ui.Bundle", "SERVER_REGISTRY_NODE")); 84 node.performPopupActionNoBlock(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.deployment.impl.ui.actions.Bundle", "LBL_Add_Server_Instance")); 85 NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.deployment.impl.ui.wizard.Bundle", "LBL_ASIW_Title")); 86 new JComboBoxOperator(dialog).selectItem("Sun Java System Application Server"); 87 new JButtonOperator(dialog,Bundle.getStringTrimmed("org.openide.Bundle", "CTL_NEXT")).push(); 88 89 if (new JLabelOperator(dialog,1).getText().equalsIgnoreCase(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.sun.ide.j2ee.ui.Bundle", "TITLE_AddServerLocationPanel"))) { 91 new JTextFieldOperator(dialog).setText(""); 92 new JTextFieldOperator(dialog).typeText(path); 93 new JButtonOperator(dialog,Bundle.getStringTrimmed("org.openide.Bundle", "CTL_NEXT")).push(); 94 } 95 new JTextFieldOperator(dialog,0).setText(""); 96 new JTextFieldOperator(dialog,1).setText(""); 97 new JTextFieldOperator(dialog,0).typeText(username); 98 new JTextFieldOperator(dialog,1).typeText(password); 99 new JButtonOperator(dialog,Bundle.getStringTrimmed("org.openide.Bundle", "CTL_FINISH")).push(); 100 new ProjectsTabOperator(); 101 } 102 103 107 public static void startStopServer(boolean start) { 108 RuntimeTabOperator runtimeTab = RuntimeTabOperator.invoke(); 109 Node serverNode = new Node(runtimeTab.getRootNode(), Bundle.getStringTrimmed("org.netbeans.modules.j2ee.deployment.impl.ui.Bundle", "SERVER_REGISTRY_NODE") 110 +"|Application Server"); 111 new org.netbeans.jemmy.EventTool().waitNoEvent(10000); 112 if (start) 113 serverNode.performPopupAction("Start"); 114 else 115 serverNode.performPopupAction("Stop"); 116 new org.netbeans.jemmy.EventTool().waitNoEvent(5000); 117 ProgressSupport.waitFinished((start?"Starting":"Stopping") + " Sun Java System Application Server", 500000); 118 new org.netbeans.jemmy.EventTool().waitNoEvent(2000); 119 } 120 121 public static void prepareDatabase() { 122 123 new Action("Tools|"+Bundle.getStringTrimmed("org.netbeans.modules.derby.Bundle", "LBL_DerbyDatabase")+ 124 "|"+Bundle.getStringTrimmed("org.netbeans.modules.derby.Bundle", "LBL_StartAction"), null).performMenu(); 125 OutputTabOperator outputOper = new OutputTabOperator(Bundle.getStringTrimmed("org.netbeans.modules.derby.Bundle", "LBL_outputtab")); 126 outputOper.getTimeouts().setTimeout("ComponentOperator.WaitStateTimeout", 120000); 127 outputOper.waitText("Server is ready to accept connections on port 1527."); 128 new Node(new RuntimeTabOperator().getRootNode(), "Databases|/sample").performPopupActionNoBlock("Connect"); 129 try { 130 NbDialogOperator dialog = new NbDialogOperator("Connect"); 131 new JTextFieldOperator(dialog,0).typeText("app"); 132 dialog.ok(); 133 } catch (TimeoutExpiredException e) {} 134 } 135 136 public void assertFiles(File dir, String fileNames[], String goldenFilePrefix) throws IOException { 137 AssertionFailedError firstExc = null; 138 for (int i=0; i<fileNames.length; i++) { 139 File file = new File (dir, fileNames[i]); 140 try { 141 File goldenFile = nbtestcase.getGoldenFile(goldenFilePrefix + fileNames[i]); 142 nbtestcase.assertFile("File "+file.getAbsolutePath()+" is different than golden file "+goldenFile.getAbsolutePath()+".", 143 file, 144 goldenFile, 145 new File (nbtestcase.getWorkDir(), fileNames[i]+".diff"), 146 new TrimmingLineDiff()); 147 } catch (AssertionFailedError e) { 148 if (firstExc == null){ 149 firstExc = e; 150 } 151 File copy = new File (nbtestcase.getWorkDirPath(), goldenFilePrefix+fileNames[i]); 152 copyFile(file,copy); 153 } 154 } 155 if (firstExc != null) 156 throw firstExc; 157 } 158 159 165 public static void copyFile(File in, File out) { 166 try { 167 out.createNewFile(); 168 FileChannel srcChannel = new FileInputStream (in).getChannel(); 169 FileChannel dstChannel = new FileOutputStream (out).getChannel(); 170 dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); 171 srcChannel.close(); 172 dstChannel.close(); 173 } catch (IOException ioe) { 174 ioe.printStackTrace(System.err); 175 } 176 } 177 178 184 public static String loadFromURL(String url_string) throws IOException { 185 URL url = new URL (url_string); 186 BufferedReader reader = new BufferedReader (new InputStreamReader (url.openStream())); 187 StringBuffer sb = new StringBuffer (); 188 String line; 189 while ((line = reader.readLine()) != null) { 190 if (sb.length() > 0) 191 sb.append("\n"); 192 sb.append(line); 193 } 194 reader.close(); 195 return sb.toString(); 196 } 197 198 206 public static String deploy(String projectName, String url, boolean projectNameInStatus) throws IOException { 207 JTreeOperator tree = ProjectsTabOperator.invoke().tree(); 208 tree.setComparator(new Operator.DefaultStringComparator(true, true)); 209 Node node = new ProjectRootNode(tree, projectName); 210 node.performPopupAction(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.earproject.ui.Bundle", "LBL_DeployAction_Name")); 211 MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", 600000); 212 MainWindowOperator.getDefault().waitStatusText(Bundle.getString("org.apache.tools.ant.module.run.Bundle", "FMT_finished_target_status", new String [] {(projectNameInStatus?projectName:"build.xml")+" (run-deploy)"})); 213 if (url != null) 214 return Utils.loadFromURL(url); 215 return null; 216 } 217 218 public static String deploy(String projectnName, String url) throws IOException { 219 return deploy(projectnName, url, false); 220 } 221 222 225 226 public static void undeploy(String category, String app) { 227 RuntimeTabOperator runtimeTab = RuntimeTabOperator.invoke(); 228 Node rootNode = new Node(runtimeTab.getRootNode(), Bundle.getStringTrimmed("org.netbeans.modules.j2ee.deployment.impl.ui.Bundle", "SERVER_REGISTRY_NODE") 229 +"|Application Server|" 230 + Bundle.getStringTrimmed("org.netbeans.modules.j2ee.sun.ide.j2ee.runtime.nodes.Bundle", "LBL_Applications") + "|" 231 + category); 232 rootNode.performPopupAction("Refresh"); 233 Node node = new Node(rootNode, app); 234 node.performPopupAction(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.sun.ide.j2ee.runtime.nodes.Bundle", "LBL_Undeploy")); 235 node.waitNotPresent(); 236 } 237 238 public static void undeploy(String app) { 239 undeploy(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.sun.ide.j2ee.runtime.nodes.Bundle", "LBL_AppModules"), app); 240 } 241 242 245 public void checkAndModify(String file, int firstLine, String firstText, 246 int secondLine, String secondText, int insertLine, boolean deleteLine, String insertText) { 247 EditorOperator editor = new EditorWindowOperator().getEditor(file); 248 if (firstText != null) { 249 if (!(editor.getText(firstLine).indexOf(firstText)>=0)) 250 nbtestcase.fail("I expect text '"+firstText+"' on line "+firstLine+" in "+file+"."+ 251 "There is text: '"+editor.getText(firstLine)+"'."); 252 } 253 if (secondText != null) { 254 if (!(editor.getText(secondLine).indexOf(secondText)>=0)) 255 nbtestcase.fail("I expect text '"+secondText+"' on line "+secondLine+" in "+file+"."+ 256 "There is text: '"+editor.getText(secondLine)+"'."); 257 } 258 if (deleteLine) 259 editor.deleteLine(insertLine); 260 if (insertText != null) 261 editor.insert(insertText, insertLine, 1); 262 editor.save(); 263 } 264 265 public static void buildProject(String projectName) { 266 Node node = new ProjectsTabOperator().getProjectRootNode(projectName); 267 node.performPopupAction(Bundle.getStringTrimmed( 268 "org.netbeans.modules.j2ee.earproject.ui.Bundle", "LBL_BuildAction_Name")); 269 MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", 300000); 270 MainWindowOperator.getDefault().waitStatusText(Bundle.getString( 271 "org.apache.tools.ant.module.run.Bundle", "FMT_finished_target_status", 272 new String [] {projectName.replace(' ', '_') + " (dist)"})); 273 new EventTool().waitNoEvent(2500); 274 } 275 276 public static void cleanProject(String projectName) { 277 Node node = new ProjectsTabOperator().getProjectRootNode(projectName); 278 node.performPopupAction(Bundle.getStringTrimmed( 279 "org.netbeans.modules.j2ee.earproject.ui.Bundle", "LBL_CleanAction_Name")); 280 MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", 300000); 281 MainWindowOperator.getDefault().waitStatusText(Bundle.getString( 282 "org.apache.tools.ant.module.run.Bundle", "FMT_finished_target_status", 283 new String [] {projectName.replace(' ', '_') + " (clean)"})); 284 new EventTool().waitNoEvent(2500); 285 } 286 287 public static void createLibrary(String name, String [] jars, String [] srcs, String [] javadocs) { 288 if ((name == null) || (name.indexOf(" ") > -1)) { 289 throw new IllegalArgumentException ("Name cannot be null nor contain spaces"); 290 } 291 if (jars == null) { 292 jars = new String [0]; 293 } 294 if (srcs == null) { 295 srcs = new String [0]; 296 } 297 if (javadocs == null) { 298 javadocs = new String [0]; 299 } 300 new ActionNoBlock("Tools|Library Manager", null).performMenu(); 301 NbDialogOperator ndo = new NbDialogOperator(Bundle.getStringTrimmed( 302 LIBRARY_BUNDLE, "CTL_LibrariesManager")); 303 new JButtonOperator(ndo, Bundle.getStringTrimmed( 304 LIBRARY_BUNDLE, "CTL_NewLibrary")).push(); 305 NbDialogOperator ndo2 = new NbDialogOperator(Bundle.getStringTrimmed( 306 LIBRARY_BUNDLE, "CTL_CreateLibrary")); 307 JTextFieldOperator jtfo = new JTextFieldOperator(ndo2, 0); 308 jtfo.clearText(); 309 jtfo.typeText(name); 310 ndo2.ok(); 311 JTabbedPaneOperator jtpo = new JTabbedPaneOperator(ndo, "Classpath"); 312 for (int i = 0; i < jars.length; i++) { 313 new JButtonOperator(jtpo, "Add JAR/Folder...").push(); 314 ndo2 = new NbDialogOperator("Browse JAR/Folder"); 315 jtfo = new JTextFieldOperator(ndo2, 0); 316 jtfo.clearText(); 317 jtfo.typeText(jars[i]); 318 new JButtonOperator(ndo2, "Add JAR/Folder").push(); 319 } 320 jtpo.selectPage("Sources"); 321 for (int i = 0; i < srcs.length; i++) { 322 new JButtonOperator(jtpo, "Add JAR/Folder...").push(); 323 ndo2 = new NbDialogOperator("Browse JAR/Folder"); 324 jtfo = new JTextFieldOperator(ndo2, 0); 325 jtfo.clearText(); 326 jtfo.typeText(srcs[i]); 327 new JButtonOperator(ndo2, "Add JAR/Folder").push(); 328 } 329 jtpo.selectPage("Javadoc"); 330 for (int i = 0; i < javadocs.length; i++) { 331 new JButtonOperator(jtpo, "Add ZIP/Folder...").push(); 332 ndo2 = new NbDialogOperator("Browse ZIP/Folder"); 333 jtfo = new JTextFieldOperator(ndo2, 0); 334 jtfo.clearText(); 335 jtfo.typeText(javadocs[i]); 336 new JButtonOperator(ndo2, "Add ZIP/Folder").push(); 337 } 338 ndo.ok(); 339 } 340 341 public static void openOutputTab() { 342 new ActionNoBlock("Window|Output", null).performMenu(); 343 } 344 } 345 | Popular Tags |