Package org.objectweb.speedo.ejb

Source Code of org.objectweb.speedo.ejb.SpeedoTestHelper

/**
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package org.objectweb.speedo.ejb;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Interface;
import org.objectweb.fractal.util.Fractal;
import org.objectweb.jorm.api.PMapper;
import org.objectweb.perseus.cache.api.CacheAttributeController;
import org.objectweb.speedo.AbstractSpeedo;
import org.objectweb.speedo.api.SpeedoProperties;
import org.objectweb.util.monolog.Monolog;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.LoggerFactory;

/**
* Provide common code for all tests covering EJB3. It is inherited by most
* of the test cases of EJB3.
*
* @author P. Dechamboux
*/
public abstract class SpeedoTestHelper extends TestCase {

  public final static String LOG_NAME = "org.objectweb.speedo.test";

  protected static EntityManagerFactory emf = null;
  protected static boolean emfProblem = false;
  public Logger logger = null;
  protected LoggerFactory loggerFactory = null;
  protected Properties emfProp;

  public SpeedoTestHelper(String s, String emname) {
    super(s);
    if (!emfProblem) {
      getEMF(emname);
    }
        loggerFactory = Monolog.monologFactory;
    logger = loggerFactory.getLogger(getLoggerName());
  }
  
  protected abstract String getLoggerName();
 
  public Logger getLogger() {
      return logger;
  }

  public  EntityManagerFactory getEMF(String emname) {
        if (emf == null) {
          synchronized(SpeedoTestHelper.class) {
            if (!emfProblem && emf == null) {
              try {
                emf = Persistence.createEntityManagerFactory(emname);
              } catch (Error e) {
                emfProblem = true;
                e.printStackTrace();
                throw e;
              }
            }
          }
        }
        return emf;
    }
   
  public Properties getEMFPropertiesFromFile() {
    emfProp = new Properties();
        InputStream is = getClass().getClassLoader()
                .getResourceAsStream("speedo-ejb.properties");
        if (is == null) {
            return null;
        }
        try {
          emfProp.load(is);
        } catch (IOException e) {
            return null;
        }
        return emfProp;
    }
   
