1 19 package validation; 20 21 import java.awt.Component ; 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.net.ServerSocket ; 27 import java.net.URI ; 28 import java.net.URLConnection ; 29 import java.util.Random ; 30 import javax.swing.JComboBox ; 31 import javax.swing.JComponent ; 32 import org.netbeans.jellytools.Bundle; 33 import org.netbeans.jellytools.EditorOperator; 34 import org.netbeans.jellytools.JellyTestCase; 35 import org.netbeans.jellytools.MainWindowOperator; 36 import org.netbeans.jellytools.NbDialogOperator; 37 import org.netbeans.jellytools.OptionsOperator; 38 import org.netbeans.jellytools.OutputTabOperator; 39 import org.netbeans.jellytools.OutputTabOperator; 40 import org.netbeans.jellytools.actions.ActionNoBlock; 41 import org.netbeans.jellytools.modules.debugger.actions.FinishDebuggerAction; 42 import org.netbeans.jellytools.modules.debugger.actions.ToggleBreakpointAction; 43 import org.netbeans.jellytools.modules.j2ee.nodes.J2eeServerNode; 44 import org.netbeans.jellytools.nodes.Node; 45 import org.netbeans.jemmy.ComponentChooser; 46 import org.netbeans.jemmy.EventTool; 47 import org.netbeans.jemmy.JemmyException; 48 import org.netbeans.jemmy.Waitable; 49 import org.netbeans.jemmy.Waiter; 50 import org.netbeans.jemmy.operators.ContainerOperator; 51 import org.netbeans.jemmy.operators.JComboBoxOperator; 52 import org.netbeans.jemmy.operators.JLabelOperator; 53 import org.netbeans.jemmy.operators.JRadioButtonOperator; 54 import org.netbeans.jemmy.operators.JSpinnerOperator; 55 import org.netbeans.jemmy.operators.JTabbedPaneOperator; 56 import org.netbeans.jemmy.operators.JTreeOperator; 57 58 61 public class Utils { 62 63 65 private static int socketPort = 9009; 66 public static final String SUN_APP_SERVER = "Sun"; 67 public static final String TOMCAT = "Tomcat"; 68 public static final String DEFAULT_SERVER = SUN_APP_SERVER; 69 70 71 public static void setTomcatProperties() throws Exception { 72 String toolsItem = Bundle.getStringTrimmed("org.netbeans.core.Bundle", "Menu/Tools"); String serverManagerItem = Bundle.getStringTrimmed( 76 "org.netbeans.modules.j2ee.deployment.impl.ui.actions.Bundle", 77 "CTL_ServerManager"); 78 new ActionNoBlock(toolsItem+"|"+serverManagerItem, null).perform(); 79 String serverManagerTitle = Bundle.getString( 81 "org.netbeans.modules.j2ee.deployment.devmodules.api.Bundle", 82 "TXT_ServerManager"); 83 NbDialogOperator serverManagerOper = new NbDialogOperator(serverManagerTitle); 84 String j2eeLabel = Bundle.getString( 85 "org.netbeans.modules.j2ee.deployment.impl.ui.Bundle", 86 "LBL_J2eeServersNode"); 87 new Node(new JTreeOperator(serverManagerOper), j2eeLabel+"|"+"Bundled Tomcat").select(); JSpinnerOperator serverPortOper = new JSpinnerOperator(serverManagerOper, 0); 90 serverPortOper.getNumberSpinner().scrollToValue((Number )serverPortOper.getNextValue()); 92 serverPortOper.setValue(new Integer (getPort())); 93 JSpinnerOperator shutdownPortOper = new JSpinnerOperator(serverManagerOper, 1); 95 shutdownPortOper.getNumberSpinner().scrollToValue((Number )shutdownPortOper.getNextValue()); 97 shutdownPortOper.setValue(new Integer (getPort())); 98 99 String startupLabel = Bundle.getString("org.netbeans.modules.tomcat5.customizer.Bundle", "TXT_Startup"); 102 new JTabbedPaneOperator(serverManagerOper).selectPage(startupLabel); 103 String socketPortLabel = Bundle.getString("org.netbeans.modules.tomcat5.customizer.Bundle", "TXT_SocketPort"); 105 new JRadioButtonOperator(serverManagerOper, socketPortLabel).setSelected(true); 106 JSpinnerOperator socketPortOper = new JSpinnerOperator(serverManagerOper, 0); 108 socketPortOper.getNumberSpinner().scrollToValue((Number )socketPortOper.getNextValue()); 110 socketPort = getPort(); 111 socketPortOper.setValue(new Integer (socketPort)); 112 113 serverManagerOper.close(); 114 } 115 116 119 public static String getSocketPort() { 120 return Integer.toString(socketPort); 121 } 122 123 126 private static int getPort() throws Exception { 127 int port = 0; 128 boolean notfree = true; 129 while(notfree) { 130 port = 49152+new Random ().nextInt(16383); 131 ServerSocket socket = null; 133 try { 134 socket = new ServerSocket (port); 135 socket.close(); 136 notfree = false; 138 } catch (IOException ioe) { 139 } 141 } 142 return port; 143 } 144 145 146 public static void finishDebugger() { 147 ContainerOperator debugToolbarOper = getDebugToolbar(); 148 new FinishDebuggerAction().perform(); 149 debugToolbarOper.waitComponentVisible(false); 151 J2eeServerNode serverNode = new J2eeServerNode(DEFAULT_SERVER); 153 serverNode.waitFinished(); 154 new EventTool().waitNoEvent(2000); 155 156 165 } 166 167 170 public static ContainerOperator getDebugToolbar() { 171 String debugToolbarLabel = Bundle.getString("org.netbeans.modules.debugger.jpda.ui.Bundle", "Toolbars/Debug"); 172 return MainWindowOperator.getDefault().getToolbar(debugToolbarLabel); 173 } 174 175 public static class ToolTipChooser implements ComponentChooser { 176 private String tooltip; 177 public ToolTipChooser(String tooltip) { 178 this.tooltip = tooltip; 179 } 180 public boolean checkComponent(Component comp) { 181 return tooltip.equals(((JComponent )comp).getToolTipText()); 182 } 183 public String getDescription() { 184 return("ToolTip equals to "+tooltip); 185 } 186 } 187 188 193 public static int setBreakpoint(EditorOperator eo, String text) throws Exception { 194 eo.select(text); final int line = eo.getLineNumber(); 196 new ToggleBreakpointAction().perform(eo.txtEditorPane()); 198 new Waiter(new Waitable() { 200 public Object actionProduced(Object editorOper) { 201 Object [] annotations = ((EditorOperator)editorOper).getAnnotations(line); 202 for (int i = 0; i < annotations.length; i++) { 203 if("Breakpoint".equals(((EditorOperator)editorOper).getAnnotationType(annotations[i]))) { return Boolean.TRUE; 205 } 206 } 207 return null; 208 } 209 public String getDescription() { 210 return("Wait breakpoint established on line "+line); } 212 }).waitAction(eo); 213 return line; 214 } 215 216 217 public static String getDefaultUrl() { 218 if(DEFAULT_SERVER.equals(SUN_APP_SERVER)) { 219 return "http://localhost:8080/"; 220 } else { 221 return "http://localhost:8084/"; 222 } 223 } 224 225 228 public static void reloadPage(final String urlSuffix) { 229 new Thread (new Runnable () { 230 public void run() { 231 try { 232 new URI (getDefaultUrl()+urlSuffix).toURL().openStream(); 233 } catch (Exception e) { 234 e.printStackTrace(); 235 } 236 } 237 }).start(); 238 } 239 240 246 public static void waitText(final String urlSuffix, final long timeout, final String text) { 247 Waitable waitable = new Waitable() { 248 public Object actionProduced(Object obj) { 249 InputStream is = null; 250 try { 251 URLConnection connection = new URI (getDefaultUrl()+urlSuffix).toURL().openConnection(); 252 connection.setReadTimeout(Long.valueOf(timeout).intValue()); 253 is = connection.getInputStream(); 254 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 255 String line = br.readLine(); 256 while(line != null) { 257 if(line.indexOf(text) > -1) { 258 return Boolean.TRUE; 259 } 260 line = br.readLine(); 261 } 262 is.close(); 263 } catch (Exception e) { 264 e.printStackTrace();; 265 return null; 266 } finally { 267 if(is != null) { 268 try { 269 is.close(); 270 } catch (IOException e) { 271 } 273 } 274 } 275 return null; 276 } 277 public String getDescription() { 278 return("Text \""+text+"\" at "+getDefaultUrl()+urlSuffix); 279 } 280 }; 281 Waiter waiter = new Waiter(waitable); 282 waiter.getTimeouts().setTimeout("Waiter.WaitingTime", timeout); 283 try { 284 waiter.waitAction(null); 285 } catch (InterruptedException e) { 286 throw new JemmyException("Exception while waiting for connection.", e); 287 } 288 } 289 290 295 public static void waitFinished(JellyTestCase test, String projectName, String target) { 296 long oldTimeout = MainWindowOperator.getDefault().getTimeouts().getTimeout("Waiter.WaitingTime"); 297 try { 298 MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", 240000); 300 MainWindowOperator.getDefault().waitStatusText("Finished building "+projectName+" ("+target+")"); 301 } finally { 302 MainWindowOperator.getDefault().getStatusTextTracer().start(); 304 MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", oldTimeout); 306 test.getLog("ServerMessages").print(new OutputTabOperator(Utils.DEFAULT_SERVER).getText()); test.getLog("RunOutput").print(new OutputTabOperator(projectName).getText()); } 310 } 311 } 312 | Popular Tags |