001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: */
022:
023: package org.objectweb.jonas.jtests.clients.entity;
024:
025: import java.util.Collection;
026: import java.util.Iterator;
027:
028: import org.objectweb.jonas.jtests.util.JTestCase;
029:
030: /**
031: * This class is used by all tests about relation ships in Entity CMP v2.
032: * @author Helene Joanin
033: */
034:
035: public abstract class A_Cmp2Util extends JTestCase {
036:
037: // No transaction
038: static final int TX_NO = 1;
039: // Transaction started by the client
040: static final int TX_CALL = 2;
041: // Transaction started by the container
042: static final int TX_CONT = 3;
043: // Transaction started by the client and rollbacked
044: static final int TX_RB = 4;
045:
046: public A_Cmp2Util(String name) {
047: super (name);
048: }
049:
050: protected void setUp() {
051: super .setUp();
052: sync(false);
053: }
054:
055: abstract boolean initStateOK() throws Exception;
056:
057: /**
058: * Check that the initial state on database is OK
059: * after running a test.
060: */
061: void checkIsInitialState() throws Exception {
062: boolean isOk = true;
063: try {
064: isOk = initStateOK();
065: } catch (Exception e) {
066: isOk = false;
067: }
068: assertTrue("Bad Initial State: " + msgerror.toString(), isOk);
069: }
070:
071: /**
072: * Compare two collections
073: */
074: static boolean isCollectionEqual(Collection c1, Collection c2) {
075: // Check if the size
076: if (c1.size() != c2.size()) {
077: return false;
078: }
079: // Check if each element of c1 belong to c2
080: Iterator it = c1.iterator();
081: boolean b = true;
082: while (it.hasNext() && (b = c2.contains(it.next())))
083: ;
084: if (it.hasNext() || !b) {
085: return false;
086: }
087: // Check if each element of c2 belong to c1
088: it = c2.iterator();
089: b = true;
090: while (it.hasNext() && (b = c1.contains(it.next())))
091: ;
092: return !it.hasNext() && b;
093: }
094:
095: /**
096: * Return the substring before the '-' of the given string.
097: * Ie, return "fred" if the given string is "fred-xxxx".
098: */
099: public static String getStringBeforeDash(String s) {
100: int iDash = s.indexOf("-");
101: if (iDash < 0) {
102: throw new IllegalArgumentException(
103: "Bad string format for getStringBeforeDash(): " + s);
104: }
105: String value = s.substring(0, iDash);
106: //debug("getStringBeforeDash("+s+")="+value);
107: return value;
108: }
109:
110: /**
111: * Return the int which corresponds to the substring after the '-' of the given string.
112: * Ie, return 19 if the given string is "fred-19".
113: */
114: public static int getIntAfterDash(String s) {
115: int iDash = s.indexOf("-");
116: if (iDash < 0) {
117: throw new IllegalArgumentException(
118: "Bad string format for getStringBeforeDash(): " + s);
119: }
120: String sInt = s.substring(iDash + 1);
121: int value = (new Integer(sInt)).intValue();
122: //debug("getIntAfterDash("+s+")="+value);
123: return value;
124: }
125:
126: }
|