This is an automated email from the git hooks/post-receive script. ebourg-guest pushed a commit to branch master in repository maven-debian-helper.
commit 4baee0d89ae1460359531b18429608bf111a071c Author: Emmanuel Bourg <[email protected]> Date: Mon Sep 2 11:43:03 2013 +0200 Refactored the UserInteraction class Ask the question again if the response is not valid --- .../debian/maven/packager/DependenciesSolver.java | 35 +++--- .../maven/packager/GenerateDebianFilesMojo.java | 33 +++-- .../maven/packager/interaction/ChoiceQuestion.java | 77 ++++++++++++ .../packager/interaction/MultilineQuestion.java | 52 ++++++++ .../maven/packager/interaction/Question.java | 70 +++++++++++ .../maven/packager/interaction/SimpleQuestion.java | 36 ++++++ .../maven/packager/interaction/YesNoQuestion.java | 60 +++++++++ .../packager/util/IgnoreDependencyQuestions.java | 9 +- .../maven/packager/util/LicensesScanner.java | 17 +-- .../maven/packager/util/UserInteraction.java | 123 ------------------ .../packager/interaction/ChoiceQuestionTest.java | 133 ++++++++++++++++++++ .../interaction/MultilineQuestionTest.java | 49 ++++++++ .../packager/interaction/SimpleQuestionTest.java | 42 +++++++ .../packager/interaction/YesNoQuestionTest.java | 82 ++++++++++++ 14 files changed, 650 insertions(+), 168 deletions(-) diff --git a/maven-packager-utils/src/main/java/org/debian/maven/packager/DependenciesSolver.java b/maven-packager-utils/src/main/java/org/debian/maven/packager/DependenciesSolver.java index 295fbc3..e6f7c12 100644 --- a/maven-packager-utils/src/main/java/org/debian/maven/packager/DependenciesSolver.java +++ b/maven-packager-utils/src/main/java/org/debian/maven/packager/DependenciesSolver.java @@ -30,6 +30,9 @@ import java.util.regex.Pattern; import javax.xml.stream.XMLStreamException; +import org.debian.maven.packager.interaction.ChoiceQuestion; +import org.debian.maven.packager.interaction.SimpleQuestion; +import org.debian.maven.packager.interaction.YesNoQuestion; import org.debian.maven.packager.util.*; import org.debian.maven.repo.Dependency; import org.debian.maven.repo.DependencyNotFoundException; @@ -58,7 +61,6 @@ import static org.debian.maven.repo.DependencyRuleSetFiles.RulesType.*; public class DependenciesSolver { private static final Logger log = Logger.getLogger(DependenciesSolver.class.getName()); - private final UserInteraction userInteraction = new UserInteraction(); private final IgnoreDependencyQuestions ignoreDependencyQuestion; private File baseDir; @@ -102,7 +104,7 @@ public class DependenciesSolver { this.outputDirectory = outputDirectory; this.scanner = scanner; this.interactive = interactive; - this.ignoreDependencyQuestion = new IgnoreDependencyQuestions(userInteraction, interactive); + this.ignoreDependencyQuestion = new IgnoreDependencyQuestions(interactive); pomTransformer.setVerbose(true); pomTransformer.setFixVersions(false); pomTransformer.setRulesFiles(initDependencyRuleSetFiles(outputDirectory, verbose)); @@ -326,8 +328,8 @@ public class DependenciesSolver { knownProjectDependencies.add(pom.getThisPom()); if (interactive && packageVersion == null) { - String q = "Enter the upstream version for the package. If you press <Enter> it will default to " + pom.getOriginalVersion(); - String v = userInteraction.ask(q); + String question = "Enter the upstream version for the package. If you press <Enter> it will default to " + pom.getOriginalVersion(); + String v = new SimpleQuestion(question).ask(); if (v.isEmpty()) { v = pom.getOriginalVersion(); } @@ -340,7 +342,7 @@ public class DependenciesSolver { } if (filterModules) { - boolean includeModule = userInteraction.askYesNo("Include the module " + IOUtil.relativePath(baseDir, projectPom) + " ?", true); + boolean includeModule = new YesNoQuestion("Include the module " + IOUtil.relativePath(baseDir, projectPom) + " ?", true).ask(); if (!includeModule) { pomTransformer.getListOfPOMs().getOrCreatePOMOptions(projectPom).setIgnore(true); pomTransformer.getRulesFiles().get(IGNORE).add(DependencyRule.newToMatch(pom.getThisPom())); @@ -363,8 +365,7 @@ public class DependenciesSolver { Rule selectedRule = askForVersionRule(pom.getThisPom()); versionToRules.put(pom.getThisPom().getVersion(), selectedRule); if (selectedRule.getPattern().equals("CUSTOM")) { - String rule = userInteraction.ask("Enter the pattern for your custom rule (in the form s/regex/replace/)") - .toLowerCase(); + String rule = new SimpleQuestion("Enter the pattern for your custom rule (in the form s/regex/replace/)").ask().toLowerCase(); selectedRule = new Rule(rule, "My custom rule " + rule); defaultRules.add(selectedRule); } @@ -380,7 +381,7 @@ public class DependenciesSolver { " is a bundle.\n" + "Inform mh_make that dependencies of type jar which may match this library should be transformed into bundles automatically?"; - boolean transformJarsIntoBundle = userInteraction.askYesNo(question2, true); + boolean transformJarsIntoBundle = new YesNoQuestion(question2, true).ask(); if (transformJarsIntoBundle) { String transformBundleRule = pom.getThisPom().getGroupId() + " " + pom.getThisPom().getArtifactId() @@ -411,7 +412,7 @@ public class DependenciesSolver { if (exploreProjects && !pom.getModules().isEmpty()) { if (interactive && !askedToFilterModules) { - filterModules = !userInteraction.askYesNo("This project contains modules. Include all modules?", true); + filterModules = !new YesNoQuestion("This project contains modules. Include all modules?", true).ask(); askedToFilterModules = true; } for (String module : pom.getModules()) { @@ -453,7 +454,7 @@ public class DependenciesSolver { choicesDescriptions.add(choice.getDescription()); } - int choice = userInteraction.askChoices(question, defaultChoice, choicesDescriptions); + int choice = new ChoiceQuestion(question, defaultChoice, choicesDescriptions).ask(); return choices.get(choice); } @@ -713,10 +714,10 @@ public class DependenciesSolver { if (interactive && pkg == null) { pkg = scanner.searchPkgContainingJar(dependency); if (pkg != null) { - String q = "[error] Package " + pkg + " does not contain Maven dependency " + dependency + " but there seem to be a match\n" + String question = "[error] Package " + pkg + " does not contain Maven dependency " + dependency + " but there seem to be a match\n" + "If the package contains already Maven artifacts but the names don't match, try to enter a substitution rule\n" + "of the form s/groupId/newGroupId/ s/artifactId/newArtifactId/ jar s/version/newVersion/ here:"; - String newRule = userInteraction.ask(q); + String newRule = new SimpleQuestion(question).ask(); if (!newRule.isEmpty()) { DependencyRule userRule = new DependencyRule(newRule); pomTransformer.getRulesFiles().get(RULES).add(userRule); @@ -724,16 +725,16 @@ public class DependenciesSolver { return resolveDependency(dependency.applyRules(Arrays.asList(userRule)), sourcePom, buildTime, mavenExtension, management, false); } } else { - String newRule = userInteraction.ask( + String newRule = new SimpleQuestion( "[error] Cannot resolve Maven dependency " + dependency + ". If you know a package that contains a compatible dependency,\n" - + "try to enter a substitution rule of the form s/groupId/newGroupId/ s/artifactId/newArtifactId/ jar s/version/newVersion/ here:\n"); + + "try to enter a substitution rule of the form s/groupId/newGroupId/ s/artifactId/newArtifactId/ jar s/version/newVersion/ here:\n").ask(); while (!newRule.isEmpty()) { DependencyRule userRule = new DependencyRule(newRule); Dependency newDependency = dependency.applyRules(Arrays.asList(userRule)); if (newDependency.equals(dependency)) { - newRule = userInteraction.ask("Your rule doesn't seem to apply on " + dependency + newRule = new SimpleQuestion("Your rule doesn't seem to apply on " + dependency + "Please enter a substitution rule of the form s/groupId/newGroupId/ s/artifactId/newArtifactId/ jar s/version/newVersion/ here," - + "or press <Enter> to give up"); + + "or press <Enter> to give up").ask(); } else { pomTransformer.getRulesFiles().get(RULES).add(userRule); System.out.println("Rescanning /usr/share/maven-repo..."); @@ -744,7 +745,7 @@ public class DependenciesSolver { } } - if (interactive && userInteraction.askYesNo("Try again to resolve the dependency?", true)) { + if (interactive && new YesNoQuestion("Try again to resolve the dependency?", true).ask()) { System.out.println("Rescanning /usr/share/maven-repo..."); pomTransformer.getRepository().scan(); // Clear caches diff --git a/maven-packager-utils/src/main/java/org/debian/maven/packager/GenerateDebianFilesMojo.java b/maven-packager-utils/src/main/java/org/debian/maven/packager/GenerateDebianFilesMojo.java index 7b0ff72..796c764 100644 --- a/maven-packager-utils/src/main/java/org/debian/maven/packager/GenerateDebianFilesMojo.java +++ b/maven-packager-utils/src/main/java/org/debian/maven/packager/GenerateDebianFilesMojo.java @@ -16,20 +16,30 @@ package org.debian.maven.packager; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; +import java.util.Set; +import java.util.StringTokenizer; +import java.util.TreeSet; + +import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Developer; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; - -import java.io.*; -import java.util.*; - -import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.project.MavenProject; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; +import org.debian.maven.packager.interaction.MultilineQuestion; +import org.debian.maven.packager.interaction.SimpleQuestion; import org.debian.maven.packager.util.LicensesScanner; import org.debian.maven.packager.util.PackageScanner; -import org.debian.maven.packager.util.UserInteraction; import org.debian.maven.repo.ListOfPOMs; /** @@ -147,7 +157,6 @@ public class GenerateDebianFilesMojo extends AbstractMojo { private PackageScanner scanner = new PackageScanner(false); private LicensesScanner licensesScanner = new LicensesScanner(); - private UserInteraction userInteraction = new UserInteraction(); public void execute() throws MojoExecutionException { File f = outputDirectory; @@ -183,10 +192,10 @@ public class GenerateDebianFilesMojo extends AbstractMojo { context.put("generateJavadoc", Boolean.valueOf(generateJavadoc)); if (project.getName() == null || project.getName().isEmpty()) { - project.setName(userInteraction.ask("POM does not contain the project name. Please enter the name of the project:")); + project.setName(new SimpleQuestion("POM does not contain the project name. Please enter the name of the project:").ask()); } if (project.getUrl() == null || project.getUrl().isEmpty()) { - project.setUrl(userInteraction.ask("POM does not contain the project URL. Please enter the URL of the project:")); + project.setUrl(new SimpleQuestion("POM does not contain the project URL. Please enter the URL of the project:").ask()); } Set<String> licenses = licensesScanner.discoverLicenses(project.getLicenses()); @@ -199,7 +208,7 @@ public class GenerateDebianFilesMojo extends AbstractMojo { String q = "Packager license for the debian/ files was not found, please enter a license name preferably in one of:\n" + "Apache Artistic BSD FreeBSD ISC CC-BY CC-BY-SA CC-BY-ND CC-BY-NC CC-BY-NC-SA CC-BY-NC-ND CC0 CDDL CPL Eiffel" + "Expat GPL LGPL GFDL GFDL-NIV LPPL MPL Perl PSF QPL W3C-Software ZLIB Zope"; - String s = userInteraction.ask(q); + String s = new SimpleQuestion(q).ask(); if (s.length() > 0) { packagerLicense = s; } @@ -223,7 +232,7 @@ public class GenerateDebianFilesMojo extends AbstractMojo { } } if (copyrightOwner == null || copyrightOwner.isEmpty()) { - copyrightOwner = userInteraction.ask("Could not find who owns the copyright for the upstream sources, please enter his name:"); + copyrightOwner = new SimpleQuestion("Could not find who owns the copyright for the upstream sources, please enter his name:").ask(); } context.put("copyrightOwner", copyrightOwner); @@ -246,7 +255,7 @@ public class GenerateDebianFilesMojo extends AbstractMojo { context.put("currentYear", new Integer(currentYear)); if (project.getDescription() == null || project.getDescription().trim().isEmpty()) { - project.setDescription(userInteraction.askMultiLine("Please enter a short description of the project, press Enter twice to stop.")); + project.setDescription(new MultilineQuestion("Please enter a short description of the project, press Enter twice to stop.").ask()); } context.put("description", formatDescription(project.getDescription())); diff --git a/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/ChoiceQuestion.java b/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/ChoiceQuestion.java new file mode 100644 index 0000000..56e59c4 --- /dev/null +++ b/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/ChoiceQuestion.java @@ -0,0 +1,77 @@ +/* + * Copyright 2013 Emmanuel Bourg + * + * 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 org.debian.maven.packager.interaction; + +import java.util.Collection; + +/** + * A question with multiple choices. + * + * @author Emmanuel Bourg + */ +public class ChoiceQuestion extends Question<Integer> { + + private int defaultChoice; + private Collection<String> choices; + + public ChoiceQuestion(String question, int defaultChoice, Collection<String> choices) { + super(question); + this.defaultChoice = defaultChoice; + this.choices = choices; + } + + @Override + public Integer ask() { + Integer choice = null; + + // keep asking the question until a valid choice is entered + while (choice == null) { + println(question); + printChoices(choices); + print("> "); + String response = readLine(); + if ("".equals(response.trim())) { + choice = defaultChoice; + } else { + try { + int c = Integer.parseInt(response); + if (c >= 0 && c < choices.size()) { + choice = c; + } + } catch (NumberFormatException e) { + } + } + } + + return choice; + } + + private void printChoices(Collection<String> choices) { + int counter = 0; + for (String choice : choices) { + StringBuilder line = new StringBuilder(); + if (counter == defaultChoice) { + line.append("[").append(counter).append("]"); + } else { + line.append(" ").append(counter).append(" "); + } + line.append(" - ").append(choice); + println(line.toString()); + ++counter; + } + } +} diff --git a/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/MultilineQuestion.java b/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/MultilineQuestion.java new file mode 100644 index 0000000..daa0c01 --- /dev/null +++ b/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/MultilineQuestion.java @@ -0,0 +1,52 @@ +/* + * Copyright 2013 Emmanuel Bourg + * + * 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 org.debian.maven.packager.interaction; + +/** + * Asks the user a question with a multi line response. + * The user finishes the response by entering two empty lines. + * + * @author Emmanuel Bourg + */ +public class MultilineQuestion extends Question<String> { + + public MultilineQuestion(String question) { + super(question); + } + + @Override + public String ask() { + println(question); + + StringBuilder answer = new StringBuilder(); + int emptyLineCount = 0; + while (emptyLineCount < 2) { + String line = readLine(); + if (line.isEmpty()) { + emptyLineCount++; + } else { + if (emptyLineCount > 0) { + emptyLineCount = 0; + answer.append("\n"); + } + answer.append(line); + answer.append("\n"); + } + } + return answer.toString().trim(); + } +} diff --git a/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/Question.java b/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/Question.java new file mode 100644 index 0000000..edad8f7 --- /dev/null +++ b/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/Question.java @@ -0,0 +1,70 @@ +/* + * Copyright 2013 Emmanuel Bourg + * + * 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 org.debian.maven.packager.interaction; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.io.PrintWriter; + +/** + * A generic question. + * + * @author Emmanuel Bourg + */ +public abstract class Question<T> { + + protected String question; + private BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + private PrintWriter out = new PrintWriter(System.out, true); + + protected Question(String question) { + this.question = question; + } + + void setInput(BufferedReader in) { + this.in = in; + } + + void setOutput(PrintWriter out) { + this.out = out; + } + + protected String readLine() { + try { + String line = in.readLine(); + return line != null ? line.trim() : ""; + } catch (IOException e) { + e.printStackTrace(); + return ""; + } + } + + protected void println(String text) { + out.println(text); + } + + protected void print(String text) { + out.print(text); + } + + /** + * Asks the question and returns the response. + */ + public abstract T ask(); +} diff --git a/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/SimpleQuestion.java b/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/SimpleQuestion.java new file mode 100644 index 0000000..0ccaa30 --- /dev/null +++ b/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/SimpleQuestion.java @@ -0,0 +1,36 @@ +/* + * Copyright 2013 Emmanuel Bourg + * + * 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 org.debian.maven.packager.interaction; + +/** + * Asks the user a question with a single line response. + * + * @author Emmanuel Bourg + */ +public class SimpleQuestion extends Question<String> { + + public SimpleQuestion(String question) { + super(question); + } + + @Override + public String ask() { + println(question); + print("> "); + return readLine(); + } +} diff --git a/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/YesNoQuestion.java b/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/YesNoQuestion.java new file mode 100644 index 0000000..fece63e --- /dev/null +++ b/maven-packager-utils/src/main/java/org/debian/maven/packager/interaction/YesNoQuestion.java @@ -0,0 +1,60 @@ +/* + * Copyright 2013 Emmanuel Bourg + * + * 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 org.debian.maven.packager.interaction; + +/** + * A yes/no question. The question is asked again if the answer is not 'y', 'n', + * 'yes' or 'no'. The answer is case insensitive. A blank response returns + * the default choice. + * + * @author Emmanuel Bourg + */ +public class YesNoQuestion extends Question<Boolean> { + + private boolean defaultChoice; + + public YesNoQuestion(String question, boolean defaultChoice) { + super(question); + this.defaultChoice = defaultChoice; + } + + @Override + public Boolean ask() { + Boolean choice = null; + + // keep asking the question until a valid choice is entered + while (choice == null) { + println(question); + print("["); + print(defaultChoice ? "Y" : "y"); + print("/"); + print(defaultChoice ? "n" : "N"); + print("]"); + print(" > "); + String response = readLine(); + if ("".equals(response.trim())) { + choice = defaultChoice; + } else if (response.equalsIgnoreCase("y") || response.equalsIgnoreCase("yes")) { + choice = true; + } else if (response.equalsIgnoreCase("n") || response.equalsIgnoreCase("no")) { + choice = false; + } + } + + return choice; + } +} diff --git a/maven-packager-utils/src/main/java/org/debian/maven/packager/util/IgnoreDependencyQuestions.java b/maven-packager-utils/src/main/java/org/debian/maven/packager/util/IgnoreDependencyQuestions.java index 16147ab..f4c86a1 100644 --- a/maven-packager-utils/src/main/java/org/debian/maven/packager/util/IgnoreDependencyQuestions.java +++ b/maven-packager-utils/src/main/java/org/debian/maven/packager/util/IgnoreDependencyQuestions.java @@ -3,12 +3,12 @@ package org.debian.maven.packager.util; import java.util.Set; import java.util.TreeSet; +import org.debian.maven.packager.interaction.YesNoQuestion; import org.debian.maven.repo.Dependency; public class IgnoreDependencyQuestions { private Set<Dependency> notIgnoredDependencies = new TreeSet<Dependency>(); - private final UserInteraction userInteraction; private final boolean interactive; // Plugins not useful for the build or whose use is against the @@ -105,9 +105,8 @@ public class IgnoreDependencyQuestions { }; - public IgnoreDependencyQuestions(UserInteraction userInteraction, boolean interactive) { + public IgnoreDependencyQuestions(boolean interactive) { this.interactive = interactive; - this.userInteraction = userInteraction; } @@ -131,8 +130,8 @@ public class IgnoreDependencyQuestions { if (!interactive || notIgnoredDependencies.contains(dependency)) { return false; } - String q = "\n" + "In " + sourcePomLoc + ":" + message + " " + dependency; - boolean ignore = userInteraction.askYesNo(q, defaultToIgnore); + String question = "\n" + "In " + sourcePomLoc + ":" + message + " " + dependency; + boolean ignore = new YesNoQuestion(question, defaultToIgnore).ask(); if (!ignore) { notIgnoredDependencies.add(dependency); } diff --git a/maven-packager-utils/src/main/java/org/debian/maven/packager/util/LicensesScanner.java b/maven-packager-utils/src/main/java/org/debian/maven/packager/util/LicensesScanner.java index f81edd0..c7d2591 100644 --- a/maven-packager-utils/src/main/java/org/debian/maven/packager/util/LicensesScanner.java +++ b/maven-packager-utils/src/main/java/org/debian/maven/packager/util/LicensesScanner.java @@ -17,14 +17,13 @@ package org.debian.maven.packager.util; import org.apache.maven.model.License; +import org.debian.maven.packager.interaction.SimpleQuestion; import java.util.List; import java.util.Set; import java.util.TreeSet; public class LicensesScanner { - private final UserInteraction userInteraction = new UserInteraction(); - public Set<String> discoverLicenses(List<License> projectLicenses) { Set<String> licenses = new TreeSet<String>(); for (License license : projectLicenses) { @@ -38,9 +37,8 @@ public class LicensesScanner { } boolean recognized = recognizeLicense(licenses, licenseName, licenseUrl); if (!recognized) { - String question = "License " + licenseName + licenseUrl + " was not recognized, please enter a license name preferably in one of:" - + getAvailableLicenses(); - String s = userInteraction.ask(question); + String s = new SimpleQuestion("License " + licenseName + licenseUrl + " was not recognized, " + + "please enter a license name preferably in one of:" + getAvailableLicenses()).ask(); if (s.length() > 0) { licenses.add(s); } @@ -55,9 +53,8 @@ public class LicensesScanner { for (String license : licenseResult.getLicenses()) { boolean recognized = recognizeLicense(licenses, license, ""); if (!recognized) { - String question = "License " + license + " was not recognized, please enter a license name preferably in one of:" - + getAvailableLicenses(); - String s = userInteraction.ask(question); + String s = new SimpleQuestion("License " + license + " was not recognized, " + + "please enter a license name preferably in one of:" + getAvailableLicenses()).ask(); if (s.length() > 0) { licenses.add(s); } @@ -65,9 +62,7 @@ public class LicensesScanner { } if (licenses.isEmpty()) { - String question = "License was not found, please enter a license name preferably in one of:" - + getAvailableLicenses(); - String s = userInteraction.ask(question); + String s = new SimpleQuestion("License was not found, please enter a license name preferably in one of:" + getAvailableLicenses()).ask(); if (s.length() > 0) { licenses.add(s); } diff --git a/maven-packager-utils/src/main/java/org/debian/maven/packager/util/UserInteraction.java b/maven-packager-utils/src/main/java/org/debian/maven/packager/util/UserInteraction.java deleted file mode 100644 index aa3ff6d..0000000 --- a/maven-packager-utils/src/main/java/org/debian/maven/packager/util/UserInteraction.java +++ /dev/null @@ -1,123 +0,0 @@ -package org.debian.maven.packager.util; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.LineNumberReader; -import java.util.ArrayList; -import java.util.List; - -public class UserInteraction { - private static final List<String> YESNO = new ArrayList<String>(2); - static { - YESNO.add("y"); - YESNO.add("n"); - } - - private String readLine() { - LineNumberReader consoleReader = new LineNumberReader(new InputStreamReader(System.in)); - try { - return consoleReader.readLine().trim(); - } catch (IOException e) { - e.printStackTrace(); - return ""; - } - } - - public String ask(String question) { - println(question); - print("> "); - return readLine(); - } - - public boolean askYesNo(String question, boolean defaultOpt) { - println(question); - print(formatChoicesShort(defaultOpt ? 0 : 1, YESNO)); - print(" > "); - String response = readLine(); - if ("".equals(response)) { - return defaultOpt; - } else { - return response.startsWith("y"); - } - } - - private String formatChoicesShort(int defaultOpt, Iterable<String> choices) { - StringBuilder sb = new StringBuilder(); - int counter = 0; - for (String choice : choices) { - if (counter > 0) { - sb.append("/"); - } - if (counter == defaultOpt) { - sb.append("[").append(choice).append("]"); - } else { - sb.append(choice); - } - ++counter; - } - return sb.toString(); - } - - public int askChoices(String question, int defaultOpt, Iterable<String> choices) { - println(question); - print(formatChoicesLong(defaultOpt, choices)); - print("> "); - String response = readLine(); - if ("".equals(response)) { - return defaultOpt; - } - try { - return Integer.parseInt(response); - } catch (NumberFormatException e) { - return defaultOpt; - } - } - - private String formatChoicesLong(int defaultOpt, Iterable<String> choices) { - StringBuilder sb = new StringBuilder(); - int counter = 0; - for (String choice : choices) { - if (counter == defaultOpt) { - sb.append("[").append(counter).append("]"); - } else { - sb.append(" ").append(counter).append(" "); - } - sb.append(" - ").append(choice).append("\n"); - ++counter; - } - return sb.toString(); - } - - /** - * Asks the user a question with a multi line response. - * - * The user finishes the response by entering two empty lines. - */ - public String askMultiLine(String question) { - println(question); - StringBuilder sb = new StringBuilder(); - int emptyEnterCount = 0; - while (emptyEnterCount < 2) { - String s = readLine(); - if (s.isEmpty()) { - emptyEnterCount++; - } else { - if (emptyEnterCount > 0) { - emptyEnterCount = 0; - sb.append("\n"); - } - sb.append(s); - sb.append("\n"); - } - } - return sb.toString(); - } - - private void println(String text) { - System.out.println(text); - } - - private void print(String text) { - System.out.print(text); - } -} diff --git a/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/ChoiceQuestionTest.java b/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/ChoiceQuestionTest.java new file mode 100644 index 0000000..5f7e02e --- /dev/null +++ b/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/ChoiceQuestionTest.java @@ -0,0 +1,133 @@ +/* + * Copyright 2013 Emmanuel Bourg + * + * 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 org.debian.maven.packager.interaction; + +import java.io.BufferedReader; +import java.io.PrintWriter; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.Arrays; + +import junit.framework.TestCase; + +public class ChoiceQuestionTest extends TestCase { + + private String EOL = System.getProperty("line.separator"); + + public void testQuestion() { + StringWriter output = new StringWriter(); + + ChoiceQuestion question = new ChoiceQuestion("What's the color of your poney?",2, Arrays.asList("Red", "Green", "Blue")); + question.setInput(new BufferedReader(new StringReader("1\n"))); + question.setOutput(new PrintWriter(output, true)); + + int answer = question.ask(); + + assertEquals("Question", "What's the color of your poney?" + EOL + + " 0 - Red" + EOL + + " 1 - Green" + EOL + + "[2] - Blue" + EOL + + "> ", output.toString()); + assertEquals("Answer", 1, answer); + } + + public void testDefaultChoice() { + StringWriter output = new StringWriter(); + + ChoiceQuestion question = new ChoiceQuestion("What's the color of your poney?",2, Arrays.asList("Red", "Green", "Blue")); + question.setInput(new BufferedReader(new StringReader("\n"))); + question.setOutput(new PrintWriter(output, true)); + + int answer = question.ask(); + + assertEquals("Question", "What's the color of your poney?" + EOL + + " 0 - Red" + EOL + + " 1 - Green" + EOL + + "[2] - Blue" + EOL + + "> ", output.toString()); + assertEquals("Answer", 2, answer); + } + + public void testOutOfRangeChoice() { + StringWriter output = new StringWriter(); + + ChoiceQuestion question = new ChoiceQuestion("What's the color of your poney?",1, Arrays.asList("Red", "Green", "Blue")); + question.setInput(new BufferedReader(new StringReader("3\n0\n"))); + question.setOutput(new PrintWriter(output, true)); + + int answer = question.ask(); + + assertEquals("Question", "What's the color of your poney?" + EOL + + " 0 - Red" + EOL + + "[1] - Green" + EOL + + " 2 - Blue" + EOL + + "> " + + "What's the color of your poney?" + EOL + + " 0 - Red" + EOL + + "[1] - Green" + EOL + + " 2 - Blue" + EOL + + "> ", output.toString()); + + assertEquals("Answer", 0, answer); + } + + public void testNegativeChoice() { + StringWriter output = new StringWriter(); + + ChoiceQuestion question = new ChoiceQuestion("What's the color of your poney?",1, Arrays.asList("Red", "Green", "Blue")); + question.setInput(new BufferedReader(new StringReader("-1\n0\n"))); + question.setOutput(new PrintWriter(output, true)); + + int answer = question.ask(); + + assertEquals("Question", "What's the color of your poney?" + EOL + + " 0 - Red" + EOL + + "[1] - Green" + EOL + + " 2 - Blue" + EOL + + "> " + + "What's the color of your poney?" + EOL + + " 0 - Red" + EOL + + "[1] - Green" + EOL + + " 2 - Blue" + EOL + + "> ", output.toString()); + + assertEquals("Answer", 0, answer); + } + + public void testNonNumericChoice() { + StringWriter output = new StringWriter(); + + ChoiceQuestion question = new ChoiceQuestion("What's the color of your poney?",1, Arrays.asList("Red", "Green", "Blue")); + question.setInput(new BufferedReader(new StringReader("X\n2\n"))); + question.setOutput(new PrintWriter(output, true)); + + int answer = question.ask(); + + assertEquals("Question", "What's the color of your poney?" + EOL + + " 0 - Red" + EOL + + "[1] - Green" + EOL + + " 2 - Blue" + EOL + + "> " + + "What's the color of your poney?" + EOL + + " 0 - Red" + EOL + + "[1] - Green" + EOL + + " 2 - Blue" + EOL + + "> ", output.toString()); + + assertEquals("Answer", 2, answer); + } +} diff --git a/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/MultilineQuestionTest.java b/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/MultilineQuestionTest.java new file mode 100644 index 0000000..f174470 --- /dev/null +++ b/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/MultilineQuestionTest.java @@ -0,0 +1,49 @@ +/* + * Copyright 2013 Emmanuel Bourg + * + * 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 org.debian.maven.packager.interaction; + +import java.io.BufferedReader; +import java.io.PrintWriter; +import java.io.StringReader; +import java.io.StringWriter; + +import junit.framework.TestCase; + +public class MultilineQuestionTest extends TestCase { + + private String EOL = System.getProperty("line.separator"); + + public void testQuestion() throws Exception { + StringWriter output = new StringWriter(); + + MultilineQuestion question = new MultilineQuestion("What's your address?"); + question.setInput(new BufferedReader( + new StringReader("\nSoftware in the Public Interest, Inc.\n" + + "P.O. Box 501248\n" + + "Indianapolis, IN 46250-6248\n" + + "United States\n\n"))); + question.setOutput(new PrintWriter(output, true)); + + String answer = question.ask(); + + assertEquals("Question", "What's your address?" + EOL, output.toString()); + assertEquals("Answer", "Software in the Public Interest, Inc.\n" + + "P.O. Box 501248\n" + + "Indianapolis, IN 46250-6248\n" + + "United States", answer); + } +} diff --git a/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/SimpleQuestionTest.java b/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/SimpleQuestionTest.java new file mode 100644 index 0000000..74e451e --- /dev/null +++ b/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/SimpleQuestionTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2013 Emmanuel Bourg + * + * 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 org.debian.maven.packager.interaction; + +import java.io.BufferedReader; +import java.io.PrintWriter; +import java.io.StringReader; +import java.io.StringWriter; + +import junit.framework.TestCase; + +public class SimpleQuestionTest extends TestCase { + + private String EOL = System.getProperty("line.separator"); + + public void testQuestion() throws Exception { + StringWriter output = new StringWriter(); + + SimpleQuestion question = new SimpleQuestion("What's your name?"); + question.setInput(new BufferedReader(new StringReader("Emmanuel\n"))); + question.setOutput(new PrintWriter(output, true)); + + String answer = question.ask(); + + assertEquals("Question", "What's your name?" + EOL + "> ", output.toString()); + assertEquals("Answer", "Emmanuel", answer); + } +} diff --git a/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/YesNoQuestionTest.java b/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/YesNoQuestionTest.java new file mode 100644 index 0000000..6374ba8 --- /dev/null +++ b/maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/YesNoQuestionTest.java @@ -0,0 +1,82 @@ +/* + * Copyright 2013 Emmanuel Bourg + * + * 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 org.debian.maven.packager.interaction; + +import java.io.BufferedReader; +import java.io.PrintWriter; +import java.io.StringReader; +import java.io.StringWriter; + +import junit.framework.TestCase; + +public class YesNoQuestionTest extends TestCase { + + private String EOL = System.getProperty("line.separator"); + + public void testQuestion() { + StringWriter output = new StringWriter(); + + YesNoQuestion question = new YesNoQuestion("Are you a Java programmer?", false); + question.setInput(new BufferedReader(new StringReader("y\n"))); + question.setOutput(new PrintWriter(output, true)); + + boolean answer = question.ask(); + + assertEquals("Question", "Are you a Java programmer?" + EOL + "[y/N] > ", output.toString()); + assertEquals("Answer", true, answer); + } + + public void testDefaultChoice() { + StringWriter output = new StringWriter(); + + YesNoQuestion question = new YesNoQuestion("Are you a Java programmer?", true); + question.setInput(new BufferedReader(new StringReader("\n"))); + question.setOutput(new PrintWriter(output, true)); + + boolean answer = question.ask(); + + assertEquals("Question", "Are you a Java programmer?" + EOL + "[Y/n] > ", output.toString()); + assertEquals("Answer", true, answer); + } + + public void testWrongAnswer() { + StringWriter output = new StringWriter(); + + YesNoQuestion question = new YesNoQuestion("Are you a Java programmer?", true); + question.setInput(new BufferedReader(new StringReader("X\nno\n"))); + question.setOutput(new PrintWriter(output, true)); + + boolean answer = question.ask(); + + assertEquals("Question", "Are you a Java programmer?" + EOL + "[Y/n] > " + + "Are you a Java programmer?" + EOL + "[Y/n] > ", output.toString()); + assertEquals("Answer", false, answer); + } + + public void testUpperCaseAnswer() { + StringWriter output = new StringWriter(); + + YesNoQuestion question = new YesNoQuestion("Are you a Java programmer?", false); + question.setInput(new BufferedReader(new StringReader("YES\n"))); + question.setOutput(new PrintWriter(output, true)); + + boolean answer = question.ask(); + + assertEquals("Question", "Are you a Java programmer?" + EOL + "[y/N] > ", output.toString()); + assertEquals("Answer", true, answer); + } +} -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/maven-debian-helper.git _______________________________________________ pkg-java-commits mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-java-commits

