KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > pobjects > intelli > TestIntelli


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  */

4 package org.objectweb.speedo.pobjects.intelli;
5
6 import org.objectweb.speedo.SpeedoTestHelper;
7 import org.objectweb.util.monolog.api.BasicLevel;
8
9 import java.util.Collection JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 import javax.jdo.Extent;
13 import javax.jdo.PersistenceManager;
14 import javax.jdo.Query;
15
16 /**
17  *
18  *
19  * @author chassase
20  */

21 public class TestIntelli extends SpeedoTestHelper {
22
23     public TestIntelli(String JavaDoc name) {
24         super(name);
25     }
26
27     protected String JavaDoc getLoggerName() {
28         return LOG_NAME + ".intelli";
29     }
30
31
32     public void testImbricatedContains() {
33         logger.log(BasicLevel.DEBUG, "testSequenceIdNavigateToPrimitive");
34         PersistenceManager pm = pmf.getPersistenceManager();
35         final int nbAdmin = 5;
36         final int nbRolePerAdmin = 5;
37         final int nbScopePerRole = 5;
38         final Class JavaDoc[] classes = new Class JavaDoc[]{Admin.class, Role.class, Scope.class};
39         autoClean(pm, classes);
40
41         //Fill the database
42
pm.currentTransaction().begin();
43         
44         
45         for(int i=0; i<nbAdmin; i++) {
46             Admin admin = new Admin();
47             admin.setF1("admin" + i);
48             for(int j=0; j<nbRolePerAdmin; j++) {
49                 Role role = new Role();
50                 admin.getRoles().add(role);
51                 role.setF1("role" + j);
52                 for(int k=0; k<nbScopePerRole; k++) {
53                     Scope scope = new Scope();
54                     role.getScopes().add(scope);
55                     scope.setF1("scope" + k);
56                     scope.setRole(role);
57                 }
58             }
59             pm.makePersistent(admin);
60         }
61         pm.currentTransaction().commit();
62
63         pm.currentTransaction().begin();
64         Query q = pm.newQuery(Admin.class);
65         q.setFilter("((roles.contains(role)) && (role.scopes.contains(scope)) && (scope.f1.matches(\"%2\")) && (scope.f1.matches(p1)))");
66         q.declareVariables("Role role; Scope scope");
67         q.declareParameters("String p1");
68         Collection JavaDoc c = (Collection JavaDoc) q.execute("%2");
69         assertEquals("Bad result size", nbAdmin * nbRolePerAdmin, c.size());
70         pm.currentTransaction().commit();
71
72         autoClean(pm, classes);
73         pm.close();
74         
75         
76     }
77
78     public void testMisc() {
79         logger.log(BasicLevel.DEBUG, "testSequenceIdNavigateToPrimitive");
80         PersistenceManager pm = pmf.getPersistenceManager();
81         final int nbSensor = 10;
82         final int nbRoomPerArea = 10;
83         final int nbStatePerSensor = 5;
84         final Class JavaDoc[] classes = new Class JavaDoc[]{Sensor.class, Area.class, State.class, Room.class};
85
86         autoClean(pm, classes);
87
88         //Fill the database
89
pm.currentTransaction().begin();
90         for(int i=0; i<nbSensor; i++) {
91             Sensor sensor = new Sensor(i);
92             for(int j=0; j<nbStatePerSensor; j++) {
93                 State state = new State(i*100 + j);
94                 sensor.getStates().add(state);
95             }
96             Area area = new Area(i);
97             sensor.setArea(area);
98             for(int j=0; j<nbRoomPerArea; j++) {
99                 Room room = new Room(i*100 + j);
100                 area.getRooms().add(room);
101             }
102             pm.makePersistent(sensor);
103         }
104         pm.currentTransaction().commit();
105         
106         pm.currentTransaction().begin();
107         Query q = pm.newQuery(Sensor.class);
108         q.setFilter("((area.rooms.contains(r)) && (r.roomid == rid) && (states.contains(s)) && (s.stateid == sid))");
109         q.declareVariables("Room r; State s");
110         q.declareParameters("int sid, int rid");
111         Collection JavaDoc c = (Collection JavaDoc) q.execute(new Integer JavaDoc(102), new Integer JavaDoc(103));
112         pm.currentTransaction().commit();
113
114         autoClean(pm, classes);
115         pm.close();
116     }
117     
118     private void autoClean(PersistenceManager pm, Class JavaDoc[] classes) {
119         pm.currentTransaction().begin();
120         for(int i=0; i<classes.length; i++) {
121             Extent extent = pm.getExtent(classes[i], false);
122             for(Iterator JavaDoc it = extent.iterator(); it.hasNext();) {
123                 pm.deletePersistent(it.next());
124             }
125             extent.closeAll();
126         }
127         pm.currentTransaction().commit();
128     }
129     
130 }
131
Popular Tags