emulatorelement.java
来自「MoMEUnit是一个单元测试的J2ME的应用程序xUnit架构实例。这是来自J」· Java 代码 · 共 292 行
JAVA
292 行
package org.momeunit.ant.taskdefs;import java.io.File;import java.util.Properties;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Task;import org.apache.tools.ant.types.Path;import org.apache.tools.ant.types.Reference;import org.apache.tools.ant.util.ClasspathUtils;import org.momeunit.ant.core.Utility;import org.momeunit.ant.emulator.Emulator;/** * MoMEUnit nested tag intended to specify and configure <code>emulator</code> * tool used to run J2ME application. Contains method * {@link #getEmulator(File, ClassLoader)} for accessing configured emulator. * Contains methods for configuring device, classpath and configuration * properties. * * @author Sergio Morozov * @version 1.1.2 */public class EmulatorElement{ public static final String DEFAULT_EMULATOR = "sun"; private EmulatorType type = null; private String className = null; private Properties props = new Properties(); private Task owner = null; private File home = null; private ClasspathUtils.Delegate emulCPDelegate = null; /** * Instantiates EmulatorElement with owning project and task. * * @param task * owning task. * @since 1.1 */ protected EmulatorElement(Task task) { super(); setTask(task); this.emulCPDelegate = ClasspathUtils.getDelegate(task); } /** * Instantiates EmulatorElement. * * @since 1.1 */ protected EmulatorElement() { this(null); } /** * Sets owning task. * * @param task * owning task. * @since 1.1 */ public void setTask(Task task) { this.owner = task; } /** * Returns classname of emulator. * * @return the classname of emulator. * @since 1.1 */ public String getEmulatorClass() { return this.className; } /** * Sets classname of emulator. * * @param emulatorClass * classname of emulator. * @since 1.1 */ public void setClassname(String emulatorClass) { Utility.assertNotEmpty(emulatorClass, "classname", "emulator"); this.className = emulatorClass; } /** * Returns type of emulator. * * @return the type of emulator. * @since 1.1 */ public String getType() { return this.type.getValue(); } /** * Sets type of emulator. * * @param type * type of emulator. * @since 1.1 */ public void setType(String type) { Utility.assertNotEmpty(type, "type", "emulator"); this.type = new EmulatorType(type); } /** * Instantiates specified emulator. * * @param emulClassName * classname of emulator. * @param loader * class loader used to load emulator class. * @return the emulator instance. */ private Emulator createEmulator(String emulClassName, ClassLoader loader) { Emulator res = null; Class emulClass = null; try { if (loader != null) emulClass = loader.loadClass(emulClassName); else emulClass = Class.forName(emulClassName); } catch (ClassNotFoundException e) { throw new BuildException("emulator classname: " + emulClassName + " not found."); } try { res = (Emulator) emulClass.newInstance(); } catch (InstantiationException e) { throw new BuildException("Can't instantiate emulator of class " + emulClassName + "\nMake sure class contains public noarg constructor.", e); } catch (IllegalAccessException e) { throw new BuildException("Error accessing class " + emulClassName, e); } catch (ClassCastException e) { throw new BuildException("Emulator " + emulClassName + " does not extend " + Emulator.class.getName(), e); } return res; } /** * Returns configured emulator. * * @param wtkHome * home directory of WTK. * @param loader * classloader used to load emulator class. * @return configured emulator. * @since 1.1 */ public Emulator getEmulator(File wtkHome, ClassLoader loader) { if (this.type != null) { if (this.className != null) throw new BuildException( "Either type or classname attribute can be set on <emulator>."); this.className = this.type.getClassName(); } Emulator res = createEmulator(this.className != null ? this.className : new EmulatorType(DEFAULT_EMULATOR).getClassName(), loader); res.setTask(this.owner); res.setWtkHome(this.home != null ? this.home : wtkHome); res.setClasspath(this.emulCPDelegate.getClasspath()); res.addProperties(this.props); return res; } /** * Returns the device used to run J2ME application. * * @return the device used to run J2ME application. * @since 1.1 */ public String getDevice() { return this.props.getProperty(Emulator.DEVICE_PROPERTY); } /** * Sets the device used to run J2ME application. * * @param device * the device used to run J2ME application. * @since 1.1 */ public void setDevice(String device) { Utility.assertNotEmpty(device, "device", "emulator"); this.props.setProperty(Emulator.DEVICE_PROPERTY, device); } /** * Adds configuration property. * * @param prop * configuration property to add. * @since 1.1 */ public void addConfiguredProperty(PropertyElement prop) { String name = prop.getName(); Utility.assertNotEmpty(name, "name", "property"); String value = prop.getValue(); this.props.setProperty(name, value != null ? value : ""); } /** * Sets classpath used to run J2ME application. * * @param classpath * classpath used to run J2ME application. * @since 1.1 */ public void setClasspath(Path classpath) { this.emulCPDelegate.setClasspath(classpath); } /** * Returns classpath used to run J2ME application. * * @return classpath used to run J2ME application.. * @since 1.1 */ public Path getClasspath() { return this.emulCPDelegate.getClasspath(); } /** * Returns reference to classpath used to run J2ME application. * * @param classpathref * reference to classpath used to run J2ME application. * @since 1.1 */ public void setClasspathref(Reference classpathref) { this.emulCPDelegate.setClasspathref(classpathref); } /** * Creates path structure of classpath. * * @return new path structure of classpath. * @since 1.1 */ public Path createClasspath() { return this.emulCPDelegate.createClasspath(); } /** * Sets Emulator home directory. * * @param home * the home emulator directory to set. * @since 1.1 */ public void setHome(File home) { Utility.assertDirectory(home, "emulator home "); this.home = home; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?