JAMES-2262 Improve MockSpamD location
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/71f6fe24 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/71f6fe24 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/71f6fe24 Branch: refs/heads/master Commit: 71f6fe24844f2662549b4d20d154d43f848cd5a3 Parents: 9c23977 Author: benwa <[email protected]> Authored: Fri Dec 22 09:41:54 2017 +0700 Committer: benwa <[email protected]> Committed: Mon Dec 25 11:24:56 2017 +0700 ---------------------------------------------------------------------- server/mailet/mailets/pom.xml | 12 +- .../transport/mailets/SpamAssassinTest.java | 4 +- server/protocols/protocols-smtp/pom.xml | 5 + .../smtpserver/SpamAssassinHandlerTest.java | 4 +- .../james/smtpserver/mock/util/MockSpamd.java | 109 ------------------- .../smtpserver/mock/util/MockSpamdTestRule.java | 46 -------- server/testing/pom.xml | 1 - .../java/org/apache/james/utils/MockSpamd.java | 109 +++++++++++++++++++ .../apache/james/utils/MockSpamdTestRule.java | 46 ++++++++ 9 files changed, 166 insertions(+), 170 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/71f6fe24/server/mailet/mailets/pom.xml ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/pom.xml b/server/mailet/mailets/pom.xml index a8f409c..f269dfa 100644 --- a/server/mailet/mailets/pom.xml +++ b/server/mailet/mailets/pom.xml @@ -135,23 +135,15 @@ </dependency> <dependency> <groupId>${project.groupId}</groupId> - <artifactId>james-server-protocols-library</artifactId> - <type>test-jar</type> - <scope>test</scope> + <artifactId>james-server-queue-api</artifactId> </dependency> <dependency> <groupId>${project.groupId}</groupId> - <artifactId>james-server-protocols-smtp</artifactId> - <version>${project.version}</version> - <type>test-jar</type> + <artifactId>james-server-testing</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>${project.groupId}</groupId> - <artifactId>james-server-queue-api</artifactId> - </dependency> - <dependency> - <groupId>${project.groupId}</groupId> <artifactId>james-server-util</artifactId> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/71f6fe24/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java index 875a4f9..aba82cd 100644 --- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java @@ -24,10 +24,10 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import javax.mail.MessagingException; -import org.apache.james.smtpserver.mock.util.MockSpamd; -import org.apache.james.smtpserver.mock.util.MockSpamdTestRule; import org.apache.james.util.Port; import org.apache.james.util.scanner.SpamAssassinInvoker; +import org.apache.james.utils.MockSpamd; +import org.apache.james.utils.MockSpamdTestRule; import org.apache.mailet.Mail; import org.apache.mailet.base.test.FakeMail; import org.apache.mailet.base.test.FakeMailetConfig; http://git-wip-us.apache.org/repos/asf/james-project/blob/71f6fe24/server/protocols/protocols-smtp/pom.xml ---------------------------------------------------------------------- diff --git a/server/protocols/protocols-smtp/pom.xml b/server/protocols/protocols-smtp/pom.xml index 2dd30c7..a18c272 100644 --- a/server/protocols/protocols-smtp/pom.xml +++ b/server/protocols/protocols-smtp/pom.xml @@ -123,6 +123,11 @@ </dependency> <dependency> <groupId>${project.groupId}</groupId> + <artifactId>james-server-testing</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> <artifactId>james-server-util</artifactId> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/71f6fe24/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SpamAssassinHandlerTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SpamAssassinHandlerTest.java b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SpamAssassinHandlerTest.java index cf1b7cc..3016e44 100644 --- a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SpamAssassinHandlerTest.java +++ b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SpamAssassinHandlerTest.java @@ -34,9 +34,9 @@ import org.apache.james.protocols.smtp.utils.BaseFakeSMTPSession; import org.apache.james.smtpserver.fastfail.SpamAssassinHandler; import org.apache.james.smtpserver.mock.MockMimeMessage; import org.apache.james.smtpserver.mock.mailet.MockMail; -import org.apache.james.smtpserver.mock.util.MockSpamd; -import org.apache.james.smtpserver.mock.util.MockSpamdTestRule; import org.apache.james.util.scanner.SpamAssassinInvoker; +import org.apache.james.utils.MockSpamd; +import org.apache.james.utils.MockSpamdTestRule; import org.apache.mailet.Mail; import org.junit.Rule; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/james-project/blob/71f6fe24/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamd.java ---------------------------------------------------------------------- diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamd.java b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamd.java deleted file mode 100644 index 45f6920..0000000 --- a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamd.java +++ /dev/null @@ -1,109 +0,0 @@ -/**************************************************************** - * 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.james.smtpserver.mock.util; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.ServerSocket; -import java.net.Socket; - -import org.apache.commons.io.IOUtils; - -import com.google.common.base.Preconditions; - -/** - * This class can be used to run a mocked SPAMD daemon - */ -public class MockSpamd implements Runnable { - - /** - * Mailcontent which is 100% spam - */ - public final static String GTUBE = "-SPAM-"; - public final static String NOT_SPAM = "Spam: False ; 3 / 5"; - public final static String SPAM = "Spam: True ; 1000 / 5"; - private BufferedReader in; - private OutputStream out; - private Socket spamd; - private ServerSocket socket; - private boolean isBinded; - - - /** - * Init the mocked SPAMD daemon - * - * @throws IOException - */ - public MockSpamd() { - isBinded = false; - } - - public int getPort() { - Preconditions.checkState(isBinded, "SpamD mock server is not binded"); - return socket.getLocalPort(); - } - - public void bind() throws IOException { - socket = new ServerSocket(0); - isBinded = true; - } - - /** - * @see java.lang.Runnable#run() - */ - @Override - public void run() { - - try { - boolean spam = false; - - // Accept connections - spamd = socket.accept(); - - in = new BufferedReader(new InputStreamReader(spamd.getInputStream())); - out = spamd.getOutputStream(); - - String line; - - // Parse the message - while ((line = in.readLine()) != null) { - if (line.contains(GTUBE)) { - spam = true; - } - } - if (spam) { - out.write(SPAM.getBytes()); - out.flush(); - } else { - out.write(NOT_SPAM.getBytes()); - out.flush(); - } - } catch (IOException e) { - // Should not happen - e.printStackTrace(); - } finally { - IOUtils.closeQuietly(in); - IOUtils.closeQuietly(out); - IOUtils.closeQuietly(spamd); - IOUtils.closeQuietly(socket); - } - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/71f6fe24/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamdTestRule.java ---------------------------------------------------------------------- diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamdTestRule.java b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamdTestRule.java deleted file mode 100644 index 4822b8f..0000000 --- a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamdTestRule.java +++ /dev/null @@ -1,46 +0,0 @@ -/**************************************************************** - * 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.james.smtpserver.mock.util; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import org.junit.rules.ExternalResource; - -public class MockSpamdTestRule extends ExternalResource { - - private ExecutorService executor = Executors.newSingleThreadExecutor(); - private MockSpamd spamd = new MockSpamd(); - - @Override - protected void before() throws Throwable { - spamd.bind(); - executor.execute(spamd); - } - - public int getPort() { - return spamd.getPort(); - } - - @Override - protected void after() { - executor.shutdownNow(); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/71f6fe24/server/testing/pom.xml ---------------------------------------------------------------------- diff --git a/server/testing/pom.xml b/server/testing/pom.xml index 705438a..3014fc8 100644 --- a/server/testing/pom.xml +++ b/server/testing/pom.xml @@ -46,7 +46,6 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <scope>test</scope> </dependency> <dependency> <groupId>org.assertj</groupId> http://git-wip-us.apache.org/repos/asf/james-project/blob/71f6fe24/server/testing/src/main/java/org/apache/james/utils/MockSpamd.java ---------------------------------------------------------------------- diff --git a/server/testing/src/main/java/org/apache/james/utils/MockSpamd.java b/server/testing/src/main/java/org/apache/james/utils/MockSpamd.java new file mode 100644 index 0000000..896df3c --- /dev/null +++ b/server/testing/src/main/java/org/apache/james/utils/MockSpamd.java @@ -0,0 +1,109 @@ +/**************************************************************** + * 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.james.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; + +import org.apache.commons.io.IOUtils; + +import com.google.common.base.Preconditions; + +/** + * This class can be used to run a mocked SPAMD daemon + */ +public class MockSpamd implements Runnable { + + /** + * Mailcontent which is 100% spam + */ + public final static String GTUBE = "-SPAM-"; + public final static String NOT_SPAM = "Spam: False ; 3 / 5"; + public final static String SPAM = "Spam: True ; 1000 / 5"; + private BufferedReader in; + private OutputStream out; + private Socket spamd; + private ServerSocket socket; + private boolean isBinded; + + + /** + * Init the mocked SPAMD daemon + * + * @throws IOException + */ + public MockSpamd() { + isBinded = false; + } + + public int getPort() { + Preconditions.checkState(isBinded, "SpamD mock server is not binded"); + return socket.getLocalPort(); + } + + public void bind() throws IOException { + socket = new ServerSocket(0); + isBinded = true; + } + + /** + * @see java.lang.Runnable#run() + */ + @Override + public void run() { + + try { + boolean spam = false; + + // Accept connections + spamd = socket.accept(); + + in = new BufferedReader(new InputStreamReader(spamd.getInputStream())); + out = spamd.getOutputStream(); + + String line; + + // Parse the message + while ((line = in.readLine()) != null) { + if (line.contains(GTUBE)) { + spam = true; + } + } + if (spam) { + out.write(SPAM.getBytes()); + out.flush(); + } else { + out.write(NOT_SPAM.getBytes()); + out.flush(); + } + } catch (IOException e) { + // Should not happen + e.printStackTrace(); + } finally { + IOUtils.closeQuietly(in); + IOUtils.closeQuietly(out); + IOUtils.closeQuietly(spamd); + IOUtils.closeQuietly(socket); + } + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/71f6fe24/server/testing/src/main/java/org/apache/james/utils/MockSpamdTestRule.java ---------------------------------------------------------------------- diff --git a/server/testing/src/main/java/org/apache/james/utils/MockSpamdTestRule.java b/server/testing/src/main/java/org/apache/james/utils/MockSpamdTestRule.java new file mode 100644 index 0000000..ea746df --- /dev/null +++ b/server/testing/src/main/java/org/apache/james/utils/MockSpamdTestRule.java @@ -0,0 +1,46 @@ +/**************************************************************** + * 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.james.utils; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.junit.rules.ExternalResource; + +public class MockSpamdTestRule extends ExternalResource { + + private ExecutorService executor = Executors.newSingleThreadExecutor(); + private MockSpamd spamd = new MockSpamd(); + + @Override + protected void before() throws Throwable { + spamd.bind(); + executor.execute(spamd); + } + + public int getPort() { + return spamd.getPort(); + } + + @Override + protected void after() { + executor.shutdownNow(); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
