KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > addressbook > AddressBookImplTest


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: AddressBookImplTest.java,v $
31  * Revision 1.4 2005/04/10 20:09:40 colinmacleod
32  * Added new themes.
33  * Changed id type to String.
34  * Changed i tag to em and b tag to strong.
35  * Improved PicoContainerFactory with NanoContainer scripts.
36  *
37  * Revision 1.3 2005/04/09 17:19:12 colinmacleod
38  * Changed copyright text to GPL v2 explicitly.
39  *
40  * Revision 1.2 2005/03/16 12:45:52 colinmacleod
41  * Fixed AddressBookImpl constructor.
42  *
43  * Revision 1.1.1.1 2005/03/10 17:50:14 colinmacleod
44  * Restructured ivata op around Hibernate/PicoContainer.
45  * Renamed ivata groupware.
46  *
47  * Revision 1.5 2004/11/12 18:17:26 colinmacleod
48  * Ordered imports.
49  *
50  * Revision 1.4 2004/11/12 15:57:08 colinmacleod
51  * Removed dependencies on SSLEXT.
52  * Moved Persistence classes to ivata masks.
53  *
54  * Revision 1.3 2004/11/03 15:39:05 colinmacleod
55  * Changed relationship between person and address:
56  * each person for now has exactly one address.
57  *
58  * Revision 1.2 2004/07/18 21:59:13 colinmacleod
59  * Removed Person from User - now you need to use addressbook/persistence manager to find the person (makes the app run faster.)
60  *
61  * Revision 1.1 2004/07/13 19:41:17 colinmacleod
62  * Moved project to POJOs from EJBs.
63  * Applied PicoContainer to services layer (replacing session EJBs).
64  * Applied Hibernate to persistence layer (replacing entity EJBs).
65  * -----------------------------------------------------------------------------
66  */

67 package com.ivata.groupware.business.addressbook;
68
69 import java.io.File JavaDoc;
70 import java.util.ArrayList JavaDoc;
71 import java.util.Date JavaDoc;
72 import java.util.HashSet JavaDoc;
73 import java.util.Iterator JavaDoc;
74 import java.util.List JavaDoc;
75 import java.util.Properties JavaDoc;
76 import java.util.Set JavaDoc;
77
78 import net.sf.hibernate.HibernateException;
79 import net.sf.hibernate.cfg.Configuration;
80
81 import com.ivata.groupware.admin.AdminTestCase;
82 import com.ivata.groupware.admin.security.server.SecuritySession;
83 import com.ivata.groupware.admin.security.user.UserDO;
84 import com.ivata.groupware.admin.setting.SettingsImpl;
85 import com.ivata.groupware.business.addressbook.address.AddressDO;
86 import com.ivata.groupware.business.addressbook.address.country.CountryDO;
87 import com.ivata.groupware.business.addressbook.person.PersonDO;
88 import com.ivata.groupware.business.addressbook.person.employee.EmployeeDO;
89 import com.ivata.groupware.business.addressbook.person.group.GroupDO;
90 import com.ivata.groupware.business.addressbook.right.AddressBookRightsImpl;
91 import com.ivata.groupware.business.addressbook.telecomaddress.TelecomAddressConstants;
92 import com.ivata.groupware.business.addressbook.telecomaddress.TelecomAddressDO;
93 import com.ivata.groupware.container.persistence.hibernate.HibernateManager;
94 import com.ivata.mask.DefaultMaskFactory;
95 import com.ivata.mask.field.DefaultFieldValueConvertorFactory;
96 import com.ivata.mask.persistence.PersistenceSession;
97 import com.ivata.mask.util.SystemException;
98 import com.ivata.mask.validation.ValidationErrors;
99
100 /**
101  * <p>
102  * Unit test for the address book implementation.
103  * </p>
104  *
105  * @see AddressBookImpl
106  * @author Colin MacLeod
107  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
108  * @since Jun 2, 2004
109  * @version $Revision: 1.4 $
110  */

