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.protocol.shared.store.LdifFileLoader;
import org.apache.directory.server.protocol.shared.transport.TcpTransport;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.net.URISyntaxException;
@ -40,17 +41,9 @@ import java.util.List;
import java.util.Set;
/**
* Helper Class to start up an Apache DS LDAP server for testing. Here is a typical use example in tests:
* <pre>
* static ApacheDsEmbedded ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif");
* Helper Class to start up an Apache DS LDAP server for testing.
*
* @BeforeClass public static void startServer() throws Exception {
* ldap.startServer();
* }
* @AfterClass public static void stopServer() throws Exception {
* ldap.stopAndCleanup();
* }
* </pre>
* Use ApacheDsRule instead of this class in tests
*/
public class ApacheDsEmbedded {
/**
@ -78,8 +71,8 @@ public class ApacheDsEmbedded {
*
* @throws Exception If something went wrong
*/
public ApacheDsEmbedded(String baseDN, String ldifFileName, String testName) {
this.workDir = new File(System.getProperty("java.io.tmpdir") + "/server-work/" + testName);
public ApacheDsEmbedded(String baseDN, String ldifFileName, File workDir) {
this.workDir = workDir;
this.baseDN = baseDN;
this.ldifFileName = ldifFileName;
this.port = 10389 + CHILD_JVM_ID;
@ -118,7 +111,6 @@ public class ApacheDsEmbedded {
public void stopAndCleanup() throws Exception {
if (server != null) server.stop();
if (service != null) service.shutdown();
workDir.delete();
}
@ -262,8 +254,6 @@ public class ApacheDsEmbedded {
entryApache.add("dc", "Apache");
service.getAdminSession().add(entryApache);
}
// We are all done !
}
private void loadSchema(String ldifFileName) throws URISyntaxException {
@ -274,30 +264,4 @@ public class ApacheDsEmbedded {
LdifFileLoader ldifLoader = new LdifFileLoader(service.getAdminSession(), ldifFile, Collections.EMPTY_LIST);
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;
import org.junit.rules.MethodRule;
import org.junit.rules.TemporaryFolder;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
@ -15,6 +16,11 @@ import org.junit.runners.model.Statement;
public class ApacheDsRule implements MethodRule {
private ApacheDsEmbedded ldap;
private final TemporaryFolder temporaryFolder;
public ApacheDsRule(TemporaryFolder temporaryFolder) {
this.temporaryFolder = temporaryFolder;
}
@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
@ -23,7 +29,7 @@ public class ApacheDsRule implements MethodRule {
@Override
public void evaluate() throws Throwable {
try {
ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", target.getClass().getName());
ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", temporaryFolder.newFolder());
ldap.startServer();
base.evaluate();
} finally {

View File

@ -10,6 +10,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.util.List;
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()) + '.';
@Rule
public static ApacheDsRule apacheDsRule = new ApacheDsRule();
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public static ApacheDsRule apacheDsRule = new ApacheDsRule(temporaryFolder);
@Test
public void testBindWithTemplates() {

View File

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