Skip to content

Commit

Permalink
[sre] Add -version command line option.
Browse files Browse the repository at this point in the history
close #537

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 15, 2016
1 parent d047ef8 commit 37b5cbc
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 14 deletions.
25 changes: 25 additions & 0 deletions sre/io.janusproject/io.janusproject.plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@
</replacements>
</configuration>
</execution>
<execution>
<id>generate-javaversion-class</id>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<basedir>${project.basedir}/src-templates</basedir>
<outputDir>../src</outputDir>
<includes>
<include>**/*.java</include>
</includes>
<replacements>
<replacement>
<token>@janusreleaseversion@</token>
<value>${janus.release_version}</value>
</replacement>
<replacement>
<token>@janusisstableversion@</token>
<value>${sarl.is_stable_version}</value>
</replacement>
</replacements>
</configuration>
</execution>
</executions>
</plugin>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2016 the original authors or authors.
*
* 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 io.janusproject;


/**
* Describes the version of the Janus platform.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 2.0.5.0
*/
@SuppressWarnings("all")
public final class JanusVersion {

/** The version number of the current release of the Janus platform.
*/
public static final String JANUS_RELEASE_VERSION = "@janusreleaseversion@"; //$NON-NLS-1$

/** Flag that indicates if the current Janus platform is a stable release.
*
* <p>A stable release is a platform that will be not more compiled and generated.
*/
public static final boolean IS_STABLE = @janusisstableversion@;

private JanusVersion() {
//
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.apache.commons.cli.ParseException;
import org.arakhne.afc.vmutil.FileSystem;

import io.sarl.lang.SARLVersion;
import io.sarl.lang.core.Agent;

/**
Expand Down Expand Up @@ -161,6 +162,10 @@ public final class Boot {
*/
public static final String CLI_OPTION_VERBOSE_LONG = "verbose"; //$NON-NLS-1$

/** Long command-line option for "display the version".
*/
public static final String CLI_OPTION_VERSION = "version"; //$NON-NLS-1$

/** Short command-line option for "change log level".
*/
public static final String CLI_OPTION_LOG_SHORT = "l"; //$NON-NLS-1$
Expand Down Expand Up @@ -223,23 +228,24 @@ public static String[] parseCommandLine(String[] args) {
try {
final CommandLine cmd = parser.parse(getOptions(), args, false);

// Show the help when there is no argument.
if (cmd.getArgs().length == 0) {
showHelp();
return null;
}

boolean noLogo = false;
boolean embedded = false;
int verbose = LoggerCreator.toInt(JanusConfig.VERBOSE_LEVEL_VALUE);

final Iterator<Option> optIterator = cmd.iterator();
while (optIterator.hasNext()) {
final Option opt = optIterator.next();
switch (opt.getLongOpt()) {
String optName = opt.getLongOpt();
if (Strings.isNullOrEmpty(optName)) {
optName = opt.getOpt();
}
switch (optName) {
case CLI_OPTION_HELP_LONG:
showHelp();
return null;
case CLI_OPTION_VERSION:
showVersion();
return null;
case CLI_OPTION_SHOWDEFAULTS_LONG:
showDefaults();
return null;
Expand Down Expand Up @@ -304,6 +310,12 @@ public static String[] parseCommandLine(String[] args) {
}
}

// Show the help when there is no argument.
if (cmd.getArgs().length == 0) {
showHelp();
return null;
}

// Change the verbosity
setVerboseLevel(verbose);
// Do nothing at exit
Expand Down Expand Up @@ -437,7 +449,7 @@ public static void main(String[] args) {
* @return the console logger.
*/
public static PrintStream getConsoleLogger() {
return consoleLogger == null ? System.err : consoleLogger;
return consoleLogger == null ? System.out : consoleLogger;
}

/**
Expand Down Expand Up @@ -501,6 +513,9 @@ public static Options getOptions() {
options.addOption(CLI_OPTION_VERBOSE_SHORT, CLI_OPTION_VERBOSE_LONG, false,
Messages.Boot_15);

options.addOption(CLI_OPTION_VERSION, false,
Messages.Boot_25);

options.addOption(CLI_OPTION_WORLDID_SHORT, CLI_OPTION_WORLDID_LONG, false,
MessageFormat.format(Messages.Boot_16,
JanusConfig.BOOT_DEFAULT_CONTEXT_ID_NAME, JanusConfig.RANDOM_DEFAULT_CONTEXT_ID_NAME));
Expand Down Expand Up @@ -615,6 +630,18 @@ public static void showClasspath() {
getExiter().exit();
}

/**
* Show the version of Janus. This function never returns.
*/
public static void showVersion() {
try (PrintWriter logger = new PrintWriter(getConsoleLogger())) {
logger.println(MessageFormat.format(Messages.Boot_26, JanusVersion.JANUS_RELEASE_VERSION));
logger.println(MessageFormat.format(Messages.Boot_27, SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING));
logger.flush();
}
getExiter().exit();
}

/**
* Show the command line arguments. This function never returns.
*
Expand All @@ -639,7 +666,7 @@ public static void showCommandLineArguments(String[] args) {
*/
@SuppressWarnings("checkstyle:regexp")
public static void showJanusLogo() {
System.out.println(Messages.Boot_21);
getConsoleLogger().println(Messages.Boot_21);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2016 the original authors or authors.
*
* 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 io.janusproject;


/**
* Describes the version of the Janus platform.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 2.0.5.0
*/
@SuppressWarnings("all")
public final class JanusVersion {

/** The version number of the current release of the Janus platform.
*/
public static final String JANUS_RELEASE_VERSION = "2.0.5.0"; //$NON-NLS-1$

/** Flag that indicates if the current Janus platform is a stable release.
*
* <p>A stable release is a platform that will be not more compiled and generated.
*/
public static final boolean IS_STABLE = false;

private JanusVersion() {
//
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class Messages extends NLS {
public static String Boot_22;
public static String Boot_23;
public static String Boot_24;
public static String Boot_25;
public static String Boot_26;
public static String Boot_27;
public static String Boot_3;
public static String Boot_4;
public static String Boot_5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Boot_21=MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM\nMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
Boot_22=Launching the agent: {0}
Boot_23=Show the classpath used by the application.
Boot_24=Add entries to the Java classpath
Boot_25=Display the version of Janus.
Boot_26=Janus: {0}
Boot_27=SARL specification: {0}
Boot_3=You must give the fully qualified name of the agent to launch.
Boot_4=Error when launching Janus: {0}
Boot_5=Janus is run inside another application.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyVararg;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.times;
Expand All @@ -53,6 +52,7 @@
import io.janusproject.Boot;
import io.janusproject.Boot.Exiter;
import io.janusproject.JanusConfig;
import io.janusproject.JanusVersion;
import io.janusproject.kernel.Kernel;
import io.janusproject.tests.testutils.AbstractJanusRunTest;
import io.janusproject.tests.testutils.AbstractJanusTest;
Expand Down Expand Up @@ -220,13 +220,13 @@ public void setPropertiesFromFile() throws IOException {

@Test
public void setConsoleLogger_default() {
assertSame(System.err, Boot.getConsoleLogger());
assertSame(System.out, Boot.getConsoleLogger());
}

@Test
public void setConsoleLogger_null() {
Boot.setConsoleLogger(null);
assertSame(System.err, Boot.getConsoleLogger());
assertSame(System.out, Boot.getConsoleLogger());
}

@Test
Expand All @@ -241,7 +241,7 @@ public void setConsoleLogger_notNull_null() {
PrintStream os = mock(PrintStream.class);
Boot.setConsoleLogger(os);
Boot.setConsoleLogger(null);
assertSame(System.err, Boot.getConsoleLogger());
assertSame(System.out, Boot.getConsoleLogger());
}

}
Expand Down Expand Up @@ -1012,6 +1012,24 @@ public void option_qqq_forNoLogo() {
verifyZeroInteractions(this.exiter);
}

@Test
public void option_version() {
Boot.parseCommandLine(args("-version"));
// The properties are null since resetProperties() is invoked for resetting the properties in
// the start-up function inherited from AbstractJanusTest
ArgumentCaptor<byte[]> array = ArgumentCaptor.forClass(byte[].class);
ArgumentCaptor<Integer> offset = ArgumentCaptor.forClass(Integer.class);
ArgumentCaptor<Integer> length = ArgumentCaptor.forClass(Integer.class);
verify(this.logger, times(1)).write(array.capture(), offset.capture(), length.capture());
final String message = new String(array.getValue(), offset.getValue(), length.getValue());
assertEquals("Janus: " + JanusVersion.JANUS_RELEASE_VERSION + "\nSARL specification: "
+ SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\n", message);
verify(this.logger, times(1)).flush();
verify(this.logger, times(1)).close();
verifyNoMoreInteractions(this.logger);
verify(this.exiter, only()).exit();
}

}

/**
Expand Down

0 comments on commit 37b5cbc

Please sign in to comment.