Repository: ant Updated Branches: refs/heads/master af74d1f6b -> b7d1e9bde
http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/util/regexp/RegexpFactory.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/util/regexp/RegexpFactory.java b/src/main/org/apache/tools/ant/util/regexp/RegexpFactory.java index 7077a21..8e1ee0a 100644 --- a/src/main/org/apache/tools/ant/util/regexp/RegexpFactory.java +++ b/src/main/org/apache/tools/ant/util/regexp/RegexpFactory.java @@ -30,10 +30,6 @@ import org.apache.tools.ant.util.ClasspathUtils; */ public class RegexpFactory extends RegexpMatcherFactory { - /** Constructor for RegexpFactory */ - public RegexpFactory() { - } - /*** * Create a new regular expression matcher instance. * @return the matcher instance @@ -51,7 +47,7 @@ public class RegexpFactory extends RegexpMatcherFactory { * @throws BuildException on error */ public Regexp newRegexp(Project p) throws BuildException { - String systemDefault = null; + String systemDefault; if (p == null) { systemDefault = System.getProperty(MagicNames.REGEXP_IMPL); } else { @@ -78,8 +74,8 @@ public class RegexpFactory extends RegexpMatcherFactory { * @see RegexpMatcherFactory#createInstance(String) */ protected Regexp createRegexpInstance(String classname) throws BuildException { - return (Regexp) ClasspathUtils.newInstance(classname, RegexpFactory.class.getClassLoader(), - Regexp.class); + return ClasspathUtils.newInstance(classname, + RegexpFactory.class.getClassLoader(), Regexp.class); } } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/util/regexp/RegexpMatcher.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/util/regexp/RegexpMatcher.java b/src/main/org/apache/tools/ant/util/regexp/RegexpMatcher.java index 7938d8b..0ee4605 100644 --- a/src/main/org/apache/tools/ant/util/regexp/RegexpMatcher.java +++ b/src/main/org/apache/tools/ant/util/regexp/RegexpMatcher.java @@ -83,7 +83,7 @@ public interface RegexpMatcher { * @return the vector of groups * @throws BuildException on error */ - Vector getGroups(String argument) throws BuildException; + Vector<String> getGroups(String argument) throws BuildException; /*** * Does this regular expression match the input, given @@ -105,6 +105,6 @@ public interface RegexpMatcher { * @return the vector of groups * @throws BuildException on error */ - Vector getGroups(String input, int options) throws BuildException; + Vector<String> getGroups(String input, int options) throws BuildException; } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/util/regexp/RegexpMatcherFactory.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/util/regexp/RegexpMatcherFactory.java b/src/main/org/apache/tools/ant/util/regexp/RegexpMatcherFactory.java index a0a8a15..22d5888 100644 --- a/src/main/org/apache/tools/ant/util/regexp/RegexpMatcherFactory.java +++ b/src/main/org/apache/tools/ant/util/regexp/RegexpMatcherFactory.java @@ -33,10 +33,6 @@ import org.apache.tools.ant.util.ClasspathUtils; */ public class RegexpMatcherFactory { - /** Constructor for RegexpMatcherFactory. */ - public RegexpMatcherFactory() { - } - /*** * Create a new regular expression instance. * @return the matcher @@ -54,7 +50,7 @@ public class RegexpMatcherFactory { * @throws BuildException on error */ public RegexpMatcher newRegexpMatcher(Project p) throws BuildException { - String systemDefault = null; + String systemDefault; if (p == null) { systemDefault = System.getProperty(MagicNames.REGEXP_IMPL); } else { @@ -78,8 +74,8 @@ public class RegexpMatcherFactory { * @exception BuildException if an error occurs */ protected RegexpMatcher createInstance(String className) throws BuildException { - return (RegexpMatcher) ClasspathUtils.newInstance(className, RegexpMatcherFactory.class - .getClassLoader(), RegexpMatcher.class); + return ClasspathUtils.newInstance(className, + RegexpMatcherFactory.class.getClassLoader(), RegexpMatcher.class); } /** http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/util/regexp/RegexpUtil.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/util/regexp/RegexpUtil.java b/src/main/org/apache/tools/ant/util/regexp/RegexpUtil.java index ebd85fa..df2b2fd 100644 --- a/src/main/org/apache/tools/ant/util/regexp/RegexpUtil.java +++ b/src/main/org/apache/tools/ant/util/regexp/RegexpUtil.java @@ -33,7 +33,7 @@ public class RegexpUtil { * @return true if the flag is set */ public static boolean hasFlag(int options, int flag) { - return ((options & flag) > 0); + return (options & flag) > 0; } /** @@ -44,7 +44,7 @@ public class RegexpUtil { * @return the options with the flag unset */ public static int removeFlag(int options, int flag) { - return (options & (0xFFFFFFFF - flag)); + return options & (0xFFFFFFFF - flag); } /** http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/mail/ErrorInQuitException.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/mail/ErrorInQuitException.java b/src/main/org/apache/tools/mail/ErrorInQuitException.java index 6f78a14..353fdf4 100644 --- a/src/main/org/apache/tools/mail/ErrorInQuitException.java +++ b/src/main/org/apache/tools/mail/ErrorInQuitException.java @@ -31,6 +31,8 @@ import java.io.IOException; */ public class ErrorInQuitException extends IOException { + private static final long serialVersionUID = 1L; + /** * Initialise from an IOException * http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/mail/MailMessage.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/mail/MailMessage.java b/src/main/org/apache/tools/mail/MailMessage.java index b4173a9..4dc25dc 100644 --- a/src/main/org/apache/tools/mail/MailMessage.java +++ b/src/main/org/apache/tools/mail/MailMessage.java @@ -32,8 +32,10 @@ import java.io.OutputStream; import java.io.PrintStream; import java.net.InetAddress; import java.net.Socket; -import java.util.Enumeration; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.Vector; +import java.util.stream.Collectors; /** * A class to help send SMTP email. @@ -109,23 +111,23 @@ public class MailMessage { private String from; /** list of email addresses to reply to */ - private Vector replyto; + private final Vector<String> replyto = new Vector<>(); /** list of email addresses to send to */ - private Vector to; + private final Vector<String> to = new Vector<>(); /** list of email addresses to cc to */ - private Vector cc; + private final Vector<String> cc = new Vector<>(); /** headers to send in the mail */ - private Vector headersKeys; - private Vector headersValues; + private final Map<String,String> headers = new LinkedHashMap<>(); private MailPrintStream out; private SmtpResponseReader in; private Socket socket; + private static final int OK_READY = 220; private static final int OK_HELO = 250; private static final int OK_FROM = 250; @@ -135,46 +137,41 @@ public class MailMessage { private static final int OK_DOT = 250; private static final int OK_QUIT = 221; - /** - * Constructs a new MailMessage to send an email. - * Use localhost as the mail server with port 25. - * - * @exception IOException if there's any problem contacting the mail server - */ - public MailMessage() throws IOException { - this(DEFAULT_HOST, DEFAULT_PORT); - } - - /** - * Constructs a new MailMessage to send an email. - * Use the given host as the mail server with port 25. - * - * @param host the mail server to use - * @exception IOException if there's any problem contacting the mail server - */ - public MailMessage(String host) throws IOException { - this(host, DEFAULT_PORT); - } - - /** - * Constructs a new MailMessage to send an email. - * Use the given host and port as the mail server. - * - * @param host the mail server to use - * @param port the port to connect to - * @exception IOException if there's any problem contacting the mail server - */ - public MailMessage(String host, int port) throws IOException { - this.port = port; - this.host = host; - replyto = new Vector(); - to = new Vector(); - cc = new Vector(); - headersKeys = new Vector(); - headersValues = new Vector(); - connect(); - sendHelo(); - } + /** + * Constructs a new MailMessage to send an email. + * Use localhost as the mail server with port 25. + * + * @exception IOException if there's any problem contacting the mail server + */ + public MailMessage() throws IOException { + this(DEFAULT_HOST, DEFAULT_PORT); + } + + /** + * Constructs a new MailMessage to send an email. + * Use the given host as the mail server with port 25. + * + * @param host the mail server to use + * @exception IOException if there's any problem contacting the mail server + */ + public MailMessage(String host) throws IOException { + this(host, DEFAULT_PORT); + } + + /** + * Constructs a new MailMessage to send an email. + * Use the given host and port as the mail server. + * + * @param host the mail server to use + * @param port the port to connect to + * @exception IOException if there's any problem contacting the mail server + */ + public MailMessage(String host, int port) throws IOException { + this.port = port; + this.host = host; + connect(); + sendHelo(); + } /** * Set the port to connect to the SMTP host. @@ -204,234 +201,222 @@ public class MailMessage { * */ public void replyto(String rto) { - this.replyto.addElement(rto); - } - - /** - * Sets the to address. Also sets the "To" header. This method may be - * called multiple times. - * - * @param to the to address - * @exception IOException if there's any problem reported by the mail server - */ - public void to(String to) throws IOException { - sendRcpt(to); - this.to.addElement(to); - } - - /** - * Sets the cc address. Also sets the "Cc" header. This method may be - * called multiple times. - * - * @param cc the cc address - * @exception IOException if there's any problem reported by the mail server - */ - public void cc(String cc) throws IOException { - sendRcpt(cc); - this.cc.addElement(cc); - } - - /** - * Sets the bcc address. Does NOT set any header since it's a *blind* copy. - * This method may be called multiple times. - * - * @param bcc the bcc address - * @exception IOException if there's any problem reported by the mail server - */ - public void bcc(String bcc) throws IOException { - sendRcpt(bcc); - // No need to keep track of Bcc'd addresses - } - - /** - * Sets the subject of the mail message. Actually sets the "Subject" - * header. - * @param subj the subject of the mail message - */ - public void setSubject(String subj) { - setHeader("Subject", subj); - } - - /** - * Sets the named header to the given value. RFC 822 provides the rules for - * what text may constitute a header name and value. - * @param name name of the header - * @param value contents of the header - */ - public void setHeader(String name, String value) { - // Blindly trust the user doesn't set any invalid headers - headersKeys.add(name); - headersValues.add(value); - } - - /** - * Returns a PrintStream that can be used to write the body of the message. - * A stream is used since email bodies are byte-oriented. A writer can - * be wrapped on top if necessary for internationalization. - * This is actually done in Message.java - * - * @return a printstream containing the data and the headers of the email - * @exception IOException if there's any problem reported by the mail server - * @see org.apache.tools.ant.taskdefs.email.Message - */ - public PrintStream getPrintStream() throws IOException { - setFromHeader(); - setReplyToHeader(); - setToHeader(); - setCcHeader(); - setHeader("X-Mailer", "org.apache.tools.mail.MailMessage (ant.apache.org)"); - sendData(); - flushHeaders(); - return out; - } - - - // RFC 822 s4.1: "From:" header must be sent - // We rely on error checking by the MTA - void setFromHeader() { - setHeader("From", from); - } - - // RFC 822 s4.1: "Reply-To:" header is optional - void setReplyToHeader() { - if (!replyto.isEmpty()) { - setHeader("Reply-To", vectorToList(replyto)); - } - } - - void setToHeader() { - if (!to.isEmpty()) { - setHeader("To", vectorToList(to)); - } - } - - void setCcHeader() { - if (!cc.isEmpty()) { - setHeader("Cc", vectorToList(cc)); - } - } - - String vectorToList(Vector v) { - StringBuffer buf = new StringBuffer(); - Enumeration e = v.elements(); - while (e.hasMoreElements()) { - buf.append(e.nextElement()); - if (e.hasMoreElements()) { - buf.append(", "); - } - } - return buf.toString(); - } - - void flushHeaders() throws IOException { - // RFC 822 s4.1: - // "Header fields are NOT required to occur in any particular order, - // except that the message body MUST occur AFTER the headers" - // (the same section specifies a recommended order, which we ignore) - final int size = headersKeys.size(); - for (int i = 0; i < size; i++) { - String name = (String) headersKeys.elementAt(i); - String value = (String) headersValues.elementAt(i); - out.println(name + ": " + value); - } - out.println(); - out.flush(); - } - - /** - * Sends the message and closes the connection to the server. - * The MailMessage object cannot be reused. - * - * @exception IOException if there's any problem reported by the mail server - */ - public void sendAndClose() throws IOException { - try { - sendDot(); - sendQuit(); - } finally { - disconnect(); - } - } - - // Make a limited attempt to extract a sanitized email address - // Prefer text in <brackets>, ignore anything in (parentheses) - static String sanitizeAddress(String s) { - int paramDepth = 0; - int start = 0; - int end = 0; - int len = s.length(); - - for (int i = 0; i < len; i++) { - char c = s.charAt(i); - if (c == '(') { - paramDepth++; - if (start == 0) { - end = i; // support "address (name)" + this.replyto.addElement(rto); + } + + /** + * Sets the to address. Also sets the "To" header. This method may be + * called multiple times. + * + * @param to the to address + * @exception IOException if there's any problem reported by the mail server + */ + public void to(String to) throws IOException { + sendRcpt(to); + this.to.addElement(to); + } + + /** + * Sets the cc address. Also sets the "Cc" header. This method may be + * called multiple times. + * + * @param cc the cc address + * @exception IOException if there's any problem reported by the mail server + */ + public void cc(String cc) throws IOException { + sendRcpt(cc); + this.cc.addElement(cc); + } + + /** + * Sets the bcc address. Does NOT set any header since it's a *blind* copy. + * This method may be called multiple times. + * + * @param bcc the bcc address + * @exception IOException if there's any problem reported by the mail server + */ + public void bcc(String bcc) throws IOException { + sendRcpt(bcc); + // No need to keep track of Bcc'd addresses + } + + /** + * Sets the subject of the mail message. Actually sets the "Subject" + * header. + * @param subj the subject of the mail message + */ + public void setSubject(String subj) { + setHeader("Subject", subj); + } + + /** + * Sets the named header to the given value. RFC 822 provides the rules for + * what text may constitute a header name and value. + * @param name name of the header + * @param value contents of the header + */ + public void setHeader(String name, String value) { + // Blindly trust the user doesn't set any invalid headers + headers.put(name, value); + } + + /** + * Returns a PrintStream that can be used to write the body of the message. + * A stream is used since email bodies are byte-oriented. A writer can + * be wrapped on top if necessary for internationalization. + * This is actually done in Message.java + * + * @return a printstream containing the data and the headers of the email + * @exception IOException if there's any problem reported by the mail server + * @see org.apache.tools.ant.taskdefs.email.Message + */ + public PrintStream getPrintStream() throws IOException { + setFromHeader(); + setReplyToHeader(); + setToHeader(); + setCcHeader(); + setHeader("X-Mailer", + "org.apache.tools.mail.MailMessage (ant.apache.org)"); + sendData(); + flushHeaders(); + return out; + } + + // RFC 822 s4.1: "From:" header must be sent + // We rely on error checking by the MTA + void setFromHeader() { + setHeader("From", from); + } + + // RFC 822 s4.1: "Reply-To:" header is optional + void setReplyToHeader() { + if (!replyto.isEmpty()) { + setHeader("Reply-To", vectorToList(replyto)); + } + } + + void setToHeader() { + if (!to.isEmpty()) { + setHeader("To", vectorToList(to)); + } + } + + void setCcHeader() { + if (!cc.isEmpty()) { + setHeader("Cc", vectorToList(cc)); + } + } + + String vectorToList(Vector<String> v) { + return v.stream().collect(Collectors.joining(", ")); + } + + void flushHeaders() throws IOException { + // RFC 822 s4.1: + // "Header fields are NOT required to occur in any particular order, + // except that the message body MUST occur AFTER the headers" + // (the same section specifies a recommended order, which we ignore) + headers.forEach((k, v)->out.printf("%s: %s%n", k, v)); + out.println(); + out.flush(); + } + + /** + * Sends the message and closes the connection to the server. + * The MailMessage object cannot be reused. + * + * @exception IOException if there's any problem reported by the mail server + */ + public void sendAndClose() throws IOException { + try { + sendDot(); + sendQuit(); + } finally { + disconnect(); } - } else if (c == ')') { - paramDepth--; + } + + // Make a limited attempt to extract a sanitized email address + // Prefer text in <brackets>, ignore anything in (parentheses) + static String sanitizeAddress(String s) { + int paramDepth = 0; + int start = 0; + int end = 0; + int len = s.length(); + + for (int i = 0; i < len; i++) { + char c = s.charAt(i); + if (c == '(') { + paramDepth++; + if (start == 0) { + end = i; // support "address (name)" + } + } else if (c == ')') { + paramDepth--; + if (end == 0) { + start = i + 1; // support "(name) address" + } + } else if (paramDepth == 0 && c == '<') { + start = i + 1; + } else if (paramDepth == 0 && c == '>') { + end = i; + } + } + if (end == 0) { - start = i + 1; // support "(name) address" + end = len; + } + + return s.substring(start, end); + } + + // * * * * * Raw protocol methods below here * * * * * + + void connect() throws IOException { + socket = new Socket(host, port); + out = new MailPrintStream( + new BufferedOutputStream(socket.getOutputStream())); + in = new SmtpResponseReader(socket.getInputStream()); + getReady(); + } + + void getReady() throws IOException { + String response = in.getResponse(); + int[] ok = { OK_READY }; + if (!isResponseOK(response, ok)) { + throw new IOException( + "Didn't get introduction from server: " + response); } - } else if (paramDepth == 0 && c == '<') { - start = i + 1; - } else if (paramDepth == 0 && c == '>') { - end = i; - } - } - - if (end == 0) { - end = len; - } - - return s.substring(start, end); - } - - // * * * * * Raw protocol methods below here * * * * * - - void connect() throws IOException { - socket = new Socket(host, port); - out = new MailPrintStream( - new BufferedOutputStream( - socket.getOutputStream())); - in = new SmtpResponseReader(socket.getInputStream()); - getReady(); - } - - void getReady() throws IOException { - String response = in.getResponse(); - int[] ok = {OK_READY}; - if (!isResponseOK(response, ok)) { - throw new IOException( - "Didn't get introduction from server: " + response); - } - } - void sendHelo() throws IOException { - String local = InetAddress.getLocalHost().getHostName(); - int[] ok = {OK_HELO}; - send("HELO " + local, ok); - } - void sendFrom(String from) throws IOException { - int[] ok = {OK_FROM}; - send("MAIL FROM: " + "<" + sanitizeAddress(from) + ">", ok); - } - void sendRcpt(String rcpt) throws IOException { - int[] ok = {OK_RCPT_1, OK_RCPT_2}; - send("RCPT TO: " + "<" + sanitizeAddress(rcpt) + ">", ok); - } - - void sendData() throws IOException { - int[] ok = {OK_DATA}; - send("DATA", ok); - } - - void sendDot() throws IOException { - int[] ok = {OK_DOT}; - send("\r\n.", ok); // make sure dot is on new line - } + } + + void sendHelo() throws IOException { + String local = InetAddress.getLocalHost().getHostName(); + int[] ok = { OK_HELO }; + send("HELO " + local, ok); + } + + void sendFrom(String from) throws IOException { + int[] ok = { OK_FROM }; + send("MAIL FROM: " + "<" + sanitizeAddress(from) + ">", ok); + } + + void sendRcpt(String rcpt) throws IOException { + int[] ok = { OK_RCPT_1, OK_RCPT_2 }; + send("RCPT TO: " + "<" + sanitizeAddress(rcpt) + ">", ok); + } + + void sendData() throws IOException { + int[] ok = { OK_DATA }; + send("DATA", ok); + } + + void sendDot() throws IOException { + int[] ok = { OK_DOT }; + send("\r\n.", ok); // make sure dot is on new line + } void sendQuit() throws IOException { - int[] ok = {OK_QUIT}; + int[] ok = { OK_QUIT }; try { send("QUIT", ok); } catch (IOException e) { @@ -440,23 +425,23 @@ public class MailMessage { } void send(String msg, int[] ok) throws IOException { - out.rawPrint(msg + "\r\n"); // raw supports <CRLF>.<CRLF> + out.rawPrint(msg + "\r\n"); // raw supports <CRLF>.<CRLF> String response = in.getResponse(); if (!isResponseOK(response, ok)) { - throw new IOException("Unexpected reply to command: " - + msg + ": " + response); + throw new IOException( + "Unexpected reply to command: " + msg + ": " + response); } } - boolean isResponseOK(String response, int[] ok) { - // Check that the response is one of the valid codes - for (int i = 0; i < ok.length; i++) { - if (response.startsWith("" + ok[i])) { - return true; - } + boolean isResponseOK(String response, int[] ok) { + // Check that the response is one of the valid codes + for (int i = 0; i < ok.length; i++) { + if (response.startsWith("" + ok[i])) { + return true; + } + } + return false; } - return false; - } void disconnect() throws IOException { if (out != null) { @@ -485,42 +470,43 @@ public class MailMessage { */ class MailPrintStream extends PrintStream { - private int lastChar; + private int lastChar; - public MailPrintStream(OutputStream out) { - super(out, true); // deprecated, but email is byte-oriented - } + public MailPrintStream(OutputStream out) { + super(out, true); // deprecated, but email is byte-oriented + } - // Mac does \n\r, but that's tough to distinguish from Windows \r\n\r\n. - // Don't tackle that problem right now. - public void write(int b) { - if (b == '\n' && lastChar != '\r') { - rawWrite('\r'); // ensure always \r\n - rawWrite(b); - } else if (b == '.' && lastChar == '\n') { - rawWrite('.'); // add extra dot - rawWrite(b); - } else { - rawWrite(b); + // Mac does \n\r, but that's tough to distinguish from Windows \r\n\r\n. + // Don't tackle that problem right now. + @Override + public void write(int b) { + if (b == '\n' && lastChar != '\r') { + rawWrite('\r'); // ensure always \r\n + rawWrite(b); + } else if (b == '.' && lastChar == '\n') { + rawWrite('.'); // add extra dot + rawWrite(b); + } else { + rawWrite(b); + } + lastChar = b; } - lastChar = b; - } - public void write(byte[] buf, int off, int len) { - for (int i = 0; i < len; i++) { - write(buf[off + i]); + @Override + public void write(byte[] buf, int off, int len) { + for (int i = 0; i < len; i++) { + write(buf[off + i]); + } } - } - void rawWrite(int b) { - super.write(b); - } + void rawWrite(int b) { + super.write(b); + } - void rawPrint(String s) { - int len = s.length(); - for (int i = 0; i < len; i++) { - rawWrite(s.charAt(i)); + void rawPrint(String s) { + int len = s.length(); + for (int i = 0; i < len; i++) { + rawWrite(s.charAt(i)); + } } - } } - http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/mail/SmtpResponseReader.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/mail/SmtpResponseReader.java b/src/main/org/apache/tools/mail/SmtpResponseReader.java index c1693f4..b6b3172 100644 --- a/src/main/org/apache/tools/mail/SmtpResponseReader.java +++ b/src/main/org/apache/tools/mail/SmtpResponseReader.java @@ -35,7 +35,6 @@ public class SmtpResponseReader { // CheckStyle:VisibilityModifier OFF - bc protected BufferedReader reader = null; // CheckStyle:VisibilityModifier ON - private StringBuffer result = new StringBuffer(); /** * Wrap this input stream. @@ -54,7 +53,7 @@ public class SmtpResponseReader { * @throws IOException on error. */ public String getResponse() throws IOException { - result.setLength(0); + StringBuilder result = new StringBuilder(); String line = reader.readLine(); // CheckStyle:MagicNumber OFF if (line != null && line.length() >= 3) { @@ -64,7 +63,7 @@ public class SmtpResponseReader { // CheckStyle:MagicNumber ON while (line != null) { - append(line); + appendTo(result, line); if (!hasMoreLines(line)) { break; } @@ -95,11 +94,10 @@ public class SmtpResponseReader { /** * Append the text from this line of the resonse. */ - private void append(String line) { + private static void appendTo(StringBuilder target, String line) { // CheckStyle:MagicNumber OFF if (line.length() > 4) { - result.append(line.substring(4)); - result.append(" "); + target.append(line.substring(4)).append(' '); } // CheckStyle:MagicNumber ON } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/tests/junit/org/apache/tools/ant/taskdefs/ProtectedJarMethodsTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ProtectedJarMethodsTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ProtectedJarMethodsTest.java index 27b3c8c..1d1a670 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ProtectedJarMethodsTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ProtectedJarMethodsTest.java @@ -21,6 +21,7 @@ package org.apache.tools.ant.taskdefs; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.List; import org.apache.tools.ant.BuildFileRule; import org.junit.Before; @@ -50,8 +51,8 @@ public class ProtectedJarMethodsTest { public void testGrabFilesAndDirs() throws IOException { buildRule.executeTarget("testIndexTests"); String archive = buildRule.getProject().getProperty(tempJar); - ArrayList dirs = new ArrayList(); - ArrayList files = new ArrayList(); + List<String> dirs = new ArrayList<>(); + List<String> files = new ArrayList<>(); String[] expectedDirs = new String[] { "META-INF/", "sub/", @@ -61,14 +62,14 @@ public class ProtectedJarMethodsTest { }; Jar.grabFilesAndDirs(archive, dirs, files); assertEquals(expectedDirs.length, dirs.size()); - for (int i = 0; i < expectedDirs.length; i++) { - assertTrue("Found " + expectedDirs[i], - dirs.contains(expectedDirs[i])); + for (String expectedDir : expectedDirs) { + assertTrue("Found " + expectedDir, + dirs.contains(expectedDir)); } assertEquals(expectedFiles.length, files.size()); - for (int i = 0; i < expectedFiles.length; i++) { - assertTrue("Found " + expectedFiles[i], - files.contains(expectedFiles[i])); + for (String expectedFile : expectedFiles) { + assertTrue("Found " + expectedFile, + files.contains(expectedFile)); } } @@ -81,26 +82,23 @@ public class ProtectedJarMethodsTest { @Test public void testFindJarNameNoMatch() { - assertNull(Jar.findJarName("foo", new String[] {"bar"})); + assertNull(Jar.findJarName("foo", new String[] { "bar" })); } @Test public void testFindJarNameSimpleMatches() { - assertEquals("foo", Jar.findJarName("foo", new String[] {"foo"})); - assertEquals("lib/foo", Jar.findJarName("foo", - new String[] {"lib/foo"})); - assertEquals("foo", Jar.findJarName("bar" + File.separatorChar + "foo", - new String[] {"foo"})); + assertEquals("foo", Jar.findJarName("foo", new String[] { "foo" })); assertEquals("lib/foo", - Jar.findJarName("bar" + File.separatorChar + "foo", - new String[] {"lib/foo"})); + Jar.findJarName("foo", new String[] { "lib/foo" })); + assertEquals("foo", Jar.findJarName("bar" + File.separatorChar + "foo", + new String[] { "foo" })); + assertEquals("lib/foo", Jar.findJarName( + "bar" + File.separatorChar + "foo", new String[] { "lib/foo" })); } @Test public void testFindJarNameLongestMatchWins() { - assertEquals("lib/foo", - Jar.findJarName("lib/foo", - new String[] {"foo", "lib/foo", - "lib/bar/foo"})); + assertEquals("lib/foo", Jar.findJarName("lib/foo", + new String[] { "foo", "lib/foo", "lib/bar/foo" })); } } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/tests/junit/org/apache/tools/ant/types/resources/LazyResourceCollectionTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/types/resources/LazyResourceCollectionTest.java b/src/tests/junit/org/apache/tools/ant/types/resources/LazyResourceCollectionTest.java index bb85d81..34bcecd 100644 --- a/src/tests/junit/org/apache/tools/ant/types/resources/LazyResourceCollectionTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/resources/LazyResourceCollectionTest.java @@ -1,186 +1,184 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.tools.ant.types.resources; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.NoSuchElementException; - - -import org.apache.tools.ant.types.Resource; -import org.apache.tools.ant.types.ResourceCollection; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -public class LazyResourceCollectionTest { - - private class StringResourceCollection implements ResourceCollection { - List resources = Arrays.<Resource>asList(); - - List createdIterators = new ArrayList(); - - public int size() { - return resources.size(); - } - - public Iterator<Resource> iterator() { - StringResourceIterator it = new StringResourceIterator(); - createdIterators.add(it); - return it; - } - - public boolean isFilesystemOnly() { - return false; - } - } - - private class StringResourceIterator implements Iterator { - int cursor = 0; - - public void remove() { - throw new UnsupportedOperationException(); - } - - public Object next() { - if (cursor < 3) { - cursor++; - return new StringResource("r" + cursor); - } - return null; - } - - public boolean hasNext() { - return cursor < 3; - } - } - - @Test - public void testLazyLoading() throws Exception { - StringResourceCollection collectionTest = new StringResourceCollection(); - LazyResourceCollectionWrapper lazyCollection = new LazyResourceCollectionWrapper(); - lazyCollection.add(collectionTest); - - Iterator<Resource> it = lazyCollection.iterator(); - assertOneCreatedIterator(collectionTest); - StringResourceIterator stringResourceIterator = (StringResourceIterator) collectionTest.createdIterators - .get(0); - assertEquals("A resource was loaded without iterating", 1, - stringResourceIterator.cursor); - - StringResource r = (StringResource) it.next(); - assertOneCreatedIterator(collectionTest); - assertEquals("r1", r.getValue()); - assertEquals("Iterating once load more than 1 resource", 2, - stringResourceIterator.cursor); - - r = (StringResource) it.next(); - assertOneCreatedIterator(collectionTest); - assertEquals("r2", r.getValue()); - assertEquals("Iterating twice load more than 2 resources", 3, - stringResourceIterator.cursor); - - r = (StringResource) it.next(); - assertOneCreatedIterator(collectionTest); - assertEquals("r3", r.getValue()); - assertEquals("Iterating 3 times load more than 3 resources", 3, - stringResourceIterator.cursor); - - try { - it.next(); - fail("NoSuchElementException should have been raised"); - } catch (NoSuchElementException e) { - // ok - } - } - - private void assertOneCreatedIterator( - StringResourceCollection testCollection) { - assertEquals("More than one iterator has been created", 1, - testCollection.createdIterators.size()); - } - - @Test - public void testCaching() throws Exception { - StringResourceCollection collectionTest = new StringResourceCollection(); - LazyResourceCollectionWrapper lazyCollection = new LazyResourceCollectionWrapper(); - lazyCollection.add(collectionTest); - - assertTrue(lazyCollection.isCache()); - Iterator<Resource> it1 = lazyCollection.iterator(); - assertOneCreatedIterator(collectionTest); - Iterator<Resource> it2 = lazyCollection.iterator(); - assertOneCreatedIterator(collectionTest); - - StringResourceIterator stringResourceIterator = (StringResourceIterator) collectionTest.createdIterators - .get(0); - assertEquals("A resource was loaded without iterating", 1, - stringResourceIterator.cursor); - - StringResource r = (StringResource) it1.next(); - assertEquals("r1", r.getValue()); - assertEquals("Iterating once load more than 1 resource", 2, - stringResourceIterator.cursor); - - r = (StringResource) it2.next(); - assertEquals("r1", r.getValue()); - assertEquals( - "The second iterator did not lookup in the cache for a resource", - 2, stringResourceIterator.cursor); - - r = (StringResource) it2.next(); - assertEquals("r2", r.getValue()); - assertEquals("Iterating twice load more than 2 resources", 3, - stringResourceIterator.cursor); - - r = (StringResource) it1.next(); - assertEquals("r2", r.getValue()); - assertEquals( - "The first iterator did not lookup in the cache for a resource", - 3, stringResourceIterator.cursor); - - r = (StringResource) it2.next(); - assertEquals("r3", r.getValue()); - assertEquals("Iterating 3 times load more than 3 resources", 3, - stringResourceIterator.cursor); - - r = (StringResource) it1.next(); - assertEquals("r3", r.getValue()); - assertEquals( - "The first iterator did not lookup in the cache for a resource", - 3, stringResourceIterator.cursor); - - try { - it1.next(); - fail("NoSuchElementException should have been raised"); - } catch (NoSuchElementException e) { - // ok - } - - try { - it2.next(); - fail("NoSuchElementException should have been raised"); - } catch (NoSuchElementException e) { - // ok - } +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.tools.ant.types.resources; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + + +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.ResourceCollection; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class LazyResourceCollectionTest { + + private class StringResourceCollection implements ResourceCollection { + List<StringResourceIterator> createdIterators = new ArrayList<>(); + + @Override + public int size() { + return 3; + } + + @Override + public Iterator<Resource> iterator() { + StringResourceIterator it = new StringResourceIterator(); + createdIterators.add(it); + return it; + } + + @Override + public boolean isFilesystemOnly() { + return false; + } + } + + private class StringResourceIterator implements Iterator<Resource> { + int cursor = 0; + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + @Override + public StringResource next() { + if (cursor < 3) { + cursor++; + return new StringResource("r" + cursor); + } + return null; + } + + @Override + public boolean hasNext() { + return cursor < 3; + } + } + + @Test + public void testLazyLoading() throws Exception { + StringResourceCollection collectionTest = new StringResourceCollection(); + LazyResourceCollectionWrapper lazyCollection = new LazyResourceCollectionWrapper(); + lazyCollection.add(collectionTest); + + Iterator<Resource> it = lazyCollection.iterator(); + assertOneCreatedIterator(collectionTest); + StringResourceIterator stringResourceIterator = + collectionTest.createdIterators.get(0); + assertEquals("A resource was loaded without iterating", 1, + stringResourceIterator.cursor); + + assertStringValue("r1", it.next()); + assertOneCreatedIterator(collectionTest); + assertEquals("Iterating once load more than 1 resource", 2, + stringResourceIterator.cursor); + + assertStringValue("r2", it.next()); + assertOneCreatedIterator(collectionTest); + assertEquals("Iterating twice load more than 2 resources", 3, + stringResourceIterator.cursor); + + assertStringValue("r3", it.next()); + assertOneCreatedIterator(collectionTest); + assertEquals("Iterating 3 times load more than 3 resources", 3, + stringResourceIterator.cursor); + + try { + it.next(); + fail("NoSuchElementException should have been raised"); + } catch (NoSuchElementException e) { + // ok + } + } + + private void assertOneCreatedIterator( + StringResourceCollection testCollection) { + assertEquals("More than one iterator has been created", 1, + testCollection.createdIterators.size()); + } + + @Test + public void testCaching() throws Exception { + StringResourceCollection collectionTest = new StringResourceCollection(); + LazyResourceCollectionWrapper lazyCollection = new LazyResourceCollectionWrapper(); + lazyCollection.add(collectionTest); + + assertTrue(lazyCollection.isCache()); + Iterator<Resource> it1 = lazyCollection.iterator(); + assertOneCreatedIterator(collectionTest); + Iterator<Resource> it2 = lazyCollection.iterator(); + assertOneCreatedIterator(collectionTest); + + StringResourceIterator stringResourceIterator = + collectionTest.createdIterators.get(0); + assertEquals("A resource was loaded without iterating", 1, + stringResourceIterator.cursor); + + assertStringValue("r1", it1.next()); + assertEquals("Iterating once load more than 1 resource", 2, + stringResourceIterator.cursor); + + assertStringValue("r1", it2.next()); + assertEquals( + "The second iterator did not lookup in the cache for a resource", 2, + stringResourceIterator.cursor); + + assertStringValue("r2", it2.next()); + assertEquals("Iterating twice load more than 2 resources", 3, + stringResourceIterator.cursor); + + assertStringValue("r2", it1.next()); + assertEquals( + "The first iterator did not lookup in the cache for a resource", 3, + stringResourceIterator.cursor); + + assertStringValue("r3", it2.next()); + assertEquals("Iterating 3 times load more than 3 resources", 3, + stringResourceIterator.cursor); + + assertStringValue("r3", it1.next()); + assertEquals( + "The first iterator did not lookup in the cache for a resource", 3, + stringResourceIterator.cursor); + + try { + it1.next(); + fail("NoSuchElementException should have been raised"); + } catch (NoSuchElementException e) { + // ok + } + + try { + it2.next(); + fail("NoSuchElementException should have been raised"); + } catch (NoSuchElementException e) { + // ok + } + } + + private void assertStringValue(String expected, Resource r) { + assertEquals(expected, r.as(StringResource.class).getValue()); } -} +}
