Skip to content

Commit

Permalink
MAILET-115 Extract arrayToString from GenericMailet
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Duprat authored and chibenwa committed Jan 11, 2017
1 parent 5a81c0d commit e59ef33
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 25 deletions.
Expand Up @@ -284,28 +284,9 @@ protected final void checkInitParameters(String[] allowedArray) throws Messaging

if (bad.size() > 0) {
throw new MessagingException("Unexpected init parameters found: "
+ arrayToString(bad.toArray()));
+ org.apache.mailet.base.StringUtils.arrayToString(bad.toArray()));
}
}

/**
* Utility method for obtaining a string representation of an array of Objects.
*/
public final String arrayToString(Object[] array) {
if (array == null) {
return "null";
}
StringBuilder sb = new StringBuilder(1024);
sb.append("[");
for (int i = 0; i < array.length; i++) {
if (i > 0) {
sb.append(",");
}
sb.append(array[i]);
}
sb.append("]");
return sb.toString();
}

}

Expand Down
16 changes: 16 additions & 0 deletions mailet/base/src/main/java/org/apache/mailet/base/StringUtils.java
Expand Up @@ -23,6 +23,8 @@
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import com.google.common.base.Joiner;

/**
* Collects useful string utility methods.
*/
Expand Down Expand Up @@ -107,4 +109,18 @@ public static String capitalizeWords(String data) {
}
return res.toString();
}

/**
* Utility method for obtaining a string representation of an array of Objects.
*/
public static String arrayToString(Object[] array) {
if (array == null) {
return "null";
}
StringBuilder sb = new StringBuilder(1024);
sb.append("[");
sb.append(Joiner.on(",").join(array));
sb.append("]");
return sb.toString();
}
}
@@ -0,0 +1,61 @@
/****************************************************************
* 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.mailet.base;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class StringUtilsTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void arrayToStringShouldReturnNullWhenArrayIsNull() {
String arrayToString = StringUtils.arrayToString(null);
assertThat(arrayToString).isEqualTo("null");
}

@Test
public void arrayToStringShouldReturnOnlyBracketsWhenArrayIsEmpty() {
String arrayToString = StringUtils.arrayToString(new String[] {});
assertThat(arrayToString).isEqualTo("[]");
}

@Test
public void arrayToStringShouldReturnOneElementWhenArrayContainsOneElement() {
String arrayToString = StringUtils.arrayToString(new String[] { "first" });
assertThat(arrayToString).isEqualTo("[first]");
}

@Test
public void arrayToStringShouldReturnSeparatedElementsWhenArrayContainsMultipleElements() {
String arrayToString = StringUtils.arrayToString(new String[] { "first", "second", "fourth" });
assertThat(arrayToString).isEqualTo("[first,second,fourth]");
}

@Test
public void arrayToStringShouldThrowWhenArrayContainsANullElement() {
expectedException.expect(NullPointerException.class);
StringUtils.arrayToString(new String[] { "first", null, "fourth" });
}
}
Expand Up @@ -32,11 +32,11 @@
import javax.mail.internet.MimeMessage;

import org.apache.james.core.MailImpl;
import org.apache.james.transport.mailets.redirect.RedirectNotify;
import org.apache.james.transport.mailets.redirect.InitParameters;
import org.apache.james.transport.mailets.redirect.MailModifier;
import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters;
import org.apache.james.transport.mailets.redirect.NotifyMailetsMessage;
import org.apache.james.transport.mailets.redirect.RedirectNotify;
import org.apache.james.transport.mailets.redirect.SpecialAddress;
import org.apache.james.transport.mailets.redirect.TypeCode;
import org.apache.james.transport.mailets.utils.MimeMessageModifier;
Expand All @@ -51,6 +51,7 @@
import org.apache.mailet.MailAddress;
import org.apache.mailet.base.DateFormats;
import org.apache.mailet.base.RFC2822Headers;
import org.apache.mailet.base.StringUtils;
import org.apache.mailet.base.mail.MimeMultipartReport;

import com.google.common.base.Optional;
Expand Down Expand Up @@ -213,7 +214,7 @@ public void service(Mail originalMail) throws MessagingException {
newMail.setRecipients(getSenderAsList(originalMail));

if (getInitParameters().isDebug()) {
log("New mail - sender: " + newMail.getSender() + ", recipients: " + arrayToString(newMail.getRecipients().toArray()) + ", name: " + newMail.getName() + ", remoteHost: " + newMail.getRemoteHost() + ", remoteAddr: " + newMail.getRemoteAddr() + ", state: " + newMail.getState()
log("New mail - sender: " + newMail.getSender() + ", recipients: " + StringUtils.arrayToString(newMail.getRecipients().toArray()) + ", name: " + newMail.getName() + ", remoteHost: " + newMail.getRemoteHost() + ", remoteAddr: " + newMail.getRemoteAddr() + ", state: " + newMail.getState()
+ ", lastUpdated: " + newMail.getLastUpdated() + ", errorMessage: " + newMail.getErrorMessage());
}

Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.apache.mailet.MailAddress;
import org.apache.mailet.base.DateFormats;
import org.apache.mailet.base.RFC2822Headers;
import org.apache.mailet.base.StringUtils;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void setRecipients(List<MailAddress> recipients) {
if (!recipients.isEmpty()) {
mail.setRecipients(recipients);
if (mailet.getInitParameters().isDebug()) {
mailet.log("recipients set to: " + mailet.arrayToString(recipients.toArray()));
mailet.log("recipients set to: " + StringUtils.arrayToString(recipients.toArray()));
}
}
}
Expand Down
Expand Up @@ -24,6 +24,7 @@

import org.apache.james.core.MailImpl;
import org.apache.mailet.Mail;
import org.apache.mailet.base.StringUtils;

public class ProcessRedirectNotify {

Expand Down Expand Up @@ -53,8 +54,8 @@ public void process(Mail originalMail) throws MessagingException {
mailModifier.setRemoteAddr();
mailModifier.setRemoteHost();

if (isDebug) {
mailet.log("New mail - sender: " + newMail.getSender() + ", recipients: " + mailet.arrayToString(newMail.getRecipients().toArray()) + ", name: " + newMail.getName() + ", remoteHost: " + newMail.getRemoteHost() + ", remoteAddr: " + newMail.getRemoteAddr() + ", state: " + newMail.getState()
if (mailet.getInitParameters().isDebug()) {
mailet.log("New mail - sender: " + newMail.getSender() + ", recipients: " + StringUtils.arrayToString(newMail.getRecipients().toArray()) + ", name: " + newMail.getName() + ", remoteHost: " + newMail.getRemoteHost() + ", remoteAddr: " + newMail.getRemoteAddr() + ", state: " + newMail.getState()
+ ", lastUpdated: " + newMail.getLastUpdated() + ", errorMessage: " + newMail.getErrorMessage());
}

Expand Down

0 comments on commit e59ef33

Please sign in to comment.