Package speculoos.jndi

Source Code of speculoos.jndi.JNDISourceTest$MyFactory

/*---------------------------------------------------------------------------
* 2005 NORSYS
* main author : nono
*
* This software is a computer program whose purpose is to provide abstraction
* for accessing directory data sources within java applications.
*
* This software is governed by the CeCILL  license under French law and
* abiding by the rules of distribution of free software.  You can  use,
* modify and/ or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and  rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty  and the software's author,  the holder of the
* economic rights,  and the successive licensors  have only  limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading,  using,  modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean  that it is complicated to manipulate,  and  that  also
* therefore means  that it is reserved for developers  and  experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and,  more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*
* Created on Oct 19, 2005
* --------------------------------------------------------------------------*/
package speculoos.jndi;

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

import javax.naming.CompoundName;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;

import junit.framework.TestCase;
import speculoos.Personne;
import speculoos.core.MapperException;
import speculoos.jndi.mappers.SearchMapperImpl;
import speculoos.jndi.mappers.StringVariable;
import speculoos.manager.MapperConfigurationException;
import speculoos.utils.TypeHelper;
import speculoos.utils.VariableString;
import fr.norsys.mockldap.TestDirContext;

/**
* Simple unit tests for JNDISource. We just test source life-cycle using
* simpleJndi as backend.
*
* @author nono
* @version $Id: JNDISourceTest.java 61 2005-10-21 14:54:44Z
*          /C=FR/ST=Nord/L=Lille/O=Norsys SA/OU=UE/CN=Arnaud
*          Bailly/emailAddress=abailly@norsys.fr $
*/
public class JNDISourceTest extends TestCase {


    public static final Properties SYNTAX = new Properties();

    static {
        SYNTAX.put("jndi.syntax.direction", "right_to_left");
        SYNTAX.put("jndi.syntax.separator", ",");
        SYNTAX.put("jndi.syntax.ignorecase", "true");
        SYNTAX.put("jndi.syntax.trimblanks", "true");
        SYNTAX.put("jndi.syntax.separator.ava", ",");
        SYNTAX.put("jndi.syntax.separator.typeval", "=");
    }

  private HashMap env;

  static public class MyFactory implements InitialContextFactory {

    public Context getInitialContext(Hashtable arg0) throws NamingException {
      return new TestDirContext(new CompoundName("o=canam",
          TestDirContext.SYNTAX)) {
        public void close() throws NamingException {
          throw new NamingException("exception");
        };
      };
    }

  }

  protected void setUp() throws Exception {
    super.setUp();
    this.env = new HashMap();
    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);
    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 */
    JNDISource src = new JNDISource();
    /* configure source */
    src.addParameters(env);
    /* add mapper */
    SearchMapperImpl smi = new SearchMapperImpl();
    VariableString root = new VariableString("cn=${nom}");
    smi.setRoot(root);
    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 */
    JNDISource src = new JNDISource();
    /* configure source */
    src.addParameters(env);
    /* add mapper */
    SearchMapperImpl smi = new SearchMapperImpl();
    VariableString root = new VariableString("cn=${nom}");
    smi.setRoot(root);
    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
    }
  }

  public void test04ConnectionFailure() throws MapperConfigurationException {
    env.put(Context.INITIAL_CONTEXT_FACTORY, "");
    /* create source */
    JNDISource src = new JNDISource();
    /* configure source */
    src.addParameters(env);
    /* add mapper */
    SearchMapperImpl smi = new SearchMapperImpl();
    VariableString root = new VariableString("cn=${nom}");
    smi.setRoot(root);
    smi.setTypeHelper(new TypeHelper());
    src.add("search1", smi);
    src.start(env);
    /* create mapper */
    try {
      SearchMapper sm = (SearchMapper) src.create("search1",
          new HashMap());
      fail("Not started; Should have thrown exception.");
    } catch (MapperException e) {
      // / OK
    }
  }


  public void test06Environment() throws MapperException {
    /* create source */
    JNDISource src = new JNDISource();
    /* 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);
    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 */
    JNDISource src = new JNDISource();
    /* 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);
    Map h = new HashMap();
    h.put("name","name");
    h.put("surname","surname");
    /* populate input */
    Personne input = new Personne();
    input.setName("NAQUIN");
    input.setSurname("thiery");
    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 */
    JNDISource src = new JNDISource();
    /* 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 */
    Map h = new HashMap();
    h.put("name","name");
    h.put("surname","surname");
    Personne input = new Personne();
    input.setName("NAQUIN");
    input.setSurname("thiery");
    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) {
    }

  }
}
TOP

Related Classes of speculoos.jndi.JNDISourceTest$MyFactory

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.