commits@javamail.java.net

[javamail~mercurial:633] Update tests to use dynamic port numbers.

From: <shannon_at_java.net>
Date: Sat, 8 Feb 2014 00:21:19 +0000

Project: javamail
Repository: mercurial
Revision: 633
Author: shannon
Date: 2014-02-08 00:20:02 UTC
Link:

Log Message:
------------
MimeUtility.encodeText() does not work with Unicode surrogate pairs - bug 6274
IMAP alerts and notifications are not sent during authentication - bug 6283
Update tests to use dynamic port numbers.
Simplify tests to use a common server driver.


Revisions:
----------
631
632
633


Modified Paths:
---------------
doc/release/CHANGES.txt
mail/src/main/java/javax/mail/internet/MimeUtility.java
mail/src/test/java/javax/mail/internet/MimeUtilityTest.java
mail/src/main/java/com/sun/mail/imap/IMAPStore.java
mail/src/test/java/com/sun/mail/imap/IMAPAlertTest.java
mail/src/test/java/com/sun/mail/imap/IMAPAuthDebugTest.java
mail/src/test/java/com/sun/mail/imap/IMAPCloseFailureTest.java
mail/src/test/java/com/sun/mail/imap/IMAPConnectFailureTest.java
mail/src/test/java/com/sun/mail/imap/IMAPHandler.java
mail/src/test/java/com/sun/mail/imap/IMAPIdleStateTest.java
mail/src/test/java/com/sun/mail/imap/IMAPIdleUntaggedResponseTest.java
mail/src/test/java/com/sun/mail/imap/IMAPLoginFailureTest.java
mail/src/test/java/com/sun/mail/imap/IMAPSaslLoginTest.java
mail/src/test/java/com/sun/mail/pop3/POP3AuthDebugTest.java
mail/src/test/java/com/sun/mail/pop3/POP3FolderClosedExceptionTest.java
mail/src/test/java/com/sun/mail/pop3/POP3Handler.java
mail/src/test/java/com/sun/mail/pop3/POP3MessageTest.java
mail/src/test/java/com/sun/mail/pop3/POP3ReadableMimeTest.java
mail/src/test/java/com/sun/mail/pop3/POP3StoreTest.java
mail/src/test/java/com/sun/mail/smtp/NopServer.java
mail/src/test/java/com/sun/mail/smtp/SMTPAuthDebugTest.java
mail/src/test/java/com/sun/mail/smtp/SMTPCloseTest.java
mail/src/test/java/com/sun/mail/smtp/SMTPConnectFailureTest.java
mail/src/test/java/com/sun/mail/smtp/SMTPHandler.java
mail/src/test/java/com/sun/mail/smtp/SMTPIOExceptionTest.java
mail/src/test/java/com/sun/mail/smtp/SMTPSaslLoginTest.java
mail/src/test/java/com/sun/mail/smtp/SMTPWriteTimeoutTest.java


Added Paths:
------------
mail/src/test/java/com/sun/mail/imap/IMAPAlertTest.java


Diffs:
------
diff -r 54e359ea2c07 -r 1ee4cc28d3c7 doc/release/CHANGES.txt
--- a/doc/release/CHANGES.txt Fri Jan 24 16:33:06 2014 -0800
+++ b/doc/release/CHANGES.txt Tue Feb 04 12:30:50 2014 -0800
@@ -27,6 +27,7 @@
 K 6238 add OAuth2 support to JavaMail
 K 6260 IMAP failures during close can leave connection unusable
 K 6261 need way to monitor IMAP responses
+K 6274 MimeUtility.encodeText() does not work with Unicode surrogate pairs
 
 
                   CHANGES IN THE 1.5.1 RELEASE

