|
From: Michael R. <mr...@us...> - 2002-01-10 23:06:48
|
Update of /cvsroot/openorb/ConcurrencyControlService/src/test/org/openorb/ccs/test
In directory usw-pr-cvs1:/tmp/cvs-serv27066/src/test/org/openorb/ccs/test
Added Files:
CCSTest.java
Log Message:
#501914: Removed the sub sir: src
Fixed several problems with the bash scripts.
--- NEW FILE: CCSTest.java ---
/**
* Redistribution and use of this software and associated
* documentation ("Software"), with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright statements
* and notices. Redistributions must also contain a copy of this
* document.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Intalio Inc. For written permission, please
* contact in...@ex....
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Intalio Inc. Exolab is a registered trademark of
* Intalio Inc.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY INTALIO AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Copyright 2000 (C) Intalio Inc. All Rights Reserved.
*
* $Id: CCSTest.java,v 1.1 2002/01/10 23:06:43 mrumpf Exp $
*
* Date Author Changes
* Sept 2000 Marina DANIEL Created
*/
package org.openorb.ccs.test;
import java.util.*;
import org.omg.CosConcurrencyControl.*;
import org.omg.PortableServer.POA;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
/**
* @author <a href="mailto:md...@in...">Marina Daniel <mailto:md...@in...></a>
* @author <a href="mailto:om...@in...">Olivier Modica <mailto:om...@in...></a>
* @version $Revision: 1.1 $ $Date: 2002/01/10 23:06:43 $
*/
public class CCSTest extends TestCase {
private org.omg.CORBA.ORB orb;
private org.openorb.util.ContextUtilities context_utilities;
private org.openorb.ccs.LockSetFactory lockSetFactory;
private Thread serverThread;
private Vector locks = new Vector();
private Hashtable resourceLock = new Hashtable();
private static final Object[] modes = {
"READ",
"WRITE",
"UPGRADE",
"INTENTION READ",
"INTENTION WRITE"
};
public boolean verbose = false;
public CCSTest(String name) {
super(name);
}
public void setUp() {
try
{
super.setUp();
String [] args = new String[0];
orb = org.omg.CORBA.ORB.init(args, null);
POA rootPOA = (POA)orb.resolve_initial_references("RootPOA");
rootPOA.the_POAManager().activate();
lockSetFactory = org.openorb.ccs.LockSetFactoryHelper.narrow((new org.openorb.ccs.kernel.LockSetFactory())._this(orb));
serverThread = new Thread(new Runnable() {
public void run()
{
try { orb.run(); }
catch(Exception ex){}
}
});
serverThread.start();
}
catch(Exception e) {e.printStackTrace();}
}
public void tearDown() {
orb.shutdown(true);
orb = null;
try {
serverThread.join(1000);
}
catch(InterruptedException ex) {}
}
public void testLocks() {
try {
// READ mode
if(verbose) System.out.println("lock R1 in READ mode");
lock("R1", 0);
assert("READ lock should have been allowed", trylock("R1",0));
if(verbose) System.out.println("READ lock is allowed");
assert("WRITE lock should not have been allowed", !trylock ("R1", 1));
if(verbose) System.out.println("WRITE lock is not allowed");
assert("UPGRADE lock should have been allowed", trylock ("R1", 2));
if(verbose) System.out.println("UPGRADE lock is allowed");
assert("INTENTION READ lock should have been allowed", trylock ("R1", 3));
if(verbose) System.out.println("INTENTION READ lock is allowed");
assert("INTENTION WRITE lock should not have been allowed", !trylock("R1", 4));
if(verbose) System.out.println("INTENTION WRITE is not allowed");
// WRITE Mode
if(verbose) System.out.println("\n change R1 lock from READ to WRITE");
changeLock("R1", 1);
assert("READ lock should not have been allowed", !trylock("R1",0));
if(verbose) System.out.println("READ lock is not allowed");
assert("WRITE lock should not have been allowed", !trylock ("R1", 1));
if(verbose) System.out.println("WRITE lock is not allowed");
assert("UPGRADE lock should not have been allowed", !trylock ("R1", 2));
if(verbose) System.out.println("UPGRADE lock is not allowed");
assert("INTENTION READ lock should not have been allowed", !trylock ("R1", 3));
if(verbose) System.out.println("INTENTION READ lock is not allowed");
assert("INTENTION WRITE lock should not have been allowed", !trylock("R1", 4));
if(verbose) System.out.println("INTENTION WRITE is not allowed");
// UPGRADE Mode
if(verbose) System.out.println("\n change R1 lock from WRITE to UPGRADE");
changeLock("R1", 2);
assert("READ lock should have been allowed", trylock("R1",0));
if(verbose) System.out.println("READ lock is allowed");
assert("WRITE lock should not have been allowed", !trylock ("R1", 1));
if(verbose) System.out.println("WRITE lock is not allowed");
assert("UPGRADE lock should not have been allowed", !trylock ("R1", 2));
if(verbose) System.out.println("UPGRADE lock is not allowed");
assert("INTENTION READ lock should have been allowed", trylock ("R1", 3));
if(verbose) System.out.println("INTENTION READ lock is allowed");
assert("INTENTION WRITE lock should not have been allowed", !trylock("R1", 4));
if(verbose) System.out.println("INTENTION WRITE is not allowed");
// INTENTION READ Mode
if(verbose) System.out.println("unlock R1 mode UPGRADE");
unlock("R1", 2);
if(verbose) System.out.println("lock R1 mode INTENTION READ");
lock("R1", 3);
assert("READ lock should have been allowed", trylock("R1",0));
if(verbose) System.out.println("READ lock is allowed");
assert("WRITE lock should not have been allowed", !trylock ("R1", 1));
if(verbose) System.out.println("WRITE lock is not allowed");
assert("UPGRADE lock should have been allowed", trylock ("R1", 2));
if(verbose) System.out.println("UPGRADE lock is allowed");
assert("INTENTION READ lock should have been allowed",trylock ("R1", 3));
if(verbose) System.out.println("INTENTION READ lock is allowed");
assert("INTENTION WRITE lock should have been allowed", trylock("R1", 4));
if(verbose) System.out.println("INTENTION WRITE is allowed");
// INTENTION WRITE Mode
if(verbose) System.out.println("change R1 lock from INTENTION READ to INTENTION WRITE");
changeLock("R1", 4);
assert("READ lock should not have been allowed", !trylock("R1",0));
if(verbose) System.out.println("READ lock is not allowed");
assert("WRITE lock should not have been allowed", !trylock ("R1", 1));
if(verbose) System.out.println("WRITE lock is not allowed");
assert("UPGRADE lock should not have been allowed", !trylock ("R1", 2));
if(verbose) System.out.println("UPGRADE lock is not allowed");
assert("INTENTION READ lock should have been allowed", trylock ("R1", 3));
if(verbose) System.out.println("INTENTION READ lock is allowed");
assert("INTENTION WRITE lock should have been allowed", trylock("R1", 4));
if(verbose) System.out.println("INTENTION WRITE is allowed");
unlock("R1", 4);
}
catch(Exception ex) { ex.printStackTrace(); fail(ex.toString()); }
}
/**
* lock the resource with the lock mode selection
* @param resourceName the resource to lock
* @param selected_index the selected lock mode
*/
private void lock(String resourceName, int selected_index)
{
if (resourceName.equals(""))
return;
if (!resourceLock.containsKey(resourceName))
{
try
{
org.omg.CosConcurrencyControl.LockSet lockset = lockSetFactory.getLockSet(resourceName, true);
lockset.lock(org.omg.CosConcurrencyControl.lock_mode.from_int(selected_index));
}
catch (org.openorb.ccs.LockSetNotFound e) {}
addLock(resourceName, selected_index);
}
else
changeLock(resourceName, selected_index);
}
/**
* change the lock mode of the resource
* @param resourceName the resource to lock
* @param selected_index the new lock mode
*/
private void changeLock(String resourceName, int selected_index)
{
Integer previousLock = (Integer)(resourceLock.get(resourceName));
if (previousLock.intValue() != selected_index)
{
try
{
org.omg.CosConcurrencyControl.LockSet lockset = lockSetFactory.getLockSet(resourceName, true);
lockset.change_mode(lock_mode.from_int(previousLock.intValue()), lock_mode.from_int(selected_index));
removeLock(resourceName, previousLock.intValue());
addLock(resourceName, selected_index);
}
catch (org.omg.CosConcurrencyControl.LockNotHeld e)
{
System.out.println("lock not held");
return;
}
catch (org.openorb.ccs.LockSetNotFound e) { System.out.println("Lock set not found");}
}
}
/**
* unlock the resource for the selection mode
* @param resourceName the resource to unlock
* @param selected_index the selected lock mode
*/
private void unlock(String resourceName, int selected_index)
{
if (resourceName.equals(""))
return;
if ((resourceLock.containsKey(resourceName))
&& ((Integer)(resourceLock.get(resourceName))).intValue() == selected_index)
{
try
{
org.omg.CosConcurrencyControl.LockSet lockset = lockSetFactory.getLockSet(resourceName, true);
lockset.unlock(org.omg.CosConcurrencyControl.lock_mode.from_int(selected_index));
removeLock(resourceName, selected_index);
}
catch (org.omg.CosConcurrencyControl.LockNotHeld e) {System.out.println("lock not held"); return;}
catch (org.openorb.ccs.LockSetNotFound e) {}
}
}
/**
* try if it is possible locking the resource with the selection mode
* @param selected_index the selected lock mode
* @return true if you can lock the resource with this selection lock mode
*/
private boolean trylock(String resourceName, int selected_index)
{
try
{
org.omg.CosConcurrencyControl.LockSet lockset = lockSetFactory.getLockSet(resourceName, true);
return lockset.try_lock(org.omg.CosConcurrencyControl.lock_mode.from_int(selected_index));
}
catch (org.openorb.ccs.LockSetNotFound e) { System.out.println("LockSet Not Found"); System.exit(0); return false;}
}
/**
* update the lock list for internal use
* @param resourceName the resource that has been locked
* @param selected_index the lock mode of the resource
*/
private void addLock(String resourceName, int selected_index)
{
String value = resourceName + " " + modes[selected_index];
resourceLock.put(resourceName, new Integer(selected_index));
locks.addElement(value);
}
/**
* update the lock list (for internal use)
* @param resourceName the resource that has been unlocked
* @param selected_index, the previous lock mode of the resource
*/
private void removeLock(String resourceName, int selected_index)
{
String value = resourceName + " " + modes[selected_index];
if (locks.contains(value))
locks.removeElement(value);
resourceLock.remove(resourceName);
}
/**
* return whether the resource is locked
* @param resourceName the resource
* @param selected_index the lock mode
*/
private boolean alreadyLocked(String resourceName, int selected_index)
{
return locks.contains(resourceName + " " + modes[selected_index]);
}
static public void main(String args[])
{
junit.textui.TestRunner.run(new TestSuite(CCSTest.class));
}
}
|