001: package net.sourceforge.jaxor.example.users.tests;
002:
003: import net.sourceforge.jaxor.QueryParams;
004: import net.sourceforge.jaxor.api.JaxorContext;
005: import net.sourceforge.jaxor.example.tests.MultiTableTestCase;
006: import net.sourceforge.jaxor.example.users.*;
007:
008: import java.util.ArrayList;
009: import java.util.List;
010:
011: /**
012: * Created By: Mike
013: * Date: Nov 22, 2003
014: * Time: 10:17:32 PM
015: *
016: * Last Checkin: $Author: mrettig $
017: * Date: $Date: 2004/02/14 16:29:11 $
018: * Revision: $Revision: 1.17 $
019: */
020: public class ManyToManyTest extends MultiTableTestCase {
021:
022: private static final UserMetaRow userMeta = new UserMetaRow();
023:
024: protected List getRows() {
025: // the test case will setup these tables in the DB.
026: List list = new ArrayList();
027: list.add(new GroupMetaRow());
028: list.add(userMeta);
029: list.add(new GroupMemberMetaRow());
030: return list;
031: }
032:
033: /**
034: * Create a users and a group, then tests various queries to ensure that they resolve the right entities.
035: */
036: public void testCreatingMembers() {
037: UserEntity user = UserFinder.newInstance("user");
038: GroupEntity group = GroupFinder.newInstance("myGroup");
039: group.setAdminUser(user);
040: GroupMemberFinder.newInstance("myGroup", "user");
041: commit();
042: assertEquals(1, user.getGroupList().size());
043: assertEquals(1, group.getUserList().size());
044: assertEquals(user, group.getUserList().get(0));
045: assertEquals(group, user.getGroupList().get(0));
046: assertEquals(user, group.getAdminUser());
047: assertEquals(group.getAdminUser(), group
048: .getAdminUserWithQuery());
049: assertEquals(group.getAdminUser(), UserFinder
050: .selectAdmin("myGroup"));
051: assertEquals(group.getAdminUserWithQuery(), group
052: .getAdminUserWithFind());
053:
054: UserList userListWithFind = group.getUserListWithFind();
055: userListWithFind.size();
056: assertEquals(userListWithFind, group.getUserList());
057: assertEquals(0, group.getNonAdminUsers().size());
058:
059: UserFinder.newInstance("non-admin-user");
060: //add the user to the group.
061: GroupMemberFinder.newInstance("myGroup", "non-admin-user");
062: commit();
063: assertEquals(2, GroupMemberFinder.selectByGroupNameList(
064: "myGroup").size());
065: // We need to refetch the group since we've inserted into the DB. The prior group instance
066: // caches the results of references.
067: group = GroupFinder.selectByGroupName("myGroup");
068: assertEquals(2, group.getUserList().size());
069: assertEquals(1, group.getNonAdminUsers().size());
070: assertEquals(user, group.getAdminUser());
071:
072: UserList genUserList = UserFinder.selectAdminsOfGroupsList();
073: assertEquals(1, genUserList.size());
074: //This is the equivalent query if written by hand.
075: UserList userList = UserFinder
076: .query("select * from user_table u, groups g where u.user_name=g.admin_user_name");
077: assertEquals(1, userList.size());
078: assertEquals(genUserList, userList);
079: }
080:
081: public void testSelectingUsersOrderedByGroup() {
082: UserEntity user = UserFinder.newInstance("user");
083: GroupEntity group = GroupFinder.newInstance("myGroup");
084: GroupMemberFinder.newInstance("myGroup", "user");
085: group.setAdminUser(user);
086: UserEntity user2 = UserFinder.newInstance("andrew");
087: GroupEntity group2 = GroupFinder.newInstance("admin");
088: GroupMemberFinder.newInstance("admin", "andrew");
089: group2.setAdminUser(user2);
090: commit();
091:
092: UserList usersByGroup = UserFinder.selectByGroupList();
093: assertEquals(2, usersByGroup.size());
094: assertEquals(user2, usersByGroup.get(0));
095: assertEquals(user, usersByGroup.get(1));
096: }
097:
098: public void testFinderExamples() {
099: UserFinder.newInstance("mike");
100: commit();
101: JaxorContext jaxorContext = getJaxor();
102: UserFinderBase finder = new UserFinderBase(jaxorContext);
103: UserEntity user = finder.selectByUserName("mike");
104:
105: user = finder.findUnique("where user_name='mike'", false);
106:
107: QueryParams params = new QueryParams();
108: params.addString("mike");
109: user = finder.findUnique("where user_name=?", params, false);
110:
111: user = finder.queryUnique("select * from "
112: + userMeta.getTableName() + " where user_name='mike'",
113: false);
114:
115: UserQuery userQuery = finder
116: .asFindQuery("where user_name='mike'");
117: userQuery.entity();
118:
119: userQuery = finder.asQuery("select * from "
120: + userMeta.getTableName() + " where user_name='mike'");
121: userQuery.entity();
122: user = userQuery.entityProxy();
123: user.getUserName();
124: UserList list = userQuery.list();
125: assertEquals(1, list.size());
126: UserResultSet userRS = userQuery.resultSet();
127: try {
128: assertTrue(userRS.hasNext());
129: userRS.next();
130: } finally {
131: userRS.close();
132: }
133: }
134:
135: public void testSettingEntityRefToNull() {
136: GroupEntity group = new GroupBase();
137: group.setAdminUser(null);
138: assertNull(group.getAdminUser());
139: }
140: }
|