From 87186f2b6b319b9eb0aa6557fd59310a26ef9a9e Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Mon, 15 Sep 2008 12:25:44 +0000 Subject: [PATCH 1/1] Test with RMI git-svn-id: https://svn.argeo.org/slc/trunk@1594 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- sandbox/argeo.slc.jemmytest/.classpath | 3 +- .../.settings/org.eclipse.jdt.core.prefs | 12 ++ .../argeo.slc.jemmytest/autouiserver.jardesc | 16 ++ sandbox/argeo.slc.jemmytest/pom.xml | 151 +++++++++--------- sandbox/argeo.slc.jemmytest/server-old.policy | 11 ++ sandbox/argeo.slc.jemmytest/server.policy | 4 + .../slc/autoui/swingtest/SwingTestJemmy.java | 30 ---- .../slc/autoui/swingtest/SwingTestUi.java | 30 ++-- .../autoui/swingtest/rmi/AutoUiContext.java | 7 + .../autoui/swingtest/rmi/AutoUiServer.java | 8 + .../swingtest/rmi/AutoUiServerImpl.java | 55 +++++++ .../slc/autoui/swingtest/rmi/AutoUiTask.java | 7 + .../slc/autoui/swingtest/JemmyRemoteTest.java | 85 ++++++++++ .../slc/autoui/swingtest/SwingTestJemmy.java | 40 +++++ 14 files changed, 344 insertions(+), 115 deletions(-) create mode 100644 sandbox/argeo.slc.jemmytest/.settings/org.eclipse.jdt.core.prefs create mode 100644 sandbox/argeo.slc.jemmytest/autouiserver.jardesc create mode 100644 sandbox/argeo.slc.jemmytest/server-old.policy create mode 100644 sandbox/argeo.slc.jemmytest/server.policy delete mode 100644 sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/SwingTestJemmy.java create mode 100644 sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiContext.java create mode 100644 sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiServer.java create mode 100644 sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiServerImpl.java create mode 100644 sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiTask.java create mode 100644 sandbox/argeo.slc.jemmytest/src/test/java/org/argeo/slc/autoui/swingtest/JemmyRemoteTest.java create mode 100644 sandbox/argeo.slc.jemmytest/src/test/java/org/argeo/slc/autoui/swingtest/SwingTestJemmy.java diff --git a/sandbox/argeo.slc.jemmytest/.classpath b/sandbox/argeo.slc.jemmytest/.classpath index 449267b0c..bddeaab31 100644 --- a/sandbox/argeo.slc.jemmytest/.classpath +++ b/sandbox/argeo.slc.jemmytest/.classpath @@ -2,7 +2,8 @@ - + + diff --git a/sandbox/argeo.slc.jemmytest/.settings/org.eclipse.jdt.core.prefs b/sandbox/argeo.slc.jemmytest/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..43b6819f9 --- /dev/null +++ b/sandbox/argeo.slc.jemmytest/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Sun Sep 14 07:26:48 CEST 2008 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.1 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.3 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=ignore +org.eclipse.jdt.core.compiler.problem.enumIdentifier=ignore +org.eclipse.jdt.core.compiler.source=1.3 diff --git a/sandbox/argeo.slc.jemmytest/autouiserver.jardesc b/sandbox/argeo.slc.jemmytest/autouiserver.jardesc new file mode 100644 index 000000000..fcde756b1 --- /dev/null +++ b/sandbox/argeo.slc.jemmytest/autouiserver.jardesc @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/sandbox/argeo.slc.jemmytest/pom.xml b/sandbox/argeo.slc.jemmytest/pom.xml index 9cec51cb2..ff6769a0b 100644 --- a/sandbox/argeo.slc.jemmytest/pom.xml +++ b/sandbox/argeo.slc.jemmytest/pom.xml @@ -1,76 +1,81 @@ - 4.0.0 - org.argeo.slc - slc-jemmy - SLC Jemmy - 0.1.1-SNAPSHOT - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.3 - 1.3 - - - - org.apache.maven.plugins - maven-source-plugin - - - attach-sources - package - - jar - - - - - - - - - - org.netbeans - jemmy - 2.2.7.5 - - - commons-logging - commons-logging - 1.1.1 - - - log4j - log4j - 1.2.15 - - - com.sun.jmx - jmxri - - - com.sun.jdmk - jmxtools - - - javax.mail - mail - - - javax.jms - jms - - - - - - - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + 4.0.0 + org.argeo.slc + slc-jemmy + SLC Jemmy + 0.1.1-SNAPSHOT + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.3 + 1.3 + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + package + + jar + + + + + + + + + + org.netbeans + jemmy + 2.2.7.5 + + + junit + junit + 3.8.2 + + + commons-logging + commons-logging + 1.1.1 + + + log4j + log4j + 1.2.15 + + + com.sun.jmx + jmxri + + + com.sun.jdmk + jmxtools + + + javax.mail + mail + + + javax.jms + jms + + + + + + + \ No newline at end of file diff --git a/sandbox/argeo.slc.jemmytest/server-old.policy b/sandbox/argeo.slc.jemmytest/server-old.policy new file mode 100644 index 000000000..de837a57a --- /dev/null +++ b/sandbox/argeo.slc.jemmytest/server-old.policy @@ -0,0 +1,11 @@ +grant codeBase "file:/home/mbaudier/dev/src/slc/sandbox/argeo.slc.jemmytest/bin/" { + permission java.security.AllPermission; +}; + +grant codeBase "file:/home/mbaudier/dev/src/slc/sandbox/argeo.slc.jemmytest/local/autouiserver.jar" { + permission java.security.AllPermission; +}; + +grant codeBase "file:/home/mbaudier/.m2/repository/org/netbeans/jemmy/2.2.7.5/jemmy-2.2.7.5.jar" { + permission java.security.AllPermission; +}; diff --git a/sandbox/argeo.slc.jemmytest/server.policy b/sandbox/argeo.slc.jemmytest/server.policy new file mode 100644 index 000000000..abf784729 --- /dev/null +++ b/sandbox/argeo.slc.jemmytest/server.policy @@ -0,0 +1,4 @@ +grant { + // Allow everything for now + permission java.security.AllPermission; +}; diff --git a/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/SwingTestJemmy.java b/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/SwingTestJemmy.java deleted file mode 100644 index dcdc57cc9..000000000 --- a/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/SwingTestJemmy.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.argeo.slc.autoui.swingtest; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.netbeans.jemmy.ClassReference; -import org.netbeans.jemmy.operators.JButtonOperator; -import org.netbeans.jemmy.operators.JFrameOperator; - -public class SwingTestJemmy { - private final static Log log = LogFactory.getLog(SwingTestJemmy.class); - - public static void main(String[] args) { - try { - ClassReference classReference = new ClassReference( - SwingTestUi.class.getName()); - classReference.startApplication(); - JFrameOperator jFrameOperator = new JFrameOperator( - "HelloWorldSwing"); - JButtonOperator jButtonOperator = new JButtonOperator( - jFrameOperator, "Button"); - jButtonOperator.push(); - String textAfterPush = jButtonOperator.getText(); - log.info("textAfterPush=" + textAfterPush); - - jFrameOperator.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/SwingTestUi.java b/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/SwingTestUi.java index 8333e1c8b..e29f2dd07 100644 --- a/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/SwingTestUi.java +++ b/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/SwingTestUi.java @@ -1,5 +1,6 @@ package org.argeo.slc.autoui.swingtest; +import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -8,24 +9,25 @@ import javax.swing.JFrame; import javax.swing.JLabel; public class SwingTestUi { - private static void createAndShowGUI() { + private static void createAndShowGUI(boolean exitOnClose) { // Create and set up the window. JFrame frame = new JFrame("HelloWorldSwing"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + if (exitOnClose) + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + frame.getContentPane().setLayout(new GridLayout(2, 1)); // Add the ubiquitous "Hello World" label. final JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); - + final JButton button = new JButton("Button"); frame.getContentPane().add(button); button.addActionListener(new ActionListener() { - - @Override public void actionPerformed(ActionEvent e) { - button.setText("Pressed!!"); - - }}); + label.setText("Pressed!!"); + } + }); // Display the window. frame.pack(); @@ -33,11 +35,17 @@ public class SwingTestUi { } public static void main(String[] args) { - // Schedule a job for the event-dispatching thread: - // creating and showing this application's GUI. + boolean noExitOnClose = false; + for (int i = 0; i < args.length; i++) { + if (args[i].equals("noExitOnClose")) { + noExitOnClose = true; + } + } + + final boolean exitOnClose = !noExitOnClose; javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { - createAndShowGUI(); + createAndShowGUI(exitOnClose); } }); } diff --git a/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiContext.java b/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiContext.java new file mode 100644 index 000000000..868a0c927 --- /dev/null +++ b/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiContext.java @@ -0,0 +1,7 @@ +package org.argeo.slc.autoui.swingtest.rmi; + +public interface AutoUiContext { + public Object getLocalRef(String key); + + public void setLocalRef(String key, Object ref); +} diff --git a/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiServer.java b/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiServer.java new file mode 100644 index 000000000..089e6f88e --- /dev/null +++ b/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiServer.java @@ -0,0 +1,8 @@ +package org.argeo.slc.autoui.swingtest.rmi; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +public interface AutoUiServer extends Remote { + public Object executeTask(AutoUiTask task) throws RemoteException; +} diff --git a/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiServerImpl.java b/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiServerImpl.java new file mode 100644 index 000000000..b32b23771 --- /dev/null +++ b/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiServerImpl.java @@ -0,0 +1,55 @@ +package org.argeo.slc.autoui.swingtest.rmi; + +import java.rmi.Naming; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; +import java.util.Map; +import java.util.TreeMap; + +public class AutoUiServerImpl extends UnicastRemoteObject implements + AutoUiServer, AutoUiContext { + private Map map = new TreeMap(); + + public AutoUiServerImpl() throws RemoteException { + super(); + } + + public Object executeTask(AutoUiTask task) throws RemoteException { + try { + return task.execute(this); + } catch (Exception e) { + throw new RemoteException("Coul not execute task.", e); + } + } + + public Object getLocalRef(String key) { + return map.get(key); + } + + public void setLocalRef(String key, Object ref) { + map.put(key, ref); + } + + public static void main(String[] args) { + if (System.getSecurityManager() == null) { + System.setSecurityManager(new SecurityManager()); + } + try { + String name = "AutoUiServer"; + AutoUiServer engine = new AutoUiServerImpl(); + // AutoUiServer stub = (AutoUiServer) UnicastRemoteObject + // .exportObject(engine, 0); + // Registry registry = LocateRegistry.getRegistry(); + // registry.rebind(name, stub); + Naming.rebind(name, engine); + System.out.println("AutoUiServer bound"); + + } catch (Exception e) { + System.err.println("AutoUiServer exception:"); + e.printStackTrace(); + } + + } +} diff --git a/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiTask.java b/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiTask.java new file mode 100644 index 000000000..86dcc1f08 --- /dev/null +++ b/sandbox/argeo.slc.jemmytest/src/main/java/org/argeo/slc/autoui/swingtest/rmi/AutoUiTask.java @@ -0,0 +1,7 @@ +package org.argeo.slc.autoui.swingtest.rmi; + +import java.io.Serializable; + +public interface AutoUiTask extends Serializable { + public Object execute(AutoUiContext context) throws Exception; +} diff --git a/sandbox/argeo.slc.jemmytest/src/test/java/org/argeo/slc/autoui/swingtest/JemmyRemoteTest.java b/sandbox/argeo.slc.jemmytest/src/test/java/org/argeo/slc/autoui/swingtest/JemmyRemoteTest.java new file mode 100644 index 000000000..48bec9c49 --- /dev/null +++ b/sandbox/argeo.slc.jemmytest/src/test/java/org/argeo/slc/autoui/swingtest/JemmyRemoteTest.java @@ -0,0 +1,85 @@ +package org.argeo.slc.autoui.swingtest; + +import java.io.Serializable; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; + +import org.argeo.slc.autoui.swingtest.rmi.AutoUiContext; +import org.argeo.slc.autoui.swingtest.rmi.AutoUiServer; +import org.argeo.slc.autoui.swingtest.rmi.AutoUiTask; +import org.netbeans.jemmy.ClassReference; +import org.netbeans.jemmy.operators.JButtonOperator; +import org.netbeans.jemmy.operators.JFrameOperator; +import org.netbeans.jemmy.operators.JLabelOperator; + +import junit.framework.TestCase; + +public class JemmyRemoteTest extends TestCase implements Serializable { + static final long serialVersionUID = 1l; + + public void testRemote() throws Exception { + if (System.getSecurityManager() == null) { + System.setSecurityManager(new SecurityManager()); + } + String name = "AutoUiServer"; + Registry registry = LocateRegistry.getRegistry("localhost"); + AutoUiServer server = (AutoUiServer) registry.lookup(name); + AutoUiTask startFrame = new AutoUiTask() { + static final long serialVersionUID = 1l; + + public Object execute(AutoUiContext context) throws Exception { + // Start application + ClassReference classReference = new ClassReference( + SwingTestUi.class.getName()); + String[] args = { "noExitOnClose" }; + classReference.startApplication(args); + + // Find components + JFrameOperator jFrameOperator = new JFrameOperator( + "HelloWorldSwing"); + JButtonOperator jButtonOperator = new JButtonOperator( + jFrameOperator, "Button"); + JLabelOperator jLabelOperator = new JLabelOperator( + jFrameOperator, "Hello World"); + + context.setLocalRef("label", jLabelOperator); + context.setLocalRef("button", jButtonOperator); + return null; + } + + }; + + AutoUiTask pushButton = new AutoUiTask() { + static final long serialVersionUID = 1l; + + public Object execute(AutoUiContext context) throws Exception { + JButtonOperator jButtonOperator = (JButtonOperator) context + .getLocalRef("button"); + JLabelOperator jLabelOperator = (JLabelOperator) context + .getLocalRef("label"); + + // Execute actions + jButtonOperator.push(); + + // Performs checks + String textAfterPush = jLabelOperator.getText(); + Boolean pressed = new Boolean(textAfterPush.equals("Pressed!!")); + return pressed; + } + + }; + + server.executeTask(startFrame); + Boolean pressed = (Boolean) server.executeTask(pushButton); + assertTrue("Has been pressed", pressed.booleanValue()); + } + + public static void main(String[] args){ + try { + JemmyRemoteTest jemmyRemoteTest = new JemmyRemoteTest(); + jemmyRemoteTest.testRemote(); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/sandbox/argeo.slc.jemmytest/src/test/java/org/argeo/slc/autoui/swingtest/SwingTestJemmy.java b/sandbox/argeo.slc.jemmytest/src/test/java/org/argeo/slc/autoui/swingtest/SwingTestJemmy.java new file mode 100644 index 000000000..023968c14 --- /dev/null +++ b/sandbox/argeo.slc.jemmytest/src/test/java/org/argeo/slc/autoui/swingtest/SwingTestJemmy.java @@ -0,0 +1,40 @@ +package org.argeo.slc.autoui.swingtest; + +import junit.framework.TestCase; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.netbeans.jemmy.ClassReference; +import org.netbeans.jemmy.operators.JButtonOperator; +import org.netbeans.jemmy.operators.JFrameOperator; +import org.netbeans.jemmy.operators.JLabelOperator; + +public class SwingTestJemmy extends TestCase { + private final static Log log = LogFactory.getLog(SwingTestJemmy.class); + + public void testSimple() throws Exception { + // Start application + ClassReference classReference = new ClassReference(SwingTestUi.class + .getName()); + String[] args = { "noExitOnClose" }; + classReference.startApplication(args); + + // Find components + JFrameOperator jFrameOperator = new JFrameOperator("HelloWorldSwing"); + JButtonOperator jButtonOperator = new JButtonOperator(jFrameOperator, + "Button"); + JLabelOperator jLabelOperator = new JLabelOperator(jFrameOperator, + "Hello World"); + + // Execute actions + jButtonOperator.push(); + + // Performs checks + String textAfterPush = jLabelOperator.getText(); + assertEquals("Pressed!!", textAfterPush); + + // Clean up + jFrameOperator.close(); + } + +} -- 2.39.2