111 public class AddressBookImplTest extends AdminTestCase {
112     // TODO: remove hard-coded value here!
113
private final static String JavaDoc DEFAULT_GROUP_ID = "7";
114     /**
115      * <p>
116      * Helper called by the constructor to create a valid hibernate
117      * configuration for all the address book's functionality.
118      * </p>
119      *
120      * @return valid hibernate configuration.
121      * @throws HibernateException
122      */

123     private static Configuration getHibernateConfiguration() throws HibernateException {
124         Configuration hibernateConfiguration = new Configuration();
125         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/admin/setting/SettingDO.hbm.xml"));
126         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/admin/security/user/UserDO.hbm.xml"));
127         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/business/addressbook/address/AddressDO.hbm.xml"));
128         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/business/addressbook/address/country/CountryDO.hbm.xml"));
129         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/business/addressbook/person/PersonDO.hbm.xml"));
130         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/business/addressbook/person/group/right/RightDO.hbm.xml"));
131         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/business/addressbook/person/group/right/detail/RightDetailDO.hbm.xml"));
132         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/business/addressbook/person/group/GroupDO.hbm.xml"));
133         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/business/addressbook/person/employee/EmployeeDO.hbm.xml"));
134         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/business/addressbook/telecomaddress/TelecomAddressDO.hbm.xml"));
135         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/navigation/menu/MenuDO.hbm.xml"));
136         hibernateConfiguration.addFile(new File JavaDoc("../hibernate/target/xdoclet/hibernatedoclet/com/ivata/groupware/navigation/menu/item/MenuItemDO.hbm.xml"));
137
138         Properties JavaDoc hibernateProperties = new Properties JavaDoc();
139         hibernateProperties.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
140         hibernateProperties.setProperty("hibernate.connection.URL", "jdbc:postgresql://localhost:5432/portal");
141         hibernateProperties.setProperty("hibernate.connection.username", "postgres");
142         hibernateProperties.setProperty("hibernate.connection.password", "");
143         hibernateProperties.setProperty("hibernate.dialect", "net.sf.hibernate.dialect.PostgreSQLDialect");
144         hibernateProperties.setProperty("hibernate.show_sql", "true");
145         hibernateConfiguration.setProperties(hibernateProperties);
146
147 // hibernateConfiguration.configure(new File("src/test/hibernate.cfg.xml"));
148
return hibernateConfiguration;
149     }
150
151     /**
152      * <p>
153      * This is the implementation instance which will be tested.
154      * </p>
155      */

156     private AddressBookImpl addressBook;
157     GroupDO defaultGroup;
158     /**
159      * <p>
160      * If a test group needs to be created, it is created here.
161      * </p>
162      */

163     List JavaDoc newGroups = new ArrayList JavaDoc();
164
165     /**
166      * <p>
167      * If a test person needs to be created, (s)he is created here.
168      * </p>
169      */

170     List JavaDoc newPeople = new ArrayList JavaDoc();
171
172     /**
173      * Constructor for AddressBookImplTest.
174      * @param arg0
175      */

176     public AddressBookImplTest(String JavaDoc arg0) throws HibernateException {
177         super(arg0);
178     }
179
180
181     /**
182      * <p>
183      * Private helper. Create a test group.
184      * </p>
185      *
186      * @param number number of the group to be created - just use 0 if you're
187      * not sure.
188      */

189     private GroupDO createTestGroup(final GroupDO parent,
190             final int number) throws SystemException {
191         SecuritySession securitySession = getSecuritySession();
192
193         PersonDO groupHead = createTestPerson(number);
194         groupHead.setFileAs("head of group " + number);
195         addressBook.amendPerson(securitySession, groupHead);
196
197         Set JavaDoc groupPeople = new HashSet JavaDoc();
198         groupPeople.add(groupHead);
199
200         GroupDO group = new GroupDO();
201         // get the default address book group
202
group.setDescription("test description " + number);
203         group.setParent(parent);
204         group.setPeople(groupPeople);
205         group.setName("test group " + number);
206         group.setHead(groupHead);
207
208         group = addressBook.addGroup(getSecuritySession(), group);
209         assertNotNull(group.getId());
210         group = addressBook.findGroupByPrimaryKey(securitySession, group.getId());
211         newGroups.add(group);
212
213         return group;
214     }
215
216
217     /**
218      * <p>
219      * Private helper. Create a test person.
220      * </p>
221      *
222      * @param number number of the person to be created - just use 0 if you're
223      * not sure.
224      */

225     private PersonDO createTestPerson(final int number) throws SystemException {
226         SecuritySession securitySession = getSecuritySession();
227         PersonDO person = new PersonDO();
228         person.setUser(null);
229         person.setGroup(defaultGroup);
230         person.setCompany("test company " + number);
231         person.setFileAs("file as " + number);
232         person.setFirstNames("first names " + number);
233         person.setLastName("last name " + number);
234         person.setSalutation("Mr " + number);
235
236         person = addressBook.addPerson(getSecuritySession(), person);
237         assertNotNull(person.getId());
238         person = addressBook.findPersonByPrimaryKey(securitySession, person.getId());
239         newPeople.add(person);
240
241         return person;
242     }
243     /**
244      * @see junit.framework.TestCase#setUp()
245      */

246     protected synchronized void setUp() throws Exception JavaDoc {
247         super.setUp();
248         SecuritySession securitySession = getSecuritySession();
249         addressBook = new AddressBookImpl(getHibernateManager(),
250                 new DefaultMaskFactory(new DefaultFieldValueConvertorFactory()),
251                 new AddressBookRightsImpl(getHibernateManager()),
252                 new SettingsImpl(getHibernateManager()));
253         defaultGroup = addressBook.findGroupByPrimaryKey(securitySession,
254                 DEFAULT_GROUP_ID);
255     }
256
257     /**
258      * @see junit.framework.TestCase#tearDown()
259      */

260     protected void tearDown() throws Exception JavaDoc {
261         super.tearDown();
262         SecuritySession securitySession = getSecuritySession();
263
264         // if there was a group created, remove!
265
Iterator JavaDoc groupIterator = newGroups.iterator();
266         while(groupIterator.hasNext()) {
267             GroupDO group = (GroupDO) groupIterator.next();
268             if (group.getId() == null) {
269                 throw new NullPointerException JavaDoc("Group "
270                     + group
271                     + " has a null id");
272             } else {
273                 addressBook.removeGroup(securitySession, group.getId());
274             }
275         }
276     }
277
278     public void testAddAddressBook() {
279     }
280
281     /**
282      * <p>
283      * Test the <code>addGroup</code> method. A group is added to the default
284      * public group and, if successful, deleted again afterwards.
285      * </p>
286      *
287      * @see AddressBookImpl#addGroup
288      * @throws SystemException
289      */

290     public void testAddGroup() throws SystemException {
291         SecuritySession securitySession = getSecuritySession();
292
293         // add 3 groups - a parend and two sub-groups
294
int totalGroups = 3;
295         GroupDO parentGroup = defaultGroup;
296         Set JavaDoc groupPeople = new HashSet JavaDoc();
297
298         for (int i = 0; i < totalGroups; ++i) {
299             GroupDO group = createTestGroup(parentGroup, i);
300
301             assertNotNull(group.getCreated());
302             assertNotNull(group.getModified());
303             assertNotNull(group.getCreatedBy());
304             assertNotNull(group.getModifiedBy());
305             assertEquals(group.getCreated(), group.getModified());
306             assertEquals(group.getCreatedBy(), group.getModifiedBy());
307             assertEquals("test description " + i, group.getDescription());
308             assertEquals("test group " + i, group.getName());
309             assertNotNull(group.getHead());
310             assertTrue(group.getPeople().size() == 1);
311             assertTrue(group.getPeople().contains(group.getHead()));
312
313             // first group is parent of the others
314
if (i==0) {
315                 parentGroup = group;
316             }
317         }
318     }
319
320     /**
321      * <p>
322      * Test the <code>addPerson</code> method. A person is added and, if
323      * successful, deleted again afterwards.
324      * </p>
325      *
326      * @see AddressBookImpl#addPerson
327      * @throws SystemException
328      */

329     public void testAddPerson() throws SystemException {
330         // get the default address book group
331
SecuritySession securitySession = getSecuritySession();
332
333
334         PersonDO person = createTestPerson(0);
335         assertNotNull(person.getCreated());
336         assertNotNull(person.getModified());
337         assertNotNull(person.getCreatedBy());
338         assertNotNull(person.getModifiedBy());
339         assertEquals(person.getCreated(), person.getModified());
340         assertEquals(person.getCreatedBy(), person.getModifiedBy());
341         assertEquals("test company 0", person.getCompany());
342         assertEquals("file as 0", person.getFileAs());
343         assertEquals("first names 0", person.getFirstNames());
344         assertEquals("last name 0", person.getLastName());
345         assertEquals("Mr 0", person.getSalutation());
346     }
347
348     public void testAddUserGroup() {
349         // TODO
350
}
351
352     public void testAmendGroup() throws SystemException {
353         SecuritySession securitySession = getSecuritySession();
354
355         // first test the basic group fields
356
GroupDO group = createTestGroup(defaultGroup.getParent(), 0);
357         group.setDescription("new test description");
358         group.setName("new name");
359         String JavaDoc id = group.getId();
360         group = addressBook.amendGroup(securitySession, group);
361         assertEquals(id, group.getId());
362         group = addressBook.findGroupByPrimaryKey(securitySession, id);
363         assertEquals("new test description", group.getDescription());
364         assertEquals("new name", group.getName());
365
366         Set JavaDoc people = new HashSet JavaDoc();
367         PersonDO person0 = createTestPerson(0);
368         people.add(person0);
369         group.setHead(person0);
370         group.setPeople(people);
371         group = addressBook.amendGroup(securitySession, group);
372         group = addressBook.findGroupByPrimaryKey(securitySession, id);
373         assertEquals(person0, group.getHead());
374         assertTrue(group.getPeople().size() == 1);
375         assertTrue(group.getPeople().contains(person0));
376
377         // TODO: amend a user group too!
378
}
379
380     public void testAmendPerson() throws SystemException {
381         SecuritySession securitySession = getSecuritySession();
382         HibernateManager hibernateManager = getHibernateManager();
383         PersistenceSession persistenceSession = hibernateManager.openSession(securitySession);
384         try {
385             // get a test person to play with!
386
PersonDO person = createTestPerson(0);
387
388             // set/test the basic things here
389
person.setCompany("new company");
390             person.setFileAs("another file as");
391             person.setFirstNames("new first names");
392             person.setJobTitle("another title");
393             person.setLastName("another last name");
394             person.setSalutation("Mister");
395             String JavaDoc id = person.getId();
396             person = addressBook.amendPerson(securitySession, person);
397             assertEquals(id, person.getId());
398             person = addressBook.findPersonByPrimaryKey(securitySession, id);
399             assertEquals("new company", person.getCompany());
400             assertEquals("another file as", person.getFileAs());
401             assertEquals("new first names", person.getFirstNames());
402             assertEquals("another title", person.getJobTitle());
403             assertEquals("another last name", person.getLastName());
404             assertEquals("Mister", person.getSalutation());
405
406             // countries - for addresses and employee record
407
CountryDO unitedKingdom = (CountryDO) hibernateManager.findInstance(persistenceSession,
408                 "addressBookCountryByCode",
409                 new Object JavaDoc[]{"UK"});
410             CountryDO slovakia = (CountryDO) hibernateManager.findInstance(persistenceSession,
411                 "addressBookCountryByCode",
412                 new Object JavaDoc[]{"SK"});
413
414             // addresses
415
AddressDO address = new AddressDO();
416             address.setCountry(slovakia);
417             address.setPostCode("346436");
418             address.setRegion("Niekeho");
419             address.setStreetAddress("Sturova 47");
420             address.setTown("Banska Bystrica");
421             person.setAddress(address);
422             addressBook.amendPerson(securitySession, person);
423             person = addressBook.findPersonByPrimaryKey(securitySession, id);
424             assertNotNull(person.getAddress());
425             address = person.getAddress();
426             assertEquals(slovakia, address.getCountry());
427             assertEquals("346436", address.getPostCode());
428             assertEquals("Niekeho", address.getRegion());
429             assertEquals("Sturova 47", address.getStreetAddress());
430             assertEquals("Banska Bystrica", address.getTown());
431
432             // date of birth
433
Date JavaDoc now = new Date JavaDoc();
434             person.setDateOfBirth(now);
435             person = addressBook.amendPerson(securitySession, person);
436             person = addressBook.findPersonByPrimaryKey(securitySession, id);
437             assertEquals(now, person.getDateOfBirth());
438
439             // employee
440
EmployeeDO employee = new EmployeeDO();
441             employee.setCountry(unitedKingdom);
442             employee.setNumber("pp12344667");
443             employee.setVacationDays(new Integer JavaDoc(22));
444             person.setEmployee(employee);
445             person = addressBook.amendPerson(securitySession, person);
446             person = addressBook.findPersonByPrimaryKey(securitySession, id);
447             assertNotNull(person.getEmployee());
448             assertEquals(employee, person.getEmployee());
449             assertEquals(unitedKingdom, person.getEmployee().getCountry());
450             assertEquals("pp12344667", person.getEmployee().getNumber());
451             assertEquals(new Integer JavaDoc(22), person.getEmployee().getVacationDays());
452
453             // change the group
454
GroupDO newGroup = createTestGroup(defaultGroup, 99);
455             person.setGroup(newGroup);
456             person = addressBook.amendPerson(securitySession, person);
457             person = addressBook.findPersonByPrimaryKey(securitySession, id);
458             assertNotNull(person.getGroup());
459             assertEquals(newGroup, person.getGroup());
460
461             // add some telecom addresses
462
// needs to come first - you need an email address for a user!!
463
Set JavaDoc telecomAddresses = new HashSet JavaDoc();
464             TelecomAddressDO telecomAddress = new TelecomAddressDO();
465             telecomAddress.setAddress("(020)12334545");
466             telecomAddress.setType(TelecomAddressConstants.TYPE_HOME);
467             telecomAddress.setNumber(0);
468             telecomAddresses.add(telecomAddress);
469             telecomAddress = new TelecomAddressDO();
470             telecomAddress.setAddress("nobody@home.com");
471             telecomAddress.setType(TelecomAddressConstants.TYPE_EMAIL);
472             telecomAddress.setNumber(1);
473             telecomAddresses.add(telecomAddress);
474             person.setTelecomAddresses(telecomAddresses);
475             person = addressBook.amendPerson(securitySession, person);
476             person = addressBook.findPersonByPrimaryKey(securitySession, id);
477             assertNotNull(person.getTelecomAddresses());
478             assertTrue(person.getTelecomAddresses().size() == 2);
479             Iterator JavaDoc telecomAddressIterator = person.getTelecomAddresses().iterator();
480             while (telecomAddressIterator.hasNext()) {
481                 telecomAddress = (TelecomAddressDO) telecomAddressIterator.next();
482                 int number = telecomAddress.getNumber();
483                 assertTrue((number >= 0) && (number < 2));
484                 switch (number) {
485                     case 0:
486                         assertEquals("(020)12334545", telecomAddress.getAddress());
487                         assertTrue(telecomAddress.getType() == TelecomAddressConstants.TYPE_HOME);
488                         break;
489                     case 1:
490                         assertEquals("nobody@home.com", telecomAddress.getAddress());
491                         assertTrue(telecomAddress.getType() == TelecomAddressConstants.TYPE_EMAIL);
492                         break;
493                     default:
494                         // CAN'T HAPPEN!
495
assertTrue(false);
496                         break;
497                 }
498             }
499
500             // change the user
501
UserDO user = new UserDO();
502             user.setDeleted(false);
503             user.setEnabled(true);
504             user.setName("test user");
505             user.setPassword("test password");
506             person.setUser(user);
507             person = addressBook.amendPerson(securitySession, person);
508             person = addressBook.findPersonByPrimaryKey(securitySession, id);
509             assertNotNull(person.getUser());
510             assertFalse(user.isDeleted());
511             assertTrue(user.isEnabled());
512             assertEquals("test user", person.getUser().getName());
513             assertEquals("test password", person.getUser().getPassword());
514         } catch (Exception JavaDoc e) {
515             persistenceSession.cancel();
516             throw new SystemException(e);
517         } finally {
518             persistenceSession.close();
519         }
520     }
521
522     public void testFindAddressBooks() throws SystemException {
523         SecuritySession securitySession = getSecuritySession();
524         HibernateManager hibernateManager = getHibernateManager();
525         PersistenceSession persistenceSession = hibernateManager.openSession(securitySession);
526
527         try {
528             GroupDO privateAddressBook = (GroupDO)
529                 hibernateManager.findByPrimaryKey(persistenceSession,
530                 GroupDO.class, new Integer JavaDoc(501));
531             List JavaDoc allAddressBooks = addressBook.findAddressBooks(securitySession, true);
532             assertTrue(allAddressBooks.size() > 0);
533             assertTrue(allAddressBooks.contains(privateAddressBook));
534             allAddressBooks = addressBook.findAddressBooks(securitySession, false);
535             assertTrue(allAddressBooks.size() > 0);
536             assertFalse(allAddressBooks.contains(privateAddressBook));
537         } catch (Exception JavaDoc e) {
538             persistenceSession.cancel();
539             throw new SystemException(e);
540         } finally {
541             persistenceSession.close();
542         }
543     }
544
545     public void testFindAllCountries() throws SystemException {
546         SecuritySession securitySession = getSecuritySession();
547         HibernateManager hibernateManager = getHibernateManager();
548         PersistenceSession persistenceSession = hibernateManager.openSession(securitySession);
549
550         try {
551             CountryDO germany = (CountryDO) hibernateManager.findInstance(persistenceSession,
552                 "addressBookCountryByCode",
553                 new Object JavaDoc[]{"DE"});
554             CountryDO slovakia = (CountryDO) hibernateManager.findInstance(persistenceSession,
555                 "addressBookCountryByCode",
556                 new Object JavaDoc[]{"SK"});
557             CountryDO unitedKingdom = (CountryDO) hibernateManager.findInstance(persistenceSession,
558                 "addressBookCountryByCode",
559                 new Object JavaDoc[]{"UK"});
560
561             List JavaDoc allCountries = addressBook.findAllCountries(securitySession);
562             assertTrue(allCountries.size() > 0);
563             assertTrue(allCountries.contains(germany));
564             assertTrue(allCountries.contains(slovakia));
565             assertTrue(allCountries.contains(unitedKingdom));
566         } catch (Exception JavaDoc e) {
567             persistenceSession.cancel();
568             throw new SystemException(e);
569         } finally {
570             persistenceSession.close();
571         }
572     }
573
574     public void testFindAllPeopleInGroup() throws SystemException {
575         SecuritySession securitySession = getSecuritySession();
576         PersonDO adminPerson = addressBook.findPersonByUserName(securitySession,
577                 securitySession.getUser().getName());
578
579         List JavaDoc allPeople = addressBook.findAllPeopleInGroup(securitySession, defaultGroup, null);
580         assertTrue(allPeople.size() > 0);
581         assertTrue(allPeople.contains(adminPerson));
582
583         allPeople = addressBook.findAllPeopleInGroup(securitySession, defaultGroup, "a");
584         assertTrue(allPeople.size() > 0);
585         assertTrue(allPeople.contains(adminPerson));
586     }
587
588     public void testFindCountryByCode() throws SystemException {
589         SecuritySession securitySession = getSecuritySession();
590         HibernateManager hibernateManager = getHibernateManager();
591         PersistenceSession persistenceSession = hibernateManager.openSession(securitySession);
592         try {
593             CountryDO germany = (CountryDO) hibernateManager.findInstance(persistenceSession,
594                 "addressBookCountryByCode",
595                 new Object JavaDoc[]{"DE"});
596             CountryDO slovakia = (CountryDO) hibernateManager.findInstance(persistenceSession,
597                 "addressBookCountryByCode",
598                 new Object JavaDoc[]{"SK"});
599             CountryDO unitedKingdom = (CountryDO) hibernateManager.findInstance(persistenceSession,
600                 "addressBookCountryByCode",
601                 new Object JavaDoc[]{"UK"});
602
603             CountryDO germanyCheck = addressBook.findCountryByCode(securitySession, "DE");
604             assertNotNull(germanyCheck);
605             assertEquals(germany, germanyCheck);
606             assertEquals("country.DE", germanyCheck.getName());
607             CountryDO slovakiaCheck = addressBook.findCountryByCode(securitySession, "SK");
608             assertNotNull(slovakiaCheck);
609             assertEquals(slovakia, slovakiaCheck);
610             assertEquals("country.SK", slovakiaCheck.getName());
611             CountryDO unitedKingdomCheck = addressBook.findCountryByCode(securitySession, "UK");
612             assertNotNull(unitedKingdomCheck);
613             assertEquals(unitedKingdom, unitedKingdomCheck);
614             assertEquals("country.UK", unitedKingdomCheck.getName());
615         } catch (Exception JavaDoc e) {
616             persistenceSession.cancel();
617             throw new SystemException(e);
618         } finally {
619             persistenceSession.close();
620         }
621     }
622
623     public void testFindGroupByPrimaryKey() throws SystemException {
624         SecuritySession securitySession = getSecuritySession();
625         GroupDO checkGroup = addressBook.findGroupByPrimaryKey(securitySession, defaultGroup.getId());
626         assertEquals(defaultGroup, checkGroup);
627     }
628
629     public void testFindGroupsByParent() throws SystemException {
630         SecuritySession securitySession = getSecuritySession();
631         List JavaDoc children = addressBook.findGroupsByParent(securitySession, defaultGroup.getParent().getId());
632         assertTrue(children.size() > 0);
633         assertTrue(children.contains(defaultGroup));
634     }
635
636     public void testFindPersonalAddressBook() throws SystemException {
637         SecuritySession securitySession = getSecuritySession();
638         HibernateManager hibernateManager = getHibernateManager();
639         PersistenceSession persistenceSession = hibernateManager.openSession(securitySession);
640         try {
641             GroupDO privateAddressBook = (GroupDO)
642                 hibernateManager.findByPrimaryKey(persistenceSession,
643                 GroupDO.class, new Integer JavaDoc(501));
644             GroupDO checkAddressBook = addressBook.findPersonalAddressBook(securitySession);
645             assertEquals(privateAddressBook, checkAddressBook);
646         } catch (Exception JavaDoc e) {
647             persistenceSession.cancel();
648             throw new SystemException(e);
649         } finally {
650             persistenceSession.close();
651         }
652     }
653
654     public void testFindPersonByPrimaryKey() throws SystemException {
655         SecuritySession securitySession = getSecuritySession();
656         PersonDO userPerson = addressBook.findPersonByUserName(securitySession,
657                 securitySession.getUser().getName());
658         PersonDO checkPerson = addressBook.findPersonByPrimaryKey(securitySession,
659             userPerson.getId());
660         assertEquals(userPerson, checkPerson);
661     }
662
663     public void testFindUserGroups() throws SystemException {
664         SecuritySession securitySession = getSecuritySession();
665         HibernateManager hibernateManager = getHibernateManager();
666         PersistenceSession persistenceSession = hibernateManager.openSession(securitySession);
667         try {
668             List JavaDoc userGroups = addressBook.findUserGroups(securitySession, true);
669
670             // add in the private user group to compare all user groups
671
GroupDO privateUserGroup = (GroupDO)
672                 hibernateManager.findByPrimaryKey(persistenceSession,
673                 GroupDO.class, new Integer JavaDoc(500));
674             assertTrue(userGroups.size() > 0);
675             userGroups.add(privateUserGroup);
676             assertTrue(userGroups.containsAll(securitySession.getUser().getGroups()));
677         } catch (Exception JavaDoc e) {
678             persistenceSession.cancel();
679             throw new SystemException(e);
680         } finally {
681             persistenceSession.close();
682         }
683     }
684
685     /*
686      * Test for ValidationErrors validate(SecuritySession, AddressDO)
687      */

688     public void testValidateSecuritySessionAddressDO() throws SystemException {
689         SecuritySession securitySession = getSecuritySession();
690         HibernateManager hibernateManager = getHibernateManager();
691         PersistenceSession persistenceSession = hibernateManager.openSession(securitySession);
692         try {
693             AddressDO testAddress = new AddressDO();
694             ValidationErrors validationErrors = addressBook.validate(securitySession,
695                 testAddress);
696             assertNotNull(validationErrors);
697             assertFalse(validationErrors.isEmpty());
698
699             /* TODO: need to convert these old-style errors
700             assertTrue(validationErrors.contains(new ValidationError("errors.required",
701                         new ValidationField("address.field.country",
702                         AddressBook.BUNDLE_PATH))));
703             assertTrue(validationErrors.contains(new ValidationError("errors.required",
704                         new ValidationField("address.field.streetAddress",
705                         AddressBook.BUNDLE_PATH))));
706             assertTrue(validationErrors.contains(new ValidationError("errors.required",
707                         new ValidationField("address.field.town",
708                         AddressBook.BUNDLE_PATH))));
709             */

710
711             CountryDO germany = (CountryDO) hibernateManager.findInstance(persistenceSession,
712                 "addressBookCountryByCode",
713                 new Object JavaDoc[]{"DE"});
714             testAddress.setCountry(germany);
715             testAddress.setStreetAddress("nothing");
716             testAddress.setTown("test");
717
718             validationErrors = addressBook.validate(securitySession,
719                         testAddress);
720             assertTrue((validationErrors == null) || validationErrors.isEmpty());
721         } catch (Exception JavaDoc e) {
722             persistenceSession.cancel();
723             throw new SystemException(e);
724         } finally {
725             persistenceSession.close();
726         }
727     }
728
729     /*
730      * Test for ValidationErrors validate(SecuritySession, GroupDO)
731      */

732     public void testValidateSecuritySessionGroupDO() throws SystemException {
733         SecuritySession securitySession = getSecuritySession();
734
735         GroupDO testGroup = new GroupDO();
736         ValidationErrors validationErrors = addressBook.validate(securitySession,
737             testGroup);
738         assertNotNull(validationErrors);
739         assertFalse(validationErrors.isEmpty());
740
741         /* TODO: need to convert these old-style errors
742         assertTrue(validationErrors.contains(new ValidationError("errors.required",
743                     new ValidationField("group.field.name",
744                     AddressBook.BUNDLE_PATH))));
745         */

746
747         testGroup.setName("test");
748         validationErrors = addressBook.validate(securitySession,
749             testGroup);
750         assertTrue((validationErrors == null) || validationErrors.isEmpty());
751     }
752
753     /*
754      * Test for ValidationErrors validate(SecuritySession, PersonDO)
755      */

756     public void testValidateSecuritySessionPersonDO() {
757         SecuritySession securitySession = getSecuritySession();
758
759         PersonDO testPerson = new PersonDO();
760         // user errors only show if the persoon is a user
761
testPerson.setUser(new UserDO());
762         ValidationErrors validationErrors = addressBook.validate(securitySession,
763             testPerson);
764         assertNotNull(validationErrors);
765         assertFalse(validationErrors.isEmpty());
766
767         /* TODO: need to convert these old-style errors
768         assertTrue(validationErrors.contains(new ValidationError("errors.required",
769                     new ValidationField("personDetails.field.fileAs",
770                     AddressBook.BUNDLE_PATH))));
771         assertTrue(validationErrors.contains(new ValidationError("errors.required",
772                     new ValidationField("personDetails.field.firstNames",
773                     AddressBook.BUNDLE_PATH))));
774         assertTrue(validationErrors.contains(new ValidationError("errors.required",
775                     new ValidationField("personDetails.field.lastName",
776                     AddressBook.BUNDLE_PATH))));
777         assertTrue(validationErrors.contains(new ValidationError("errors.required",
778                     new ValidationField("personDetails.field.addressBookName",
779                     AddressBook.BUNDLE_PATH))));
780         // this user has no email address
781         assertTrue(validationErrors.contains(new ValidationError("errors.required",
782                     new ValidationField("user.field.requiredEmailAddress",
783                     AddressBook.BUNDLE_PATH))));
784         assertTrue(validationErrors.contains(new ValidationError("errors.required",
785                     new ValidationField("user.field.userName",
786                     AddressBook.BUNDLE_PATH))));
787         */

788
789
790         testPerson.setFileAs("test");
791         testPerson.setFirstNames("test");
792         testPerson.setLastName("test");
793         testPerson.setGroup(defaultGroup);
794         testPerson.getUser().setName("test");
795         Set JavaDoc telecomAddresses = new HashSet JavaDoc();
796         TelecomAddressDO emailAddress = new TelecomAddressDO();
797         emailAddress.setAddress("test@test.com");
798         emailAddress.setType(TelecomAddressConstants.TYPE_EMAIL);
799         telecomAddresses.add(emailAddress);
800         testPerson.setTelecomAddresses(telecomAddresses);
801
802         validationErrors = addressBook.validate(securitySession,
803             testPerson);
804         assertTrue((validationErrors == null) || validationErrors.isEmpty());
805     }
806
807 }
808
Popular Tags