001: package info.jtrac;
002:
003: import info.jtrac.domain.Config;
004: import info.jtrac.domain.Counts;
005: import info.jtrac.domain.CountsHolder;
006: import info.jtrac.domain.Field;
007: import info.jtrac.domain.Item;
008: import info.jtrac.domain.ItemItem;
009: import info.jtrac.domain.Metadata;
010: import info.jtrac.domain.Space;
011: import info.jtrac.domain.User;
012: import info.jtrac.domain.State;
013: import info.jtrac.domain.UserSpaceRole;
014: import info.jtrac.util.ItemUtils;
015: import java.util.HashSet;
016: import java.util.List;
017: import java.util.Map;
018: import java.util.Set;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.userdetails.UserDetails;
022:
023: /**
024: * JUnit test cases for the business implementation as well as the DAO
025: * Tests assume that a database is available, and with HSQLDB around this is not
026: * an issue.
027: */
028: public class JtracTest extends JtracTestBase {
029:
030: private Space getSpace() {
031: Space space = new Space();
032: space.setPrefixCode("TEST");
033: space.setName("Test Space");
034: return space;
035: }
036:
037: private Metadata getMetadata() {
038: Metadata metadata = new Metadata();
039: String xmlString = "<metadata><fields>"
040: + "<field name='cusInt01' label='Test Label'/>"
041: + "<field name='cusInt02' label='Test Label 2'/>"
042: + "</fields></metadata>";
043: metadata.setXmlString(xmlString);
044: return metadata;
045: }
046:
047: //==========================================================================
048:
049: public void testGeneratedPasswordIsAlwaysDifferent() {
050: String p1 = jtrac.generatePassword();
051: String p2 = jtrac.generatePassword();
052: assertTrue(!p1.equals(p2));
053: }
054:
055: public void testEncodeClearTextPassword() {
056: assertEquals("21232f297a57a5a743894a0e4a801fc3", jtrac
057: .encodeClearText("admin"));
058: }
059:
060: public void testMetadataInsertAndLoad() {
061: Metadata m1 = getMetadata();
062: jtrac.storeMetadata(m1);
063: assertTrue(m1.getId() > 0);
064: Metadata m2 = jtrac.loadMetadata(m1.getId());
065: assertTrue(m2 != null);
066: Map<Field.Name, Field> fields = m2.getFields();
067: assertTrue(fields.size() == 2);
068: }
069:
070: public void testUserInsertAndLoad() {
071: User user = new User();
072: user.setLoginName("test");
073: user.setEmail("test@jtrac.com");
074: jtrac.storeUser(user);
075: User user1 = jtrac.loadUser("test");
076: assertTrue(user1.getEmail().equals("test@jtrac.com"));
077: User user2 = dao.findUsersByEmail("test@jtrac.com").get(0);
078: assertTrue(user2.getLoginName().equals("test"));
079: }
080:
081: public void testUserSpaceRolesInsert() {
082: Space space = getSpace();
083: Metadata metadata = getMetadata();
084:
085: space.setMetadata(metadata);
086: jtrac.storeSpace(space);
087:
088: User user = new User();
089: user.setLoginName("test");
090:
091: user.addSpaceWithRole(space, "ROLE_TEST");
092: jtrac.storeUser(user);
093:
094: User u1 = jtrac.loadUser("test");
095:
096: GrantedAuthority[] gas = u1.getAuthorities();
097: assertEquals(2, gas.length);
098: assertEquals("ROLE_USER", gas[0].getAuthority());
099: assertEquals("ROLE_TEST_TEST", gas[1].getAuthority());
100:
101: List<UserSpaceRole> userSpaceRoles = jtrac
102: .findUserRolesForSpace(space.getId());
103: assertEquals(1, userSpaceRoles.size());
104: UserSpaceRole usr = userSpaceRoles.get(0);
105: assertEquals("test", usr.getUser().getLoginName());
106: assertEquals("ROLE_TEST", usr.getRoleKey());
107:
108: List<User> users = jtrac.findUsersForUser(u1);
109: assertEquals(1, users.size());
110:
111: List<User> users2 = jtrac.findUsersForSpace(space.getId());
112: assertEquals(1, users2.size());
113:
114: }
115:
116: public void testConfigStoreAndLoad() {
117: Config config = new Config("testParam", "testValue");
118: jtrac.storeConfig(config);
119: String value = jtrac.loadConfig("testParam");
120: assertEquals("testValue", value);
121: }
122:
123: public void testStoreAndLoadUserWithAdminRole() {
124: User user = new User();
125: user.setLoginName("test");
126: user.addSpaceWithRole(null, "ROLE_ADMIN");
127: jtrac.storeUser(user);
128:
129: UserDetails ud = jtrac.loadUserByUsername("test");
130:
131: Set<String> set = new HashSet<String>();
132: for (GrantedAuthority ga : ud.getAuthorities()) {
133: set.add(ga.getAuthority());
134: }
135:
136: assertEquals(2, set.size());
137: assertTrue(set.contains("ROLE_USER"));
138: assertTrue(set.contains("ROLE_ADMIN"));
139:
140: }
141:
142: public void testDefaultAdminUserHasAdminRole() {
143: UserDetails ud = jtrac.loadUserByUsername("admin");
144: Set<String> set = new HashSet<String>();
145: for (GrantedAuthority ga : ud.getAuthorities()) {
146: set.add(ga.getAuthority());
147: }
148: assertEquals(2, set.size());
149: assertTrue(set.contains("ROLE_USER"));
150: assertTrue(set.contains("ROLE_ADMIN"));
151: }
152:
153: public void testItemInsertAndCounts() {
154: Space s = getSpace();
155: jtrac.storeSpace(s);
156: User u = new User();
157: u.setLoginName("test");
158: u.addSpaceWithRole(s, "DEFAULT");
159: jtrac.storeUser(u);
160: Item i = new Item();
161: i.setSpace(s);
162: i.setAssignedTo(u);
163: i.setLoggedBy(u);
164: i.setStatus(State.CLOSED);
165: jtrac.storeItem(i, null);
166: assertEquals(1, i.getSequenceNum());
167:
168: CountsHolder ch = jtrac.loadCountsForUser(u);
169: assertEquals(1, ch.getTotalAssignedToMe());
170: assertEquals(1, ch.getTotalLoggedByMe());
171: assertEquals(1, ch.getTotalTotal());
172:
173: Counts c = ch.getCounts().get(s.getId());
174: assertEquals(1, c.getLoggedByMe());
175: assertEquals(1, c.getAssignedToMe());
176: assertEquals(1, c.getTotal());
177: }
178:
179: public void testRemoveSpaceRoleDoesNotOrphanDatabaseRecord() {
180: Space space = getSpace();
181: jtrac.storeSpace(space);
182: long spaceId = space.getId();
183: User user = new User();
184: user.setLoginName("test");
185: user.addSpaceWithRole(space, "ROLE_ADMIN");
186: jtrac.storeUser(user);
187: long id = jdbcTemplate
188: .queryForLong("select id from user_space_roles where space_id = "
189: + spaceId);
190: UserSpaceRole usr = jtrac.loadUserSpaceRole(id);
191: assertEquals(spaceId, usr.getSpace().getId());
192: jtrac.removeUserSpaceRole(usr);
193: endTransaction();
194: assertEquals(
195: 0,
196: jdbcTemplate
197: .queryForInt("select count(0) from user_space_roles where space_id = "
198: + spaceId));
199: }
200:
201: public void testFindSpacesWhereGuestAllowed() {
202: Space space = getSpace();
203: space.setGuestAllowed(true);
204: jtrac.storeSpace(space);
205: assertEquals(1, jtrac.findSpacesWhereGuestAllowed().size());
206: }
207:
208: public void testRenameSpaceRole() {
209: Space space = getSpace();
210: jtrac.storeSpace(space);
211: User u = new User();
212: u.setLoginName("test");
213: u.addSpaceWithRole(space, "DEFAULT");
214: jtrac.storeUser(u);
215: assertEquals(
216: 1,
217: jdbcTemplate
218: .queryForInt("select count(0) from user_space_roles where role_key = 'DEFAULT'"));
219: jtrac.bulkUpdateRenameSpaceRole(space, "DEFAULT", "NEWDEFAULT");
220: assertEquals(
221: 0,
222: jdbcTemplate
223: .queryForInt("select count(0) from user_space_roles where role_key = 'DEFAULT'"));
224: assertEquals(
225: 1,
226: jdbcTemplate
227: .queryForInt("select count(0) from user_space_roles where role_key = 'NEWDEFAULT'"));
228: }
229:
230: public void testGetItemAsHtmlDoesNotThrowException() {
231: Config config = new Config("mail.server.host", "dummyhost");
232: jtrac.storeConfig(config);
233: // now email sending is switched on
234: Space s = getSpace();
235: jtrac.storeSpace(s);
236: User u = new User();
237: u.setLoginName("test");
238: u.setName("Test User");
239: u.setEmail("test");
240: u.addSpaceWithRole(s, "DEFAULT");
241: jtrac.storeUser(u);
242: Item i = new Item();
243: i.setSpace(s);
244: i.setAssignedTo(u);
245: i.setLoggedBy(u);
246: i.setStatus(State.CLOSED);
247: // next step will internally try to render item as Html for sending e-mail
248: jtrac.storeItem(i, null);
249: System.out.println(ItemUtils.getAsXml(i).asXML());
250: }
251:
252: public void testDeleteItemThatHasRelatedItems() {
253: Space s = getSpace();
254: jtrac.storeSpace(s);
255: User u = new User();
256: u.setLoginName("test");
257: u.setEmail("dummy");
258: u.addSpaceWithRole(s, "DEFAULT");
259: jtrac.storeUser(u);
260: //========================
261: Item i0 = new Item();
262: i0.setSpace(s);
263: i0.setAssignedTo(u);
264: i0.setLoggedBy(u);
265: i0.setStatus(State.CLOSED);
266: jtrac.storeItem(i0, null);
267: //=======================
268: Item i1 = new Item();
269: i1.setSpace(s);
270: i1.setAssignedTo(u);
271: i1.setLoggedBy(u);
272: i1.setStatus(State.CLOSED);
273: i1.addRelated(i0, ItemItem.DEPENDS_ON);
274: jtrac.storeItem(i1, null);
275: //========================
276: Item i2 = new Item();
277: i2.setSpace(s);
278: i2.setAssignedTo(u);
279: i2.setLoggedBy(u);
280: i2.setStatus(State.CLOSED);
281: i2.addRelated(i1, ItemItem.DUPLICATE_OF);
282: jtrac.storeItem(i2, null);
283: assertEquals(3, jtrac.loadCountOfHistoryInvolvingUser(u));
284: // can we remove i1?
285: Item temp = jtrac.loadItem(i1.getId());
286: jtrac.removeItem(temp);
287: }
288:
289: }
|