geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject [5/8] incubator-geode git commit: GEODE-1939: change test category to IntegrationTest
Date Tue, 27 Sep 2016 23:35:58 GMT
GEODE-1939: change test category to IntegrationTest

* remove class hierarchy because it repeats running of tests
* reformat, privatize vars and methods
* remove catching of unexpected exceptions
* remove system out statements
* move all setup to setup method
* fix minor concurrency bug in JSSESocketJUnitTest


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/ddf4f3db
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/ddf4f3db
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/ddf4f3db

Branch: refs/heads/feature/GEODE-1801
Commit: ddf4f3dbc6cbd1ca68ba812128c25163aa5c23ac
Parents: 9f422dd
Author: Kirk Lund <klund@apache.org>
Authored: Tue Sep 27 13:50:21 2016 -0700
Committer: Kirk Lund <klund@apache.org>
Committed: Tue Sep 27 13:54:40 2016 -0700

----------------------------------------------------------------------
 .../geode/internal/net/JSSESocketJUnitTest.java | 111 ++++++-------------
 .../net/SocketCreatorFactoryJUnitTest.java      |  77 +++++++------
 2 files changed, 76 insertions(+), 112 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/ddf4f3db/geode-core/src/test/java/org/apache/geode/internal/net/JSSESocketJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/internal/net/JSSESocketJUnitTest.java
b/geode-core/src/test/java/org/apache/geode/internal/net/JSSESocketJUnitTest.java
index 4a62ec7..e63a46f 100755
--- a/geode-core/src/test/java/org/apache/geode/internal/net/JSSESocketJUnitTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/net/JSSESocketJUnitTest.java
@@ -30,6 +30,8 @@ import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.util.Properties;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.LogManager;
@@ -43,9 +45,12 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
+import org.junit.contrib.java.lang.system.SystemErrRule;
+import org.junit.contrib.java.lang.system.SystemOutRule;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.TestName;
 
+import org.apache.geode.distributed.ClientSocketFactory;
 import org.apache.geode.distributed.internal.DistributionConfig;
 import org.apache.geode.distributed.internal.DistributionConfigImpl;
 import org.apache.geode.internal.AvailablePort;
