Author: lzoubek(a)redhat.com
Date: 2010-03-30 05:15:44 -0400 (Tue, 30 Mar 2010)
New Revision: 21131
Added:
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/ActionItemSniffer.java
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/ActionItemWriter.java
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/LabelEntity.java
Log:
added SWTBot extensions usefull when opening views/exports/imports/wizards
Added:
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/ActionItemSniffer.java
===================================================================
---
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/ActionItemSniffer.java
(rev 0)
+++
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/ActionItemSniffer.java 2010-03-30
09:15:44 UTC (rev 21131)
@@ -0,0 +1,318 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.ui.bot.ext.generator;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Stack;
+import java.util.Vector;
+import org.junit.BeforeClass;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Widget;
+import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
+import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
+import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
+import org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory;
+import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
+import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
+import org.jboss.tools.ui.bot.ext.SWTTestExt;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+(a)RunWith(SWTBotJunit4ClassRunner.class)
+public class ActionItemSniffer extends SWTTestExt {
+
+ private static SWTWorkbenchBot bot;
+
+ @Test
+ public void generateLabels() {
+
+ Stack<SWTBotTreeItem> stack = new Stack<SWTBotTreeItem>();
+
+ // retrieve createnew tree items
+ List<LabelEntity> createNew = new Vector<LabelEntity>();
+
+ bot.menu("File").menu("New").menu("Other...").click();
+ SWTBotShell shell = bot.shell("New");
+ shell.activate();
+
+ for (SWTBotTreeItem item : bot.tree().getAllItems()) {
+ getItems(item, stack, createNew);
+ }
+ bot.button("Cancel").click();
+ stack.clear();
+
+ // retrieve perspectives
+ List<LabelEntity> perspectives = new Vector<LabelEntity>();
+ bot.menu("Window").menu("Open
Perspective").menu("Other...").click();
+ shell = bot.shell("Open Perspective");
+ shell.activate();
+ for (int i = 0; i < bot.table().rowCount(); i++) {
+ LabelEntity le = new LabelEntity();
+ le.setName(bot.table().getTableItem(i).getText());
+ perspectives.add(le);
+ }
+ bot.button("Cancel").click();
+ stack.clear();
+
+ // retrieve views
+ List<LabelEntity> views = new Vector<LabelEntity>();
+ bot.menu("Window").menu("Show
View").menu("Other...").click();
+ shell = bot.shell("Show View");
+ shell.activate();
+ for (SWTBotTreeItem item : bot.tree().getAllItems()) {
+ getItems(item, stack, views);
+ }
+ bot.button("Cancel").click();
+ stack.clear();
+
+ // retrieve Import options
+ List<LabelEntity> imports = new Vector<LabelEntity>();
+ bot.menu("File").menu("Import...").click();
+ shell = bot.shell("Import");
+ shell.activate();
+ for (SWTBotTreeItem item : bot.tree().getAllItems()) {
+ getItems(item, stack, imports);
+ }
+ bot.button("Cancel").click();
+ stack.clear();
+
+ // retrieve exports
+ List<LabelEntity> exports = new Vector<LabelEntity>();
+ bot.menu("File").menu("Export...").click();
+ shell = bot.shell("Export");
+ shell.activate();
+ for (SWTBotTreeItem item : bot.tree().getAllItems()) {
+ getItems(item, stack, exports);
+ }
+ bot.button("Cancel").click();
+ stack.clear();
+
+ // retrieve preferences tree
+ List<LabelEntity> preferences = new Vector<LabelEntity>();
+ bot.menu("Window").menu("Preferences").click();
+ shell = bot.shell("Preferences");
+ shell.activate();
+ for (SWTBotTreeItem item : bot.tree().getAllItems()) {
+ getItems(item, stack, preferences);
+ }
+ bot.button("Cancel").click();
+ stack.clear();
+
+ List<LabelEntity> servers = new Vector<LabelEntity>();
+ bot.menu("File").menu("New").menu("Other...").click();
+ shell = bot.shell("New");
+ shell.activate();
+ bot.tree().expandNode("Server").select("Server");
+ bot.button("Next >").click();
+ for (SWTBotTreeItem item : bot.tree().getAllItems()) {
+ getItems(item, stack, servers);
+ }
+ fillEntityDetails(servers, true);
+ bot.button("Cancel").click();
+ stack.clear();
+
+
+ // switch to java perspective
+ bot.menu("Window").menu("Open
Perspective").menu("Other...").click();
+ shell = bot.shell("Open Perspective");
+ shell.activate();
+ bot.table().select("Java");
+ bot.button("OK").click();
+ // first create some project
+ bot.menu("File").menu("New").menu("Other...").click();
+ shell = bot.shell("New");
+ shell.activate();
+ bot.tree().getTreeItem("Java Project").select();
+ bot.button("Next >").click();
+ bot.textWithLabel("Project name:").setText("aaa");
+ bot.sleep(10000);
+ bot.button("Finish").click();
+ bot.sleep(20000);
+ // go through all wizards, click next, sniff labels and back
+ bot.menu("File").menu("New").menu("Other...").click();
+ shell = bot.shell("New");
+ shell.activate();
+ fillEntityDetails(createNew, true);
+ bot.button("Cancel").click();
+
+ bot.menu("File").menu("Import...").click();
+ shell = bot.shell("Import");
+ shell.activate();
+ fillEntityDetails(imports, true);
+ bot.button("Cancel").click();
+
+ bot.menu("File").menu("Export...").click();
+ shell = bot.shell("Export");
+ shell.activate();
+ fillEntityDetails(exports, true);
+ bot.button("Cancel").click();
+
+ // finally generate java sources
+ ActionItemWriter gen = new ActionItemWriter();
+ gen.getEntityMap().put("View", views);
+ gen.getEntityMap().put("Server", servers);
+ gen.getEntityMap().put("Perspective", perspectives);
+ gen.getEntityMap().put("NewObject", createNew);
+ gen.getEntityMap().put("Import", imports);
+ gen.getEntityMap().put("Export", exports);
+ gen.getEntityMap().put("Preference", preferences);
+ try {
+ gen.generateInterfaces();
+ gen.generateClasses();
+ } catch (Exception e) {
+ log.error(e);
+ }
+
+ }
+
+ private void fillEntityDetails(List<LabelEntity> createNew, boolean mustNext) {
+ for (final LabelEntity le : createNew) {
+ // filter some items
+ if (le.getName().equals("Create a Sample Web Service")) {
+ continue;
+ }
+ if (le.getPath().size() > 0 &&
le.getPath().get(0).equals("Test")) {
+ continue;
+ }
+ log.info("Scanning " + le.toString());
+ Iterator<String> iter = le.getPath().iterator();
+
+ SWTBotTreeItem item = bot.tree().getTreeItem(iter.next());
+ item.expand();
+ try {
+
+ while (iter.hasNext()) {
+ String nodeName = iter.next();
+ item = item.getNode(nodeName);
+ item.expand();
+ }
+
+ } catch (WidgetNotFoundException ex) {
+ log.error("Error processing treeView, skipping, item "
+ + le.toString());
+ log.error(ex);
+ continue;
+ }
+ item.select();
+ if (mustNext) {
+ if (bot.button("Next >").isEnabled()) {
+ bot.button("Next >").click();
+ } else {
+
+ log.info("Wizard '" + le.getName()
+ + "' does not lead anywhere");
+ continue;
+ }
+ }
+ // retrieve all interesting labels, checkboxes on form
+ Display.getDefault().syncExec(new Runnable() {
+
+ private boolean isTextFieldLabel(String label) {
+ return label.trim().endsWith(":")
+ || label.trim().endsWith("*");
+ }
+
+ @SuppressWarnings("unchecked")
+ public void run() {
+ List<Label> labels = bot.getFinder().findControls(
+ WidgetMatcherFactory.widgetOfType(Label.class));
+ int i=0;
+ for (Label l : labels) {
+ if (i==0) {
+ i++;
+ continue;
+ }
+ String text = l.getText().replaceAll("\\&", "");
+ if (!"".equals(text) && isTextFieldLabel(text)) {
+ le.getTextFields().add(text.trim());
+ }
+ }
+ List<Widget> chboxes = bot.getFinder().findControls(
+ WidgetMatcherFactory.allOf(WidgetMatcherFactory
+ .withStyle(SWT.CHECK, "SWT.CHECK"),
+ WidgetMatcherFactory
+ .widgetOfType(Button.class)));
+ for (Widget l : chboxes) {
+ String text = ((Button) l).getText().replaceAll("\\&",
+ "");
+ if (!"".equals(text)) {
+ le.getChbFields().add(text);
+ }
+ }
+ }
+ });
+ if (mustNext) {
+ bot.button("< Back").click();
+ }
+ }
+ }
+
+ private void createEntity(Stack<SWTBotTreeItem> stack,
+ List<LabelEntity> entities) {
+ SWTBotTreeItem item = stack.peek();
+ LabelEntity le = new LabelEntity();
+ // log.info("Found entity "+item.getText());
+ le.setName(item.getText());
+ for (SWTBotTreeItem s : stack) {
+ le.getPath().add(s.getText());
+ }
+ entities.add(le);
+ }
+
+ public void getItems(SWTBotTreeItem item, Stack<SWTBotTreeItem> stack,
+ List<LabelEntity> entities) {
+ stack.push(item);
+ item.expand();
+ SWTBotTreeItem[] items = item.getItems();
+ if (items.length == 0) {
+ // printStack(stack);
+ createEntity(stack, entities);
+ } else {
+ for (SWTBotTreeItem i : items) {
+ getItems(i, stack, entities);
+ }
+ }
+ stack.pop();
+ }
+
+ public void getItemsAllNodes(SWTBotTreeItem item,
+ Stack<SWTBotTreeItem> stack, List<LabelEntity> entities) {
+ stack.push(item);
+ item.expand();
+ SWTBotTreeItem[] items = item.getItems();
+ createEntity(stack, entities);
+ if (items.length == 0) {
+ // printStack(stack);
+
+ } else {
+ for (SWTBotTreeItem i : items) {
+ getItems(i, stack, entities);
+ }
+ }
+ stack.pop();
+ }
+
+
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ bot = new SWTWorkbenchBot();
+ try {
+ bot.viewByTitle("Welcome").close();
+ } catch (WidgetNotFoundException ex) {
+
+ }
+ bot.sleep(3000);
+ }
+}
Property changes on:
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/ActionItemSniffer.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/ActionItemWriter.java
===================================================================
---
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/ActionItemWriter.java
(rev 0)
+++
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/ActionItemWriter.java 2010-03-30
09:15:44 UTC (rev 21131)
@@ -0,0 +1,176 @@
+package org.jboss.tools.ui.bot.ext.generator;
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ActionItemWriter {
+
+ private static final String outputDir =
"/data/svn/jbosstools/jst/tests/org.jboss.tools.ui.bot.ext/src";
+ private static final String packageName = "org.jboss.tools.ui.bot.ext.gen";
+ private static final String packageDir = packageName.replaceAll("\\.",
"/");
+ private static final String interfaceName = "IActionItem";
+ private static final String className = "ActionItem";
+ private static final String appName="JBDS 3.0.0-#50-GA";
+ private final Map<String, List<LabelEntity>> entityMap = new
LinkedHashMap<String, List<LabelEntity>>();
+
+ public Map<String, List<LabelEntity>> getEntityMap() {
+ return entityMap;
+ }
+
+ private void genConstants(LabelEntity entity, PrintWriter pw,
+ String prefix) {
+
+ for (String constant : entity.getTextFields()) {
+ addJavaDoc(pw, "text field labeled '" + constant + "'",
prefix);
+ pw.println(prefix + "public static final String TEXT_"
+ + LabelEntity.getConstantName(constant)
+ + " = \"" + constant + "\";");
+
+ }
+ for (String constant : entity.getChbFields()) {
+ addJavaDoc(pw, "checkbox field labeled '" + constant +
"'", prefix);
+ pw.println(prefix + "public static final String CHB_"
+ + LabelEntity.getConstantName(constant)
+ + " = \"" + constant + "\";");
+ }
+
+ }
+ private void writePrintMethod(PrintWriter pw, String prefix) {
+ addJavaDoc(pw, "gets string representation of item", prefix);
+ pw.println(prefix+"public static String getItemString(" + interfaceName +
" item) {");
+ pw.println(prefix);
+ pw.println(prefix+"\tStringBuilder sb = new StringBuilder();");
+ pw.println(prefix+"\tfor (String s :item.getGroupPath()) {");
+ pw.println(prefix+"\t\tsb.append(s+\"->\");");
+ pw.println(prefix+"\t}");
+ pw.println(prefix+"\tsb.append(item.getName());");
+ pw.println(prefix+"\treturn sb.toString();");
+ pw.println(prefix+"}");
+ }
+ /**
+ * generates common IActionItem interface file, for each entity list in entityMap
+ * is also generated interface file derived from IActionItem
+ * @throws Exception
+ */
+ public void generateInterfaces() throws Exception {
+ PrintWriter pw = createFile(interfaceName);
+ addHeader(pw);
+ pw.println("import java.util.List;");
+ pw.println();
+ pw.println("public interface " + interfaceName + " {");
+ pw.println();
+ addJavaDoc(pw, "gets label name", "\t");
+ pw.println("\tpublic String getName();");
+ addJavaDoc(pw, "gets path (in tree,list) to reach leaf returned by
'getName()'", "\t");
+ pw.println("\tpublic List<String> getGroupPath();");
+ pw.println();
+ pw.println("}");
+ pw.println();
+ pw.close();
+ System.out.println("DONE");
+ generateSubIntefaces();
+ }
+
+ private void generateSubIntefaces() throws Exception {
+
+ for (String key : entityMap.keySet()) {
+ PrintWriter pw = createFile("I"+key);
+ addHeader(pw);
+ pw.println("import
"+packageName+"."+interfaceName+";");
+ pw.println();
+ pw.println("public interface I" + key + " extends
"+interfaceName+" {");
+ pw.println("}");
+ pw.close();
+ System.out.println("DONE");
+ pw.println();
+ }
+
+ }
+
+ /**
+ * generates class files of entities
+ * @throws Exception
+ */
+ public void generateClasses() throws Exception {
+ PrintWriter pw = createFile(className);
+ addHeader(pw);
+ pw.println("import java.util.List;");
+ pw.println("import java.util.Vector;");
+ pw.println();
+ pw.println("public class " + className + " {");
+ pw.println();
+ writePrintMethod(pw, "");
+ for (String key : entityMap.keySet()) {
+ genLEClass(pw, key, entityMap.get(key), "\t");
+ pw.println();
+ }
+ pw.println("}");
+ pw.close();
+ System.out.println("DONE");
+ }
+
+ private static void genIFaceImplementation(LabelEntity entity, PrintWriter pw,
+ String prefix, String ifaceName) {
+
+ addJavaDoc(pw, "represents item : "
+ + entity.getDescriptionText(), prefix);
+ pw.println(prefix + "public static final " + ifaceName
+ + " LABEL = new " + ifaceName + "() {");
+ pw.println(prefix + "\tpublic String getName() { return \""
+ + entity.getName() + "\";}");
+ pw.println(prefix + "\tpublic List<String> getGroupPath() {");
+ pw.println(prefix + "\t\tList<String> l = new
Vector<String>();");
+ for (int i = 0; i < entity.getPath().size() - 1; i++) {
+ pw.println(prefix + "\t\tl.add(\"" + entity.getPath().get(i)
+ + "\");");
+ }
+ pw.println(prefix + "\t\treturn l;");
+ pw.println(prefix + "\t}");
+ pw.println(prefix + "};");
+ }
+
+ private void genLEClass(PrintWriter pw, String SubClassName,
+ List<LabelEntity> items, String prefix) {
+ pw.println(prefix + "public static class " + SubClassName + " {");
+ prefix=prefix+"\t";
+ for (LabelEntity le : items) {
+ pw.println(prefix + "public static class "
+ + le.getClassName() + " {");
+ genIFaceImplementation(le, pw, prefix + "\t","I"+SubClassName);
+ genConstants(le, pw, prefix+"\t");
+ pw.println(prefix + "\t}");
+ }
+ pw.println();
+ pw.println(prefix + "}");
+ }
+
+ private static void addJavaDoc(PrintWriter pw,String content, String prefix) {
+ pw.println(prefix+"/**");
+ pw.println(prefix+"* "+content );
+ pw.println(prefix+"*/");
+ }
+ private void addHeader(PrintWriter pw) {
+ addJavaDoc(pw, "Generated "+ new Date()+ " "+appName,
"");
+ pw.println("package " + packageName + ";");
+ pw.println();
+ }
+
+ private PrintWriter createFile(String className) throws Exception {
+ String file = outputDir + "/" + packageDir + "/" + className +
".java";
+ System.out.println("Generating file " + file);
+ new File(outputDir + "/" + packageDir).mkdirs();
+ return new PrintWriter(new File(file));
+ }
+
+ public static void main(String[] args) {
+ try {
+ new ActionItemWriter().generateInterfaces();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}
Property changes on:
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/ActionItemWriter.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/LabelEntity.java
===================================================================
---
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/LabelEntity.java
(rev 0)
+++
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/LabelEntity.java 2010-03-30
09:15:44 UTC (rev 21131)
@@ -0,0 +1,99 @@
+package org.jboss.tools.ui.bot.ext.generator;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.Vector;
+
+public class LabelEntity {
+
+
+ private String name;
+ private List<String> path;
+ private Set<String> textFields;
+ private Set<String> chbFields;
+ public void setName(String name) {
+ this.name = name;
+ }public String getName() {
+ return name;
+ }
+ public List<String> getPath() {
+ if (path==null) {
+ path = new Vector<String>();
+ }
+ return path;
+ }
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ for (String s : getPath()) {
+ sb.append(s+"->");
+ }
+ sb.delete(sb.length()-2,sb.length());
+ sb.append(" " + getName());
+ return sb.toString();
+ }
+
+ public Set<String> getTextFields() {
+ if (textFields==null) {
+ textFields=new HashSet<String>();
+ }
+ return textFields;
+ }
+ public Set<String> getChbFields() {
+ if (chbFields==null) {
+ chbFields=new HashSet<String>();
+ }
+ return chbFields;
+ }
+ private static String commonFiltering(String name) {
+ return name.replaceAll("\\=|\\?|,|\\[|\\]|\\'|\\:|\\.|\\(|\\)|-|/|\\*",
"");
+ }
+ public String getDescriptionText() {
+ StringBuilder sb = new StringBuilder();
+ Iterator<String> iter = this.getPath().iterator();
+ if (iter.hasNext()) {
+ String item=iter.next();
+ sb.append(item);
+ while (iter.hasNext()) {
+ sb.append("->");
+ sb.append(iter.next());
+ }
+ }
+ return sb.toString();
+ }
+ public String getConstantName() {
+ StringBuilder sb = new StringBuilder();
+ Iterator<String> iter = this.getPath().iterator();
+ if (iter.hasNext()) {
+ String item=commonFiltering(iter.next());
+ sb.append(item.replaceAll(" ", "_").toUpperCase());
+ while (iter.hasNext()) {
+ sb.append("__");
+ sb.append(commonFiltering(iter.next()).replaceAll(" ",
"_").toUpperCase());
+ }
+ } else {
+ sb.append(commonFiltering(this.getName()).replaceAll(" ",
"_").toUpperCase());
+ }
+ return sb.toString();
+ }
+ public static String getConstantName(String s) {
+ return commonFiltering(s).replaceAll(" ", "_").toUpperCase();
+ }
+ public String getClassName() {
+ StringBuilder sb = new StringBuilder();
+ Iterator<String> iter = this.getPath().iterator();
+ if (iter.hasNext()) {
+ String item=commonFiltering(iter.next());
+ sb.append(item.replaceAll(" ", ""));
+ while (iter.hasNext()) {
+ //sb.append("X");
+ sb.append(commonFiltering(iter.next()).replaceAll(" ", ""));
+ }
+ } else {
+ sb.append(commonFiltering(this.getName()).replaceAll(" ",
"").toUpperCase());
+ }
+ return sb.toString();
+ }
+}
Property changes on:
trunk/jst/tests/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/generator/LabelEntity.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain