Testing: Changing ApacheDsRule to not use the same workdir for LDAP server

Original commit: elastic/x-pack-elasticsearch@532d02b014
This commit is contained in:
Alexander Reelsen 2014-09-11 16:30:26 +02:00
parent 1588c761ea
commit 176517ba7e
4 changed files with 22 additions and 44 deletions

View File

@ -30,6 +30,7 @@ import org.apache.directory.server.i18n.I18n;
import org.apache.directory.server.ldap.LdapServer; import org.apache.directory.server.ldap.LdapServer;
import org.apache.directory.server.protocol.shared.store.LdifFileLoader; import org.apache.directory.server.protocol.shared.store.LdifFileLoader;
import org.apache.directory.server.protocol.shared.transport.TcpTransport; import org.apache.directory.server.protocol.shared.transport.TcpTransport;
import org.junit.rules.TemporaryFolder;
import java.io.File; import java.io.File;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -40,17 +41,9 @@ import java.util.List;
import java.util.Set; import java.util.Set;
/** /**
* Helper Class to start up an Apache DS LDAP server for testing. Here is a typical use example in tests: * Helper Class to start up an Apache DS LDAP server for testing.
* <pre>
* static ApacheDsEmbedded ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif");
* *
* @BeforeClass public static void startServer() throws Exception { * Use ApacheDsRule instead of this class in tests
* ldap.startServer();
* }
* @AfterClass public static void stopServer() throws Exception {
* ldap.stopAndCleanup();
* }
* </pre>
*/ */
public class ApacheDsEmbedded { public class ApacheDsEmbedded {
/** /**
@ -78,8 +71,8 @@ public class ApacheDsEmbedded {
* *
* @throws Exception If something went wrong * @throws Exception If something went wrong
*/ */
public ApacheDsEmbedded(String baseDN, String ldifFileName, String testName) { public ApacheDsEmbedded(String baseDN, String ldifFileName, File workDir) {
this.workDir = new File(System.getProperty("java.io.tmpdir") + "/server-work/" + testName); this.workDir = workDir;
this.baseDN = baseDN; this.baseDN = baseDN;
this.ldifFileName = ldifFileName; this.ldifFileName = ldifFileName;
this.port = 10389 + CHILD_JVM_ID; this.port = 10389 + CHILD_JVM_ID;
@ -118,7 +111,6 @@ public class ApacheDsEmbedded {
public void stopAndCleanup() throws Exception { public void stopAndCleanup() throws Exception {
if (server != null) server.stop(); if (server != null) server.stop();
if (service != null) service.shutdown(); if (service != null) service.shutdown();
workDir.delete();
} }
@ -262,8 +254,6 @@ public class ApacheDsEmbedded {
entryApache.add("dc", "Apache"); entryApache.add("dc", "Apache");
service.getAdminSession().add(entryApache); service.getAdminSession().add(entryApache);
} }
// We are all done !
} }
private void loadSchema(String ldifFileName) throws URISyntaxException { private void loadSchema(String ldifFileName) throws URISyntaxException {
@ -274,30 +264,4 @@ public class ApacheDsEmbedded {
LdifFileLoader ldifLoader = new LdifFileLoader(service.getAdminSession(), ldifFile, Collections.EMPTY_LIST); LdifFileLoader ldifLoader = new LdifFileLoader(service.getAdminSession(), ldifFile, Collections.EMPTY_LIST);
ldifLoader.execute(); ldifLoader.execute();
} }
/**
* Main class.
*
* @param args Not used.
*/
public static void main(String[] args) {
try {
String baseDir = "o=sevenSeas";
String ldifImport = "seven-seas.ldif";
// Create the server
ApacheDsEmbedded ads = new ApacheDsEmbedded(baseDir, ldifImport, "test");
ads.startServer();
// Read an entry
Entry result = ads.service.getAdminSession().lookup(new Dn(baseDir));
// And print it if available
System.out.println("Found entry : " + result);
// optionally we can start a server too
ads.stopAndCleanup();
} catch (Exception e) {
// Ok, we have something wrong going on ...
e.printStackTrace();
}
}
} }

View File

@ -6,6 +6,7 @@
package org.elasticsearch.shield.authc.ldap; package org.elasticsearch.shield.authc.ldap;
import org.junit.rules.MethodRule; import org.junit.rules.MethodRule;
import org.junit.rules.TemporaryFolder;
import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement; import org.junit.runners.model.Statement;
@ -15,6 +16,11 @@ import org.junit.runners.model.Statement;
public class ApacheDsRule implements MethodRule { public class ApacheDsRule implements MethodRule {
private ApacheDsEmbedded ldap; private ApacheDsEmbedded ldap;
private final TemporaryFolder temporaryFolder;
public ApacheDsRule(TemporaryFolder temporaryFolder) {
this.temporaryFolder = temporaryFolder;
}
@Override @Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) { public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
@ -23,7 +29,7 @@ public class ApacheDsRule implements MethodRule {
@Override @Override
public void evaluate() throws Throwable { public void evaluate() throws Throwable {
try { try {
ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", target.getClass().getName()); ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", temporaryFolder.newFolder());
ldap.startServer(); ldap.startServer();
base.evaluate(); base.evaluate();
} finally { } finally {

View File

@ -10,6 +10,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ElasticsearchTestCase; import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -20,7 +21,10 @@ public class LdapConnectionTests extends ElasticsearchTestCase {
public static String SETTINGS_PREFIX = LdapRealm.class.getPackage().getName().substring("com.elasticsearch.".length()) + '.'; public static String SETTINGS_PREFIX = LdapRealm.class.getPackage().getName().substring("com.elasticsearch.".length()) + '.';
@Rule @Rule
public static ApacheDsRule apacheDsRule = new ApacheDsRule(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public static ApacheDsRule apacheDsRule = new ApacheDsRule(temporaryFolder);
@Test @Test
public void testBindWithTemplates() { public void testBindWithTemplates() {

View File

@ -18,6 +18,7 @@ import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
@ -41,7 +42,10 @@ public class LdapRealmTest extends ElasticsearchTestCase {
} }
@Rule @Rule
public static ApacheDsRule apacheDsRule = new ApacheDsRule(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public static ApacheDsRule apacheDsRule = new ApacheDsRule(temporaryFolder);
@Test @Test
public void testRestHeaderRegistration() { public void testRestHeaderRegistration() {