1 5 package org.exoplatform.services.organization.hibernate; 6 7 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 import net.sf.hibernate.Transaction; 11 import org.exoplatform.commons.utils.PageList; 12 import org.exoplatform.container.PortalContainer; 13 import org.exoplatform.services.database.HibernateService; 14 import org.exoplatform.services.log.LogService; 15 import org.exoplatform.services.log.LogUtil; 16 import org.exoplatform.services.organization.*; 17 import org.exoplatform.services.organization.impl.GroupImpl; 18 import org.exoplatform.services.organization.impl.MembershipImpl; 19 import org.exoplatform.test.BasicTestCase; 20 27 public class TestHibernateOrganizationService extends BasicTestCase{ 28 29 private static OrganizationService service; 30 private static HibernateService hservice_ ; 31 32 public TestHibernateOrganizationService(String s) { 33 super(s); 34 } 35 36 protected String getDescription() { 37 return "test hibernate organization service"; 38 } 39 40 public void setUp() throws Exception { 41 if (service == null) { 42 PortalContainer manager = PortalContainer.getInstance(); 43 service = (OrganizationService) manager.getComponentInstanceOfType(OrganizationService.class); 44 LogUtil.setLevel("org.exoplatform.services.organization", LogService.DEBUG, true) ; 45 LogUtil.setLevel("org.exoplatform.services.database", LogService.DEBUG, true) ; 46 hservice_ = 47 (HibernateService) manager.getComponentInstanceOfType(HibernateService.class) ; 48 } 49 } 50 51 public void tearDown() throws Exception { 52 hservice_.closeSession(); 53 } 54 55 public void testUser() throws Exception { 56 net.sf.hibernate.Session session = hservice_.openSession() ; 57 String USER = "test" ; 58 Transaction tx = session.beginTransaction() ; 59 User user = service.createUserInstance() ; 60 user.setUserName(USER) ; 61 user.setPassword("exo") ; 62 user.setFirstName("Exo") ; 63 user.setLastName("Platform") ; 64 user.setEmail("exo@exoportal.org") ; 65 service.createUser(user); 66 tx.commit(); 67 68 User u = service.findUserByName(USER); 69 assertEquals(u.getFirstName(),"Exo"); 70 UserProfile up = service.findUserProfileByName(USER); 71 assertTrue("Expect user profile is found: ", up != null); 72 73 Query query = new Query() ; 74 PageList users = service.findUsers(query) ; 75 assertTrue("Expect 1 user found ", users.getAvailable() >= 1); 76 77 u.setFirstName("Exo(Update)"); 78 service.saveUser(u); 79 up.getUserInfoMap().put("user.gender", "male"); 80 service.saveUserProfile(up); 81 up = service.findUserProfileByName(USER); 82 String male = (String ) up.getUserInfoMap().get("user.gender"); 83 assertEquals("Expect user profile is updated: ", "male", male); 84 85 PageList piterator = service.getUserPageList(10) ; 86 assertTrue (piterator.currentPage().size() > 1) ; 87 88 service.removeUser(USER); 89 } 90 91 public void testGroup() throws Exception { 92 Group groupParent = service.createGroupInstance() ; 93 groupParent.setGroupName("GroupParent"); 94 service.createGroup(groupParent); 95 assertTrue(((GroupImpl)groupParent).getId() != null); 96 97 Group groupChild = service.createGroupInstance() ; 98 groupChild.setGroupName("Group"); 99 100 service.addChild(groupParent, groupChild); 101 102 groupParent = service.findGroupById(groupParent.getId()); 103 assertEquals(groupParent.getGroupName(),"GroupParent"); 104 105 groupChild = service.findGroupById(groupChild.getId()); 106 assertEquals(groupChild.getParentId(), groupParent.getId()); 107 108 groupChild.setLabel("GroupRenamed"); 109 service.saveGroup(groupChild); 110 assertEquals(service.findGroupById(groupChild.getId()).getLabel(),"GroupRenamed"); 111 112 service.removeGroup(groupChild); 113 assertNotNull(service.findGroupById(groupParent.getId())); 114 115 service.addChild(groupParent, groupChild); 116 Collection groups = service.findGroups(groupParent) ; 117 assertEquals(1, groups.size()); 118 119 service.removeGroup(groupParent); 120 assertNull(service.findGroupById(groupParent.getId())); 121 assertNull(service.findGroupById(groupChild.getId())); 122 } 123 124 public void testMembership() throws Exception { 125 MembershipType mt = service.createMembershipTypeInstance(); 126 mt.setName("Hierarchical") ; 127 mt.setDescription("This is a test") ; 128 mt.setOwner("exo") ; 129 service.createMembershipType(mt); 130 mt = service.findMembershipType("Hierarchical"); 131 assertTrue("Membership is found", mt != null ) ; 132 mt.setDescription("This is a test (update)") ; 133 service.saveMembershipType(mt); 134 mt = service.findMembershipType("Hierarchical"); 135 assertTrue("Membership is found", mt != null ) ; 136 137 User user = service.createUserInstance() ; 138 user.setUserName("benj") ; 139 user.setPassword("benj") ; 140 user.setFirstName("benjamin") ; 141 user.setLastName("mestrallet") ; 142 user.setEmail("benjmestrallet@users.sourceforge.net") ; 143 service.createUser(user); 144 145 Group group = service.createGroupInstance() ; 146 group.setGroupName("Group"); 147 service.createGroup(group); 148 149 Membership membership = service.createMembershipInstance(); 150 membership.setMembershipType("Hierarchical"); 151 service.createMembership(membership); 152 assertTrue(((MembershipImpl)membership).getId() != null); 153 service.linkMembership("benj", group, membership); 154 Collection groups = service.findGroupByMembership("benj", "Hierarchical") ; 155 assertEquals("expect 1 group " , 1, groups.size()); 156 157 Collection c = service.findMembershipsByUserAndGroup("benj", group.getId()); 158 assertTrue(c.size() > 0); 159 160 mt = service.removeMembershipType("Hierarchical"); 161 mt = service.findMembershipType("Hierarchical"); 162 assertTrue("Membership is not found", mt == null ) ; 163 Collection memberships = service.findMembershipsByUserAndGroup("benj", group.getId()); 165 assertTrue(memberships.size() == 0); 166 167 membership = service.createMembershipInstance(); 168 membership.setMembershipType("Hierarchical"); 169 service.createMembership(membership); 170 service.linkMembership("benj", group, membership); 171 172 groups = service.findGroupsOfUser("benj"); 173 assertEquals("expect 2 group: " , 2, groups.size()); 174 groups = service.findGroupByMembership("benj", "Hierarchical") ; 175 Iterator i = groups.iterator() ; 176 while(i.hasNext()) { 177 Group g = (Group) i.next() ; 178 System.out.println("-----------------------" + group.getId()) ; 179 } 180 assertEquals("expect one group: " , 1, groups.size()) ; 181 group = (Group)groups.iterator().next() ; 182 assertEquals("find group with group name 'Group'" , "Group", group.getGroupName()); 183 184 service.removeGroup(group); 185 memberships = service.findMembershipsByUserAndGroup("benj", "Group"); 186 assertTrue(memberships.size() == 0); 187 service.removeUser("benj"); 189 memberships = service.findMembershipsByUser("benj"); 190 assertEquals("Check system role: ",0, memberships.size()); 191 } 192 193 public void testGroovyScript() throws Exception { 194 Iterator i = PortalContainer.getInstance(). 195 getComponentInstancesOfType(UserEventListener .class).iterator() ; 196 boolean hasGroovyListener = false ; 197 while(i.hasNext()) { 198 Object obj = i.next() ; 199 if(obj.getClass().getName().indexOf("Groovy") >= 0) { 200 hasGroovyListener = true ; 201 } 202 System.out.println("user event listener : " + obj) ; 203 } 204 if(!hasGroovyListener) { 205 fail("expect the groovy listener example") ; 206 } 207 } 208 } | Popular Tags |