diff -r 54e359ea2c07 -r 1ee4cc28d3c7 mail/src/main/java/javax/mail/internet/MimeUtility.java
--- a/mail/src/main/java/javax/mail/internet/MimeUtility.java Fri Jan 24 16:33:06 2014 -0800
+++ b/mail/src/main/java/javax/mail/internet/MimeUtility.java Tue Feb 04 12:30:50 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 1997-2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -783,9 +783,14 @@
         if ((len > avail) && ((size = string.length()) > 1)) {
             // If the length is greater than 'avail', split 'string'
             // into two and recurse.
- doEncode(string.substring(0, size/2), b64, jcharset,
- avail, prefix, first, encodingWord, buf);
- doEncode(string.substring(size/2, size), b64, jcharset,
+ // Have to make sure not to split a Unicode surrogate pair.
+ int split = size / 2;
+ if (Character.isHighSurrogate(string.charAt(split-1)))
+ split--;
+ if (split > 0)
+ doEncode(string.substring(0, split), b64, jcharset,
+ avail, prefix, first, encodingWord, buf);
+ doEncode(string.substring(split, size), b64, jcharset,
                      avail, prefix, false, encodingWord, buf);
         } else {
             // length <= than 'avail'. Encode the given string

diff -r 54e359ea2c07 -r 1ee4cc28d3c7 mail/src/test/java/javax/mail/internet/MimeUtilityTest.java
--- a/mail/src/test/java/javax/mail/internet/MimeUtilityTest.java Fri Jan 24 16:33:06 2014 -0800
+++ b/mail/src/test/java/javax/mail/internet/MimeUtilityTest.java Tue Feb 04 12:30:50 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 1997-2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -135,4 +135,39 @@
         assertTrue(encodings.contains(
                         MimeUtility.getEncoding(new DataHandler(bads))));
     }
+
+ /**
+ * Test that encoding a Unicode string with surrogate pairs
+ * doesn't split the encoding between the pairs.
+ */
+ @Test
+ public void testSurrogatePairs() throws Exception {
+ // test a specific case
+ String sp = "a" +
+ "\ud801\udc00\ud801\udc00\ud801\udc00\ud801\udc00" +
+ "\ud801\udc00\ud801\udc00\ud801\udc00\ud801\udc00" +
+ "\ud801\udc00\ud801\udc00\ud801\udc00\ud801\udc00" +
+ "\ud801\udc00\ud801\udc00\ud801\udc00\ud801\udc00";
+ String en = MimeUtility.encodeText(sp, "utf-8", "B");
+ String dt = MimeUtility.decodeText(en);
+ // encoding it and decoding it shouldn't change it
+ assertEquals(dt, sp);
+ String[] w = en.split(" ");
+ // the first word should end with the second half of a pair
+ String dw = MimeUtility.decodeWord(w[0]);
+ assertTrue(dw.charAt(dw.length()-1) == '\udc00');
+ // and the second word should start with the first half of a pair
+ dw = MimeUtility.decodeWord(w[1]);
+ assertTrue(dw.charAt(0) == '\ud801');
+
+ // test various string lengths
+ int ch = 0xFE000;
+ String test = "";
+ for (int i = 0; i < 50; i++) {
+ test += new String(Character.toChars(ch));
+ String encoded = MimeUtility.encodeText(test, "UTF-8", "B");
+ String decoded = MimeUtility.decodeText(encoded);
+ assertEquals(decoded, test);
+ }
+ }
 }


diff -r 1ee4cc28d3c7 -r 24525dad674d doc/release/CHANGES.txt
--- a/doc/release/CHANGES.txt Tue Feb 04 12:30:50 2014 -0800
+++ b/doc/release/CHANGES.txt Wed Feb 05 17:08:16 2014 -0800
@@ -28,6 +28,7 @@
 K 6260 IMAP failures during close can leave connection unusable
 K 6261 need way to monitor IMAP responses
 K 6274 MimeUtility.encodeText() does not work with Unicode surrogate pairs
+K 6283 IMAP alerts and notifications are not sent during authentication
 
 
                   CHANGES IN THE 1.5.1 RELEASE

diff -r 1ee4cc28d3c7 -r 24525dad674d mail/src/main/java/com/sun/mail/imap/IMAPStore.java
--- a/mail/src/main/java/com/sun/mail/imap/IMAPStore.java Tue Feb 04 12:30:50 2014 -0800
+++ b/mail/src/main/java/com/sun/mail/imap/IMAPStore.java Wed Feb 05 17:08:16 2014 -0800
@@ -685,8 +685,9 @@
                                 ", host=" + host +
                                 ", user=" + traceUser(user) +
                                 ", password=" + tracePassword(password));
+ protocol.addResponseHandler(nonStoreResponseHandler);
                 login(protocol, user, password);
-
+ protocol.removeResponseHandler(nonStoreResponseHandler);
                 protocol.addResponseHandler(this);
 
                 usingSSL = protocol.isSSL(); // in case anyone asks
@@ -923,8 +924,10 @@
                         refreshPassword();
                     // Use cached host, port and timeout values.
                     p = newIMAPProtocol(host, port);
+ p.addResponseHandler(nonStoreResponseHandler);
                     // Use cached auth info
                     login(p, user, password);
