Package speculoos.jndi

Source Code of speculoos.jndi.PooledSourceTest

package speculoos.jndi;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.spi.InitialContextFactory;

import junit.framework.TestCase;
import speculoos.Personne;
import speculoos.core.MapperException;
import speculoos.jndi.JNDISource;
import speculoos.jndi.SearchMapper;
import speculoos.jndi.mappers.SearchMapperImpl;
import speculoos.jndi.mappers.StringVariable;
import speculoos.jndi.pool.ConnectionFactory;
import speculoos.jndi.pool.JNDIPooledSource;
import speculoos.manager.MapperConfigurationException;
import speculoos.utils.TypeHelper;
import speculoos.utils.VariableString;

public class PooledSourceTest extends TestCase {

  private HashMap env;

  protected void setUp() throws Exception {
    super.setUp();
    this.env = new HashMap();
    /* default naming context */
    env.put(Context.INITIAL_CONTEXT_FACTORY,
        "fr.norsys.mockldap.TestDirContextFactory");
    env.put("fr.norsys.mockldap.testsource", "canam-personnes-sample.ldif");
  }

  public void test01MapperCreation() throws NamingException, MapperException {
    /* create source */
    JNDISource src = new JNDISource();
    /* configure source */
    src.addParameters(env);
    /* add mapper */
    SearchMapperImpl smi = new SearchMapperImpl("search1");
    VariableString root = new VariableString("cn=${nom}");
    smi.setRoot(root);
    VariableString filter = new VariableString("email=${email}");
    smi.getInputChain().addMapper(new StringVariable("filter", filter));
    smi.setTypeHelper(new TypeHelper());
    src.add("search1", smi);
    /* start source */
    src.start(env);
    /* create mapper */
    SearchMapper sm = (SearchMapper) src.create("search1", new HashMap());
    /* ensure mapper is new */
    assertNotSame(sm, smi);
    /* close */
    src.release(sm);
    src.stop();
  }

  public void test02NotStarted() throws MapperException {
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* add mapper */
    SearchMapperImpl smi = new SearchMapperImpl();
    VariableString root = new VariableString("cn=${nom}");
    smi.setRoot(root);
    VariableString filter = new VariableString("email=${email}");
    smi.getInputChain().addMapper(new StringVariable("filter", filter));
    smi.setTypeHelper(new TypeHelper());
    src.add("search1", smi);
    /* create mapper */
    try {
      SearchMapper sm = (SearchMapper) src.create("search1",
          new HashMap());
      fail("Not started; Should have thrown exception.");
    } catch (IllegalStateException e) {
      // / OK
    }
  }

  public void test03UnknownMapper() throws MapperException {
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* add mapper */
    SearchMapperImpl smi = new SearchMapperImpl();
    VariableString root = new VariableString("cn=${nom}");
    smi.setRoot(root);
    VariableString filter = new VariableString("email=${email}");
    smi.getInputChain().addMapper(new StringVariable("filter", filter));
    smi.setTypeHelper(new TypeHelper());
    src.add("search1", smi);
    src.start(env);
    /* create mapper */
    try {
      SearchMapper sm = (SearchMapper) src
          .create("search", new HashMap());
      fail("Not started; Should have thrown exception.");
    } catch (IllegalArgumentException e) {
      // / OK
    }
  }

  static public class MyFactory implements InitialContextFactory {

    public Context getInitialContext(Hashtable arg0) throws NamingException {
      throw new NamingException("exception");
    }

  }

  /*
   * failure to create connnections
   */
  public void test04ConnectionFailure() throws MapperConfigurationException {
    env.put(Context.INITIAL_CONTEXT_FACTORY,
        "speculoos.jndi.pool.PooledSourceTest$MyFactory");
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* add mapper */
    SearchMapperImpl smi = new SearchMapperImpl();
    VariableString root = new VariableString("cn=${nom}");
    smi.setRoot(root);
    VariableString filter = new VariableString("email=${email}");
    smi.getInputChain().addMapper(new StringVariable("filter", filter));
    smi.setTypeHelper(new TypeHelper());
    src.add("search1", smi);
    /* create mapper */
    try {
      src.start(env);
      fail("Not started; Should have thrown exception.");
    } catch (MapperException e) {
      // / OK
    }
  }

