001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: */package org.objectweb.speedo.pobjects.intelli;
004:
005: import org.objectweb.speedo.SpeedoTestHelper;
006: import org.objectweb.util.monolog.api.BasicLevel;
007:
008: import java.util.Collection;
009: import java.util.Iterator;
010:
011: import javax.jdo.Extent;
012: import javax.jdo.PersistenceManager;
013: import javax.jdo.Query;
014:
015: /**
016: *
017: *
018: * @author chassase
019: */
020: public class TestIntelli extends SpeedoTestHelper {
021:
022: public TestIntelli(String name) {
023: super (name);
024: }
025:
026: protected String getLoggerName() {
027: return LOG_NAME + ".intelli";
028: }
029:
030: public void testImbricatedContains() {
031: logger.log(BasicLevel.DEBUG,
032: "testSequenceIdNavigateToPrimitive");
033: PersistenceManager pm = pmf.getPersistenceManager();
034: final int nbAdmin = 5;
035: final int nbRolePerAdmin = 5;
036: final int nbScopePerRole = 5;
037: final Class[] classes = new Class[] { Admin.class, Role.class,
038: Scope.class };
039: autoClean(pm, classes);
040:
041: //Fill the database
042: pm.currentTransaction().begin();
043:
044: for (int i = 0; i < nbAdmin; i++) {
045: Admin admin = new Admin();
046: admin.setF1("admin" + i);
047: for (int j = 0; j < nbRolePerAdmin; j++) {
048: Role role = new Role();
049: admin.getRoles().add(role);
050: role.setF1("role" + j);
051: for (int k = 0; k < nbScopePerRole; k++) {
052: Scope scope = new Scope();
053: role.getScopes().add(scope);
054: scope.setF1("scope" + k);
055: scope.setRole(role);
056: }
057: }
058: pm.makePersistent(admin);
059: }
060: pm.currentTransaction().commit();
061:
062: pm.currentTransaction().begin();
063: Query q = pm.newQuery(Admin.class);
064: q
065: .setFilter("((roles.contains(role)) && (role.scopes.contains(scope)) && (scope.f1.matches(\"%2\")) && (scope.f1.matches(p1)))");
066: q.declareVariables("Role role; Scope scope");
067: q.declareParameters("String p1");
068: Collection c = (Collection) q.execute("%2");
069: assertEquals("Bad result size", nbAdmin * nbRolePerAdmin, c
070: .size());
071: pm.currentTransaction().commit();
072:
073: autoClean(pm, classes);
074: pm.close();
075:
076: }
077:
078: public void testMisc() {
079: logger.log(BasicLevel.DEBUG,
080: "testSequenceIdNavigateToPrimitive");
081: PersistenceManager pm = pmf.getPersistenceManager();
082: final int nbSensor = 10;
083: final int nbRoomPerArea = 10;
084: final int nbStatePerSensor = 5;
085: final Class[] classes = new Class[] { Sensor.class, Area.class,
086: State.class, Room.class };
087:
088: autoClean(pm, classes);
089:
090: //Fill the database
091: pm.currentTransaction().begin();
092: for (int i = 0; i < nbSensor; i++) {
093: Sensor sensor = new Sensor(i);
094: for (int j = 0; j < nbStatePerSensor; j++) {
095: State state = new State(i * 100 + j);
096: sensor.getStates().add(state);
097: }
098: Area area = new Area(i);
099: sensor.setArea(area);
100: for (int j = 0; j < nbRoomPerArea; j++) {
101: Room room = new Room(i * 100 + j);
102: area.getRooms().add(room);
103: }
104: pm.makePersistent(sensor);
105: }
106: pm.currentTransaction().commit();
107:
108: pm.currentTransaction().begin();
109: Query q = pm.newQuery(Sensor.class);
110: q
111: .setFilter("((area.rooms.contains(r)) && (r.roomid == rid) && (states.contains(s)) && (s.stateid == sid))");
112: q.declareVariables("Room r; StateItf s");
113: q.declareParameters("int sid, int rid");
114: Collection c = (Collection) q.execute(new Integer(102),
115: new Integer(103));
116: pm.currentTransaction().commit();
117:
118: autoClean(pm, classes);
119: pm.close();
120: }
121:
122: private void autoClean(PersistenceManager pm, Class[] classes) {
123: pm.currentTransaction().begin();
124: for (int i = 0; i < classes.length; i++) {
125: Extent extent = pm.getExtent(classes[i], false);
126: for (Iterator it = extent.iterator(); it.hasNext();) {
127: pm.deletePersistent(it.next());
128: }
129: extent.closeAll();
130: }
131: pm.currentTransaction().commit();
132: }
133:
134: }
|