| 1 | /* | |
| 2 | MailClena - Copyright (C) 2018, Aiki IT | |
| 3 | ||
| 4 | This program is free software: you can redistribute it and/or modify | |
| 5 | it under the terms of the GNU General Public License as published by | |
| 6 | the Free Software Foundation, either version 3 of the License, or | |
| 7 | (at your option) any later version. | |
| 8 | ||
| 9 | This program is distributed in the hope that it will be useful, | |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | GNU General Public License for more details. | |
| 13 | ||
| 14 | You should have received a copy of the GNU General Public License | |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | ||
| 17 | */ | |
| 18 | package de.aikiit.mailclena; | |
| 19 | ||
| 20 | import com.google.common.annotations.VisibleForTesting; | |
| 21 | import com.google.common.base.Strings; | |
| 22 | import lombok.NoArgsConstructor; | |
| 23 | import lombok.extern.log4j.Log4j2; | |
| 24 | import org.apache.commons.cli.*; | |
| 25 | ||
| 26 | import java.util.Optional; | |
| 27 | ||
| 28 | /** | |
| 29 | * Extract application configuration from given main()-arguments. | |
| 30 | */ | |
| 31 | @Log4j2 | |
| 32 | @NoArgsConstructor | |
| 33 | public final class MailClenaParameterParser { | |
| 34 | ||
| 35 | /** | |
| 36 | * Extracts any given configuration parameters into a {@link MailConfiguration}. | |
| 37 | * | |
| 38 | * @param args command-line arguments. | |
| 39 | * @return complete mail configuration. | |
| 40 | * @throws IllegalArgumentException if any parameter cannot be parsed properly. | |
| 41 | */ | |
| 42 | Optional<MailConfiguration> extractConfiguration(String... args) throws IllegalArgumentException { | |
| 43 |
2
1. extractConfiguration : negated conditional → KILLED 2. extractConfiguration : negated conditional → KILLED |
if (args == null || args.length == 0) { |
| 44 |
1
1. extractConfiguration : removed call to de/aikiit/mailclena/MailClenaParameterParser::printHelp → SURVIVED |
printHelp(); |
| 45 | } else { | |
| 46 | ||
| 47 | try { | |
| 48 | final CommandLineParser parser = new DefaultParser(); | |
| 49 | final CommandLine cmd = parser.parse(getAvailableOptions(), args); | |
| 50 | final MailConfiguration.MailConfigurationBuilder mailConfigurationBuilder = MailConfiguration.builder(); | |
| 51 | ||
| 52 |
1
1. extractConfiguration : negated conditional → KILLED |
if (!Strings.isNullOrEmpty(cmd.getOptionValue(MailClenaCommandLineOptions.HOST.getOpt())) && |
| 53 |
1
1. extractConfiguration : negated conditional → KILLED |
!Strings.isNullOrEmpty(cmd.getOptionValue(MailClenaCommandLineOptions.USERNAME.getOpt())) && |
| 54 |
1
1. extractConfiguration : negated conditional → KILLED |
!Strings.isNullOrEmpty(cmd.getOptionValue(MailClenaCommandLineOptions.PASSWORD.getOpt()))) { |
| 55 | mailConfigurationBuilder.host(cmd.getOptionValue(MailClenaCommandLineOptions.HOST.getOpt())); | |
| 56 | mailConfigurationBuilder.username(cmd.getOptionValue(MailClenaCommandLineOptions.USERNAME.getOpt())); | |
| 57 | mailConfigurationBuilder.password(cmd.getOptionValue(MailClenaCommandLineOptions.PASSWORD.getOpt())); | |
| 58 | ||
| 59 |
1
1. extractConfiguration : negated conditional → KILLED |
if (!Strings.isNullOrEmpty(cmd.getOptionValue(MailClenaCommandLineOptions.COMMAND.getOpt()))) { |
| 60 | mailConfigurationBuilder.command(cmd.getOptionValue(MailClenaCommandLineOptions.COMMAND.getOpt())); | |
| 61 | } else { | |
| 62 | mailConfigurationBuilder.command("list"); | |
| 63 | } | |
| 64 | ||
| 65 | final MailConfiguration mailConfiguration = mailConfigurationBuilder.build(); | |
| 66 | log.debug("Extracted configuration from given parameters : {}", mailConfiguration); | |
| 67 |
1
1. extractConfiguration : replaced return value with Optional.empty for de/aikiit/mailclena/MailClenaParameterParser::extractConfiguration → KILLED |
return Optional.of(mailConfiguration); |
| 68 | } | |
| 69 | ||
| 70 | } catch (ParseException | NullPointerException e) { | |
| 71 | log.error("Unable to parse given command line parameters", e); | |
| 72 |
1
1. extractConfiguration : removed call to de/aikiit/mailclena/MailClenaParameterParser::printHelp → SURVIVED |
printHelp(); |
| 73 | throw new IllegalArgumentException("Exception while parsing command line arguments"); | |
| 74 | } | |
| 75 | } | |
| 76 | return Optional.empty(); | |
| 77 | } | |
| 78 | ||
| 79 | private void printHelp() { | |
| 80 | final HelpFormatter formatter = new HelpFormatter(); | |
| 81 |
1
1. printHelp : removed call to org/apache/commons/cli/HelpFormatter::printHelp → SURVIVED |
formatter.printHelp("MailClena", getAvailableOptions()); |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Convert enumeration elements into command-line options. | |
| 86 | * | |
| 87 | * @return available parameter options converted as CLI option elements. | |
| 88 | */ | |
| 89 | @VisibleForTesting | |
| 90 | Options getAvailableOptions() { | |
| 91 | Options o = new Options(); | |
| 92 | ||
| 93 | for (MailClenaCommandLineOptions option : MailClenaCommandLineOptions.values()) { | |
| 94 | o.addOption(new Option(option.getOpt(), option.name().toLowerCase(), true, option.getDescription())); | |
| 95 | } | |
| 96 |
1
1. getAvailableOptions : replaced return value with null for de/aikiit/mailclena/MailClenaParameterParser::getAvailableOptions → KILLED |
return o; |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * Available command-line options to set MailClena's configuration parameters. | |
| 101 | */ | |
| 102 | @VisibleForTesting | |
| 103 | enum MailClenaCommandLineOptions { | |
| 104 | /** | |
| 105 | * Specify host name option. | |
| 106 | */ | |
| 107 | HOST("h", "Hostname - example: https://imap.yourisp.org"), | |
| 108 | /** | |
| 109 | * Specify username option. | |
| 110 | */ | |
| 111 | USERNAME("u", "Username - example: myuser@tld.org"), | |
| 112 | /** | |
| 113 | * Specify password option. | |
| 114 | */ | |
| 115 | PASSWORD("p", "Password - example: myfancypassword"), | |
| 116 | /** | |
| 117 | * Specify command for execution within the application. | |
| 118 | */ | |
| 119 | COMMAND("c", "Command to execute - example: 'list' or 'clean', if no operation is given defaults to 'list'"); | |
| 120 | ||
| 121 | private final String opt; | |
| 122 | private final String desc; | |
| 123 | ||
| 124 | MailClenaCommandLineOptions(final String option, final String description) { | |
| 125 | this.opt = option; | |
| 126 | this.desc = description; | |
| 127 | } | |
| 128 | ||
| 129 | /** | |
| 130 | * Returns the shortcut of the current command. | |
| 131 | * | |
| 132 | * @return option shortcut of the current command-line option. | |
| 133 | */ | |
| 134 | String getOpt() { | |
| 135 |
1
1. getOpt : replaced return value with "" for de/aikiit/mailclena/MailClenaParameterParser$MailClenaCommandLineOptions::getOpt → KILLED |
return opt; |
| 136 | } | |
| 137 | ||
| 138 | /** | |
| 139 | * Returns a longer and verbald description of the current command. | |
| 140 | * | |
| 141 | * @return verbal description of the current command-line option. | |
| 142 | */ | |
| 143 | String getDescription() { | |
| 144 |
1
1. getDescription : replaced return value with "" for de/aikiit/mailclena/MailClenaParameterParser$MailClenaCommandLineOptions::getDescription → SURVIVED |
return desc; |
| 145 | } | |
| 146 | } | |
| 147 | ||
| 148 | } | |
Mutations | ||
| 43 |
1.1 2.2 |
|
| 44 |
1.1 |
|
| 52 |
1.1 |
|
| 53 |
1.1 |
|
| 54 |
1.1 |
|
| 59 |
1.1 |
|
| 67 |
1.1 |
|
| 72 |
1.1 |
|
| 81 |
1.1 |
|
| 96 |
1.1 |
|
| 135 |
1.1 |
|
| 144 |
1.1 |