+ p.removeResponseHandler(nonStoreResponseHandler);
                 } catch(Exception ex1) {
                     if (p != null)
                         try {

diff -r 1ee4cc28d3c7 -r 24525dad674d mail/src/test/java/com/sun/mail/imap/IMAPAlertTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPAlertTest.java Wed Feb 05 17:08:16 2014 -0800
@@ -0,0 +1,121 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright (c) 2009-2010 Oracle and/or its affiliates. All rights reserved.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common Development
+ * and Distribution License("CDDL") (collectively, the "License"). You
+ * may not use this file except in compliance with the License. You can
+ * obtain a copy of the License at
+ * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
+ * or packager/legal/LICENSE.txt. See the License for the specific
+ * language governing permissions and limitations under the License.
+ *
+ * When distributing the software, include this License Header Notice in each
+ * file and include the License file at packager/legal/LICENSE.txt.
+ *
+ * GPL Classpath Exception:
+ * Oracle designates this particular file as subject to the "Classpath"
+ * exception as provided by Oracle in the GPL Version 2 section of the License
+ * file that accompanied this code.
+ *
+ * Modifications:
+ * If applicable, add the following below the License Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyright [year] [name of copyright owner]"
+ *
+ * Contributor(s):
+ * If you wish your version of this file to be governed by only the CDDL or
+ * only the GPL Version 2, indicate your decision by adding "[Contributor]
+ * elects to include this software in this distribution under the [CDDL or GPL
+ * Version 2] license." If you don't indicate a single choice of license, a
+ * recipient has the option to distribute your version of this file under
+ * either the CDDL, the GPL Version 2 or to extend the choice of license to
+ * its licensees as provided above. However, if you add GPL Version 2 code
+ * and therefore, elected the GPL Version 2 license, then the option applies
+ * only if the new code is made subject to such option by the copyright
+ * holder.
+ */
+
+package com.sun.mail.imap;
+
+import java.io.IOException;
+import java.util.Properties;
+
+import javax.mail.Session;
+import javax.mail.Store;
+import javax.mail.event.StoreListener;
+import javax.mail.event.StoreEvent;
+
+import org.junit.Test;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Test alerts.
+ */
+public final class IMAPAlertTest {
+
+ private volatile boolean gotAlert = false;
+
+ @Test
+ public void test() {
+ IMAPServer server = null;
+ try {
+ final IMAPHandler handler = new IMAPHandlerAlert();
+ server = new IMAPServer(handler, 26422);
+ server.start();
+ Thread.sleep(1000);
+
+ final Properties properties = new Properties();
+ properties.setProperty("mail.imap.host", "localhost");
+ properties.setProperty("mail.imap.port", "26422");
+ properties.setProperty("mail.debug.auth", "true");
+ final Session session = Session.getInstance(properties);
+ session.setDebug(true);
+
+ final Store store = session.getStore("imap");
+ store.addStoreListener(new StoreListener() {
+ public void notification(StoreEvent e) {
+ String s;
+ if (e.getMessageType() == StoreEvent.ALERT) {
+ s = "ALERT: ";
+ gotAlert = true;
+ } else
+ s = "NOTICE: ";
+ System.out.println(s + e.getMessage());
+ }
+ });
+ try {
+ store.connect("test", "test");
+ assertTrue(gotAlert);
+
+ } catch (Exception ex) {
+ System.out.println(ex);
+ //ex.printStackTrace();
+ fail(ex.toString());
+ } finally {
+ store.close();
+ }
+ } catch (final Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ } finally {
+ if (server != null) {
+ server.quit();
+ }
+ }
+ }
+
+ /**
+ * Custom handler. Returns an alert message at login.
+ */
+ private static final class IMAPHandlerAlert extends IMAPHandler {
+ @Override
+ public void login() throws IOException {
+ untagged("OK [ALERT] account is over quota");
+ super.login();
+ }
+ }
+}


diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/imap/IMAPAlertTest.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPAlertTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPAlertTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2010 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -48,6 +48,8 @@
 import javax.mail.event.StoreListener;
 import javax.mail.event.StoreEvent;
 
+import com.sun.mail.test.TestServer;
+
 import org.junit.Test;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -61,19 +63,19 @@
 
     @Test
     public void test() {
- IMAPServer server = null;
+ TestServer server = null;
         try {
             final IMAPHandler handler = new IMAPHandlerAlert();
- server = new IMAPServer(handler, 26422);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             final Properties properties = new Properties();
             properties.setProperty("mail.imap.host", "localhost");
- properties.setProperty("mail.imap.port", "26422");
- properties.setProperty("mail.debug.auth", "true");
+ properties.setProperty("mail.imap.port", "" + server.getPort());
+ //properties.setProperty("mail.debug.auth", "true");
             final Session session = Session.getInstance(properties);
- session.setDebug(true);
+ //session.setDebug(true);
 
             final Store store = session.getStore("imap");
             store.addStoreListener(new StoreListener() {
@@ -84,11 +86,12 @@
                         gotAlert = true;
                     } else
                         s = "NOTICE: ";
- System.out.println(s + e.getMessage());
+ //System.out.println(s + e.getMessage());
                 }
             });
             try {
                 store.connect("test", "test");
+ Thread.sleep(1000); // time for event to be delivered
                 assertTrue(gotAlert);
 
             } catch (Exception ex) {

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/imap/IMAPAuthDebugTest.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPAuthDebugTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPAuthDebugTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2011 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -46,6 +46,8 @@
 import javax.mail.Session;
 import javax.mail.Store;
 
+import com.sun.mail.test.TestServer;
+
 import org.junit.Test;
 import org.junit.Rule;
 import org.junit.rules.Timeout;
@@ -98,15 +100,15 @@
      * Scan the debug output looking for "expect", return true if found.
      */
     public boolean test(Properties properties, String expect) {
- IMAPServer server = null;
+ TestServer server = null;
         try {
             final IMAPHandler handler = new IMAPHandler();
- server = new IMAPServer(handler, 26422);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             properties.setProperty("mail.imap.host", "localhost");
- properties.setProperty("mail.imap.port", "26422");
+ properties.setProperty("mail.imap.port", "" + server.getPort());
             final Session session = Session.getInstance(properties);
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
             PrintStream ps = new PrintStream(bos);

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/imap/IMAPCloseFailureTest.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPCloseFailureTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPCloseFailureTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -47,6 +47,8 @@
 import javax.mail.Store;
 import javax.mail.Folder;
 
+import com.sun.mail.test.TestServer;
+
 import org.junit.Test;
 import static org.junit.Assert.fail;
 
@@ -56,7 +58,6 @@
 public final class IMAPCloseFailureTest {
 
     private static final String HOST = "localhost";
- private static final int PORT = 26422;
 
     static class NoIMAPHandler extends IMAPHandler {
         static boolean first = true;
@@ -93,15 +94,15 @@
     }
 
     public void testClose(IMAPHandler handler) {
- IMAPServer server = null;
+ TestServer server = null;
         try {
- server = new IMAPServer(handler, PORT);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             Properties properties = new Properties();
             properties.setProperty("mail.imap.host", HOST);
- properties.setProperty("mail.imap.port", "" + PORT);
+ properties.setProperty("mail.imap.port", "" + server.getPort());
             Session session = Session.getInstance(properties);
             //session.setDebug(true);
 

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/imap/IMAPConnectFailureTest.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPConnectFailureTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPConnectFailureTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -59,19 +59,19 @@
 public final class IMAPConnectFailureTest {
 
     private static final String HOST = "localhost";
- private static final int PORT = 26422;
     private static final int CTO = 20;
 
     @Test
     public void testNoServer() {
         try {
             // verify that port is not being used
- ServerSocket ss = new ServerSocket(PORT);
+ ServerSocket ss = new ServerSocket(0);
+ int port = ss.getLocalPort();
             ss.close();
 
             Properties properties = new Properties();
             properties.setProperty("mail.imap.host", HOST);
- properties.setProperty("mail.imap.port", "" + PORT);
+ properties.setProperty("mail.imap.port", "" + port);
             properties.setProperty("mail.imap.connectiontimeout", "" + CTO);
             Session session = Session.getInstance(properties);
             //session.setDebug(true);
@@ -84,7 +84,7 @@
             } catch (MailConnectException mcex) {
                 // success!
                 assertEquals(HOST, mcex.getHost());
- assertEquals(PORT, mcex.getPort());
+ assertEquals(port, mcex.getPort());
                 assertEquals(CTO, mcex.getConnectionTimeout());
             } catch (Exception ex) {
                 System.out.println(ex);

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/imap/IMAPHandler.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPHandler.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPHandler.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -40,40 +40,18 @@
 
 package com.sun.mail.imap;
 
-import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
-import java.net.Socket;
 import java.util.StringTokenizer;
-import java.util.logging.Logger;
 import java.util.logging.Level;
 
+import com.sun.mail.test.ProtocolHandler;
+
 /**
  * Handle IMAP connection.
  *
- * Inspired by, and derived from, POP3Handler by sbo.
- *
- * @author sbo
  * @author Bill Shannon
  */
-public class IMAPHandler implements Runnable, Cloneable {
-
- /** Logger for this class. */
- protected static final Logger LOGGER =
- Logger.getLogger(IMAPHandler.class.getName());
-
- /** Client socket. */
- private Socket clientSocket;
-
- /** Quit? */
- private boolean quit;
-
- /** Writer to socket. */
- private PrintWriter writer;
-
- /** Reader from socket. */
- private BufferedReader reader;
+public class IMAPHandler extends ProtocolHandler {
 
     /** Current line. */
     private String currentLine;
@@ -85,44 +63,6 @@
     protected String capabilities = "IMAP4REV1 IDLE";
 
     /**
- * Sets the client socket.
- *
- * @param clientSocket
- * client socket
- */
- public final void setClientSocket(final Socket clientSocket) {
- this.clientSocket = clientSocket;
- }
-
- /**
- * {_at_inheritDoc}
- */
- public final void run() {
- try {
- writer = new PrintWriter(clientSocket.getOutputStream());
- reader = new BufferedReader(
- new InputStreamReader(clientSocket.getInputStream()));
-
- sendGreetings();
-
- while (!quit) {
- handleCommand();
- }
-
- //clientSocket.close();
- } catch (final Exception e) {
- LOGGER.log(Level.SEVERE, "Error", e);
- } finally {
- try {
- if (clientSocket != null)
- clientSocket.close();
- } catch (final IOException ioe) {
- LOGGER.log(Level.SEVERE, "Error", ioe);
- }
- }
- }
-
- /**
      * Send greetings.
      *
      * @throws IOException unable to write to socket
@@ -376,30 +316,4 @@
         ok();
         exit();
     }
-
- /**
- * Quit.
- */
- public void exit() {
- quit = true;
- try {
- if (clientSocket != null && !clientSocket.isClosed()) {
- clientSocket.close();
- clientSocket = null;
- }
- } catch (final IOException e) {
- LOGGER.log(Level.SEVERE, "Error", e);
- }
- }
-
- /**
- * {_at_inheritDoc}
- */
- public Object clone() {
- try {
- return super.clone();
- } catch (final CloneNotSupportedException e) {
- throw new AssertionError(e);
- }
- }
 }

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/imap/IMAPIdleStateTest.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPIdleStateTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPIdleStateTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -47,6 +47,7 @@
 import javax.mail.Store;
 
 import com.sun.mail.imap.IMAPStore;
+import com.sun.mail.test.TestServer;
 
 import org.junit.Test;
 import org.junit.Rule;
@@ -64,16 +65,16 @@
 
     @Test
     public void test() {
- IMAPServer server = null;
+ TestServer server = null;
         try {
             final IMAPHandler handler = new IMAPHandlerIdleBye();
- server = new IMAPServer(handler, 26422);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             final Properties properties = new Properties();
             properties.setProperty("mail.imap.host", "localhost");
- properties.setProperty("mail.imap.port", "26422");
+ properties.setProperty("mail.imap.port", "" + server.getPort());
             final Session session = Session.getInstance(properties);
             //session.setDebug(true);
 

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/imap/IMAPIdleUntaggedResponseTest.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPIdleUntaggedResponseTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPIdleUntaggedResponseTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2010 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -48,6 +48,8 @@
 import javax.mail.Store;
 import javax.mail.Message;
 
+import com.sun.mail.test.TestServer;
+
 import org.junit.Test;
 import org.junit.Rule;
 import org.junit.rules.Timeout;
@@ -65,16 +67,16 @@
 
     @Test
     public void test() {
- IMAPServer server = null;
+ TestServer server = null;
         try {
             final IMAPHandler handler = new IMAPHandlerIdleExists();
- server = new IMAPServer(handler, 26422);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             final Properties properties = new Properties();
             properties.setProperty("mail.imap.host", "localhost");
- properties.setProperty("mail.imap.port", "26422");
+ properties.setProperty("mail.imap.port", "" + server.getPort());
             final Session session = Session.getInstance(properties);
             //session.setDebug(true);
 

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/imap/IMAPLoginFailureTest.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPLoginFailureTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPLoginFailureTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2012 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -48,6 +48,7 @@
 import javax.mail.MessagingException;
 
 import com.sun.mail.test.SavedSocketFactory;
+import com.sun.mail.test.TestServer;
 
 import org.junit.Test;
 import static org.junit.Assert.assertTrue;
@@ -65,7 +66,7 @@
      */
     @Test
     public void testSocketClosed() {
- IMAPServer server = null;
+ TestServer server = null;
         try {
             final IMAPHandler handler = new IMAPHandler() {
                 public void sendGreetings() throws IOException {
@@ -73,14 +74,14 @@
                     super.sendGreetings();
                 }
             };
- server = new IMAPServer(handler, 26422);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             SavedSocketFactory ssf = new SavedSocketFactory();
             Properties properties = new Properties();
             properties.setProperty("mail.imap.host", "localhost");
- properties.setProperty("mail.imap.port", "26422");
+ properties.setProperty("mail.imap.port", "" + server.getPort());
             properties.put("mail.imap.socketFactory", ssf);
             final Session session = Session.getInstance(properties);
             //session.setDebug(true);

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/imap/IMAPSaslLoginTest.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPSaslLoginTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPSaslLoginTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -47,6 +47,8 @@
 import javax.mail.Store;
 import javax.mail.MessagingException;
 
+import com.sun.mail.test.TestServer;
+
 import org.junit.Test;
 import static org.junit.Assert.fail;
 
@@ -60,16 +62,16 @@
      */
     @Test
     public void testSaslLogin() {
- IMAPServer server = null;
+ TestServer server = null;
         try {
             IMAPHandler handler = new IMAPSaslHandler();
- server = new IMAPServer(handler, 26422);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             Properties properties = new Properties();
             properties.setProperty("mail.imap.host", "localhost");
- properties.setProperty("mail.imap.port", "26422");
+ properties.setProperty("mail.imap.port", "" + server.getPort());
             properties.setProperty("mail.imap.sasl.enable", "true");
             properties.setProperty("mail.imap.sasl.mechanisms", "DIGEST-MD5");
             Session session = Session.getInstance(properties);

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/imap/IMAPServer.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPServer.java Wed Feb 05 17:08:16 2014 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,135 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright (c) 2009-2012 Oracle and/or its affiliates. All rights reserved.
- *
- * The contents of this file are subject to the terms of either the GNU
- * General Public License Version 2 only ("GPL") or the Common Development
- * and Distribution License("CDDL") (collectively, the "License"). You
- * may not use this file except in compliance with the License. You can
- * obtain a copy of the License at
- * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
- * or packager/legal/LICENSE.txt. See the License for the specific
- * language governing permissions and limitations under the License.
- *
- * When distributing the software, include this License Header Notice in each
- * file and include the License file at packager/legal/LICENSE.txt.
- *
- * GPL Classpath Exception:
- * Oracle designates this particular file as subject to the "Classpath"
- * exception as provided by Oracle in the GPL Version 2 section of the License
- * file that accompanied this code.
- *
- * Modifications:
- * If applicable, add the following below the License Header, with the fields
- * enclosed by brackets [] replaced by your own identifying information:
- * "Portions Copyright [year] [name of copyright owner]"
- *
- * Contributor(s):
- * If you wish your version of this file to be governed by only the CDDL or
- * only the GPL Version 2, indicate your decision by adding "[Contributor]
- * elects to include this software in this distribution under the [CDDL or GPL
- * Version 2] license." If you don't indicate a single choice of license, a
- * recipient has the option to distribute your version of this file under
- * either the CDDL, the GPL Version 2 or to extend the choice of license to
- * its licensees as provided above. However, if you add GPL Version 2 code
- * and therefore, elected the GPL Version 2 license, then the option applies
- * only if the new code is made subject to such option by the copyright
- * holder.
- */
-
-package com.sun.mail.imap;
-
-import java.io.IOException;
-import java.net.ServerSocket;
-import java.net.Socket;
-
-/**
- * IMAP Server.
- *
- * Inspired by, and derived from, POP3Server by sbo.
- *
- * @author sbo
- * @author Bill Shannon
- */
-public final class IMAPServer extends Thread {
-
- /** Server socket. */
- private ServerSocket serverSocket;
-
- /** Keep on? */
- private volatile boolean keepOn;
-
- /** Port to listen. */
- private final int port;
-
- /** IMAP handler. */
- private final IMAPHandler handler;
-
- /**
- * IMAP server.
- *
- * @param handler
- * handler
- * @param port
- * port to listen
- */
- public IMAPServer(final IMAPHandler handler, final int port) {
- this.handler = handler;
- this.port = port;
- }
-
- /**
- * Exit IMAP server.
- */
- public void quit() {
- try {
- keepOn = false;
- if (serverSocket != null && !serverSocket.isClosed()) {
- serverSocket.close();
- serverSocket = null;
- }
- } catch (final IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- /**
- * Open a server socket.
- *
- * @return server socket
- * @throws IOException unable to open socket
- */
- private ServerSocket openServerSocket() throws IOException {
- return new ServerSocket(port, 0, null);
- }
-
- /**
- * {_at_inheritDoc}
- */
- public void run() {
- try {
- keepOn = true;
-
- try {
- serverSocket = openServerSocket();
- } catch (final IOException e) {
- throw new RuntimeException(e);
- }
-
- while (keepOn) {
- try {
- final Socket clientSocket = serverSocket.accept();
- final IMAPHandler imapHandler =
- (IMAPHandler)handler.clone();
- imapHandler.setClientSocket(clientSocket);
- new Thread(imapHandler).start();
- } catch (final IOException e) {
- //e.printStackTrace();
- }
- }
- } finally {
- quit();
- }
- }
-}

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/pop3/POP3AuthDebugTest.java
--- a/mail/src/test/java/com/sun/mail/pop3/POP3AuthDebugTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/pop3/POP3AuthDebugTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2011 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -46,6 +46,8 @@
 import javax.mail.Session;
 import javax.mail.Store;
 
+import com.sun.mail.test.TestServer;
+
 import org.junit.Test;
 import org.junit.Rule;
 import org.junit.rules.Timeout;
@@ -98,15 +100,15 @@
      * Scan the debug output looking for "expect", return true if found.
      */
     public boolean test(Properties properties, String expect) {
- POP3Server server = null;
+ TestServer server = null;
         try {
             final POP3Handler handler = new POP3Handler();
- server = new POP3Server(handler, 26421);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             properties.setProperty("mail.pop3.host", "localhost");
- properties.setProperty("mail.pop3.port", "26421");
+ properties.setProperty("mail.pop3.port", "" + server.getPort());
             final Session session = Session.getInstance(properties);
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
             PrintStream ps = new PrintStream(bos);

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/pop3/POP3FolderClosedExceptionTest.java
--- a/mail/src/test/java/com/sun/mail/pop3/POP3FolderClosedExceptionTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/pop3/POP3FolderClosedExceptionTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2011 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -49,6 +49,8 @@
 import javax.mail.Message;
 import javax.mail.FolderClosedException;
 
+import com.sun.mail.test.TestServer;
+
 import org.junit.Test;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.fail;
@@ -68,16 +70,16 @@
      */
     @Test
     public void testFolderClosedExceptionBody() {
- POP3Server server = null;
+ TestServer server = null;
         try {
             final POP3Handler handler = new POP3HandlerTimeoutBody();
- server = new POP3Server(handler, 26421);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             final Properties properties = new Properties();
             properties.setProperty("mail.pop3.host", "localhost");
- properties.setProperty("mail.pop3.port", "26421");
+ properties.setProperty("mail.pop3.port", "" + server.getPort());
             final Session session = Session.getInstance(properties);
             //session.setDebug(true);
 
@@ -140,16 +142,16 @@
      */
     @Test
     public void testFolderClosedExceptionHeaders() {
- POP3Server server = null;
+ TestServer server = null;
         try {
             final POP3Handler handler = new POP3HandlerTimeoutHeader();
- server = new POP3Server(handler, 26421);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             final Properties properties = new Properties();
             properties.setProperty("mail.pop3.host", "localhost");
- properties.setProperty("mail.pop3.port", "26421");
+ properties.setProperty("mail.pop3.port", "" + server.getPort());
             final Session session = Session.getInstance(properties);
             //session.setDebug(true);
 

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/pop3/POP3Handler.java
--- a/mail/src/test/java/com/sun/mail/pop3/POP3Handler.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/pop3/POP3Handler.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2012 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -40,38 +40,20 @@
 
 package com.sun.mail.pop3;
 
-import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
-import java.net.Socket;
 import java.util.StringTokenizer;
 import java.util.logging.Logger;
 import java.util.logging.Level;
 
+import com.sun.mail.test.ProtocolHandler;
+
 /**
  * Handle connection.
- *
+ *
  * @author sbo
  */
-public class POP3Handler implements Runnable, Cloneable {
-
- /** Logger for this class. */
- private static final Logger LOGGER =
- Logger.getLogger(POP3Handler.class.getName());
-
- /** Client socket. */
- private Socket clientSocket;
-
- /** Quit? */
- private boolean quit;
-
- /** Writer to socket. */
- private PrintWriter writer;
-
- /** Reader from socket. */
- private BufferedReader reader;
-
+public class POP3Handler extends ProtocolHandler {
+
     /** Current line. */
     private String currentLine;
 
@@ -107,56 +89,18 @@
             "--xxx--\r\n";
 
     /**
- * Sets the client socket.
- *
- * @param clientSocket
- * client socket
- */
- public final void setClientSocket(final Socket clientSocket) {
- this.clientSocket = clientSocket;
- }
-
- /**
- * {_at_inheritDoc}
- */
- public final void run() {
- try {
- this.writer = new PrintWriter(this.clientSocket.getOutputStream());
- this.reader = new BufferedReader(
- new InputStreamReader(this.clientSocket.getInputStream()));
-
- this.sendGreetings();
-
- while (!this.quit) {
- this.handleCommand();
- }
-
- //this.clientSocket.close();
- } catch (final Exception e) {
- LOGGER.log(Level.SEVERE, "Error", e);
- } finally {
- try {
- if (this.clientSocket != null)
- this.clientSocket.close();
- } catch (final IOException ioe) {
- LOGGER.log(Level.SEVERE, "Error", ioe);
- }
- }
- }
-
- /**
      * Send greetings.
- *
+ *
      * @throws IOException
      * unable to write to socket
      */
     public void sendGreetings() throws IOException {
         this.println("+OK POP3 CUSTOM");
     }
-
+
     /**
      * Send String to socket.
- *
+ *
      * @param str
      * String to send
      * @throws IOException
@@ -167,22 +111,22 @@
         this.writer.print("\r\n");
         this.writer.flush();
     }
-
+
     /**
      * Handle command.
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
     public void handleCommand() throws IOException {
         this.currentLine = this.reader.readLine();
-
+
         if (this.currentLine == null) {
             LOGGER.severe("Current line is null!");
             this.exit();
             return;
         }
-
+
         final StringTokenizer st = new StringTokenizer(this.currentLine, " ");
         final String commandName = st.nextToken().toUpperCase();
         final String arg = st.hasMoreTokens() ? st.nextToken() : null;
@@ -191,7 +135,7 @@
             this.exit();
             return;
         }
-
+
         if (commandName.equals("STAT")) {
             this.stat();
         } else if (commandName.equals("LIST")) {
@@ -221,20 +165,20 @@
             this.println("-ERR unknown command");
         }
     }
-
+
     /**
      * STAT command.
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
     public void stat() throws IOException {
         this.println("+OK 2 " + (msg1.length() + msg2.length()));
     }
-
+
     /**
      * LIST command.
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
@@ -247,7 +191,7 @@
 
     /**
      * RETR command.
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
@@ -261,40 +205,40 @@
         this.writer.write(msg);
         this.println(".");
     }
-
+
     /**
      * DELE command.
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
     public void dele() throws IOException {
         this.println("-ERR DELE not supported");
     }
-
+
     /**
      * NOOP command.
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
     public void noop() throws IOException {
         this.println("+OK");
     }
-
+
     /**
      * RSET command.
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
     public void rset() throws IOException {
         this.println("+OK");
     }
-
+
     /**
      * QUIT command.
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
@@ -302,11 +246,11 @@
         this.println("+OK");
         this.exit();
     }
-
+
     /**
      * TOP command.
      * XXX - ignores number of lines argument
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
@@ -320,10 +264,10 @@
         this.writer.write(top);
         this.println(".");
     }
-
+
     /**
      * UIDL command.
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
@@ -333,50 +277,24 @@
         this.writer.println("2 2");
         this.println(".");
     }
-
+
     /**
      * USER command.
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
     public void user() throws IOException {
         this.println("+OK");
     }
-
+
     /**
      * PASS command.
- *
+ *
      * @throws IOException
      * unable to read/write to socket
      */
     public void pass() throws IOException {
         this.println("+OK");
     }
-
- /**
- * Quit.
- */
- public void exit() {
- this.quit = true;
- try {
- if (this.clientSocket != null && !this.clientSocket.isClosed()) {
- this.clientSocket.close();
- this.clientSocket = null;
- }
- } catch (final IOException e) {
- LOGGER.log(Level.SEVERE, "Error", e);
- }
- }
-
- /**
- * {_at_inheritDoc}
- */
- public Object clone() {
- try {
- return super.clone();
- } catch (final CloneNotSupportedException e) {
- throw new AssertionError(e);
- }
- }
 }

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/pop3/POP3MessageTest.java
--- a/mail/src/test/java/com/sun/mail/pop3/POP3MessageTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/pop3/POP3MessageTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2011 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -51,6 +51,8 @@
 import javax.mail.Part;
 import javax.mail.MessagingException;
 
+import com.sun.mail.test.TestServer;
+
 import org.junit.Test;
 import org.junit.BeforeClass;
 import org.junit.AfterClass;
@@ -64,20 +66,20 @@
  */
 public final class POP3MessageTest {
 
- private static POP3Server server = null;
+ private static TestServer server = null;
     private static Store store;
     private static Folder folder;
 
     private static void startServer(boolean cached) {
         try {
             final POP3Handler handler = new POP3Handler();
- server = new POP3Server(handler, 26421);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             final Properties properties = new Properties();
             properties.setProperty("mail.pop3.host", "localhost");
- properties.setProperty("mail.pop3.port", "26421");
+ properties.setProperty("mail.pop3.port", "" + server.getPort());
             if (cached)
                 properties.setProperty("mail.pop3.filecache.enable", "true");
             final Session session = Session.getInstance(properties);

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/pop3/POP3ReadableMimeTest.java
--- a/mail/src/test/java/com/sun/mail/pop3/POP3ReadableMimeTest.java Wed Feb 05 17:08:16 2014 -0800
+++ b/mail/src/test/java/com/sun/mail/pop3/POP3ReadableMimeTest.java Fri Feb 07 16:20:02 2014 -0800
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2012 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -53,6 +53,7 @@
 import javax.mail.MessagingException;
 
 import com.sun.mail.util.ReadableMime;
+import com.sun.mail.test.TestServer;
 
 import org.junit.Test;
 import org.junit.BeforeClass;
@@ -67,20 +68,20 @@
  */
 public final class POP3ReadableMimeTest {
 
- private static POP3Server server = null;
+ private static TestServer server = null;
     private static Store store;
     private static Folder folder;
 
     private static void startServer(boolean cached) {
         try {
             final POP3Handler handler = new POP3Handler();
- server = new POP3Server(handler, 26421);
+ server = new TestServer(handler);
             server.start();
             Thread.sleep(1000);
 
             final Properties properties = new Properties();
             properties.setProperty("mail.pop3.host", "localhost");
- properties.setProperty("mail.pop3.port", "26421");
+ properties.setProperty("mail.pop3.port", "" + server.getPort());
             if (cached)
                 properties.setProperty("mail.pop3.filecache.enable", "true");
             final Session session = Session.getInstance(properties);

diff -r 24525dad674d -r d3ecea237434 mail/src/test/java/com/sun/mail/pop3/POP3Server.java
--- a/mail/src/test/java/com/sun/mail/pop3/POP3Server.java Wed Feb 05 17:08:16 2014 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,133 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright (c) 2009-2012 Oracle and/or its affiliates. All rights reserved.
- *
- * The contents of this file are subject to the terms of either the GNU
- * General Public License Version 2 only ("GPL") or the Common Development
- * and Distribution License("CDDL") (collectively, the "License"). You
- * may not use this file except in compliance with the License. You can
- * obtain a copy of the License at
- * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
- * or packager/legal/LICENSE.txt. See the License for the specific
- * language governing permissions and limitations under the License.
- *
- * When distributing the software, include this License Header Notice in each
- * file and include the License file at packager/legal/LICENSE.txt.
- *
- * GPL Classpath Exception:
- * Oracle designates this particular file as subject to the "Classpath"
- * exception as provided by Oracle in the GPL Version 2 section of the License
- * file that accompanied this code.
- *
- * Modifications:
- * If applicable, add the following below the License Header, with the fields
- * enclosed by brackets [] replaced by your own identifying information:
- * "Portions Copyright [year] [name of copyright owner]"
- *
- * Contributor(s):
- * If you wish your version of this file to be governed by only the CDDL or
- * only the GPL Version 2, indicate your decision by adding "[Contributor]
- * elects to include this software in this distribution under the [CDDL or GPL
- * Version 2] license." If you don't indicate a single choice of license, a
- * recipient has the option to distribute your version of this file under
- * either the CDDL, the GPL Version 2 or to extend the choice of license to
- * its licensees as provided above. However, if you add GPL Version 2 code
- * and therefore, elected the GPL Version 2 license, then the option applies
- * only if the new code is made subject to such option by the copyright
- * holder.
- */
-
-package com.sun.mail.pop3;
-
-import java.io.IOException;
-import java.net.ServerSocket;
-import java.net.Socket;
-
-/**
- * POP3 Server.
- *
- * @author sbo
- */
-public final class POP3Server extends Thread {
-
- /** Server socket. */
- private ServerSocket serverSocket;
-
- /** Keep on? */
- private volatile boolean keepOn;
-
- /** Port to listen. */
- private final int port;
-
- /** POP3 handler. */
- private final POP3Handler handler;
-
- /**
- * POP3 server.
- *
- * @param handler
- * handler
- * @param port
- * port to listen
- */
- public POP3Server(final POP3Handler handler, final int port) {
- this.handler = handler;
- this.port = port;
- }
-
- /**
- * Exit POP3 server.
- */
- public void quit() {
- try {
- this.keepOn = false;
- if (this.serverSocket != null && !this.serverSocket.isClosed()) {
- this.serverSocket.close();
- this.serverSocket = null;
- }
- } catch (final IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- /**
- * Open a server socket.
- *
- * @return server socket
- * @throws IOException
- * unable to open socket
- */
- private ServerSocket openServerSocket() throws IOException {
- return new ServerSocket(this.port, 0, null);
- }
-
- /**
- * {_at_inheritDoc}
- */
- public void run() {
- try {
- this.keepOn = true;
-
- try {
- this.
[truncated due to length]