You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@axis.apache.org by ve...@apache.org on 2017/01/28 23:00:53 UTC

svn commit: r1780750 - in /axis/axis2/java/rampart/branches/RAMPART-433/modules/rampart-integration/src/test/java/org/apache/rampart: RampartKerberosTest.java util/KerberosServer.java

Author: veithen
Date: Sat Jan 28 23:00:53 2017
New Revision: 1780750

URL: http://svn.apache.org/viewvc?rev=1780750&view=rev
Log:
Convert KerberosServer into a JUnit rule.

Modified:
    axis/axis2/java/rampart/branches/RAMPART-433/modules/rampart-integration/src/test/java/org/apache/rampart/RampartKerberosTest.java
    axis/axis2/java/rampart/branches/RAMPART-433/modules/rampart-integration/src/test/java/org/apache/rampart/util/KerberosServer.java

Modified: axis/axis2/java/rampart/branches/RAMPART-433/modules/rampart-integration/src/test/java/org/apache/rampart/RampartKerberosTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/branches/RAMPART-433/modules/rampart-integration/src/test/java/org/apache/rampart/RampartKerberosTest.java?rev=1780750&r1=1780749&r2=1780750&view=diff
==============================================================================
--- axis/axis2/java/rampart/branches/RAMPART-433/modules/rampart-integration/src/test/java/org/apache/rampart/RampartKerberosTest.java (original)
+++ axis/axis2/java/rampart/branches/RAMPART-433/modules/rampart-integration/src/test/java/org/apache/rampart/RampartKerberosTest.java Sat Jan 28 23:00:53 2017
@@ -85,6 +85,9 @@ public class RampartKerberosTest {
         }
     };
     
+    @ClassRule
+    public static final KerberosServer kerberosServer = new KerberosServer();
+    
     /**
      * Stores any original JAAS configuration set via {@link #JAAS_CONF_SYS_PROP} property to restore it after test execution.
      */
@@ -189,12 +192,10 @@ public class RampartKerberosTest {
     }
     
     @Before
-    public void setUpKerberos() throws Exception {
+    public void setUp() throws Exception {
         System.setProperty("sun.security.krb5.debug", "true");
         System.setProperty("sun.security.jgss.debug", "true");
         
-        KerberosServer.startKerberosServer();
-                        
         //configure JGSS
         krb5Conf = System.getProperty(KRB5_CONF_SYS_PROP);
         
@@ -209,9 +210,7 @@ public class RampartKerberosTest {
     }
 
     @After
-    public void tearDownKerberos() throws Exception {
-        KerberosServer.stopKerberosServer();
-        
+    public void tearDown() throws Exception {
         if (jaasConf != null) {
             System.setProperty(JAAS_CONF_SYS_PROP, jaasConf);
         }
@@ -262,7 +261,7 @@ public class RampartKerberosTest {
                     KERBEROS_CONF_KDC_PORT_TOKEN, krb5ConfTemplate.getAbsolutePath()));
         }
             
-        krb5ConfContent = krb5ConfContent.replace(KERBEROS_CONF_KDC_PORT_TOKEN, String.valueOf(KerberosServer.getPort()));
+        krb5ConfContent = krb5ConfContent.replace(KERBEROS_CONF_KDC_PORT_TOKEN, String.valueOf(kerberosServer.getPort()));
         
         File krb5Conf = new File(tmpDir, this.getClass().getSimpleName() + "_krb5.conf");
         FileOutputStream krb5ConfOut = null;

Modified: axis/axis2/java/rampart/branches/RAMPART-433/modules/rampart-integration/src/test/java/org/apache/rampart/util/KerberosServer.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/branches/RAMPART-433/modules/rampart-integration/src/test/java/org/apache/rampart/util/KerberosServer.java?rev=1780750&r1=1780749&r2=1780750&view=diff
==============================================================================
--- axis/axis2/java/rampart/branches/RAMPART-433/modules/rampart-integration/src/test/java/org/apache/rampart/util/KerberosServer.java (original)
+++ axis/axis2/java/rampart/branches/RAMPART-433/modules/rampart-integration/src/test/java/org/apache/rampart/util/KerberosServer.java Sat Jan 28 23:00:53 2017
@@ -46,37 +46,32 @@ import org.apache.directory.server.kerbe
 import org.apache.directory.server.protocol.shared.transport.Transport;
 import org.apache.directory.server.protocol.shared.transport.UdpTransport;
 import org.apache.directory.shared.kerberos.codec.types.EncryptionType;
+import org.junit.rules.ExternalResource;
 
 /**
  * Runs an Apache DS Kerberos server.
- * @see org.apache.wss4j.integration.test.common.KerberosServiceStarter
  */
-public class KerberosServer {
+public final class KerberosServer extends ExternalResource {
 
     private static final Log log = LogFactory.getLog(KerberosServer.class);
     
     /**
      * The used DirectoryService instance
      */
-    private static DirectoryService directoryService;
+    private DirectoryService directoryService;
 
     /**
      * The used KdcServer instance
      */
-    private static KdcServer kdcServer;
+    private KdcServer kdcServer;
 
-    private static Provider provider = null;
-    private static int providerPos = 2;
+    private Provider provider;
+    private int providerPos = 2;
     
-    private static File workDir = null;
+    private File workDir;
     
-    /**
-     * Starts an Apache DS Kerberos server with dynamically allocated port.
-     * 
-     * @return
-     * @throws Exception
-     */
-    public static synchronized void startKerberosServer() throws Exception {
+    @Override
+    protected void before() throws Throwable {
         int kdcPort = PortAllocator.allocatePort();
         
         DatagramSocket datagramSocket = new DatagramSocket(kdcPort);
@@ -155,7 +150,7 @@ public class KerberosServer {
      * @return The Apache DS Kerberos server port.
      * @throws IllegalArgumentException If server or respective transport are not initialized
      */
-    public static synchronized int getPort() throws IllegalArgumentException {
+    public int getPort() throws IllegalArgumentException {
         if (kdcServer == null) {
             throw new IllegalStateException("Kerberos server is not initialized");
         }
@@ -176,18 +171,16 @@ public class KerberosServer {
                         UdpTransport.class.getName()));
     } 
     
-    /**
-     * Stops the Apache DS Kerberos server.
-     * @throws Exception
-     */
-    public static synchronized void stopKerberosServer() throws Exception {
+    @Override
+    protected void after() {
         log.info("Stop called");
         try {        
             if (directoryService != null) {
                 try {
                     directoryService.shutdown();
-                }
-                finally {                    
+                } catch (Exception ex) {
+                    log.error("Failed to stop server", ex);
+                } finally {
                     try {
                         FileUtils.deleteDirectory(workDir);
                     }