From: Jani Laakso Date: Thu, 7 Feb 2008 07:57:55 +0000 (+0000) Subject: Added few tests utility classes that can be used for X-Git-Tag: 6.7.0.beta1~5097 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=615d19da714c73bf3509d532ce21f3c4108e0e2b;p=vaadin-framework.git Added few tests utility classes that can be used for * Logging (displays caller class, method, linenumber which is clickable on Eclipse console) * LogPrintWriter which can be used to gather all data send from server to client (if you wish not to use firebug) * RandomComponents which provides means to create random layouts and random components / demo custom components * Test listeners for checking robustness / memory leaks svn changeset:3721/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/tests/util/Log.java b/src/com/itmill/toolkit/tests/util/Log.java new file mode 100644 index 0000000000..9e9721f876 --- /dev/null +++ b/src/com/itmill/toolkit/tests/util/Log.java @@ -0,0 +1,183 @@ +package com.itmill.toolkit.tests.util; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; + +/** + * + * Execution output and error messages should be handled through this class. It + * is likely that we need these messages back to TT Server at some point just to + * figure out what went wrong. + * + */ +public class Log { + + // 3 (errors only) + // 2 (+ warnings) + // 1 (+logs) + // 0 (all, print messages also to System.out) + public static final int debug = 0; + + // Should class.method() and it's call times be told on debug? + public static final boolean showClassInformation = true; + + public static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + public static int DEBUG = 0; + + public static int LOG = 1; + + public static int WARN = 2; + + public static int ERROR = 3; + + // public List messages = new LinkedList(); + + private static Log log; + + public static HashMap classMethodCallCounter = new HashMap(); + + static { + log = new Log(); + } + + public static void reset() { + // log.messages = new LinkedList(); + classMethodCallCounter = new HashMap(); + } + + public static String getNow() { + return df.format(new Date()); + } + + private Log() { + } + + public static String getSource() { + StackTraceElement[] st = new Throwable().fillInStackTrace() + .getStackTrace(); + try { + String key = ""; + // Class fromCallerClass = Class.forName(st[4].getClassName()); + // String fromMethodName = st[4].getMethodName(); + // Class callerClass = Class.forName(st[3].getClassName()); + String methodName = st[3].getMethodName(); + int line = st[3].getLineNumber(); + + String clazz = st[3].getClassName() + ".java"; + // String clazz = st[3].getClassName().substring( + // st[3].getClassName().lastIndexOf('.') + 1) + // + ".java"; + key = "(" + clazz + ":" + line + ")" + " " + methodName; + Integer value = (Integer) classMethodCallCounter.get(key); + if (value == null) + value = new Integer(1); + else + value = new Integer(value.intValue() + 1); + classMethodCallCounter.put(key, value); + return value.intValue() + ": " + key; + } catch (Exception e) { + return "unknown class.method"; + } + + // List stacks = new ArrayList(); + // for (int i = 0; i < st.length; i++) { + // try { + // Class callerClass = Class.forName(st[i].getClassName()); + // String methodName = st[i].getMethodName(); + // System.out.println(i + " = " + callerClass + ", " + methodName); + // stacks.add(callerClass.getSimpleName()); + // } catch (ClassNotFoundException e) { + // } + // } + // System.out.println("\n"); + // return ""; + + } + + public static String getClassMethodCounters() { + String result = ""; + for (final Iterator it = classMethodCallCounter.keySet().iterator(); it + .hasNext();) { + String key = (String) it.next(); + result += classMethodCallCounter.get(key) + ": " + key + "\n"; + } + return result; + } + + // public String toString() { + // StringBuffer sb = new StringBuffer(2048); + // for (final Iterator it = messages.iterator(); it.hasNext();) { + // Message msg = (Message) it.next(); + // sb.append(msg.toString() + "\n"); + // } + // return sb.toString(); + // } + + public void add(int type, String message) { + String source = getSource(); + // log.messages.add(new Message(DEBUG, message, source)); + if (type >= debug) { + if (showClassInformation) + System.out.println(source + ": " + message); + else + System.out.println(message); + } + } + + public static void debug(String message) { + log.add(DEBUG, message); + } + + public static void log(String message) { + log.add(LOG, message); + } + + public static void warn(String message) { + log.add(WARN, message); + } + + public static void error(String message) { + log.add(ERROR, message); + } + + public class Message { + + private final long timemillis = System.currentTimeMillis(); + + private final int type; + + private final String source; + + private final String message; + + public Message(int type, String message, String source) { + this.source = source; + this.type = type; + this.message = message; + } + + public String toString() { + return df.format(new Date(timemillis)) + ";" + source + ";" + type + + ";" + message; + } + + } + + // public List getMessages() { + // return messages; + // } + + /** + * Simple way to check for memory consumption without profiler. + */ + public static String getMemoryStatistics() { + System.gc(); + return "Memory:\n" + Runtime.getRuntime().totalMemory() + " (Total)\n" + + +Runtime.getRuntime().freeMemory() + " (Free)\n"; + + } +} diff --git a/src/com/itmill/toolkit/tests/util/LogPrintWriter.java b/src/com/itmill/toolkit/tests/util/LogPrintWriter.java new file mode 100644 index 0000000000..80427a995b --- /dev/null +++ b/src/com/itmill/toolkit/tests/util/LogPrintWriter.java @@ -0,0 +1,32 @@ +package com.itmill.toolkit.tests.util; + +import java.io.PrintWriter; +import java.io.Writer; + +/** + * Use for collecting HTTP response. + * + */ +public class LogPrintWriter extends PrintWriter { + + private final StringBuffer result = new StringBuffer(256); + + public LogPrintWriter(Writer out) { + super(out); + } + + public void print(String s) { + result.append(s); + super.print(s); + } + + public void write(String s) { + result.append(s); + super.write(s); + } + + public String getResult() { + return result.toString(); + } + +} \ No newline at end of file diff --git a/src/com/itmill/toolkit/tests/util/MultiListener.java b/src/com/itmill/toolkit/tests/util/MultiListener.java new file mode 100644 index 0000000000..efdc7f9dc8 --- /dev/null +++ b/src/com/itmill/toolkit/tests/util/MultiListener.java @@ -0,0 +1,31 @@ +package com.itmill.toolkit.tests.util; + +import com.itmill.toolkit.data.Container.ItemSetChangeEvent; +import com.itmill.toolkit.data.Container.ItemSetChangeListener; +import com.itmill.toolkit.data.Container.PropertySetChangeEvent; +import com.itmill.toolkit.data.Container.PropertySetChangeListener; +import com.itmill.toolkit.data.Property.ValueChangeEvent; +import com.itmill.toolkit.data.Property.ValueChangeListener; +import com.itmill.toolkit.ui.Button; +import com.itmill.toolkit.ui.Button.ClickEvent; + +public class MultiListener implements Button.ClickListener, + PropertySetChangeListener, ItemSetChangeListener, ValueChangeListener { + + public void buttonClick(ClickEvent event) { + Log.debug("ClickEvent from " + event.getButton().getCaption()); + } + + public void containerPropertySetChange(PropertySetChangeEvent event) { + Log.debug("containerPropertySetChange from " + event.getContainer()); + } + + public void containerItemSetChange(ItemSetChangeEvent event) { + Log.debug("containerItemSetChange from " + event.getContainer()); + } + + public void valueChange(ValueChangeEvent event) { + Log.debug("valueChange from " + event.getProperty()); + } + +} diff --git a/src/com/itmill/toolkit/tests/util/RandomComponents.java b/src/com/itmill/toolkit/tests/util/RandomComponents.java new file mode 100644 index 0000000000..39355686d6 --- /dev/null +++ b/src/com/itmill/toolkit/tests/util/RandomComponents.java @@ -0,0 +1,265 @@ +package com.itmill.toolkit.tests.util; + +import java.util.ArrayList; +import java.util.Random; + +import com.itmill.toolkit.data.Container.ItemSetChangeListener; +import com.itmill.toolkit.data.Container.PropertySetChangeListener; +import com.itmill.toolkit.data.Property.ValueChangeListener; +import com.itmill.toolkit.demo.featurebrowser.ButtonExample; +import com.itmill.toolkit.demo.featurebrowser.ClientCachingExample; +import com.itmill.toolkit.demo.featurebrowser.ComboBoxExample; +import com.itmill.toolkit.demo.featurebrowser.EmbeddedBrowserExample; +import com.itmill.toolkit.demo.featurebrowser.JavaScriptAPIExample; +import com.itmill.toolkit.demo.featurebrowser.LabelExample; +import com.itmill.toolkit.demo.featurebrowser.LayoutExample; +import com.itmill.toolkit.demo.featurebrowser.NotificationExample; +import com.itmill.toolkit.demo.featurebrowser.RichTextExample; +import com.itmill.toolkit.demo.featurebrowser.SelectExample; +import com.itmill.toolkit.demo.featurebrowser.TableExample; +import com.itmill.toolkit.demo.featurebrowser.TreeExample; +import com.itmill.toolkit.demo.featurebrowser.ValueInputExample; +import com.itmill.toolkit.demo.featurebrowser.WindowingExample; +import com.itmill.toolkit.terminal.ExternalResource; +import com.itmill.toolkit.terminal.ThemeResource; +import com.itmill.toolkit.ui.AbstractComponent; +import com.itmill.toolkit.ui.Button; +import com.itmill.toolkit.ui.ComponentContainer; +import com.itmill.toolkit.ui.DateField; +import com.itmill.toolkit.ui.Embedded; +import com.itmill.toolkit.ui.GridLayout; +import com.itmill.toolkit.ui.Label; +import com.itmill.toolkit.ui.Link; +import com.itmill.toolkit.ui.OrderedLayout; +import com.itmill.toolkit.ui.Panel; +import com.itmill.toolkit.ui.Select; +import com.itmill.toolkit.ui.TabSheet; +import com.itmill.toolkit.ui.TextField; + +public class RandomComponents { + + private Random seededRandom = new Random(1); + + public RandomComponents() { + + } + + public void setRandom(Random rand) { + this.seededRandom = rand; + } + + /** + * Get random component container + * + * @param caption + * @return + */ + public ComponentContainer getRandomComponentContainer(String caption) { + ComponentContainer result = null; + final int randint = seededRandom.nextInt(5); + switch (randint) { + case 0: + result = new OrderedLayout(OrderedLayout.ORIENTATION_HORIZONTAL); + ((OrderedLayout) result).setCaption("OrderedLayout_horizontal_" + + caption); + break; + case 1: + result = new OrderedLayout(OrderedLayout.ORIENTATION_VERTICAL); + ((OrderedLayout) result).setCaption("OrderedLayout_vertical_" + + caption); + break; + case 2: + GridLayout gl; + if (seededRandom.nextInt(1) > 0) { + gl = new GridLayout(); + } else { + gl = new GridLayout(seededRandom.nextInt(3) + 1, seededRandom + .nextInt(3) + 1); + } + gl.setCaption("GridLayout_" + caption); + gl.setDescription(gl.getCaption()); + for (int x = 0; x < gl.getColumns(); x++) { + for (int y = 0; y < gl.getRows(); y++) { + // gl.addComponent(getExamplePicture("x=" + x + ", y=" + y), + // x, y); + gl.addComponent(new Label("x=" + x + ", y=" + y)); + } + } + result = gl; + break; + case 3: + result = new Panel(); + ((Panel) result).setCaption("Panel_" + caption); + break; + case 4: + final TabSheet ts = new TabSheet(); + ts.setCaption("TabSheet_" + caption); + // randomly select one of the tabs + final int selectedTab = seededRandom.nextInt(3); + final ArrayList tabs = new ArrayList(); + for (int i = 0; i < 3; i++) { + String tabCaption = "tab" + i; + if (selectedTab == i) { + tabCaption = "tabX"; + } + tabs.add(new OrderedLayout()); + ts.addTab((ComponentContainer) tabs.get(tabs.size() - 1), + tabCaption, null); + } + ts.setSelectedTab((ComponentContainer) tabs.get(selectedTab)); + result = ts; + break; + } + + return result; + } + + public AbstractComponent getRandomComponent(int caption) { + AbstractComponent result = null; + final int randint = seededRandom.nextInt(22); + MultiListener l = new MultiListener(); + switch (randint) { + case 0: + // Label + result = new Label(); + result.setCaption("Label component " + caption); + break; + case 1: + // Button + result = new Button(); + result.setCaption("Button component " + caption); + // some listeners + ((Button) result).addListener((Button.ClickListener) l); + break; + case 2: + // TextField + result = new TextField(); + result.setCaption("TextField component " + caption); + break; + case 3: + // Select + result = new Select("Select component " + caption); + result.setCaption("Select component " + caption); + result.setImmediate(true); + ((Select) result).setNewItemsAllowed(true); + // items + ((Select) result).addItem("first"); + ((Select) result).addItem("first"); + ((Select) result).addItem("first"); + ((Select) result).addItem("second"); + ((Select) result).addItem("third"); + ((Select) result).addItem("fourth"); + // some listeners + ((Select) result).addListener((ValueChangeListener) l); + ((Select) result).addListener((PropertySetChangeListener) l); + ((Select) result).addListener((ItemSetChangeListener) l); + break; + case 4: + // Link + result = new Link("", new ExternalResource("http://www.itmill.com")); + result.setCaption("Link component " + caption); + break; + case 5: + // Link + result = new Panel(); + result.setCaption("Panel component " + caption); + ((Panel) result) + .addComponent(new Label( + "Panel is a container for other components, by default it draws a frame around it's " + + "extremities and may have a caption to clarify the nature of the contained components' purpose." + + " Panel contains an layout where the actual contained components are added, " + + "this layout may be switched on the fly.")); + ((Panel) result).setWidth(250); + break; + case 6: + // Datefield + result = new DateField(); + ((DateField) result).setStyleName("calendar"); + ((DateField) result).setValue(new java.util.Date()); + result.setCaption("Calendar component " + caption); + break; + case 7: + // Datefield + result = new DateField(); + ((DateField) result).setValue(new java.util.Date()); + result.setCaption("Calendar component " + caption); + break; + case 8: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new ButtonExample()); + break; + case 9: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new ClientCachingExample()); + break; + case 10: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new ComboBoxExample()); + break; + case 11: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new EmbeddedBrowserExample()); + break; + case 12: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new JavaScriptAPIExample()); + break; + case 13: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new LabelExample()); + break; + case 14: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new LayoutExample()); + break; + case 15: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new NotificationExample()); + break; + case 16: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new RichTextExample()); + break; + case 17: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new SelectExample()); + break; + case 18: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new TableExample()); + break; + case 19: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new TreeExample()); + break; + case 20: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new ValueInputExample()); + break; + case 21: + result = new OrderedLayout(); + ((OrderedLayout) result).addComponent(new WindowingExample()); + break; + } + + return result; + } + + /** + * Add demo components to given container + * + * @param container + */ + public void fillLayout(ComponentContainer container, int numberOfComponents) { + for (int i = 0; i < numberOfComponents; i++) { + container.addComponent(getRandomComponent(i)); + } + } + + public AbstractComponent getExamplePicture(String caption) { + final ThemeResource res = new ThemeResource("test.png"); + final Embedded em = new Embedded("Embedded " + caption, res); + return em; + } + +} diff --git a/src/com/itmill/toolkit/tests/util/TestClickListener.java b/src/com/itmill/toolkit/tests/util/TestClickListener.java new file mode 100644 index 0000000000..18de177ddd --- /dev/null +++ b/src/com/itmill/toolkit/tests/util/TestClickListener.java @@ -0,0 +1,37 @@ +package com.itmill.toolkit.tests.util; + +import java.util.HashMap; + +import com.itmill.toolkit.ui.Button; +import com.itmill.toolkit.ui.Button.ClickEvent; + +public class TestClickListener implements Button.ClickListener { + + private static final HashMap buttonListeners = new HashMap(); + + String name = ""; + int count = 0; + + public TestClickListener(String name) { + Integer count = null; + try { + count = (Integer) buttonListeners.get(name); + count = new Integer(count.intValue() + 1); + buttonListeners.put(name, count); + } catch (Exception e) { + count = new Integer(1); + buttonListeners.put(name, count); + } + + this.name = name; + this.count = count.intValue(); + + System.out.println("Created listener " + name + ", id=" + count); + } + + public void buttonClick(ClickEvent event) { + System.out + .println("ClickEvent from listener " + name + ", id=" + count); + } + +}