@@ -62,48 +67,39 @@ import org.apache.geode.util.test.TestUtil;
 @Category(IntegrationTest.class)
 public class JSSESocketJUnitTest {
 
-  public
-  @Rule
-  TestName name = new TestName();
-
-  private static final org.apache.logging.log4j.Logger logger = LogService.getLogger();
+  private static volatile boolean factoryInvoked;
 
-  ServerSocket acceptor;
-  Socket server;
+  private ServerSocket acceptor;
+  private Socket server;
+  private int randport;
 
-  static ByteArrayOutputStream baos = new ByteArrayOutputStream();
+  @Rule
+  public TestName name = new TestName();
 
-  private int randport = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
+  @Rule
+  public SystemOutRule systemOutRule = new SystemOutRule();
 
   @Before
   public void setUp() throws Exception {
-    System.out.println("\n\n########## setup " + name.getMethodName() + " ############\n\n");
-    server = null;
-    acceptor = null;
-    baos.reset();
+    randport = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
   }
 
   @After
   public void tearDown() throws Exception {
-    System.out.println("\n\n########## teardown " + name.getMethodName() + " ############\n\n");
-
     if (server != null) {
       server.close();
     }
     if (acceptor != null) {
       acceptor.close();
     }
-    System.out.println(baos.toString());
     SocketCreatorFactory.close();
   }
 
-  //----- test methods ------
-
   @Test
   public void testSSLSocket() throws Exception {
-    final Object[] receiver = new Object[1];
+    systemOutRule.mute().enableLog();
 
-    TestAppender.create();
+    final Object[] receiver = new Object[1];
 
     // Get original base log level
     Level originalBaseLevel = LogService.getBaseLogLevel();
@@ -136,7 +132,7 @@ public class JSSESocketJUnitTest {
       Socket client = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.CLUSTER).connectForServer(InetAddress.getByName("localhost"),
randport);
 
       ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
-      String expected = new String("testing " + name.getMethodName());
+      String expected = "testing " + name.getMethodName();
       oos.writeObject(expected);
       oos.flush();
 
@@ -144,27 +140,19 @@ public class JSSESocketJUnitTest {
 
       client.close();
       serverSocket.close();
-      if (expected.equals(receiver[0])) {
-        System.out.println("received " + receiver[0] + " as expected.");
-      } else {
-        throw new Exception("Expected \"" + expected + "\" but received \"" + receiver[0]
+ "\"");
-      }
+      assertEquals("Expected \"" + expected + "\" but received \"" + receiver[0] + "\"",
expected, receiver[0]);
 
-      String logOutput = baos.toString();
-      StringReader sreader = new StringReader(logOutput);
-      LineNumberReader reader = new LineNumberReader(sreader);
-      int peerLogCount = 0;
-      String line = null;
-      while ((line = reader.readLine()) != null) {
+      String stdOut = systemOutRule.getLog();
+      int foundExpectedString = 0;
 
-        if (line.matches(".*peer CN=.*")) {
-          System.out.println("Found peer log statement.");
-          peerLogCount++;
-        }
-      }
-      if (peerLogCount != 2) {
-        throw new Exception("Expected to find to peer identities logged.");
+      Pattern pattern = Pattern.compile(".*peer CN=.*");
+      Matcher matcher = pattern.matcher(stdOut);
+      while (matcher.find()) {
+        foundExpectedString++;
       }
+
+      assertEquals(2, foundExpectedString);
+
     } finally {
       // Reset original base log level
       LogService.setBaseLogLevel(originalBaseLevel);
@@ -198,22 +186,7 @@ public class JSSESocketJUnitTest {
     }
   }
 
-  static boolean factoryInvoked;
-
-  public static class TSocketFactory implements org.apache.geode.distributed.ClientSocketFactory
{
-
-    public TSocketFactory() {
-    }
-
-    public Socket createSocket(InetAddress address, int port) throws IOException {
-      JSSESocketJUnitTest.factoryInvoked = true;
-      throw new IOException("splort!");
-    }
-  }
-
-  //------------- utilities -----
-
-  protected File findTestJKS() {
+  private File findTestJKS() {
     return new File(TestUtil.getResourcePath(getClass(), "/ssl/trusted.keystore"));
   }
 
@@ -237,32 +210,14 @@ public class JSSESocketJUnitTest {
     return t;
   }
 
-  public static final class TestAppender extends AbstractAppender {
-
-    private static final String APPENDER_NAME = TestAppender.class.getName();
-    private final static String SOCKET_CREATOR_CLASSNAME = SocketCreator.class.getName();
+  private static class TSocketFactory implements ClientSocketFactory {
 
-    private TestAppender() {
-      super(APPENDER_NAME, null, PatternLayout.createDefaultLayout());
-      start();
-    }
-
-    public static Appender create() {
-      Appender appender = new TestAppender();
-      Logger socketCreatorLogger = (Logger) LogManager.getLogger(SOCKET_CREATOR_CLASSNAME);
-      LoggerConfig config = socketCreatorLogger.getContext().getConfiguration().getLoggerConfig(SOCKET_CREATOR_CLASSNAME);
-      config.addAppender(appender, Level.DEBUG, null);
-      return appender;
+    public TSocketFactory() {
     }
 
-    @SuppressWarnings("synthetic-access")
-    @Override
-    public void append(final LogEvent event) {
-      try {
-        baos.write(new String(event.getMessage().getFormattedMessage() + "\n").getBytes());
-      } catch (IOException ioex) {
-        logger.warn(ioex);
-      }
+    public Socket createSocket(InetAddress address, int port) throws IOException {
+      JSSESocketJUnitTest.factoryInvoked = true;
+      throw new IOException("splort!");
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/ddf4f3db/geode-core/src/test/java/org/apache/geode/internal/net/SocketCreatorFactoryJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/internal/net/SocketCreatorFactoryJUnitTest.java
b/geode-core/src/test/java/org/apache/geode/internal/net/SocketCreatorFactoryJUnitTest.java
index 16e2837..b9faff0 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/net/SocketCreatorFactoryJUnitTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/net/SocketCreatorFactoryJUnitTest.java
@@ -22,20 +22,27 @@ import java.io.File;
 import java.io.IOException;
 import java.util.Properties;
 
+import org.junit.After;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
 import org.apache.geode.distributed.internal.DistributionConfigImpl;
 import org.apache.geode.internal.security.SecurableCommunicationChannel;
 import org.apache.geode.test.dunit.Assert;
-import org.apache.geode.test.junit.categories.UnitTest;
+import org.apache.geode.test.junit.categories.IntegrationTest;
 import org.apache.geode.util.test.TestUtil;
 
-@Category(UnitTest.class)
-public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest {
+@Category(IntegrationTest.class)
+public class SocketCreatorFactoryJUnitTest {
+
+  @After
+  public void tearDown() throws Exception {
+    SocketCreatorFactory.close();
+  }
 
   @Test
-  public void testNewSSLConfigSSLComponentLocator() {
+  public void testNewSSLConfigSSLComponentLocator() throws Exception {
     Properties properties = configureSSLProperties(SecurableCommunicationChannel.LOCATOR.getConstant());
 
     DistributionConfigImpl distributionConfig = new DistributionConfigImpl(properties);
@@ -50,7 +57,7 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest {
   }
 
   @Test
-  public void testNewSSLConfigSSLComponentALL() {
+  public void testNewSSLConfigSSLComponentALL() throws Exception {
     Properties properties = configureSSLProperties(SecurableCommunicationChannel.ALL.getConstant());
 
     DistributionConfigImpl distributionConfig = new DistributionConfigImpl(properties);
@@ -65,7 +72,7 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest {
   }
 
   @Test
-  public void testNewSSLConfigSSLComponentCLUSTER() {
+  public void testNewSSLConfigSSLComponentCLUSTER() throws Exception {
     Properties properties = configureSSLProperties(SecurableCommunicationChannel.CLUSTER.getConstant());
 
     DistributionConfigImpl distributionConfig = new DistributionConfigImpl(properties);
@@ -80,7 +87,7 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest {
   }
 
   @Test
-  public void testNewSSLConfigSSLComponentGATEWAY() {
+  public void testNewSSLConfigSSLComponentGATEWAY() throws Exception {
     Properties properties = configureSSLProperties(SecurableCommunicationChannel.GATEWAY.getConstant());
 
     DistributionConfigImpl distributionConfig = new DistributionConfigImpl(properties);
@@ -95,7 +102,7 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest
{
   }
 
   @Test
-  public void testNewSSLConfigSSLComponentHTTP_SERVICE() {
+  public void testNewSSLConfigSSLComponentHTTP_SERVICE() throws Exception {
     Properties properties = configureSSLProperties(SecurableCommunicationChannel.WEB.getConstant());
 
     DistributionConfigImpl distributionConfig = new DistributionConfigImpl(properties);
@@ -110,7 +117,7 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest
{
   }
 
   @Test
-  public void testNewSSLConfigSSLComponentJMX() {
+  public void testNewSSLConfigSSLComponentJMX() throws Exception {
     Properties properties = configureSSLProperties(SecurableCommunicationChannel.JMX.getConstant());
 
     DistributionConfigImpl distributionConfig = new DistributionConfigImpl(properties);
@@ -125,7 +132,7 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest
{
   }
 
   @Test
-  public void testNewSSLConfigSSLComponentSERVER() {
+  public void testNewSSLConfigSSLComponentSERVER() throws Exception {
     Properties properties = configureSSLProperties(SecurableCommunicationChannel.SERVER.getConstant());
 
     DistributionConfigImpl distributionConfig = new DistributionConfigImpl(properties);
@@ -141,7 +148,7 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest
{
   }
 
   @Test
-  public void testNewSSLConfigSSLComponentCombinations1() {
+  public void testNewSSLConfigSSLComponentCombinations1() throws Exception {
     Properties properties = configureSSLProperties(commaDelimitedString(SecurableCommunicationChannel.CLUSTER.getConstant(),
SecurableCommunicationChannel.SERVER.getConstant()));
 
     DistributionConfigImpl distributionConfig = new DistributionConfigImpl(properties);
@@ -156,9 +163,8 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest
{
   }
 
   @Test
-  public void testNewSSLConfigSSLComponentCombinations2() {
-    Properties properties = configureSSLProperties(commaDelimitedString(SecurableCommunicationChannel.CLUSTER.getConstant(),
SecurableCommunicationChannel.SERVER.getConstant(), SecurableCommunicationChannel.WEB
-      .getConstant(), SecurableCommunicationChannel.JMX.getConstant()));
+  public void testNewSSLConfigSSLComponentCombinations2() throws Exception {
+    Properties properties = configureSSLProperties(commaDelimitedString(SecurableCommunicationChannel.CLUSTER.getConstant(),
SecurableCommunicationChannel.SERVER.getConstant(), SecurableCommunicationChannel.WEB.getConstant(),
SecurableCommunicationChannel.JMX.getConstant()));
 
     DistributionConfigImpl distributionConfig = new DistributionConfigImpl(properties);
     SocketCreatorFactory.setDistributionConfig(distributionConfig);
@@ -172,7 +178,7 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest
{
   }
 
   @Test
-  public void testNewSSLConfigSSLComponentAliasWithMultiKeyStore() {
+  public void testNewSSLConfigSSLComponentAliasWithMultiKeyStore() throws Exception {
     Properties properties = configureSSLProperties(SecurableCommunicationChannel.ALL.getConstant());
 
     properties.setProperty(SSL_KEYSTORE, TestUtil.getResourcePath(getClass(), "/org/apache/geode/internal/net/multiKey.jks"));
@@ -193,7 +199,7 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest
{
   }
 
   @Test
-  public void testNewSSLConfigSSLComponentWithoutAliasWithMultiKeyStore() {
+  public void testNewSSLConfigSSLComponentWithoutAliasWithMultiKeyStore() throws Exception
{
     Properties properties = configureSSLProperties(SecurableCommunicationChannel.ALL.getConstant());
 
     properties.setProperty(SSL_KEYSTORE, TestUtil.getResourcePath(getClass(), "/org/apache/geode/internal/net/multiKey.jks"));
@@ -210,28 +216,24 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest
{
     Assert.assertTrue(SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.LOCATOR).useSSL());
   }
 
-  private Properties configureSSLProperties(String sslComponents) {
+  private Properties configureSSLProperties(String sslComponents) throws IOException {
+    File jks = findTestJKS();
+
     Properties properties = new Properties();
-    try {
-      File jks = findTestJKS();
-
-      properties.setProperty(MCAST_PORT, "0");
-      properties.setProperty(SSL_REQUIRE_AUTHENTICATION, "true");
-      properties.setProperty(SSL_CIPHERS, "TLS_RSA_WITH_AES_256_CBC_SHA256,TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,TLS_DHE_DSS_WITH_AES_256_CBC_SHA256,TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA");
-      properties.setProperty(SSL_PROTOCOLS, "TLSv1,TLSv1.1,TLSv1.2");
-      properties.setProperty(SSL_KEYSTORE, jks.getCanonicalPath());
-      properties.setProperty(SSL_KEYSTORE_PASSWORD, "password");
-      properties.setProperty(SSL_KEYSTORE_TYPE, "JKS");
-      properties.setProperty(SSL_TRUSTSTORE, jks.getCanonicalPath());
-      properties.setProperty(SSL_TRUSTSTORE_PASSWORD, "password");
-      properties.setProperty(SSL_ENABLED_COMPONENTS, sslComponents);
-    } catch (IOException e) {
-      Assert.fail("Failed to configure the cluster");
-    }
+    properties.setProperty(MCAST_PORT, "0");
+    properties.setProperty(SSL_REQUIRE_AUTHENTICATION, "true");
+    properties.setProperty(SSL_CIPHERS, "TLS_RSA_WITH_AES_256_CBC_SHA256,TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,TLS_DHE_DSS_WITH_AES_256_CBC_SHA256,TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA");
+    properties.setProperty(SSL_PROTOCOLS, "TLSv1,TLSv1.1,TLSv1.2");
+    properties.setProperty(SSL_KEYSTORE, jks.getCanonicalPath());
+    properties.setProperty(SSL_KEYSTORE_PASSWORD, "password");
+    properties.setProperty(SSL_KEYSTORE_TYPE, "JKS");
+    properties.setProperty(SSL_TRUSTSTORE, jks.getCanonicalPath());
+    properties.setProperty(SSL_TRUSTSTORE_PASSWORD, "password");
+    properties.setProperty(SSL_ENABLED_COMPONENTS, sslComponents);
+
     return properties;
   }
 
-
   private String commaDelimitedString(final String... sslComponents) {
     StringBuilder stringBuilder = new StringBuilder();
     for (String sslComponent : sslComponents) {
@@ -241,20 +243,27 @@ public class SocketCreatorFactoryJUnitTest extends JSSESocketJUnitTest
{
     return stringBuilder.substring(0, stringBuilder.length() - 1);
   }
 
+  @Ignore("Test is not implemented")
   @Test
   public void testLegacyServerSSLConfig() {
   }
 
+  @Ignore("Test is not implemented")
   @Test
   public void testLegacyJMXSSLConfig() {
   }
 
+  @Ignore("Test is not implemented")
   @Test
   public void testLegacyGatewaySSLConfig() {
   }
 
+  @Ignore("Test is not implemented")
   @Test
   public void testLegacyHttpServiceSSLConfig() {
   }
 
+  private File findTestJKS() {
+    return new File(TestUtil.getResourcePath(getClass(), "/ssl/trusted.keystore"));
+  }
 }


Mime
View raw message