    public String getConnectionDriverNameProperty(Properties p) {
        String dcn = p.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
        if (dcn == null) {
            dcn = p.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD);
            if (dcn != null) {
                System.err.println("The property '" +
                        SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD
                        + "' is deprecated, you have to use '"
                        + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME
                        + "'.");
            } else {
                dcn = p.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD2);
                if (dcn == null) {
                    System.out.println(p);
                    fail("No driver class name specified");
                } else {
                    System.err.println("The property '" +
                            SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD2
                            + "' is deprecated, you have to use '"
                            + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME
                            + "'.");
                   
                }
            }
        }
        return dcn;
    }
   
  public Properties getPMFProperties() {
        if (Boolean.getBoolean("org.objectweb.speedo.useFile")) {
          emfProp = getEMFPropertiesFromFile();
          return emfProp;
        }
    String debug = System.getProperty("org.objectweb.speedo.debug", "false");
    String dcn = getConnectionDriverNameProperty(System.getProperties());
    String user = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, "");
    String pass = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, "");
    String url = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL);
    if (url == null)
      Assert.fail("No database url specified");
    emfProp = new Properties();
    emfProp.setProperty(
        SpeedoProperties.JDO_PERSISTENCE_MANAGER_FACTORY_CLASS,
      System.getProperty(SpeedoProperties.JDO_PERSISTENCE_MANAGER_FACTORY_CLASS,
        "org.objectweb.speedo.Speedo"));
    emfProp.setProperty("org.objectweb.speedo.debug", debug);
    emfProp.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME, dcn);
    emfProp.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, user);
    emfProp.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, pass);
    emfProp.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL, url);
    emfProp.setProperty(SpeedoProperties.CACHE_SIZE, "nolimit");
    return emfProp;
  }

    public void assertSameCollection(String msg, Collection expected, Collection found) {
      try {
        if(expected == null) {
            assertNull(msg + " null collection expected", found);
            return;
        }
        Assert.assertNotNull(msg + " non null collection expected", found);
        Assert.assertEquals(msg + " not same size", expected.size(), found.size());
        Iterator it = expected.iterator();
        while(it.hasNext()) {
            Object element  = it.next();
            Assert.assertTrue(msg + " the found collection (" + found
              + ") does not contains the element "
                    + element , found.contains(element));
        }
        it = found.iterator();
        while(it.hasNext()) {
            Object element  = it.next();
            assertTrue(msg + " the found collection contains an unexpected element "
                    + element , expected.contains(element));
        }
      } catch (Error e) {
        throw e;
      }
    }

    public void assertSameList(String msg, List expected, List found) {
        if(expected == null) {
            Assert.assertNull(msg + " null list expected", found);
            return;
        }
        Assert.assertNotNull(msg + " non null list expected", found);
        Assert.assertEquals(msg + " not same size", expected.size(), found.size());
        for(int i=0; i<expected.size(); i++) {
            Assert.assertEquals(msg + " bad element at the index" + i,
                    expected.get(i), found.get(i));
        }
    }

  public PMapper getMapper(EntityManagerFactory _emf) throws Exception {
        Interface fcemf = (Interface) ((AbstractSpeedo) _emf).getDelegate();
    return (PMapper) getSubComponent(
            fcemf.getFcItfOwner(),
            "mapper")
            .getFcInterface("mapper");
  }

  protected CacheAttributeController getCacheAttributeController(EntityManagerFactory _emf) throws Exception {
        Interface fcemf = (Interface) ((AbstractSpeedo) _emf).getDelegate();
    Component c = getSubComponent(
            ((Interface)fcemf).getFcItfOwner(),
            "tpm.cache-manager.cache-manager");
    return (CacheAttributeController) Fractal.getAttributeController(c);
  }

  private Component getSubComponent(Component parent,
                    String path) throws Exception {
    //System.out.println("path: " + path);

    StringTokenizer st = new StringTokenizer(path, ".", false);
    Component res = parent;
    while(st.hasMoreTokens()) {
      String commponenentname = st.nextToken();
      Component[] children = Fractal.getContentController(res)
          .getFcSubComponents();
      int i = 0;
      //System.out.println("search: " + commponenentname);
      while(i<children.length &&
          !Fractal.getNameController(children[i])
            .getFcName().equals(commponenentname)) {
        //System.out.println("current: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
        i++;
      }
      if (i<children.length) {
        //System.out.println("found: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
        res = children[i];
      } else {
        //System.out.println("not found");
        return null;
      }
    }
    return res;
  }

  public Component getSubComponent(Component parent,
      String path, String s) throws Exception {
    //System.out.println("path: " + path);
    StringTokenizer st = new StringTokenizer(path, ".", false);
    Component res = parent;
    while(st.hasMoreTokens()) {
      String commponenentname = st.nextToken();
      Component[] children = Fractal.getContentController(res)
      .getFcSubComponents();
      int i = 0;
      //System.out.println("search: " + commponenentname);
      while(i<children.length &&
          !Fractal.getNameController(children[i])
          .getFcName().equals(commponenentname)) {
        //System.out.println("current: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
        i++;
      }
      if (i<children.length) {
        //System.out.println("found: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
        res = children[i];
      } else {
        //System.out.println("not found");
        return null;
      }
    }
    return res;
  }
 
  protected int deleteAllInstances(Class clazz) {
    EntityManager em = emf.getEntityManager();
    em.getTransaction().begin();
    int i = 0;
/*    Extent e = em.getExtent(clazz, true);
    Iterator it = e.iterator();
    while(it.hasNext()) {
        em.remove(it.next());
        i++;
    }
    e.closeAll();*/
    em.getTransaction().commit();
    em.close();
    logger.log(BasicLevel.DEBUG, "Delete " + i + " instance(s) of the class " + clazz.getName());
    return i;
  }

  public static int getIntProperty(String propertyName, int defaultValue) {
    String v = System.getProperty(propertyName);
    if (v == null) {
      return defaultValue;
    }
    try {
      return Integer.parseInt(v);
    } catch (NumberFormatException e) {
      return defaultValue;
    }
  }
    public final static int length(final int val) {
        int length = 0;
        int c = val;
        while (c > 0) {
            c = c / 10;
            length ++;
        }
        return length;
    }
   
    public final static String i2s(final int val, final int length) {
        final StringBuffer sb = new StringBuffer(length);
        int c = val;
        for(int i=0; i<length; i++) {
            if (c == 0) {
                sb.insert(0, '0');
            } else {
                sb.insert(0, c % 10);
                c = c / 10;
            }
        }
        return sb.toString();
    }
}
TOP

Related Classes of org.objectweb.speedo.ejb.SpeedoTestHelper

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.