Package org.objectweb.speedo

Source Code of org.objectweb.speedo.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;

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.jdo.Extent;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

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.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;

/**
*
* @author S.Chassande-Barrioz
*/
public abstract class SpeedoTestHelper extends TestCase {

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

  protected static PersistenceManagerFactory pmf = null;
  protected static boolean pmfProblem = false;
  public Logger logger = null;
  protected LoggerFactory loggerFactory = null;
  protected Properties pmfProp;

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

  public  PersistenceManagerFactory getPMF() {
        if (pmf == null) {
          synchronized(SpeedoTestHelper.class) {
            if (!pmfProblem && pmf == null) {
              try {
                pmf = JDOHelper.getPersistenceManagerFactory(getPMFProperties());
              } catch (Error e) {
                pmfProblem = true;
                e.printStackTrace();
                throw e;
              }
            }
          }
        }
        return pmf;
    }
   
  public Properties getPMFPropertiesFromFile() {
    pmfProp = new Properties();
        InputStream is = getClass().getClassLoader()
                .getResourceAsStream("speedo-jdo.properties");
        if (is == null) {
            return null;
        }
        try {
          pmfProp.load(is);
        } catch (IOException e) {
            return null;
        }
        return pmfProp;
    }
   
    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")) {
          pmfProp = getPMFPropertiesFromFile();
          return pmfProp;
        }
    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");
    pmfProp = new Properties();
    pmfProp.setProperty(
        SpeedoProperties.JDO_PERSISTENCE_MANAGER_FACTORY_CLASS,
      System.getProperty(SpeedoProperties.JDO_PERSISTENCE_MANAGER_FACTORY_CLASS,
        "org.objectweb.speedo.Speedo"));
    pmfProp.setProperty("org.objectweb.speedo.debug", debug);
    pmfProp.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME, dcn);
    pmfProp.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, user);
    pmfProp.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, pass);
    pmfProp.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL, url);
    pmfProp.setProperty(SpeedoProperties.CACHE_SIZE, "nolimit");
    return pmfProp;
  }

    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(PersistenceManagerFactory _pmf) throws Exception {
        Interface fcpmf = (Interface) ((AbstractSpeedo) _pmf).getDelegate();
    return (PMapper) getSubComponent(
            fcpmf.getFcItfOwner(),
            "mapper")
            .getFcInterface("mapper");
  }

  protected CacheAttributeController getCacheAttributeController(PersistenceManagerFactory _pmf) throws Exception {
        Interface fcpmf = (Interface) ((AbstractSpeedo) _pmf).getDelegate();
    Component c = getSubComponent(
            ((Interface)fcpmf).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) {
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.currentTransaction().begin();
    Extent e = pm.getExtent(clazz, true);
    Iterator it = e.iterator();
    int i = 0;
    while(it.hasNext()) {
        pm.deletePersistent(it.next());
        i++;
    }
    e.closeAll();
    pm.currentTransaction().commit();
    pm.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.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.