/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * 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.1 of the License, or 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.jonas.jtests.clients.entity;

import java.util.Collection;
import java.util.Iterator;

import org.objectweb.jonas.jtests.util.JTestCase;


/**
 * This class is used by all tests about relation ships in Entity CMP v2.
 * @author Helene Joanin
 */

public abstract class A_Cmp2Util extends JTestCase {

    // No transaction
    static final int TX_NO = 1;
    // Transaction started by the client
    static final int TX_CALL = 2;
    // Transaction started by the container
    static final int TX_CONT = 3;
    // Transaction started by the client and rollbacked
    static final int TX_RB = 4;

    public A_Cmp2Util(String name) {
        super(name);
    }

    protected void setUp() {
        super.setUp();
        sync(false);
    }

    abstract boolean initStateOK() throws Exception;

    /**
     * Check that the initial state on database is OK
     * after running a test.
     */
    void checkIsInitialState() throws Exception {
        boolean isOk = true;
        try {
            isOk = initStateOK();
        } catch (Exception e) {
            isOk = false;
        }
        assertTrue("Bad Initial State: " + msgerror.toString(), isOk);
    }

    /**
     * Compare two collections
     */
    static boolean isCollectionEqual(Collection c1, Collection c2) {
        // Check if the size
        if (c1.size() != c2.size()) {
            return false;
        }
        // Check if each element of c1 belong to c2
        Iterator it = c1.iterator();
        boolean b = true;
        while(it.hasNext() && (b = c2.contains(it.next())));
        if (it.hasNext() || !b) {
            return false;
        }
        // Check if each element of c2 belong to c1
        it = c2.iterator();
        b = true;
        while(it.hasNext() && (b = c1.contains(it.next())));
        return !it.hasNext() && b;
    }

    /**
     * Return the substring before the '-' of the given string.
     * Ie, return "fred" if the given string is "fred-xxxx".
     */
    public static String getStringBeforeDash(String s) {
        int iDash = s.indexOf("-");
        if (iDash < 0) {
            throw new IllegalArgumentException("Bad string format for getStringBeforeDash(): "+s);
        }
        String value = s.substring(0, iDash);
        //debug("getStringBeforeDash("+s+")="+value);
        return value ;
    }

    /**
     * Return the int which corresponds to the substring after the '-' of the given string.
     * Ie, return 19 if the given string is "fred-19".
     */
    public static int getIntAfterDash(String s) {
        int iDash = s.indexOf("-");
        if (iDash < 0) {
            throw new IllegalArgumentException("Bad string format for getStringBeforeDash(): "+s);
        }
        String sInt = s.substring(iDash+1);
        int value = (new Integer(sInt)).intValue();
        //debug("getIntAfterDash("+s+")="+value);
        return value;
    }

}
