$darkmode
DENOPTIM
MainTest.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.main;
20
21import static org.junit.jupiter.api.Assertions.assertEquals;
22import static org.junit.jupiter.api.Assertions.assertTrue;
23
24import java.io.File;
25
26import org.junit.jupiter.api.Test;
27import org.junit.jupiter.api.io.TempDir;
28
29import denoptim.io.DenoptimIO;
30import denoptim.logging.Version;
31import denoptim.main.Main.RunType;
32import denoptim.programs.RunTimeParameters.ParametersType;
33
34
35public class MainTest
36{
37 private final String SEP = System.getProperty("file.separator");
38
39 @TempDir
40 File tempDir;
41
42//------------------------------------------------------------------------------
43
44 @Test
45 public void testDefineProgramBehavior() throws Exception
46 {
47 assertTrue(this.tempDir.isDirectory(),"Should be a directory");
48 String inputPathName = tempDir.getAbsolutePath() + SEP + "input.par";
49 DenoptimIO.writeData(inputPathName,
50 ParametersType.GA_PARAMS.getKeywordRoot(), false);
51 String inputPathName2 = tempDir.getAbsolutePath() + SEP + "input2.par";
52 DenoptimIO.writeData(inputPathName2,
53 ParametersType.GA_PARAMS.getKeywordRoot(), false);
54
55 //
56 // Simplest call (launch GUI)
57 //
58 Behavior b = Main.defineProgramBehavior(new String[] {});
59 assertEquals(0, b.exitStatus, "Exit status");
60 assertEquals(RunType.GUI, b.runType, "Type of run");
61
62 //
63 // Single argument call with no further program run.
64 //
65 b = Main.defineProgramBehavior(new String[] {
66 "-"+CLIOptions.help.getOpt() });
67 assertEquals(0, b.exitStatus, "Exit status");
68 assertTrue(b.helpMsg.contains("usage:"), "Help Msg");
69 b = Main.defineProgramBehavior(new String[] {
70 "--"+CLIOptions.version.getLongOpt() });
71 assertEquals(0, b.exitStatus, "Exit status");
72 assertTrue(b.helpMsg.startsWith(Version.VERSION), "Version msg");
73
74 //
75 // Testing the request for a specific type of run
76 //
77 b = Main.defineProgramBehavior(new String[] {
78 "-"+CLIOptions.run.getOpt(), "GA", inputPathName});
79 assertEquals(0, b.exitStatus, "Exit status");
80 assertEquals(RunType.GA, b.runType, "Type of run");
81 assertTrue(b.cmd.getArgList().contains(inputPathName),"Input file");
82
83 b = Main.defineProgramBehavior(new String[] {
84 "-"+CLIOptions.run.getOpt(), "gA", inputPathName});
85 assertEquals(0, b.exitStatus, "Exit status");
86 assertEquals(RunType.GA, b.runType, "Type of run");
87
88 b = Main.defineProgramBehavior(new String[] {
89 "-"+CLIOptions.run.getOpt(), "GUI"});
90 assertEquals(1, b.exitStatus, "Exit status");
91 assertTrue(b.errorMsg.contains("not enabled from CLI"), "Error Msg");
92
93
94 b = Main.defineProgramBehavior(new String[] {inputPathName,
95 "-"+CLIOptions.run.getOpt(), "FSE"});
96 assertEquals(0, b.exitStatus, "Exit status");
97 assertEquals(RunType.FSE, b.runType, "Type of run");
98 assertTrue(b.cmd.getArgList().contains(inputPathName),"Input file");
99
100 //
101 // Testing the request for a specific type of run (wrong request)
102 //
103 b = Main.defineProgramBehavior(new String[] {
104 "-"+CLIOptions.run.getOpt(), "GAG", inputPathName});
105 assertEquals(1, b.exitStatus, "Exit status");
106 assertTrue(b.errorMsg.contains(CLIOptions.run.getLongOpt() + " option"),
107 "Illegal run type");
108
109 //
110 // Test unrecognized options
111 //
112 b = Main.defineProgramBehavior(new String[] {inputPathName,
113 "-"+CLIOptions.run.getOpt(), "FSE", "--something"});
114 //System.out.println(b.helpMsg);
115 //System.out.println(b.errorMsg);
116 assertEquals(1, b.exitStatus, "Exit status");
117 assertTrue(b.helpMsg.contains("usage:"), "Help Msg");
118 assertTrue(b.errorMsg.contains("Unrecognized option"), "Error Msg");
119
120 //
121 // Test non-existing input file
122 //
123 b = Main.defineProgramBehavior(new String[] {inputPathName+"_missing"});
124 assertEquals(1, b.exitStatus, "Exit status");
125 assertTrue(b.errorMsg.contains("not found"), "Error Msg");
126
127 b = Main.defineProgramBehavior(new String[] {inputPathName+"_missing"});
128 assertEquals(1, b.exitStatus, "Exit status");
129 assertTrue(b.errorMsg.contains("not found"), "Error Msg");
130
131 //
132 //Test multiple input files
133 //
134 b = Main.defineProgramBehavior(new String[] {inputPathName,
135 inputPathName2});
136 assertEquals(0, b.exitStatus, "Exit status");
137 assertEquals(RunType.GUI, b.runType, "Type of run");
138 assertTrue(b.cmd.getArgList().contains(inputPathName),"Input file");
139 assertTrue(b.cmd.getArgList().contains(inputPathName2),"Input file");
140
141 }
142}
Utility methods for input/output.
static void writeData(String fileName, String data, boolean append)
Write text-like data file.
Class handling DENOPTIM's version identifier for headers.
Definition: Version.java:36
static final String VERSION
Version identifier (from pom.xml via Maven properties)
Definition: Version.java:48
Represents the behavior of the program at start-up.
Definition: Behavior.java:31
RunType runType
The type of run that is requested.
Definition: Behavior.java:51
String errorMsg
The error message.
Definition: Behavior.java:40
CommandLine cmd
The parsed command line arguments.
Definition: Behavior.java:56
int exitStatus
A non-zero value means some error has occurred and the program will terminate.
Definition: Behavior.java:46
String helpMsg
The help message.
Definition: Behavior.java:35
static Option help
Option requesting the printing of the help message.
Definition: CLIOptions.java:37
static Option version
Option requesting only the printing of the version.
Definition: CLIOptions.java:42
static Option run
Option controlling the type of run.
Definition: CLIOptions.java:47
Entry point of any kind of run of the DENOPTIM program.
Definition: Main.java:58
static Behavior defineProgramBehavior(String[] args)
Does the processing of the application arguments and decides what the program should do.
Definition: Main.java:439
final String SEP
Definition: MainTest.java:37
void testDefineProgramBehavior()
Definition: MainTest.java:45
Types of runs that can be requested to the DENOPTIM Main class.
Definition: Main.java:62
GA
Run the genetic algorithm with DenoptimGA.
Definition: Main.java:71
GUI
Launch the graphical user interface denoptim.gui.GUI.
Definition: Main.java:66
FSE
Run a combinatorial generation of candidates with FragSpaceExplorer.
Definition: Main.java:77
GA_PARAMS
Parameters pertaining the genetic algorithm.