  public void test06Environment() throws MapperException {
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* substitution */
    Map nenv = new HashMap();
    nenv.put("var1", "${toto}");
    nenv.put("var2", "tutu${titi}");
    src.addParameters(nenv);
    /* start */
    Map genv = new HashMap();
    genv.put("toto", "toto");
    genv.put("titi", "titi");
    /* add mapper */
    SearchMapperImpl smi = new SearchMapperImpl();
    VariableString root = new VariableString("cn=${nom}");
    smi.setRoot(root);
    VariableString filter = new VariableString("email=${email}");
    smi.getInputChain().addMapper(new StringVariable("filter", filter));
    smi.setTypeHelper(new TypeHelper());
    src.add("search1", smi);
    src.start(genv);
    /* check environnement is passed to mapper */
    SearchMapperImpl sm = (SearchMapperImpl) src.create("search1", genv);
    assertEquals("Environment badly configured", "tututiti", sm
        .getEnvironment().get("var2"));
    assertEquals("Environment badly configured", "toto", sm
        .getEnvironment().get("toto"));
  }

  /**
   * Multiple releases of same object are idempotent.
   *
   * @throws MapperException
   *
   *
   */
  public void test07IdempotentRelease() throws MapperException {
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* substitution */
    Map nenv = new HashMap();
    nenv.put("var1", "${toto}");
    nenv.put("var2", "tutu${titi}");
    src.addParameters(nenv);
    /* start */
    Map genv = new HashMap();
    genv.put("toto", "toto");
    genv.put("titi", "titi");
    /* add mapper */
    SearchMapperImpl smi = new SearchMapperImpl("search1");
    VariableString root = new VariableString("ou=Personnes");
    smi.setRoot(root);
    VariableString filter = new VariableString(
        "(fullName=${self.name} ${self.surname})");
    smi.getInputChain().addMapper(new StringVariable("filter", filter));
    smi.setTypeHelper(new TypeHelper());
      src.add("search1", smi);
    src.start(genv);
    /* check environnement is passed to mapper */
    SearchMapperImpl sm = (SearchMapperImpl) src.create("search1", genv);
    /* populate input */
    Personne input = new Personne();
    input.setName("NAQUIN");
    input.setSurname("thiery");
    Map h = new HashMap();
    h.put("name","name");
    h.put("surname","surname");
    sm.setOutput(Personne.class,h);
    sm.map(input, null);
    src.release(sm);
    try {
      src.release(sm);
    } catch (Exception e) {
      fail("Multiple releases should not throw exceptions");
    }
  }



  /**
   * Checks mappers fail after release.
   *
   * @throws MapperException
   *
   */
  public void test08NoMapAfterSearchRelease() throws MapperException {
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* substitution */
    Map nenv = new HashMap();
    nenv.put("var1", "${toto}");
    nenv.put("var2", "tutu${titi}");
    src.addParameters(nenv);
    /* start */
    Map genv = new HashMap();
    genv.put("toto", "toto");
    genv.put("titi", "titi");
    /* add mapper */
    SearchMapperImpl smi = new SearchMapperImpl("search1");
    VariableString root = new VariableString("ou=Personnes");
    smi.setRoot(root);
    VariableString filter = new VariableString(
        "(fullName=${self.name} ${self.surname})");
    smi.getInputChain().addMapper(new StringVariable("filter", filter));
    smi.setTypeHelper(new TypeHelper());
      src.add("search1", smi);
    src.start(genv);
    /* check environnement is passed to mapper */
    SearchMapperImpl sm = (SearchMapperImpl) src.create("search1", genv);
    /* populate input */
    Personne input = new Personne();
    input.setName("NAQUIN");
    input.setSurname("thiery");
    Map h = new HashMap();
    h.put("name","name");
    h.put("surname","surname");
    sm.setOutput(Personne.class,h);
    sm.map(input, null);
    src.release(sm);
    try {
      input.setSurname("thiery");
      sm.map(input, null);
      fail("Map should fail after  releases");
    } catch (Exception e) {
    }
  }

  public static class MyCnxFactory implements ConnectionFactory {

    public MyCnxFactory(Map map) {
    }

    public DirContext connect() throws Exception {
      return null;
    }

    public void disconnect(DirContext obj) throws Exception {
    }

    public void setMaximumConnection(int maxPoolSize) {
    }

  }

