[Arakhnę-Dev] [78] Cleaning source code. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 78
Author: galland
Date: 2009-08-05 22:42:59 +0200 (Wed, 05 Aug 2009)
Log Message:
-----------
Cleaning source code.
Modified Paths:
--------------
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/VMCommandLine.java
Added Paths:
-----------
trunk/arakhneVmutils/java/src/test/java/
trunk/arakhneVmutils/java/src/test/java/org/
trunk/arakhneVmutils/java/src/test/java/org/arakhne/
trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/
trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/VMCommandLineTest.java
trunk/arakhneVmutils/java/src/test/resources/
trunk/arakhneVmutils/java/src/test/resources/org/
trunk/arakhneVmutils/java/src/test/resources/org/arakhne/
trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/
trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test.txt
Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java 2009-08-04 12:28:20 UTC (rev 77)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java 2009-08-05 20:42:59 UTC (rev 78)
@@ -39,7 +39,15 @@
* @version $Name$ $Revision$ $Date$
*/
public class FileSystem {
+
+ /** Character used to specify a file extension.
+ */
+ public static final char EXTENSION_SEPARATOR_CHAR = '.';
+ /** String used to specify a file extension.
+ */
+ public static final String EXTENSION_SEPARATOR = "."; //$NON-NLS-1$
+
/** Replies if the current operating system uses case-sensitive filename.
*
* @return <code>true</code> if the filenames on the current file system are case sensitive,
@@ -69,7 +77,7 @@
* @return the character used to separate the basename and the file extension.
*/
public static char getFileExtensionCharacter() {
- return '.';
+ return EXTENSION_SEPARATOR_CHAR;
}
/** Replies the dirname of the specified file.
@@ -92,6 +100,16 @@
return new File(filename).getName();
}
+ /** Replies the basename of the specified file with the extension.
+ *
+ * @param filename is the name to parse.
+ * @return the basename of the specified file with the extension.
+ */
+ public static String largeBasename(File filename) {
+ if (filename==null) return null;
+ return filename.getName();
+ }
+
/** Reply the basename of the specified file without the last extension.
*
* @param filename is the name to parse.
@@ -148,7 +166,7 @@
String largeBasename = new File(filename).getName();
int idx = largeBasename.lastIndexOf(getFileExtensionCharacter());
if (idx<=0) return ""; //$NON-NLS-1$
- return largeBasename.substring(idx+1);
+ return largeBasename.substring(idx);
}
/** Reply the extension of the specified file.
@@ -213,11 +231,19 @@
*/
public static String join(String... elements) {
StringBuffer buf = new StringBuffer();
- for (String elt : elements) {
- if (elt!=null && elt.length()>0) {
- if (buf.length()>0) buf.append(File.separatorChar);
+ boolean first = true;
+ boolean empty;
+ for(String elt : elements) {
+ empty = (elt==null || elt.length()==0);
+ if (first && empty) {
+ buf.append(File.separatorChar);
+ }
+ else if (!empty) {
+ if (buf.charAt(buf.length()-1)!=File.separatorChar)
+ buf.append(File.separatorChar);
buf.append(elt);
}
+ first = false;
}
return buf.toString();
}
@@ -233,6 +259,7 @@
*/
public static boolean hasExtension(String filename, String extension) {
if (filename==null) return false;
+ if (!extension.startsWith(EXTENSION_SEPARATOR)) extension = EXTENSION_SEPARATOR+extension;
String ext = extension(filename);
if (ext==null) return false;
if (isCaseSensitiveFilenameSystem())
@@ -266,6 +293,16 @@
return filename.substring(0,idx);
}
+ /** Remove the extension from the specified filename.
+ *
+ * @param filename is the filename to parse.
+ * @return the filename without the extension.
+ */
+ public static String removeExtension(File filename) {
+ if (filename==null) return null;
+ return removeExtension(filename.getAbsolutePath());
+ }
+
/** Replace the extension of the specified filename by the given extension.
* If the filename has no extension, the specifiedone will be added.
*
@@ -275,12 +312,14 @@
*/
public static String replaceExtension(String filename, String extension) {
if (filename==null) return null;
- int idx = filename.lastIndexOf(getFileExtensionCharacter());
+ if (!extension.startsWith(EXTENSION_SEPARATOR)) extension = EXTENSION_SEPARATOR+extension;
+ int idx = filename.lastIndexOf(EXTENSION_SEPARATOR_CHAR);
StringBuffer buf = new StringBuffer();
if (idx<=0) buf.append(filename);
- else buf.append(filename.substring(0, idx));
- buf.append(getFileExtensionCharacter());
- buf.append(extension);
+ else {
+ buf.append(filename.substring(0, idx));
+ buf.append(extension);
+ }
return buf.toString();
}
@@ -457,6 +496,54 @@
return null;
}
+ /** Replies the system shared library directory for the specified software.
+ * <p>
+ * On Unix operating systems, the system directory for a
+ * software is by default {@code /usr/lib/software} where {@code software}
+ * is the given parameter (case-sensitive). On Windows® operating systems, the user
+ * directory for a software is by default
+ * {@code C:<span>\</span>Program Files<span>\</span>software}
+ * where {@code software} is the given parameter (case-insensitive).
+ *
+ * @param software is the name of the concerned software.
+ * @return the configuration directory of the software for the current user.
+ */
+ public static File getSystemSharedLibraryDirectoryFor(String software) {
+ OperatingSystem os = OperatingSystem.getCurrentOS();
+ if (os.isUnixCompliant()) {
+ File[] roots = File.listRoots();
+ return new File(new File(join(roots[0].getAbsolutePath(),"usr","lib")), software); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ else if (os==OperatingSystem.WIN) {
+ File pfDirectory;
+ for(File root : File.listRoots()) {
+ pfDirectory = new File(root, "Program Files"); //$NON-NLS-1$
+ if (pfDirectory.isDirectory()) {
+ return new File(root, software);
+ }
+ }
+ }
+ return null;
+ }
+
+ /** Replies the system shared library directory for the specified software.
+ * <p>
+ * On Unix operating systems, the system directory for a
+ * software is by default {@code /usr/lib/software} where {@code software}
+ * is the given parameter (case-sensitive). On Windows® operating systems, the user
+ * directory for a software is by default
+ * {@code C:<span>\</span>Program Files<span>\</span>software}
+ * where {@code software} is the given parameter (case-insensitive).
+ *
+ * @param software is the name of the concerned software.
+ * @return the configuration directory of the software for the current user.
+ */
+ public static String getSystemSharedLibraryDirectoryNameFor(String software) {
+ File f = getSystemSharedLibraryDirectoryFor(software);
+ if (f==null) return null;
+ return f.getAbsolutePath();
+ }
+
/** Convert an URL which represents a local file into a File.
*
* @param url is the URL to convert.
@@ -554,7 +641,8 @@
if (allowResourceSearch) {
if (urlDescription.toLowerCase().startsWith("resource:")) { //$NON-NLS-1$
- url = ClassLoader.getSystemResource(urlDescription.substring(9));
+ String resourceName = urlDescription.substring(9);
+ url = ClassLoader.getSystemResource(resourceName);
if (url!=null) return url;
}
Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/VMCommandLine.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/VMCommandLine.java 2009-08-04 12:28:20 UTC (rev 77)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/VMCommandLine.java 2009-08-05 20:42:59 UTC (rev 78)
@@ -1,7 +1,7 @@
/*
* $Id$
*
- * Copyright (C) 2004-2008 Stéphane GALLAND
+ * Copyright (C) 2004-2009 Stéphane GALLAND
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -26,6 +26,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
@@ -35,14 +36,14 @@
* This utility class permits to get the java command line for the current VM.
*
* @author Stéphane GALLAND <galland@xxxxxxxxxxx>
- * @version $Name: $ $Revision: 1.1 $ $Date: 2007-02-20 08:52:28 $
+ * @version $Name$ $Revision$ $Date$
*/
public class VMCommandLine {
- private static Class<?> __classToLaunch = null;
+ private static Class<?> classToLaunch = null;
private static boolean analyzed = false;
- private static SortedMap<String,String> __options = null;
- private static String[] __parameters = null;
+ private static SortedMap<String,List<Object>> commandLineOptions = null;
+ private static String[] commandLineParameters = null;
/** Replies a binary executable filename depending of the current platform.
*
@@ -104,10 +105,10 @@
* @param parameters is the parameters to pass to the <code>main</code>.
*/
public static void saveVMParameters(Class<?> class_to_launch, String... parameters) {
- __classToLaunch = class_to_launch;
- __parameters = parameters;
- if (__options!=null) __options.clear();
- __options = null;
+ classToLaunch = class_to_launch;
+ commandLineParameters = parameters;
+ if (commandLineOptions!=null) commandLineOptions.clear();
+ commandLineOptions = null;
analyzed = false;
}
@@ -118,7 +119,7 @@
* @param parameters is the parameters to pass to the <code>main</code>.
*/
public static void saveVMParametersIfNotSet(Class<?> class_to_launch, String... parameters) {
- if (__classToLaunch==null) {
+ if (classToLaunch==null) {
saveVMParameters(class_to_launch, parameters);
}
}
@@ -130,35 +131,54 @@
* @throws IOException
*/
public static Process relaunchVM() throws IOException {
- if (__classToLaunch==null) return null;
- return launchVM(__classToLaunch, getAllCommandLineParameters());
+ if (classToLaunch==null) return null;
+ return launchVM(classToLaunch, getAllCommandLineParameters());
}
- /** Replies the command line parameters.
+ /** Replies the command line inclusing the options and the standard parameters.
*
- * @return the command line parameters.
+ * @return the command line.
*/
protected static String[] getAllCommandLineParameters() {
- int osize = __options==null ? 0 : __options.size();
- int psize = __parameters==null ? 0 : __parameters.length;
+ int osize = commandLineOptions==null ? 0 : commandLineOptions.size();
+ int psize = commandLineParameters==null ? 0 : commandLineParameters.length;
int tsize = (osize>0 && psize>0) ? 1 : 0;
- String[] params = new String[osize+psize+tsize];
+ List<String> params = new ArrayList<String>(osize+tsize);
if (osize>0) {
- int i=0;
- String value;
- for(Entry<String,String> entry : __options.entrySet()) {
- value = entry.getValue();
- if ((value!=null)&&(!"".equals(value))) //$NON-NLS-1$
- params[i] = "-"+entry.getKey()+"="+entry.getValue(); //$NON-NLS-1$ //$NON-NLS-2$
- else
- params[i] = "-"+entry.getKey(); //$NON-NLS-1$
- i++;
+ List<Object> values;
+ String name, prefix, v;
+ for(Entry<String,List<Object>> entry : commandLineOptions.entrySet()) {
+ name = entry.getKey();
+ prefix = (name.length()>1) ? "--" : "-"; //$NON-NLS-1$ //$NON-NLS-2$
+ values = entry.getValue();
+ if (values==null || values.isEmpty()) {
+ params.add(prefix+name);
+ }
+ else {
+ for(Object value : values) {
+ if (value!=null) {
+ v = value.toString();
+ if (v!=null && v.length()>0) {
+ params.add(prefix+name+"="+v); //$NON-NLS-1$
+ }
+ else {
+ params.add(prefix+name);
+ }
+ }
+ }
+ }
}
}
- if (tsize>0) params[osize] = "--"; //$NON-NLS-1$
+ if (tsize>0) params.add("--"); //$NON-NLS-1$
+
+ String[] tab = new String[params.size()+psize];
+ params.toArray(tab);
+ params.clear();
+
if (psize>0)
- System.arraycopy(__parameters, 0, params, osize+tsize, psize);
- return params;
+ System.arraycopy(commandLineParameters, 0, tab, osize+tsize, psize);
+
+ return tab;
}
/** Replies the command line parameters.
@@ -166,7 +186,7 @@
* @return the list of the parameters on the command line
*/
public static String[] getCommandLineParameters() {
- return __parameters==null ? new String[0] : __parameters;
+ return commandLineParameters==null ? new String[0] : commandLineParameters;
}
/** Shift the command line parameters by one on the left.
@@ -176,42 +196,181 @@
*/
public static String shiftCommandLineParameters() {
String removed = null;
- if (__parameters!=null) {
- if (__parameters.length==0) {
- __parameters = null;
+ if (commandLineParameters!=null) {
+ if (commandLineParameters.length==0) {
+ commandLineParameters = null;
}
- else if (__parameters.length==1) {
- removed = __parameters[0];
- __parameters = null;
+ else if (commandLineParameters.length==1) {
+ removed = commandLineParameters[0];
+ commandLineParameters = null;
}
else {
- removed = __parameters[0];
- String[] newTab = new String[__parameters.length-1];
- System.arraycopy(__parameters,1,newTab,0,__parameters.length-1);
- __parameters = newTab;
+ removed = commandLineParameters[0];
+ String[] newTab = new String[commandLineParameters.length-1];
+ System.arraycopy(commandLineParameters,1,newTab,0,commandLineParameters.length-1);
+ commandLineParameters = newTab;
}
}
return removed;
}
- /** Replies the command line parameters.
+ /** Replies the command line options.
*
* @return the list of options passed on the command line
*/
- public static SortedMap<String,String> getCommandLineOptions() {
- if (__options!=null)
- return Collections.unmodifiableSortedMap(__options);
- return new TreeMap<String,String>();
+ public static Map<String,List<Object>> getCommandLineOptions() {
+ if (commandLineOptions!=null)
+ return Collections.unmodifiableSortedMap(commandLineOptions);
+ return Collections.emptyMap();
}
+ /** Replies one command option.
+ *
+ * @param name is the name of the option
+ * @return the option value or <code>null</code> if the option is not on the command line.
+ */
+ public static List<Object> getCommandLineOption(String name) {
+ if (commandLineOptions!=null) {
+ if (commandLineOptions.containsKey(name)) {
+ List<Object> value = commandLineOptions.get(name);
+ return value==null ? Collections.emptyList() : value;
+ }
+ }
+ return null;
+ }
+
+ /** Replies if an option was specified on the command line.
+ *
+ * @param name is the name of the option
+ * @return <code>true</code> if the option was found on the command line, otherwise <code>false</code>.
+ */
+ public static boolean hasCommandLineOption(String name) {
+ return (commandLineOptions!=null && commandLineOptions.containsKey(name));
+ }
+
+ private static boolean registerOptionValue(SortedMap<String,List<Object>> options, String name, Object value, OptionType type) {
+ boolean success = true;
+
+ List<Object> values = options.get(name);
+ if (values==null) {
+ values = new ArrayList<Object>();
+ options.put(name, values);
+ }
+ switch(type) {
+ case AUTO_INCREMENTED:
+ {
+ long v;
+ if (values.isEmpty()) v = -1;
+ else {
+ value = values.get(0);
+ if (value==null) v = 0;
+ else if (!(value instanceof Number)) {
+ v = Long.parseLong(value.toString());
+ }
+ else {
+ v = ((Number)value).longValue();
+ }
+ }
+ if (values.isEmpty()) values.add(Long.valueOf(v+1));
+ else values.set(0, Long.valueOf(v+1));
+ }
+ break;
+ case FLAG:
+ if (value==null) {
+ if (values.isEmpty())
+ value = Boolean.TRUE;
+ else
+ value = values.get(0);
+ }
+ else if (!(value instanceof Boolean)) {
+ value = Boolean.parseBoolean(value.toString());
+ }
+ if (values.isEmpty()) values.add(value);
+ else values.set(0, value);
+ break;
+ case MANDATORY_BOOLEAN:
+ case OPTIONAL_BOOLEAN:
+ if (value==null) {
+ value = Boolean.TRUE;
+ }
+ else if (!(value instanceof Boolean)) {
+ value = Boolean.parseBoolean(value.toString());
+ }
+ values.add(value);
+ break;
+ case MANDATORY_FLOAT:
+ case OPTIONAL_FLOAT:
+ try {
+ if (value==null) {
+ value = Double.valueOf(0.);
+ }
+ else if (!(value instanceof Number)) {
+ value = Double.parseDouble(value.toString());
+ }
+ else {
+ value = Double.valueOf(((Number)value).doubleValue());
+ }
+ }
+ catch(NumberFormatException e) {
+ if (type.isOptional()) {
+ success = false;
+ value = Double.valueOf(0.);
+ }
+ else throw e;
+ }
+ values.add(value);
+ break;
+ case MANDATORY_INTEGER:
+ case OPTIONAL_INTEGER:
+ try {
+ if (value==null) {
+ value = Long.valueOf(0);
+ }
+ else if (!(value instanceof Number)) {
+ value = Long.parseLong(value.toString());
+ }
+ else {
+ value = Long.valueOf(((Number)value).longValue());
+ }
+ }
+ catch(NumberFormatException e) {
+ if (type.isOptional()) {
+ success = false;
+ value = Long.valueOf(0);
+ }
+ else throw e;
+ }
+ values.add(value);
+ break;
+ case MANDATORY_STRING:
+ case OPTIONAL_STRING:
+ values.add(value==null ? "" : value.toString()); //$NON-NLS-1$
+ break;
+ case SIMPLE:
+ values.add(value);
+ break;
+ }
+
+ return success;
+ }
+
/** Analyse the command line to extract the options.
* <p>
* The options will be recognized thanks to the <var>optionDefinitions</var>.
* Each entry of <var>optionDefinitions</var> describes an option. They must
* have one of the following formats:
* <ul>
- * <li>{@code name>}: a simple option without value or flag,</li>
- * <li>{@code name=>}: an option with a mandatory string value,</li>
+ * <li>{@code name}: a simple option without value or flag,</li>
+ * <li>{@code name=s}: an option with a mandatory string value,</li>
+ * <li>{@code name:s}: an option with an optional string value,</li>
+ * <li>{@code name=i}: an option with a mandatory integer value,</li>
+ * <li>{@code name:i}: an option with an optional integer value,</li>
+ * <li>{@code name=f}: an option with a mandatory floating-point value,</li>
+ * <li>{@code name:f}: an option with an optional floating-point value,</li>
+ * <li>{@code name=b}: an option with a mandatory boolean value,</li>
+ * <li>{@code name:b}: an option with an optional boolean value,</li>
+ * <li>{@code name+}: an option with an autoincremented integer value,</li>
+ * <li>{@code name!}: an option which could be flaged or not: {@code --name} or {@code --noname}.</li>
* </ul>
*
* @param optionDefinitions is the list of definitions of the available command line options.
@@ -219,37 +378,88 @@
public static void splitOptionsAndParameters(String... optionDefinitions) {
if (analyzed) return;
+ List<String> params = new ArrayList<String>();
+ SortedMap<String,List<Object>> options = new TreeMap<String,List<Object>>();
+ String opt;
+
// Analyze definitions
- Map<String,Integer> defs = new TreeMap<String,Integer>();
+ Map<String,OptionType> defs = new TreeMap<String,OptionType>();
for (String def : optionDefinitions) {
- if (def.endsWith("=")) { //$NON-NLS-1$
- defs.put(def.substring(0, def.length()-1), 1);
+ if (def.endsWith("!")) { //$NON-NLS-1$
+ opt = def.substring(0, def.length()-1);
+ defs.put(opt, OptionType.FLAG);
+ registerOptionValue(options, opt, Boolean.FALSE, OptionType.FLAG);
}
+ else if (def.endsWith("+")) { //$NON-NLS-1$
+ opt = def.substring(0, def.length()-1);
+ defs.put(opt, OptionType.AUTO_INCREMENTED);
+ registerOptionValue(options, opt, Long.valueOf(0), OptionType.AUTO_INCREMENTED);
+ }
+ else if (def.endsWith("=b")) { //$NON-NLS-1$
+ opt = def.substring(0, def.length()-2);
+ defs.put(opt, OptionType.MANDATORY_BOOLEAN);
+ }
+ else if (def.endsWith(":b")) { //$NON-NLS-1$
+ opt = def.substring(0, def.length()-2);
+ defs.put(opt, OptionType.OPTIONAL_BOOLEAN);
+ }
+ else if (def.endsWith("=f")) { //$NON-NLS-1$
+ opt = def.substring(0, def.length()-2);
+ defs.put(opt, OptionType.MANDATORY_FLOAT);
+ }
+ else if (def.endsWith(":f")) { //$NON-NLS-1$
+ opt = def.substring(0, def.length()-2);
+ defs.put(opt, OptionType.OPTIONAL_FLOAT);
+ }
+ else if (def.endsWith("=i")) { //$NON-NLS-1$
+ opt = def.substring(0, def.length()-2);
+ defs.put(opt, OptionType.MANDATORY_INTEGER);
+ }
+ else if (def.endsWith(":i")) { //$NON-NLS-1$
+ opt = def.substring(0, def.length()-2);
+ defs.put(opt, OptionType.OPTIONAL_INTEGER);
+ }
+ else if (def.endsWith("=s")) { //$NON-NLS-1$
+ opt = def.substring(0, def.length()-2);
+ defs.put(opt, OptionType.MANDATORY_STRING);
+ }
+ else if (def.endsWith(":s")) { //$NON-NLS-1$
+ opt = def.substring(0, def.length()-2);
+ defs.put(opt, OptionType.OPTIONAL_STRING);
+ }
else {
- defs.put(def, 0);
+ defs.put(def, OptionType.SIMPLE);
}
}
- ArrayList<String> params = new ArrayList<String>();
- SortedMap<String,String> options = new TreeMap<String,String>();
-
int idx;
- String opt, base, val;
- Integer code;
- String waitingValue = null;
+ String base, nbase, val;
+ OptionType type;
+ OptionType waitingValue = null;
+ String valueOptionName = null;
boolean allParameters = false;
+ boolean success;
- for(String param : __parameters) {
+ for(String param : commandLineParameters) {
if (allParameters) {
params.add(param);
continue;
}
- else if (waitingValue!=null){
- options.put(waitingValue, param);
+
+ if (waitingValue!=null && waitingValue.isMandatory()) {
+ // Expect a value as the next parameter
+ success = registerOptionValue(options, valueOptionName, param, waitingValue);
waitingValue = null;
- continue;
+ valueOptionName = null;
+ if (success) continue;
}
- else if ("--".equals(param)) { //$NON-NLS-1$
+
+ if ("--".equals(param)) { //$NON-NLS-1$
+ if (waitingValue!=null) {
+ registerOptionValue(options, valueOptionName, null, waitingValue);
+ waitingValue = null;
+ valueOptionName = null;
+ }
allParameters = true;
continue;
}
@@ -262,11 +472,25 @@
else if (param.startsWith("-")) { //$NON-NLS-1$
opt = param.substring(1);
}
+ else if (waitingValue!=null) {
+ success = registerOptionValue(options, valueOptionName, param, waitingValue);
+ waitingValue = null;
+ valueOptionName = null;
+ if (!success) params.add(param);
+ continue;
+ }
else {
params.add(param);
continue;
}
+ if (waitingValue!=null) {
+ success = registerOptionValue(options, valueOptionName, param, waitingValue);
+ waitingValue = null;
+ valueOptionName = null;
+ if (success) continue;
+ }
+
idx = opt.indexOf('=');
if (idx>0) {
base = opt.substring(0,idx);
@@ -277,25 +501,55 @@
val = null;
}
- code = defs.get(base);
- if (code!=null) {
- if (code==1) {
- if (val==null)
- waitingValue = base;
+ nbase = null;
+ type = defs.get(base);
+ if (type==null && base.toLowerCase().startsWith("no")) { //$NON-NLS-1$
+ nbase = base.substring(2);
+ type = defs.get(nbase);
+ }
+ if (type!=null) {
+ switch(type) {
+ case FLAG:
+ if (nbase==null)
+ registerOptionValue(options, base, Boolean.TRUE, type);
else
- options.put(base, val);
+ registerOptionValue(options, nbase, Boolean.FALSE, type);
+ break;
+ case MANDATORY_FLOAT:
+ case MANDATORY_BOOLEAN:
+ case MANDATORY_INTEGER:
+ case MANDATORY_STRING:
+ case OPTIONAL_FLOAT:
+ case OPTIONAL_BOOLEAN:
+ case OPTIONAL_INTEGER:
+ case OPTIONAL_STRING:
+ if (val!=null) {
+ registerOptionValue(options, base, val, type);
+ }
+ else {
+ waitingValue = type;
+ valueOptionName = base;
+ }
+ break;
+ default:
+ registerOptionValue(options, base, val, type);
}
- else {
- options.put(base, null);
- }
}
+ else {
+ // Not a recognized option, assuming simple
+ registerOptionValue(options, base, val, OptionType.SIMPLE);
+ }
}
- __parameters = new String[params.size()];
- params.toArray(__parameters);
+ if (waitingValue!=null && waitingValue.isMandatory()) {
+ throw new IllegalStateException("expected a value for command line option "+valueOptionName); //$NON-NLS-1$
+ }
+
+ commandLineParameters = new String[params.size()];
+ params.toArray(commandLineParameters);
params.clear();
- __options = options;
+ commandLineOptions = options;
analyzed = true;
}
@@ -332,7 +586,7 @@
* @see #VMCommandLine(Class, String[])
*/
public VMCommandLine() {
- if (__classToLaunch==null) {
+ if (classToLaunch==null) {
throw new IllegalArgumentException("you must call the other constructor previously"); //$NON-NLS-1$
}
}
@@ -343,20 +597,31 @@
* @return <code>true</code> if the option is present, otherwise <code>false</code>
*/
public boolean hasOption(String optionLabel) {
- SortedMap<String,String> options = getCommandLineOptions();
- return options.containsKey(optionLabel);
+ return hasCommandLineOption(optionLabel);
}
- /** Replies if the given option is present on the command line.
+ /** Replies the first value of the option.
*
* @param optionLabel is the name of the option
- * @return the option value or <code>null</code> if the option is not present.
+ * @return the option value or <code>null</code> if the option is not present or has no value.
*/
- public String getOption(String optionLabel) {
- SortedMap<String,String> options = getCommandLineOptions();
- return options.get(optionLabel);
+ public Object getFirstOptionValue(String optionLabel) {
+ List<Object> options = getCommandLineOption(optionLabel);
+ if (options==null || options.isEmpty()) return null;
+ return options.get(0);
}
+ /** Replies the values of the option.
+ *
+ * @param optionLabel is the name of the option
+ * @return the option values or <code>null</code> if the option is not present.
+ */
+ public List<Object> getOptionValues(String optionLabel) {
+ List<Object> options = getCommandLineOption(optionLabel);
+ if (options==null) return null;
+ return Collections.unmodifiableList(options);
+ }
+
/** Replies the parameters on the command line that are not options.
*
* @return the parameters.
@@ -404,4 +669,81 @@
return index>=0 && index<params.length && params[index]!=null;
}
+ /**
+ * @author Stéphane GALLAND <galland@xxxxxxxxxxx>
+ * @version $Name$ $Revision$ $Date$
+ */
+ private static enum OptionType {
+
+ /**
+ * a simple option without value or flag.
+ */
+ SIMPLE,
+
+ /** an option with a mandatory string value.
+ */
+ MANDATORY_STRING,
+
+ /** an option with an optional string value.
+ */
+ OPTIONAL_STRING,
+
+ /** an option with a mandatory integer value.
+ */
+ MANDATORY_INTEGER,
+
+ /** an option with an optional integer value.
+ */
+ OPTIONAL_INTEGER,
+
+ /** an option with a mandatory floating-point value.
+ */
+ MANDATORY_FLOAT,
+
+ /** an option with an optional floating-point value.
+ */
+ OPTIONAL_FLOAT,
+
+ /** an option with a mandatory boolean value.
+ */
+ MANDATORY_BOOLEAN,
+
+ /** an option with an optional boolean value.
+ */
+ OPTIONAL_BOOLEAN,
+
+ /** an option with an auto-incremented integer value.
+ */
+ AUTO_INCREMENTED,
+
+ /** an option which could be flaged or not: {@code --name} or {@code --noname}.
+ */
+ FLAG;
+
+ /** Replies if the value is mandatory.
+ */
+ public boolean isMandatory() {
+ return this==MANDATORY_BOOLEAN
+ || this==MANDATORY_FLOAT
+ || this==MANDATORY_STRING
+ || this==MANDATORY_INTEGER;
+ }
+
+ /** Replies if the value is optional.
+ */
+ public boolean isOptional() {
+ return this==OPTIONAL_BOOLEAN
+ || this==OPTIONAL_FLOAT
+ || this==OPTIONAL_STRING
+ || this==OPTIONAL_INTEGER;
+ }
+
+ /** Replies if a value is allowed.
+ */
+ public boolean hasValue() {
+ return this!=SIMPLE && this!=AUTO_INCREMENTED && this!=FLAG;
+ }
+
+ }
+
}
Added: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java (rev 0)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2009-08-05 20:42:59 UTC (rev 78)
@@ -0,0 +1,208 @@
+package org.arakhne.vmutil;
+
+import java.io.File;
+import java.net.URL;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+public class FileSystemTest extends TestCase {
+
+ private static final File f1 = new File("/home/test.x.z.z"); //$NON-NLS-1$
+ private static final File f2 = new File("/home"); //$NON-NLS-1$
+
+ public void testDirname() {
+ assertEquals("/home", FileSystem.dirname(f1.getAbsolutePath())); //$NON-NLS-1$
+ assertEquals("/", FileSystem.dirname(f2.getAbsolutePath())); //$NON-NLS-1$
+ }
+
+ public void testLargeBasenameString() {
+ assertEquals("test.x.z.z", FileSystem.largeBasename(f1.getAbsolutePath())); //$NON-NLS-1$
+ assertEquals("home", FileSystem.largeBasename(f2.getAbsolutePath())); //$NON-NLS-1$
+ }
+
+ public void testLargeBasenameFile() {
+ assertEquals("test.x.z.z", FileSystem.largeBasename(f1)); //$NON-NLS-1$
+ assertEquals("home", FileSystem.largeBasename(f2)); //$NON-NLS-1$
+ }
+
+ public void testBasenameString() {
+ assertEquals("test.x.z", FileSystem.basename(f1.getAbsolutePath())); //$NON-NLS-1$
+ assertEquals("home", FileSystem.basename(f2.getAbsolutePath())); //$NON-NLS-1$
+ }
+
+ public void testBasenameFile() {
+ assertEquals("test.x.z", FileSystem.basename(f1)); //$NON-NLS-1$
+ assertEquals("home", FileSystem.basename(f2)); //$NON-NLS-1$
+ }
+
+ public void testShortBasenameString() {
+ assertEquals("test", FileSystem.shortBasename(f1.getAbsolutePath())); //$NON-NLS-1$
+ assertEquals("home", FileSystem.shortBasename(f2.getAbsolutePath())); //$NON-NLS-1$
+ }
+
+ public void testShortBasenameFile() {
+ assertEquals("test", FileSystem.shortBasename(f1)); //$NON-NLS-1$
+ assertEquals("home", FileSystem.shortBasename(f2)); //$NON-NLS-1$
+ }
+
+ public void testExtensionString() {
+ assertEquals(".z", FileSystem.extension(f1.getAbsolutePath())); //$NON-NLS-1$
+ assertEquals("", FileSystem.extension(f2.getAbsolutePath())); //$NON-NLS-1$
+ }
+
+ public void testExtensionFile() {
+ assertEquals(".z", FileSystem.extension(f1)); //$NON-NLS-1$
+ assertEquals("", FileSystem.extension(f2)); //$NON-NLS-1$
+ }
+
+ public void testExtensionsString() {
+ assertTrue(Arrays.equals(new String[]{"x","z","z"}, FileSystem.extensions(f1.getAbsolutePath()))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ assertTrue(Arrays.equals(new String[0], FileSystem.extensions(f2.getAbsolutePath())));
+ }
+
+ public void testExtensionsFile() {
+ assertTrue(Arrays.equals(new String[]{"x","z","z"}, FileSystem.extensions(f1))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ assertTrue(Arrays.equals(new String[0], FileSystem.extensions(f2)));
+ }
+
+ public void testSplitString() {
+ assertTrue(Arrays.equals(
+ new String[] {
+ "", //$NON-NLS-1$
+ "home", //$NON-NLS-1$
+ "test.x.z.z", //$NON-NLS-1$
+ },
+ FileSystem.split(f1.getAbsolutePath())));
+ assertTrue(Arrays.equals(
+ new String[] {
+ "", //$NON-NLS-1$
+ "home", //$NON-NLS-1$
+ },
+ FileSystem.split(f2.getAbsolutePath())));
+ }
+
+ public void testSplitFile() {
+ assertTrue(Arrays.equals(
+ new String[] {
+ "", //$NON-NLS-1$
+ "home", //$NON-NLS-1$
+ "test.x.z.z", //$NON-NLS-1$
+ },
+ FileSystem.split(f1)));
+ assertTrue(Arrays.equals(
+ new String[] {
+ "", //$NON-NLS-1$
+ "home", //$NON-NLS-1$
+ },
+ FileSystem.split(f2)));
+ }
+
+ public void testJoin() {
+ assertEquals(f1.getAbsolutePath(),
+ FileSystem.join(
+ "", //$NON-NLS-1$
+ "home", //$NON-NLS-1$
+ "test.x.z.z")); //$NON-NLS-1$
+ }
+
+ public void testHasExtensionStringString() {
+ /*assertTrue(FileSystem.hasExtension(f1.getAbsolutePath(), ".z")); //$NON-NLS-1$
+ assertTrue(FileSystem.hasExtension(f1.getAbsolutePath(), "z")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(f1.getAbsolutePath(), ".x")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(f1.getAbsolutePath(), "")); //$NON-NLS-1$
+ assertTrue(FileSystem.hasExtension(f2.getAbsolutePath(), "")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(f2.getAbsolutePath(), ".z")); //$NON-NLS-1$
+ */
+ }
+
+ public void testHasExtensionFileString() {
+ /*assertTrue(FileSystem.hasExtension(f1, ".z")); //$NON-NLS-1$
+ assertTrue(FileSystem.hasExtension(f1, "z")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(f1, ".x")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(f1, "")); //$NON-NLS-1$
+ assertTrue(FileSystem.hasExtension(f2, "")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(f2, ".z")); //$NON-NLS-1$
+ */
+ }
+
+ public void testRemoveExtensionString() {
+ assertEquals("/home/test.x.z", FileSystem.removeExtension(f1.getAbsolutePath())); //$NON-NLS-1$
+ assertEquals("/home", FileSystem.removeExtension(f2.getAbsolutePath())); //$NON-NLS-1$
+ }
+
+ public void testRemoveExtensionFile() {
+ assertEquals("/home/test.x.z", FileSystem.removeExtension(f1)); //$NON-NLS-1$
+ assertEquals("/home", FileSystem.removeExtension(f2)); //$NON-NLS-1$
+ }
+
+ public void testReplaceExtensionStringString() {
+ assertEquals("/home/test.x.z.toto", FileSystem.replaceExtension(f1.getAbsolutePath(), ".toto")); //$NON-NLS-1$ //$NON-NLS-2$
+ assertEquals("/home", FileSystem.replaceExtension(f2.getAbsolutePath(), ".toto")); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void testReplaceExtensionFileString() {
+ assertEquals(new File("/home/test.x.z.toto"), FileSystem.replaceExtension(f1, ".toto")); //$NON-NLS-1$ //$NON-NLS-2$
+ assertEquals(new File("/home"), FileSystem.replaceExtension(f2, ".toto")); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void testConvertStringToUrl() throws Exception {
+ assertNull(FileSystem.convertStringToUrl(null, true));
+ assertNull(FileSystem.convertStringToUrl("", true)); //$NON-NLS-1$
+ assertNull(FileSystem.convertStringToUrl(null, false));
+ assertNull(FileSystem.convertStringToUrl("", false)); //$NON-NLS-1$
+
+ assertEquals(new URL("http://www.arakhne.org/"), //$NON-NLS-1$
+ FileSystem.convertStringToUrl("http://www.arakhne.org/", true)); //$NON-NLS-1$
+ assertEquals(new URL("http://www.arakhne.org/"), //$NON-NLS-1$
+ FileSystem.convertStringToUrl("http://www.arakhne.org/", false)); //$NON-NLS-1$
+
+ assertEquals(new URL("file:"+f1.getAbsolutePath()), //$NON-NLS-1$
+ FileSystem.convertStringToUrl("file:"+f1.getAbsolutePath(), true)); //$NON-NLS-1$
+ assertEquals(new URL("file:"+f1.getAbsolutePath()), //$NON-NLS-1$
+ FileSystem.convertStringToUrl("file:"+f1.getAbsolutePath(), false)); //$NON-NLS-1$
+
+ assertEquals(new File("jar:/home/test/j.jar").toURI().toURL(), //$NON-NLS-1$
+ FileSystem.convertStringToUrl("jar:/home/test/j.jar", true)); //$NON-NLS-1$
+ assertEquals(new File("jar:/home/test/j.jar").toURI().toURL(), //$NON-NLS-1$
+ FileSystem.convertStringToUrl("jar:/home/test/j.jar", false)); //$NON-NLS-1$
+
+ assertEquals(new File("jar:/home/test/j.jar!/org/arakhne/vmutil/ff.properties").toURI().toURL(), //$NON-NLS-1$
+ FileSystem.convertStringToUrl("jar:/home/test/j.jar!/org/arakhne/vmutil/ff.properties", true)); //$NON-NLS-1$
+ assertEquals(new File("jar:/home/test/j.jar!/org/arakhne/vmutil/ff.properties").toURI().toURL(), //$NON-NLS-1$
+ FileSystem.convertStringToUrl("jar:/home/test/j.jar!/org/arakhne/vmutil/ff.properties", false)); //$NON-NLS-1$
+
+ assertEquals(new URL("jar:file:/home/test/j.jar!/org/arakhne/vmutil/ff.properties"), //$NON-NLS-1$
+ FileSystem.convertStringToUrl("jar:file:/home/test/j.jar!/org/arakhne/vmutil/ff.properties", true)); //$NON-NLS-1$
+ assertEquals(new URL("jar:file:/home/test/j.jar!/org/arakhne/vmutil/ff.properties"), //$NON-NLS-1$
+ FileSystem.convertStringToUrl("jar:file:/home/test/j.jar!/org/arakhne/vmutil/ff.properties", false)); //$NON-NLS-1$
+
+ URL testResource = FileSystemTest.class.getResource("/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+ assertNotNull(testResource);
+ URL testResource2 = new URL("file:/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+
+ assertEquals(testResource,
+ FileSystem.convertStringToUrl("resource:/org/arakhne/vmutil/test.txt", true)); //$NON-NLS-1$
+ assertEquals(null,
+ FileSystem.convertStringToUrl("resource:/org/arakhne/vmutil/test.txt", false)); //$NON-NLS-1$
+
+ assertEquals(testResource,
+ FileSystem.convertStringToUrl("/org/arakhne/vmutil/test.txt", true)); //$NON-NLS-1$
+ assertEquals(testResource2,
+ FileSystem.convertStringToUrl("/org/arakhne/vmutil/test.txt", false)); //$NON-NLS-1$
+ }
+
+ public void testConvertUrlToFile() throws Exception {
+ assertEquals(f1,
+ FileSystem.convertUrlToFile(new URL("file:"+f1.getAbsolutePath()))); //$NON-NLS-1$
+
+ try {
+ FileSystem.convertUrlToFile(new URL("http://www.arakhne.org")); //$NON-NLS-1$
+ fail("not a file URL"); //$NON-NLS-1$
+ }
+ catch(IllegalArgumentException _) {
+ //
+ }
+ }
+
+}
Added: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/VMCommandLineTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/VMCommandLineTest.java (rev 0)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/VMCommandLineTest.java 2009-08-05 20:42:59 UTC (rev 78)
@@ -0,0 +1,237 @@
+package org.arakhne.vmutil;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+public class VMCommandLineTest extends TestCase {
+
+ private static final String[] commandLine = new String[] {
+ "-D=true", "-v", "clean", "-v", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
+ "-F", "-b", "-v", "package", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
+ "-F", "123", "-nob", "installters", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
+ "-S", "-b", "--", "-v"}; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
+
+ private static final String[] commandLine2 = new String[] {"-D=true"}; //$NON-NLS-1$
+
+ private static final String[] optionDefinitions = new String[] {
+ "D=b", "S=s", "F:f", "v+", "b!"}; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ VMCommandLine.saveVMParameters(null, new String[0]);
+ }
+
+ public final void testSaveVMParameters() {
+ assertTrue(Arrays.equals(new String[0], VMCommandLine.getCommandLineParameters()));
+ VMCommandLine.saveVMParameters(VMCommandLineTest.class, commandLine);
+ assertTrue(Arrays.equals(commandLine, VMCommandLine.getCommandLineParameters()));
+ VMCommandLine.saveVMParameters(VMCommandLineTest.class, commandLine2);
+ assertTrue(Arrays.equals(commandLine2, VMCommandLine.getCommandLineParameters()));
+ }
+
+ public final void testSaveVMParametersIfNotSet() {
+ assertTrue(Arrays.equals(new String[0], VMCommandLine.getCommandLineParameters()));
+ VMCommandLine.saveVMParametersIfNotSet(VMCommandLineTest.class, commandLine);
+ assertTrue(Arrays.equals(commandLine, VMCommandLine.getCommandLineParameters()));
+ VMCommandLine.saveVMParametersIfNotSet(VMCommandLineTest.class, commandLine2);
+ assertTrue(Arrays.equals(commandLine, VMCommandLine.getCommandLineParameters()));
+ }
+
+ public final void testShiftCommandLineParameters() {
+ VMCommandLine.saveVMParameters(VMCommandLineTest.class, commandLine);
+ assertEquals("-D=true", VMCommandLine.shiftCommandLineParameters()); //$NON-NLS-1$
+ assertTrue(Arrays.equals(new String[] {
+ "-v", "clean", "-v", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+ "-F", "-b", "-v", "package", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
+ "-F", "123", "-nob", "installters", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
+ "-S", "-b", "--", "-v"}, //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
+ VMCommandLine.getCommandLineParameters()));
+ }
+
+ public final void testGetCommandLineOptions() {
+ assertEquals(Collections.emptyMap(), VMCommandLine.getCommandLineOptions());
+ }
+
+ public final void testSplitOptionsAndParameters() {
+ VMCommandLine.saveVMParameters(VMCommandLineTest.class, commandLine);
+ VMCommandLine.splitOptionsAndParameters(optionDefinitions);
+
+ Map<String,List<Object>> options = VMCommandLine.getCommandLineOptions();
+ String[] parameters = VMCommandLine.getCommandLineParameters();
+ List<Object> values;
+
+ assertNotNull(options);
+ assertEquals(5, options.size());
+
+ assertTrue(options.containsKey("D")); //$NON-NLS-1$
+ values = options.get("D"); //$NON-NLS-1$
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals(true, values.get(0));
+
+ assertTrue(options.containsKey("v")); //$NON-NLS-1$
+ values = options.get("v"); //$NON-NLS-1$
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals((long)3, values.get(0));
+
+ assertTrue(options.containsKey("F")); //$NON-NLS-1$
+ values = options.get("F"); //$NON-NLS-1$
+ assertNotNull(values);
+ assertEquals(2, values.size());
+ assertEquals(0., values.get(0));
+ assertEquals(123., values.get(1));
+
+ assertTrue(options.containsKey("b")); //$NON-NLS-1$
+ values = options.get("b"); //$NON-NLS-1$
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals(false, values.get(0));
+
+ assertTrue(options.containsKey("S")); //$NON-NLS-1$
+ values = options.get("S"); //$NON-NLS-1$
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals("-b", values.get(0)); //$NON-NLS-1$
+
+ values = options.get("nob"); //$NON-NLS-1$
+ assertNull(values);
+
+ assertNotNull(parameters);
+ assertEquals(4, parameters.length);
+ assertEquals("clean", parameters[0]); //$NON-NLS-1$
+ assertEquals("package", parameters[1]); //$NON-NLS-1$
+ assertEquals("installters", parameters[2]); //$NON-NLS-1$
+ assertEquals("-v", parameters[3]); //$NON-NLS-1$
+ }
+
+ public final void testGetCommandLineOption() {
+ assertNull(VMCommandLine.getCommandLineOption("S")); //$NON-NLS-1$
+
+ VMCommandLine.saveVMParameters(VMCommandLineTest.class, commandLine);
+ VMCommandLine.splitOptionsAndParameters(optionDefinitions);
+
+ List<Object> values;
+ values = VMCommandLine.getCommandLineOption("S"); //$NON-NLS-1$
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals("-b", values.get(0)); //$NON-NLS-1$
+
+ assertNull(VMCommandLine.getCommandLineOption("nob")); //$NON-NLS-1$
+ }
+
+ public final void testHasCommandLineOption() {
+ assertFalse(VMCommandLine.hasCommandLineOption("S")); //$NON-NLS-1$
+
+ VMCommandLine.saveVMParameters(VMCommandLineTest.class, commandLine);
+ VMCommandLine.splitOptionsAndParameters(optionDefinitions);
+
+ assertTrue(VMCommandLine.hasCommandLineOption("S")); //$NON-NLS-1$
+ assertFalse(VMCommandLine.hasCommandLineOption("nob")); //$NON-NLS-1$
+ }
+
+ public final void testVMCommandLineClassOfQStringArray() {
+ VMCommandLine c = new VMCommandLine(VMCommandLineTest.class, commandLine);
+ assertTrue(Arrays.equals(commandLine, c.getParameters()));
+ }
+
+ public final void testVMCommandLineClassOfQStringArrayStringArray() {
+ VMCommandLine c = new VMCommandLine(VMCommandLineTest.class, optionDefinitions, commandLine);
+ assertTrue(Arrays.equals(new String[] {
+ "clean", "package", "installters", "-v" //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
+ }, c.getParameters()));
+ }
+
+ public final void testHasOption() {
+ VMCommandLine c = new VMCommandLine(VMCommandLineTest.class, optionDefinitions, commandLine);
+ assertTrue(c.hasOption("S")); //$NON-NLS-1$
+ assertTrue(c.hasOption("b")); //$NON-NLS-1$
+ assertFalse(c.hasOption("nob")); //$NON-NLS-1$
+ }
+
+ public final void testGetFirstOptionValue() {
+ VMCommandLine c = new VMCommandLine(VMCommandLineTest.class, optionDefinitions, commandLine);
+ assertEquals("-b", c.getFirstOptionValue("S")); //$NON-NLS-1$ //$NON-NLS-2$
+ assertEquals(false, c.getFirstOptionValue("b")); //$NON-NLS-1$
+ assertEquals(0., c.getFirstOptionValue("F")); //$NON-NLS-1$
+ assertNull(c.getFirstOptionValue("nob")); //$NON-NLS-1$
+ }
+
+ public final void testGetOptionValues() {
+ VMCommandLine c = new VMCommandLine(VMCommandLineTest.class, optionDefinitions, commandLine);
+ List<Object> values;
+
+ values = c.getOptionValues("D"); //$NON-NLS-1$
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals(true, values.get(0));
+
+ values = c.getOptionValues("v"); //$NON-NLS-1$
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals((long)3, values.get(0));
+
+ values = c.getOptionValues("F"); //$NON-NLS-1$
+ assertNotNull(values);
+ assertEquals(2, values.size());
+ assertEquals(0., values.get(0));
+ assertEquals(123., values.get(1));
+
+ values = c.getOptionValues("b"); //$NON-NLS-1$
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals(false, values.get(0));
+
+ values = c.getOptionValues("S"); //$NON-NLS-1$
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals("-b", values.get(0)); //$NON-NLS-1$
+
+ assertNull(c.getOptionValues("nob")); //$NON-NLS-1$
+ }
+
+ public final void testGetParameters() {
+ VMCommandLine c = new VMCommandLine(VMCommandLineTest.class, optionDefinitions, commandLine);
+ assertNotSame(commandLine, c.getParameters());
+ assertTrue(Arrays.equals(new String[] {
+ "clean", "package", "installters", "-v" //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
+ }, c.getParameters()));
+ }
+
+ public final void testShiftParameters() {
+ VMCommandLine c = new VMCommandLine(VMCommandLineTest.class, optionDefinitions, commandLine);
+ assertEquals("clean", c.shiftParameters()); //$NON-NLS-1$
+ assertNotSame(commandLine, c.getParameters());
+ assertTrue(Arrays.equals(new String[] {
+ "package", "installters", "-v" //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+ }, c.getParameters()));
+ }
+
+ public final void testGetParameterCount() {
+ VMCommandLine c = new VMCommandLine(VMCommandLineTest.class, optionDefinitions, commandLine);
+ assertEquals(4, c.getParameterCount());
+ }
+
+ public final void testGetParameterAt() {
+ VMCommandLine c = new VMCommandLine(VMCommandLineTest.class, optionDefinitions, commandLine);
+ assertEquals("clean", c.getParameterAt(0)); //$NON-NLS-1$
+ assertEquals("package", c.getParameterAt(1)); //$NON-NLS-1$
+ assertEquals("installters", c.getParameterAt(2)); //$NON-NLS-1$
+ assertEquals("-v", c.getParameterAt(3)); //$NON-NLS-1$
+ }
+
+ public final void testIsParameterExists() {
+ VMCommandLine c = new VMCommandLine(VMCommandLineTest.class, optionDefinitions, commandLine);
+ assertTrue(c.isParameterExists(0));
+ assertTrue(c.isParameterExists(1));
+ assertTrue(c.isParameterExists(2));
+ assertTrue(c.isParameterExists(3));
+ assertFalse(c.isParameterExists(5));
+ }
+
+}
Added: trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test.txt
===================================================================
--- trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test.txt (rev 0)
+++ trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test.txt 2009-08-05 20:42:59 UTC (rev 78)
@@ -0,0 +1 @@
+FOR UNIT TEST ONLY
\ No newline at end of file