You are viewing a plain text version of this content. The canonical link for it is here.
Posted to kato-commits@incubator.apache.org by sp...@apache.org on 2009/11/12 10:41:27 UTC
svn commit: r835326 - in
/incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck:
api/ harness/ harness/scenario/
Author: spoole
Date: Thu Nov 12 10:41:27 2009
New Revision: 835326
URL: http://svn.apache.org/viewvc?rev=835326&view=rev
Log:
Refactored scenerio setup into more manageable pieces
Added:
incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/CheckPointHandler.java
incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/CheckPointRunner.java
incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ConfigurationExtractor.java
incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/IScenerioSetupHandler.java
incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenarioElement.java
- copied, changed from r834806, incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/AbstractScenarioSetup.java
incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenerioSetupHandler.java
Removed:
incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/api/IScenarioElement.java
incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/AbstractScenarioSetup.java
Modified:
incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/ValueCollector.java
incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenarioLauncher.java
Modified: incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/ValueCollector.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/ValueCollector.java?rev=835326&r1=835325&r2=835326&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/ValueCollector.java (original)
+++ incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/ValueCollector.java Thu Nov 12 10:41:27 2009
@@ -23,7 +23,7 @@
import org.apache.kato.tck.api.TCKException;
-class ValueCollector {
+public class ValueCollector {
private Map variables=new TreeMap();
@@ -78,4 +78,10 @@
list.addAll(variables.keySet());
return list;
}
+ /**
+ * @return
+ */
+ public int size() {
+ return variables.size();
+ }
}
Added: incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/CheckPointHandler.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/CheckPointHandler.java?rev=835326&view=auto
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/CheckPointHandler.java (added)
+++ incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/CheckPointHandler.java Thu Nov 12 10:41:27 2009
@@ -0,0 +1,94 @@
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+
+package org.apache.kato.tck.harness.scenario;
+
+import java.lang.reflect.Method;
+import java.util.LinkedList;
+import java.util.List;
+
+
+class CheckPointHandler {
+
+ private List startedThreads=new LinkedList();
+ private int registeredThreads=0;
+
+ /**
+ * @param execute
+ */
+ public CheckPointHandler() {
+
+ }
+
+ /**
+ * Thread reached completion point..
+ *
+ */
+ public void checkpoint(Method method) {
+
+ startedThreads.remove(method);
+ }
+
+ /**
+ *
+ */
+ public void complete() {
+
+ while (hasOutstandingThreads()) {
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ ;
+ }
+ }
+
+ }
+
+ /**
+ * @return
+ */
+ public int getThreadCount() {
+ return registeredThreads;
+ }
+
+ /**
+ * Handle new testcase configure method
+ *
+ * Do this by creating a thread to run the method
+ *
+ */
+ public void handle(Method method, ScenarioElement testcase) {
+
+
+ CheckPointRunner runner = new CheckPointRunner(this, method,
+ testcase);
+
+ startedThreads.add(method); // keep track of it
+ registeredThreads++;
+
+ Thread t = new Thread(runner, method.getDeclaringClass().getName()
+ + "#" + method.getName());
+ t.start();
+
+ }
+
+ public boolean hasOutstandingThreads() {
+ return startedThreads.size() > 0;
+ }
+
+ public int outstandingThreads() {
+ return startedThreads.size();
+ }
+
+}
\ No newline at end of file
Added: incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/CheckPointRunner.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/CheckPointRunner.java?rev=835326&view=auto
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/CheckPointRunner.java (added)
+++ incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/CheckPointRunner.java Thu Nov 12 10:41:27 2009
@@ -0,0 +1,65 @@
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+
+package org.apache.kato.tck.harness.scenario;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.apache.kato.tck.api.ICheckpoint;
+
+class CheckPointRunner implements Runnable {
+
+ private CheckPointHandler handler = null;
+ private Method method = null;
+ private ScenarioElement scenario = null;
+
+ public CheckPointRunner(CheckPointHandler checkPointHandler,
+ Method method, ScenarioElement scenario) {
+ this.method = method;
+ this.scenario = scenario;
+ this.handler = checkPointHandler;
+ }
+
+ public void run() {
+
+ // run method
+ ICheckpoint checkpoint = new ICheckpoint() {
+
+ public void checkpoint() {
+ handler.checkpoint(method);
+ while (true) {
+ try {
+ Thread.sleep(10000);
+ } catch (InterruptedException e) {
+ }
+ }
+ }
+ };
+
+ try {
+ method.invoke(scenario, new Object[] { checkpoint });
+ // Thread.sleep(10000);
+ } catch (IllegalArgumentException e) {
+ checkpoint.checkpoint();
+
+ } catch (IllegalAccessException e) {
+ checkpoint.checkpoint();
+ } catch (InvocationTargetException e) {
+ checkpoint.checkpoint();
+ }
+
+ }
+
+}
\ No newline at end of file
Added: incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ConfigurationExtractor.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ConfigurationExtractor.java?rev=835326&view=auto
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ConfigurationExtractor.java (added)
+++ incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ConfigurationExtractor.java Thu Nov 12 10:41:27 2009
@@ -0,0 +1,196 @@
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+
+package org.apache.kato.tck.harness.scenario;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.kato.tck.api.TCKException;
+import org.apache.kato.tck.harness.MapCollector;
+import org.apache.kato.tck.harness.ValueCollector;
+
+/**
+ *
+ */
+public class ConfigurationExtractor implements IScenerioSetupHandler{
+ private static File home=new File(System.getProperty("user.home"));
+ private MapCollector envvars = null;
+ private ValueCollector vmargs = null;
+ private String configDirName=null;
+ PrintStream envout=null;
+ PrintStream jvmout=null;
+
+ public ConfigurationExtractor() {
+ envvars=new MapCollector();
+ vmargs=new ValueCollector();
+
+ }
+ private void extractEnvVarConfig(ScenarioElement element, Method method) {
+ try {
+ Class[] parms = method.getParameterTypes();
+ if (parms == null || parms.length == 0) {
+ Class returnType = method.getReturnType();
+ if (returnType.equals(String[].class)) {
+ String[] result = (String[]) method.invoke(element, null);
+ if(result!=null) {
+ for(int i=0;i<result.length;i++) {
+ envvars.collect(result[i], method);
+ }
+ }
+
+ }
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new TCKException("Exception using envvar method " + method, e);
+
+ }
+ }
+
+ /**
+ * Extract configuration information for an instance of IScenarioElement
+ * All declared methods are scanned and those that are
+ * prefixed with "env" or "jvmarg" and have a String[] return value ,are public
+ * and not static are considered. If the method takes parameters it is ignored
+ *
+ *
+ */
+ public void execute(ScenarioElement element) {
+ // get methods
+ Class me = element.getClass();
+ Method m[] = me.getDeclaredMethods();
+
+ // run all methods where the method name starts with configure
+ for (int j = 0; j < m.length; j++) {
+ Method method = m[j];
+
+ // ignore non public and methods that return values
+ if(Modifier.isPublic(method.getModifiers()) ==false) continue;
+ if(Modifier.isStatic(method.getModifiers()) ==true ) continue;
+ if(method.getReturnType().equals(String[].class)==false) continue;
+ if(method.getParameterTypes().length>0) continue;
+
+ String methodName = m[j].getName();
+ if (methodName.startsWith("env")) {
+
+ extractEnvVarConfig(element, method);
+
+ } else if (methodName.startsWith("jvmarg")) {
+ reportVMVar(element, method);
+ }
+
+ }
+ }
+
+ private void reportVMVar(ScenarioElement element, Method method) {
+ try {
+ Class[] parms = method.getParameterTypes();
+ if (parms == null || parms.length == 0) {
+ Class returnType = method.getReturnType();
+ if (returnType.equals(String[].class)) {
+ String[] result = (String[]) method.invoke(element, null);
+ if(result!=null) {
+ for(int i=0;i<result.length;i++) {
+ vmargs.collect(result[i], method);
+ }
+ }
+ }
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new TCKException("Exception using jvmarg method " + method, e);
+
+ }
+ }
+
+ private void writeList(List entries, PrintStream out) {
+
+ Iterator i=entries.iterator();
+ while(i.hasNext()) {
+ out.println(i.next());
+ }
+
+ }
+ /**
+ * @param configdir
+ */
+ public void setConfigDirectory(String configdir) {
+ this.configDirName=configdir;
+
+
+
+
+
+
+ }
+
+ /**
+ * @param envfile
+ * @return
+ * @throws FileNotFoundException
+ */
+ private PrintStream buildStream(File out) throws FileNotFoundException {
+
+ FileOutputStream fos=new FileOutputStream(out);
+ return new PrintStream(fos);
+
+ }
+ /* (non-Javadoc)
+ * @see org.apache.kato.tck.harness.scenario.IScenerioSetupHandler#close()
+ */
+ public void close() {
+ writeList(envvars.asList(),envout);
+ writeList(vmargs.asList(),jvmout);
+ envout.close();
+ jvmout.close();
+
+
+ }
+ /* (non-Javadoc)
+ * @see org.apache.kato.tck.harness.scenario.IScenerioSetupHandler#open()
+ */
+ public void open() throws IOException {
+ if(configDirName!=null) {
+ home=new File(configDirName);
+ home.mkdirs();
+ }
+
+ File envfile=new File(home,"tck.envfile.txt");
+ File vmfile=new File(home,"tck.jvmfile.txt");
+
+ envout=buildStream(envfile);
+ jvmout=buildStream(vmfile);
+
+
+
+
+ }
+ /* (non-Javadoc)
+ * @see org.apache.kato.tck.harness.scenario.IScenerioSetupHandler#getStatus()
+ */
+ public String getStatus() {
+ return "Scenerio Configuration : environmental variables="+envvars.size()+" jvm arguments="+vmargs.size();
+ }
+
+}
Added: incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/IScenerioSetupHandler.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/IScenerioSetupHandler.java?rev=835326&view=auto
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/IScenerioSetupHandler.java (added)
+++ incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/IScenerioSetupHandler.java Thu Nov 12 10:41:27 2009
@@ -0,0 +1,32 @@
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+
+package org.apache.kato.tck.harness.scenario;
+
+import java.io.IOException;
+
+/**
+ *
+ */
+public interface IScenerioSetupHandler {
+
+ void open() throws IOException;
+ void execute(ScenarioElement o);
+ void close();
+ /**
+ * @return
+ */
+ String getStatus();
+
+}
Copied: incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenarioElement.java (from r834806, incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/AbstractScenarioSetup.java)
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenarioElement.java?p2=incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenarioElement.java&p1=incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/AbstractScenarioSetup.java&r1=834806&r2=835326&rev=835326&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/AbstractScenarioSetup.java (original)
+++ incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenarioElement.java Thu Nov 12 10:41:27 2009
@@ -14,17 +14,17 @@
package org.apache.kato.tck.harness.scenario;
-import org.apache.kato.tck.api.IScenarioElement;
+
/**
*
*/
-public class AbstractScenarioSetup {
+public class ScenarioElement {
private ScenarioLauncher launcher=ScenarioLauncher.getDefaultLauncher();
- public void configure(IScenarioElement element) {
+ public ScenarioElement() {
- launcher.configure(element);
+ launcher.register(this);
}
}
Modified: incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenarioLauncher.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenarioLauncher.java?rev=835326&r1=835325&r2=835326&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenarioLauncher.java (original)
+++ incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenarioLauncher.java Thu Nov 12 10:41:27 2009
@@ -14,8 +14,12 @@
package org.apache.kato.tck.harness.scenario;
-import java.lang.reflect.InvocationTargetException;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.PrintStream;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
@@ -27,341 +31,278 @@
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
-import org.apache.commons.cli.ParseException;
import org.apache.kato.tck.api.ICheckpoint;
-import org.apache.kato.tck.api.IScenarioElement;
import org.apache.kato.tck.api.TCKException;
/**
*
*/
public class ScenarioLauncher {
- class CheckPointRunner implements Runnable {
+ private static ScenarioLauncher defaultLauncher = new ScenarioLauncher();
- private Method method=null;
- private IScenarioElement scenario=null;
- private CheckPointHandler handler=null;
-
- public CheckPointRunner(CheckPointHandler checkPointHandler, Method method, IScenarioElement scenario) {
- this.method=method;
- this.scenario=scenario;
- this.handler=checkPointHandler;
- }
+ private IScenerioSetupHandler handler=null;
- public void run() {
-
- // run method
- ICheckpoint checkpoint=new ICheckpoint(){
- public void checkpoint() {
- handler.checkpoint(method);
- while (true) {
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- }
- }
- }};
-
- try {
- method.invoke(scenario, new Object[]{checkpoint});
- // Thread.sleep(10000);
- } catch (IllegalArgumentException e) {
- checkpoint.checkpoint();
-
- } catch (IllegalAccessException e) {
- checkpoint.checkpoint();
- } catch (InvocationTargetException e) {
- checkpoint.checkpoint();
- }
-
-
- }
-
- }
+ private ScenarioLauncher() {
+
+ }
+ /**
+ * Builds a Command line configuration object for use in parsing
+ * the command line
+ * @return
+ */
+ private static Options buildOptions() {
+ Options options = new Options();
+ Option scenario = OptionBuilder.withArgName("id").hasArg()
+ .withDescription("scenario to run").create("scenario");
+ options.addOption(scenario);
- class CheckPointHandler {
-
- private List startedThreads=new LinkedList();
+ Option clazz = OptionBuilder.withArgName("classname").hasArg()
+ .withDescription("classname to run").create("class");
+ options.addOption(clazz);
+
+ Option mode = OptionBuilder.withArgName("modetype").hasArg()
+ .withDescription("run mode: report or execute").create("mode");
+ mode.setRequired(true);
+ options.addOption(mode);
+
+ Option home = OptionBuilder.withArgName("file").hasArg()
+ .withDescription("directory to store configuration (defaults to '.tck' in user home)").create("configdir");
+ options.addOption(home);
- /**
- * Handle new testcase configure method
- *
- * Do this by creating a thread to run the method
- *
- */
- public void handle(Method method,IScenarioElement testcase) {
- CheckPointRunner runner=new CheckPointRunner(this,method,testcase);
- startedThreads.add(method); // keep track of it
- Thread t=new Thread(runner,method.getDeclaringClass().getName()+ "#"+method.getName());
- t.start();
-
- }
-
- public boolean hasOutstandingThreads() {
- return startedThreads.size()>0;
- }
-
-
- /**
- * Thread reached completion point..
- *
- */
- public void checkpoint(Method method) {
-
-
- startedThreads.remove(method);
- }
-
-
- public int outstandingThreads() {
- return startedThreads.size();
- }
-
- /**
- *
- */
- public void complete() {
-
- while(hasOutstandingThreads()) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- ;
- }
- }
-
- }
+ return options;
+ }
+ /**
+ * @return
+ */
+ public static ScenarioLauncher getDefaultLauncher() {
+ return defaultLauncher;
+ }
+ /**
+ * Load a class by name
+ *
+ * @param value dot formed fully qualified name of class to load
+ * @return class loader or null if not available
+ */
+ private static Class loadScenarioClass(String value) {
+ try {
+ Class clazz = Class.forName(value);
- /**
- * @return
- */
- public int getThreadCount() {
- return startedThreads.size();
+ return clazz;
+ } catch (ClassNotFoundException e) {
+ return null;
}
-
- }
- private Options options=null;
- private List anchors=new LinkedList();
- private CheckPointHandler handler=null;
-
- private static ScenarioLauncher defaultLauncher=new ScenarioLauncher();
-
+ }
/**
* @param args
*/
public static void main(String[] args) {
-
- ScenarioLauncher l=getDefaultLauncher();
- if(l.configure(args)==false) {
+ ScenarioLauncher l = getDefaultLauncher();
+ try {
+ if (l.setup(args) == false) {
System.exit(-1);
+ }
+ } catch(Throwable t) {
+ t.printStackTrace();
+ l.printHelp();
+
+ System.exit(-2);
}
- System.out.println("Configure system with "+l.anchors.size()+" anchored objects "+l.handler.getThreadCount()+" configured threads");
- System.exit(0);
+ System.out.println(l.getHandler().getStatus());
+ System.exit(0);
+
}
+
+
+
+
+
/**
* @return
*/
- public static ScenarioLauncher getDefaultLauncher() {
- return defaultLauncher;
+ private IScenerioSetupHandler getHandler() {
+ return handler;
}
+
+
+
+
+
+ private Options options = null;
+
+
+
/**
- * Configure the system based on the scenarios declared
- * in the input parameters
- *
- * @return true or false depending on configuration status
+ * Anchors the provided object in a static list so that
+ * it cannot be garbage collected.
+ *
+ * If the object is an instance of IScenarioElement then
+ * the objects configuration methods are executed according to
+ * the contract decribed by IScenarioElement
+ *
+ * @param o object to configure
*/
- private boolean configure(String[] args) {
+ public void register(ScenarioElement o) {
- options = buildOptions();
-
- // create the parser
- CommandLineParser parser = new GnuParser();
- try {
- // parse the command line arguments
- CommandLine line = parser.parse( options, args );
- if(line.hasOption("class")==false && line.hasOption("scenario")==false) {
- error("at least one of -scenario or -class must be specified");
- return false;
-
- }
- else {
- // all good so far get classes
- List classestoload=new LinkedList();
-
- Option[] parms=line.getOptions();
-
- for(int i=0;i<parms.length;i++) {
- Option parm=parms[i];
- String opt=parm.getOpt();
- if(opt.equals("scenario")) {
- String value=parm.getValue();
- String clazzname="org.apache.kato.tck.scenario"+value+".ScenarioLauncher"+value;
- Class clazz=loadScenarioClass(clazzname);
- if(clazz==null) {
- error("unable to locate scenario class "+clazzname+" to match scenario "+value);
- return false;
- }
- classestoload.add(clazz);
-
- }
- else if(opt.equals("class")) {
- String value=parm.getValue();
- Class clazz=loadClass(value);
- if(clazz==null) {
- error("unable to locate class called "+value);
- return false;
- }
- classestoload.add(clazz);
- }
- }
-
-
-
- // run configuration proper by instantiating the
- // classes. Classes are at liberty to call back into
- // the scenario launcher for purposes of using the
- // thread element configurator
- handler=new CheckPointHandler();
-
- Iterator i=classestoload.iterator();
- while(i.hasNext()) {
- Class clazz=(Class) i.next();
- Object o;
- try {
- o = clazz.newInstance();
- configure(o);
- } catch (Throwable e) {
- e.printStackTrace();
- error("unable to construct scenario for "+clazz);
- return false;
- }
-
-
- }
-
- handler.complete();
-
- return true;
-
- }
-
- }
- catch( ParseException exp ) {
-
- error(exp.getMessage());
- return false;
- }
-
-
+ handler.execute(o);
}
+ /**
+ * Reports error and displays launcher instructions
+ *
+ * @param msg message to report
+ */
private void error(String msg) {
- System.err.println( "Setup failed : "+msg);
- HelpFormatter formatter = new HelpFormatter();
- formatter.printHelp( getClass().getName(), options );
-
+ System.err.println("Setup failed : " + msg);
+ printHelp();
+
}
-
- private static Class loadScenarioClass(String value) {
+ /**
+ * run configuration proper by instantiating the classes. Classes are at
+ * liberty to call back into the scenario launcher for purposes of using the
+ * thread element configurator
+ **/
+
+ private boolean executeScenarioSetup(List classestoload) {
+
- try {
- Class clazz=Class.forName(value);
+
+ Iterator i = classestoload.iterator();
+ while (i.hasNext()) {
+ Class clazz = (Class) i.next();
- return clazz;
- } catch (ClassNotFoundException e) {
- return null;
- }
-
-
- }
-private static Class loadClass(String value) {
-
- try {
- Class clazz=Class.forName(value);
- return clazz;
- } catch (ClassNotFoundException e) {
- return null;
+ try {
+ clazz.newInstance();
+
+
+ } catch (Throwable e) {
+ e.printStackTrace();
+ error("unable to construct scenario for " + clazz);
+ return false;
+ }
+
}
-
-
- }
- private static Options buildOptions() {
- Options options = new Options();
- Option scenario = OptionBuilder.withArgName( "id" )
- .hasArg()
- .withDescription( "scenario to run" )
- .create( "scenario" );
- options.addOption(scenario);
-
- Option clazz = OptionBuilder.withArgName( "classname" )
- .hasArg()
- .withDescription( "classname to run" )
- .create( "class" );
- options.addOption(clazz);
- return options;
+ return true;
}
- public void configure(Object o) {
-
- anchors.add(o);
+
+
+ /**
+ *
+ */
+ private void printHelp() {
+
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp(getClass().getName(), options);
- if(o instanceof IScenarioElement) {
- IScenarioElement element=(IScenarioElement) o;
- configureElement(element);
- }
}
/**
- * @param element
+ * Configure the system based on the scenarios declared in the input
+ * parameters
+ *
+ * @return true or false depending on configuration status
*/
- private void configureElement(IScenarioElement element) {
- // get methods
- Class me=element.getClass();
- Method m[] = me.getMethods();
-
- // run all methods where the method name starts with configure
- for (int j = 0; j < m.length; j++)
- {
- Method method=m[j];
- String methodName=m[j].getName();
- if (methodName.startsWith("config"))
- {
-
-
- try {
- Class[] parms=method.getParameterTypes();
- if(parms==null || parms.length==0) {
- method.invoke(element, null);
- }
- else {
- if(parms[0].isAssignableFrom(ICheckpoint.class)) {
- handler.handle(method,element);
+ private boolean setup(String[] args) {
+
+ options = buildOptions();
+
+ // create the parser
+ CommandLineParser parser = new GnuParser();
+
+ try {
+ // parse the command line arguments
+ CommandLine line = parser.parse(options, args);
+ if (line.hasOption("class") == false
+ && line.hasOption("scenario") == false) {
+ error("at least one of -scenario or -class must be specified");
+ return false;
+
+ } else {
+ // all good so far get classes
+ List classestoload = new LinkedList();
+
+ Option[] parms = line.getOptions();
+
+ for (int i = 0; i < parms.length; i++) {
+ Option parm = parms[i];
+ String opt = parm.getOpt();
+ if (opt.equals("scenario")) {
+ String value = parm.getValue();
+ String clazzname = "org.apache.kato.tck.scenario"
+ + value + ".ScenarioLauncher" + value;
+ Class clazz = loadScenarioClass(clazzname);
+ if (clazz == null) {
+ error("unable to locate scenario class "
+ + clazzname + " to match scenario " + value);
+ return false;
}
- else {
- throw new TCKException("unable to use configure method "+method.toString()+" as has unexpected method signature");
+ classestoload.add(clazz);
+
+ } else if (opt.equals("class")) {
+ String value = parm.getValue();
+ Class clazz = loadScenarioClass(value);
+ if (clazz == null) {
+ error("unable to locate class called " + value);
+ return false;
}
+ classestoload.add(clazz);
}
-
- } catch (Exception e) {
- e.printStackTrace();
- throw new TCKException("Exception using configure method "+method,e);
+ }
+
+
+
+ String mode = line.getOptionValue("mode");
+ if (mode.equals("run")) {
+ handler=new ScenerioSetupHandler();
+ }
+ else if(mode.equals("config")) {
+ ConfigurationExtractor x=new ConfigurationExtractor();
+
+ if(line.hasOption("configdir")) {
+ String configdir=line.getOptionValue("configdir");
+ x.setConfigDirectory(configdir);
+ }
+
+ handler=x;
}
+ else {
+ error("mode option is neither run or config");
+ return false;
+ }
+
+ handler.open();
+
+ // instantiate objects which will call back
+ // to handler to configure or setup
+
+ executeScenarioSetup(classestoload);
+
+ handler.close();
+
+ return true;
+
}
- }
+
+ } catch (Exception exp) {
+
+ throw new TCKException(exp);
+ }
+
}
+
}
Added: incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenerioSetupHandler.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenerioSetupHandler.java?rev=835326&view=auto
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenerioSetupHandler.java (added)
+++ incubator/kato/trunk/org.apache.kato/kato.tck.harness/src/main/java/org/apache/kato/tck/harness/scenario/ScenerioSetupHandler.java Thu Nov 12 10:41:27 2009
@@ -0,0 +1,140 @@
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+
+package org.apache.kato.tck.harness.scenario;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.kato.tck.api.ICheckpoint;
+import org.apache.kato.tck.api.TCKException;
+
+
+/**
+ *
+ */
+public class ScenerioSetupHandler implements IScenerioSetupHandler {
+
+ private CheckPointHandler checkpointhandler = new CheckPointHandler();
+ private List scenarioElements = new LinkedList();
+
+ /**
+ * Configure an instance of IScenarioElement
+ * All declared methods are scanned and those that are
+ * prefixed with "config" and have no return value , are public
+ * and not static are considered.
+ * If the method takes no parameters or its take a single parameter
+ * of type ICheckpoint then it is called otherwise it is ignored
+ *
+ */
+ public void execute(ScenarioElement element) {
+
+ // record now to anchor the object
+ // run configure when all have been registered
+
+ scenarioElements.add(element);
+
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.kato.tck.harness.scenario.IScenerioSetupHandler#close()
+ */
+ public void close() {
+
+ Iterator i=scenarioElements.iterator();
+
+ while(i.hasNext()) {
+
+ ScenarioElement element=(ScenarioElement) i.next();
+
+ configureElement(element);
+ }
+
+ while(checkpointhandler.hasOutstandingThreads()) {
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.kato.tck.harness.scenario.IScenerioSetupHandler#open()
+ */
+ public void open() throws IOException {
+ checkpointhandler=new CheckPointHandler();
+ scenarioElements=new LinkedList();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.kato.tck.harness.scenario.IScenerioSetupHandler#getStatus()
+ */
+ public String getStatus() {
+
+ return "Scenerio Setup : elements registered="+scenarioElements.size()+" threads started="+checkpointhandler.getThreadCount();
+ }
+
+ private void configureElement(ScenarioElement element) {
+
+
+ // get methods
+ Class me = element.getClass();
+ Method m[] = me.getDeclaredMethods();
+
+ // run all methods where the method name starts with configure
+ for (int j = 0; j < m.length; j++) {
+ Method method = m[j];
+ String methodName = method.getName();
+ int mods=method.getModifiers();
+
+ if (methodName.startsWith("config")==false) return;
+
+
+ // ignore non public and methods that return values
+ if(Modifier.isPublic(mods) ==false) continue;
+ if(Modifier.isStatic(mods) ==true ) continue;
+ if(method.getReturnType().equals(void.class)==false) continue;
+
+
+ try {
+ Class[] parms = method.getParameterTypes();
+ if (parms == null || parms.length == 0) {
+ method.invoke(element, null);
+ } else {
+ if (parms[0].isAssignableFrom(ICheckpoint.class)) {
+
+ checkpointhandler.handle(method, element);
+ }
+
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new TCKException("Exception using configure method "
+ + method, e);
+
+ }
+ }
+
+ }
+
+}