  public void test09CustomConnectionFactory()
      throws MapperConfigurationException {
    env.put(JNDIPooledSource.FACTORY,
        "speculoos.jndi.PooledSourceTest$MyCnxFactory");
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* add mapper */
    SearchMapperImpl smi = new SearchMapperImpl();
    VariableString root = new VariableString("cn=${nom}");
    smi.setRoot(root);
    VariableString filter = new VariableString("email=${email}");
    smi.getInputChain().addMapper(new StringVariable("filter", filter));
    smi.setTypeHelper(new TypeHelper());
    src.add("search1", smi);
    /* create mapper */
    src.start(env);
    src.stop();
  }

  public void test10ErrorCustomConnectionFactory() {
    env.put(JNDIPooledSource.FACTORY, "toto");
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* create mapper */
    try {
      src.start(env);
      fail("connection factory does not exist : should have thrown exception");
    } catch (MapperConfigurationException e) {
      // OK
    }
  }

  public static class MyCnxFactory2 implements ConnectionFactory {

    public DirContext connect() throws Exception {
      return null;
    }

    public void disconnect(DirContext obj) throws Exception {
    }

    public void setMaximumConnection(int maxPoolSize) {
    }

  }

  public void test11NoCtorConnectionFactory() {
    env.put(JNDIPooledSource.FACTORY,
        "speculoos.jndi.pool.PooledSourceTest$MyCnxFactory2");
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* create mapper */
    try {
      src.start(env);
      fail("connection factory ctor does not exist : should have thrown exception");
    } catch (MapperConfigurationException e) {
      // OK
    }
  }

  public static class MyCnxFactory3 implements ConnectionFactory {

    public MyCnxFactory3(Map map) {
      throw new RuntimeException("exception");
    }

    public DirContext connect() throws Exception {
      return null;
    }

    public void disconnect(DirContext obj) throws Exception {
    }

    public void setMaximumConnection(int maxPoolSize) {
    }

  }

  public void test12FailedCtorConnectionFactory() {
    env.put(JNDIPooledSource.FACTORY,
        "speculoos.jndi.pool.PooledSourceTest$MyCnxFactory3");
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* create mapper */
    try {
      src.start(env);
      fail("connection factory ctor fails : should have thrown exception");
    } catch (MapperConfigurationException e) {
      // OK
    }
  }

  public void test13NegativeMinSize() {
    env.put(JNDIPooledSource.MIN_POOL_SIZE, "-3");
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* create mapper */
    try {
      src.start(env);
      fail("minimum pool size incorrect: should have thrown exception");
    } catch (MapperConfigurationException e) {
      // OK
    }
  }

  public void test14NanMinSize() {
    env.put(JNDIPooledSource.MIN_POOL_SIZE, "toto");
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* create mapper */
    try {
      src.start(env);
      fail("minimum pool size incorrect: should have thrown exception");
    } catch (MapperConfigurationException e) {
      // OK
    }
  }

  public void test15IncorrectMaxSize() {
    env.put(JNDIPooledSource.MIN_POOL_SIZE, "2");
    env.put(JNDIPooledSource.MAX_POOL_SIZE, "1");
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* create mapper */
    try {
      src.start(env);
      fail("maximum pool size incorrect: should have thrown exception");
    } catch (MapperConfigurationException e) {
      // OK
    }
  }

  public void test16NanMaxSize() {
    env.put(JNDIPooledSource.MAX_POOL_SIZE, "tutu");
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* create mapper */
    try {
      src.start(env);
      fail("maximum pool size incorrect: should have thrown exception");
    } catch (MapperConfigurationException e) {
      // OK
    }
  }
  public void test17IncorrectTimeout() {
    env.put(JNDIPooledSource.TIMEOUT, "-5");
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* create mapper */
    try {
      src.start(env);
      fail("timeout incorrect: should have thrown exception");
    } catch (MapperConfigurationException e) {
      // OK
    }
  }

  public void test18NanTimeout() {
    env.put(JNDIPooledSource.TIMEOUT, "tutu");
    /* create source */
    JNDIPooledSource src = new JNDIPooledSource();
    /* configure source */
    src.addParameters(env);
    /* create mapper */
    try {
      src.start(env);
      fail("timeout incorrect: should have thrown exception");
    } catch (MapperConfigurationException e) {
      // OK
    }
  }


}
TOP

Related Classes of speculoos.jndi.PooledSourceTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.