Skip to content

Commit

Permalink
Fixes greenmail-mail-test#175 : Support verbose mode for GreenMail an…
Browse files Browse the repository at this point in the history
…d standalone runner
  • Loading branch information
marcelmay committed Jan 4, 2017
1 parent e11ee9a commit 52092a0
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public final void stopService() {
* @return the session.
*/
public Session createSession(Properties properties) {
return createSession(properties, false);
return createSession(properties, setup.isVerbose());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@
* </ul>
* <p/>
* <h2>Protocol specific setups</h2>
* Replace PROTOCOL with a value from {@value com.icegreen.greenmail.util.ServerSetup#PROTOCOLS}:
* Replace PROTOCOL with a value from {@link com.icegreen.greenmail.util.ServerSetup#PROTOCOLS}:
* <ul>
* <li>greenmail.PROTOCOL.port</li>
* <li>greenmail.PROTOCOL.hostname (defaults to {@link ServerSetup#getLocalHostAddress()}</li>
* </ul>
*/
public class PropertiesBasedServerSetupBuilder {

/**
* Enables verbose JavaMail debug output by setting JavaMail 'mail.debug' property.
*/
public static final String GREENMAIL_VERBOSE = "greenmail.verbose";

/**
* Creates a server setup based on provided properties.
*
Expand All @@ -60,6 +65,12 @@ public ServerSetup[] build(Properties properties) {
addSetup(hostname, protocol, properties, serverSetups);
}

for(ServerSetup setup: serverSetups) {
if(properties.containsKey(GREENMAIL_VERBOSE)) {
setup.setVerbose(true);
}
}

return serverSetups.toArray(new ServerSetup[serverSetups.size()]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class ServerSetup {
private long readTimeout = -1L;
private long connectionTimeout = -1L;
private long writeTimeout = -1L;
private boolean verbose = false;

/**
* Timeout when GreenMail starts a server, in milliseconds.
*/
Expand Down Expand Up @@ -147,6 +149,17 @@ public long getServerStartupTimeout() {
return serverStartupTimeout;
}

public boolean isVerbose() {
return verbose;
}

/**
* @param verbose if true enables JavaMail debug output by setting JavaMail property 'mail.debug'
*/
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}

/**
* Sets the server startup timeout in milliseconds.
*
Expand Down Expand Up @@ -258,6 +271,7 @@ public String toString() {
", connectionTimeout=" + connectionTimeout +
", writeTimeout=" + writeTimeout +
", serverStartupTimeout=" + serverStartupTimeout +
", verbose=" + isVerbose() +
'}';
}

Expand All @@ -282,6 +296,7 @@ public ServerSetup createCopy(String bindAddress) {
setup.setConnectionTimeout(getConnectionTimeout());
setup.setReadTimeout(getReadTimeout());
setup.setWriteTimeout(getWriteTimeout());
setup.setVerbose(isVerbose());

return setup;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.Properties;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class PropertiesBasedServerSetupBuilderTest {
@Test
Expand Down Expand Up @@ -35,13 +37,24 @@ public void testCreate() {
assert setups != null;
assertArrayEquals(setups, new ServerSetup[]{ServerSetupTest.SMTP});

// With debug
properties.clear();
properties.setProperty("greenmail.setup.test.smtp", "");
properties.setProperty(PropertiesBasedServerSetupBuilder.GREENMAIL_VERBOSE, "");
setups = setupBuilder.build(properties);
assert setups != null;
assertArrayEquals(setups, new ServerSetup[]{ServerSetupTest.SMTP});
assertTrue(setups[0].isVerbose());

// With hostname
properties.clear();
properties.setProperty("greenmail.setup.test.smtp", "");
properties.setProperty("greenmail.hostname", "0.0.0.0");
setups = setupBuilder.build(properties);
assert setups != null;
assertArrayEquals(setups, new ServerSetup[]{ServerSetupTest.SMTP.createCopy("0.0.0.0")});
assertFalse(setups[0].isVerbose());


// Default test setups
for(ServerSetup setup: ServerSetupTest.ALL) {
Expand Down
12 changes: 12 additions & 0 deletions greenmail-site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,15 @@ <h4>Starting GreenMail standalone</h4>
</div>
</td>
</tr>
<tr>
<td class="text-nowrap">-Dgreenmail.verbose</td>
<td>Enables verbose mode.
Useful if you want to see debug and protocol level output.
<div class="bs-example">
Example: <code>-Dgreenmail.verbose</code>
</div>
</td>
</tr>
<tr>
<td class="text-nowrap">-Dlog4j.configuration</td>
<td><p>Configures log4j using given configuration file.</p>
Expand Down Expand Up @@ -891,6 +900,9 @@ <h4>How can I create or delete a mail user?</h4>
greenMail.getManagers().getUserManager().deleteUser(user1); // Delete user
</code></pre><!-- @formatter:on -->

<h4>How can I get more verbose output?</h4>
<p>Enable the verbose mode, which prints debug output and protocol level communication.<br/>
See <a href="http://www.icegreen.com/greenmail/javadocs/com/icegreen/greenmail/util/ServerSetup.html#setVerbose">ServerSetup.setVerbose(boolean)</a> or GreenMail standalone runner option <a href="#deploy_standalone">greenmail.verbose</a> option.
</div>

<div id="download" class="bs-docs-section anchor">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

/**
* Enables GreenMail to run in standalone mode.
*
* <p>
* Example: java -Dgreenmail.smtp -Dgreenmail.users=test1:pwd1 -jar greenmail.jar
*
* @see PropertiesBasedServerSetupBuilder
* @see PropertiesBasedGreenMailConfigurationBuilder
*/
public class GreenMailStandaloneRunner {
private final Logger log = LoggerFactory.getLogger(GreenMailStandaloneRunner.class);
private GreenMail greenMail;

/**
* Start and configure GreenMail using given properties.
Expand All @@ -36,13 +37,32 @@ public void doRun(Properties properties) {
printUsage(System.out);

} else {
GreenMail greenMail = new GreenMail(serverSetup);
greenMail = new GreenMail(serverSetup);
log.info("Starting GreenMail standalone using " + Arrays.toString(serverSetup));
greenMail.withConfiguration(new PropertiesBasedGreenMailConfigurationBuilder().build(properties))
.start();
}
}

protected static void configureLogging(Properties properties) {
// Init logging: Try standard log4j configuration mechanism before falling back to
// provided logging configuration
String log4jConfig = System.getProperty("log4j.configuration");
if (null == log4jConfig) {
if (properties.containsKey(PropertiesBasedServerSetupBuilder.GREENMAIL_VERBOSE)) {
DOMConfigurator.configure(GreenMailStandaloneRunner.class.getResource("/log4j-verbose.xml"));
} else {
DOMConfigurator.configure(GreenMailStandaloneRunner.class.getResource("/log4j.xml"));
}
} else {
if (log4jConfig.toLowerCase().endsWith(".xml")) {
DOMConfigurator.configure(log4jConfig);
} else {
PropertyConfigurator.configure(log4jConfig);
}
}
}

private void printUsage(PrintStream out) {
// Don't use logger
out.println("Usage: java OPTIONS -jar greenmail.jar");
Expand All @@ -58,8 +78,9 @@ private void printUsage(PrintStream out) {
{"-Dgreenmail.<protocol|all>.port=...", "Specifies port. Requires additional hostname parameter."},
{"-Dgreenmail.users=<logon:pwd@domain>[,...]", "Specifies mail users, eg foo:[email protected],foo2:[email protected]."},
{"Note: domain must be DNS resolvable!"},
{"-Dgreenmail.auth.disabled ","Disables authentication check so that any password works."},
{"-Dgreenmail.auth.disabled ", "Disables authentication check so that any password works."},
{"Also automatically provisions previously non-existent users."},
{"-Dgreenmail.verbose ", "Enables verbose mode, including JavaMail debug output"},
};
for (String[] opt : options) {
if (opt.length == 1) {
Expand All @@ -81,20 +102,13 @@ private void printUsage(PrintStream out) {
out.println(" Starts SMTP on 0.0.0.0:3025 and IMAP on 0.0.0.0:3143");
}

public static void main(String[] args) {
// Init logging: Try standard log4j configuration mechanism before falling back to
// provided logging configuration
String log4jConfig = System.getProperty("log4j.configuration");
if (null == log4jConfig) {
DOMConfigurator.configure(GreenMailStandaloneRunner.class.getResource("/log4j.xml"));
} else {
if (log4jConfig.toLowerCase().endsWith(".xml")) {
DOMConfigurator.configure(log4jConfig);
} else {
PropertyConfigurator.configure(log4jConfig);
}
}
GreenMail getGreenMail() {
return greenMail;
}

new GreenMailStandaloneRunner().doRun(System.getProperties());
public static void main(String[] args) {
final Properties properties = System.getProperties();
configureLogging(properties);
new GreenMailStandaloneRunner().doRun(properties);
}
}
26 changes: 26 additions & 0 deletions greenmail-standalone/src/main/resources/log4j-verbose.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<!--
| For more configuration information and examples see the Jakarta Log4j
| website http://jakarta.apache.org/log4j and for the output pattern format
| http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
-->

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="DEBUG"/>

<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-7.7r %-5.5p %25.25c{2}| %m%n"/>
</layout>
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="CONSOLE"/>
</root>

</log4j:configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

import com.icegreen.greenmail.configuration.PropertiesBasedGreenMailConfigurationBuilder;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.PropertiesBasedServerSetupBuilder;
import com.icegreen.greenmail.util.ServerSetupTest;
import org.junit.Test;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Store;
import javax.mail.*;
import java.util.Properties;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class GreenMailStandaloneRunnerTest {

@Test
public void testDoRun() throws MessagingException {
GreenMailStandaloneRunner runner = new GreenMailStandaloneRunner();
final Properties properties = new Properties();
properties.setProperty(PropertiesBasedServerSetupBuilder.GREENMAIL_VERBOSE, "");
properties.setProperty("greenmail.setup.test.smtp", "");
properties.setProperty("greenmail.setup.test.imap", "");
properties.setProperty(PropertiesBasedGreenMailConfigurationBuilder.GREENMAIL_USERS,
Expand All @@ -28,7 +28,9 @@ public void testDoRun() throws MessagingException {
GreenMailUtil.sendTextEmail("test2@localhost", "test1@localhost", "Standalone test", "It worked",
ServerSetupTest.SMTP);

Store store = GreenMailUtil.getSession(ServerSetupTest.IMAP).getStore("imap");
final Session session = runner.getGreenMail().getImap().createSession();
assertTrue(session.getDebug());
Store store = session.getStore("imap");
try {
store.connect("test2", "pwd2");
final Folder folder = store.getFolder("INBOX");
Expand Down

0 comments on commit 52092a0

Please sign in to comment.