Source Code Cross Referenced for FileSystemGroupsTest.java in  » Portal » uPortal_rel-2-6-1-GA » org » jasig » portal » groups » filesystem » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » uPortal_rel 2 6 1 GA » org.jasig.portal.groups.filesystem 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2003 The JA-SIG Collaborative.  All rights reserved.
002:         *  See license distributed with this file and
003:         *  available online at http://www.uportal.org/license.html
004:         */
005:
006:        package org.jasig.portal.groups.filesystem;
007:
008:        import java.io.BufferedWriter;
009:        import java.io.File;
010:        import java.io.FileWriter;
011:        import java.sql.Connection;
012:        import java.util.ArrayList;
013:        import java.util.Collection;
014:        import java.util.Iterator;
015:        import java.util.List;
016:        import java.util.Map;
017:
018:        import javax.sql.DataSource;
019:
020:        import junit.framework.TestCase;
021:        import junit.framework.TestSuite;
022:
023:        import org.jasig.portal.EntityIdentifier;
024:        import org.jasig.portal.EntityTypes;
025:        import org.jasig.portal.groups.EntityImpl;
026:        import org.jasig.portal.groups.GroupServiceConfiguration;
027:        import org.jasig.portal.groups.GroupsException;
028:        import org.jasig.portal.groups.IEntity;
029:        import org.jasig.portal.groups.IEntityGroup;
030:        import org.jasig.portal.groups.IEntityGroupStore;
031:        import org.jasig.portal.groups.IGroupConstants;
032:        import org.jasig.portal.groups.IGroupMember;
033:        import org.jasig.portal.rdbm.TransientDatasource;
034:
035:        /**
036:         * This class was rewritten to eliminate external dependencies, chiefly
037:         * on the composite group service.  Although this was mostly achieved,
038:         * 2 dependencies remain.  It needs a file system to read via java.io, 
039:         * since this is what is being tested, and it requires a composite
040:         * group service configuration document.  I will eventually remove these
041:         * dependencies but it seemed better to get the test in now.  I was 
042:         * thinking we could eventually use something like Apache Commons VFS 
043:         * to set up a virtual file system (a future enhancement for the 
044:         * FileSystem group service).  In the meantime, this class must create a 
045:         * GROUPS_ROOT directory and write to it.  The class first tries to 
046:         * create the directory in the user.home.  If unsuccessful, it tries to 
047:         * create it in the current directory, and if this is unsuccessful, 
048:         * it dies.     
049:         *
050:         * @author Dan Ellentuck
051:         * @version $Revision: 35608 $
052:         */
053:        public class FileSystemGroupsTest extends TestCase {
054:            private static Class GROUP_CLASS;
055:            private static Class IPERSON_CLASS;
056:            private static String CR = "\n";
057:
058:            private String[] testEntityKeys;
059:            private String[] testFileNames;
060:            private List testGroupKeys;
061:            private int numTestFiles;
062:            private int numTestEntities;
063:
064:            private String GROUPS_ROOT;
065:            private File groupsRoot;
066:            private String IPERSON_GROUPS_ROOT;
067:            private List allFiles = null, directoryFiles = null,
068:                    keyFiles = null;
069:            private String NON_EXISTENT_ID = "xyzxyzxyz";
070:            private IEntityGroupStore groupStore;
071:            private String GROUP_SEPARATOR;
072:
073:            private DataSource testDataSource;
074:
075:            /**
076:             * FileSystemGroupsTest.
077:             */
078:            public FileSystemGroupsTest(String name) {
079:                super (name);
080:            }
081:
082:            /**
083:             */
084:            protected void addIdsToFile(File f) {
085:                long now = System.currentTimeMillis() / 10;
086:                long div = now % 5;
087:                div += 5;
088:
089:                try {
090:                    String line = null, start = null;
091:                    BufferedWriter bw = new BufferedWriter(new FileWriter(f));
092:
093:                    bw.write("# test file written at " + new java.util.Date());
094:                    bw.newLine();
095:                    bw.write("#");
096:                    bw.newLine();
097:
098:                    for (int i = 0; i < numTestEntities; i++) {
099:                        start = ((i > 0) && (i % div == 0)) ? "   " : "";
100:                        line = start + testEntityKeys[i] + " is entity "
101:                                + (i + 1);
102:                        bw.write(line);
103:                        bw.newLine();
104:                    }
105:
106:                    bw.write("# end of test file ");
107:                    bw.newLine();
108:                    bw.write("#");
109:                    bw.newLine();
110:
111:                    bw.close();
112:
113:                } // end try
114:                catch (Exception ex) {
115:                    print("FileSystemGroupsTest.addIdsToFile(): "
116:                            + ex.getMessage());
117:                }
118:            }
119:
120:            /**
121:             * @return org.jasig.portal.groups.IEntityGroup
122:             */
123:            private IEntityGroup findGroup(File file) throws GroupsException {
124:                String key = getKeyFromFile(file);
125:                return findGroup(key);
126:            }
127:
128:            /**
129:             * Note that this is the local, not composite, key.
130:             * @return org.jasig.portal.groups.IEntityGroup
131:             */
132:            private IEntityGroup findGroup(String key) throws GroupsException {
133:                return getGroupStore().find(key);
134:            }
135:
136:            /**
137:             * @return org.jasig.portal.services.GroupService
138:             */
139:            private Collection getGroupMembers(IGroupMember gm)
140:                    throws GroupsException {
141:                Collection list = new ArrayList();
142:                for (Iterator itr = gm.getMembers(); itr.hasNext();) {
143:                    list.add(itr.next());
144:                }
145:                return list;
146:            }
147:
148:            private File getGroupsRoot() {
149:                if (groupsRoot == null) {
150:                    groupsRoot = createGroupsRoot();
151:                }
152:                return groupsRoot;
153:            }
154:
155:            /**
156:             * Try to create the groups root directory in the user.home and if
157:             * not possible try in the current directory.
158:             * @return java.io.File
159:             */
160:            private File createGroupsRoot() {
161:                File gr = null;
162:                String userHome = System.getProperty("user.home");
163:                if (userHome != null) {
164:                    File uh = new File(userHome);
165:                    if (uh.exists() && uh.canWrite()) {
166:                        gr = new File(uh.getPath() + File.separator
167:                                + "GROUPS-ROOT");
168:                    }
169:                } else {
170:                    gr = new File("GROUPS-ROOT");
171:                }
172:
173:                return (gr.mkdir()) ? gr : null;
174:            }
175:
176:            /**
177:             * @return FileSystemGroupStore
178:             */
179:            private FileSystemGroupStore getGroupStore() throws GroupsException {
180:                if (groupStore == null) {
181:                    GroupServiceConfiguration config = new GroupServiceConfiguration();
182:                    Map atts = config.getAttributes();
183:                    atts.put("nodeSeparator", IGroupConstants.NODE_SEPARATOR);
184:                    groupStore = new FileSystemGroupStore(config);
185:                }
186:                return (FileSystemGroupStore) groupStore;
187:            }
188:
189:            /**
190:             * @return org.jasig.portal.groups.IEntityGroup
191:             */
192:            private String getKeyFromFile(File file) throws GroupsException {
193:                String key = file.getPath();
194:                if (key.startsWith(GROUPS_ROOT)) {
195:                    key = key.substring(GROUPS_ROOT.length());
196:                    if (GROUP_SEPARATOR.equals(String
197:                            .valueOf(FileSystemGroupStore.PERIOD))) {
198:                        key = key.replace(FileSystemGroupStore.PERIOD,
199:                                FileSystemGroupStore.SUBSTITUTE_PERIOD);
200:                    }
201:                }
202:                return key;
203:            }
204:
205:            /**
206:             * @return org.jasig.portal.groups.IEntity
207:             */
208:            private IEntity getNewIPersonEntity(String key)
209:                    throws GroupsException {
210:                return getNewEntity(IPERSON_CLASS, key);
211:            }
212:
213:            /**
214:             * @return org.jasig.portal.groups.IEntity
215:             */
216:            private IEntity getNewEntity(Class type, String key)
217:                    throws GroupsException {
218:                return new EntityImpl(key, type);
219:            }
220:
221:            /**
222:             *  @return java.lang.String
223:             * @param length int
224:             */
225:            private String getRandomString(java.util.Random r, int length) {
226:
227:                char[] chars = new char[length];
228:
229:                for (int i = 0; i < length; i++) {
230:                    int diff = (r.nextInt(25));
231:                    int charValue = (int) 'A' + diff;
232:                    chars[i] = (char) charValue;
233:                }
234:                return new String(chars);
235:            }
236:
237:            /**
238:             * Starts the application.
239:             * @param args an array of command-line arguments
240:             */
241:            public static void main(java.lang.String[] args) throws Exception {
242:                String[] mainArgs = { "org.jasig.portal.groups.filesystem.FileSystemGroupsTest" };
243:                print("START TESTING FILESYSTEM GROUP STORE" + CR);
244:                junit.swingui.TestRunner.main(mainArgs);
245:                print(CR + "END TESTING FILESYSTEM GROUP STORE");
246:            }
247:
248:            /**
249:             * @param msg java.lang.String
250:             */
251:            private static void print(String msg) {
252:                java.sql.Timestamp ts = new java.sql.Timestamp(System
253:                        .currentTimeMillis());
254:                System.out.println(ts + " : " + msg);
255:            }
256:
257:            /**
258:             */
259:            protected void setUp() {
260:                //    print("Entering FilesystemGroupsTester.setUp()");
261:                try {
262:                    if (GROUP_CLASS == null) {
263:                        GROUP_CLASS = Class
264:                                .forName("org.jasig.portal.groups.IEntityGroup");
265:                    }
266:                    if (IPERSON_CLASS == null) {
267:                        IPERSON_CLASS = Class
268:                                .forName("org.jasig.portal.security.IPerson");
269:                    }
270:
271:                    numTestEntities = 10;
272:                    numTestFiles = 2;
273:                    allFiles = new ArrayList();
274:                    directoryFiles = new ArrayList();
275:                    keyFiles = new ArrayList();
276:
277:                    char sep = getGroupStore().getGoodSeparator();
278:                    File groupsRootDir, iPersonGroupsRootDir;
279:                    String fileName = null;
280:                    File f = null, ff = null, fff = null;
281:
282:                    int i = 0, j = 0, k = 0;
283:                    int totalNumTestFiles = numTestFiles + numTestFiles
284:                            * numTestFiles + numTestFiles * numTestFiles
285:                            * numTestFiles;
286:
287:                    // Entities and their keys:
288:                    testEntityKeys = new String[numTestEntities];
289:                    java.util.Random random = new java.util.Random();
290:                    for (i = 0; i < numTestEntities; i++) {
291:                        testEntityKeys[i] = (getRandomString(random, 3) + i);
292:                    }
293:
294:                    // File names:
295:                    testFileNames = new String[totalNumTestFiles];
296:                    random = new java.util.Random();
297:                    for (i = 0; i < totalNumTestFiles; i++) {
298:                        testFileNames[i] = (getRandomString(random, 3) + i);
299:                    }
300:
301:                    // GroupKeys:
302:                    testGroupKeys = new ArrayList();
303:
304:                    // Create directory structure:
305:                    File gr = getGroupsRoot();
306:                    if (gr == null) {
307:                        print("COULD NOT CREATE GROUPS ROOT DIRECTORY!!!");
308:                        print("You must have WRITE permission on either user.home or the current directory.");
309:                        throw new RuntimeException(
310:                                "Could not create groups root directory.");
311:                    }
312:                    String tempGroupsRoot = gr.getAbsolutePath();
313:                    getGroupStore().setGroupsRootPath(tempGroupsRoot);
314:                    GROUPS_ROOT = getGroupStore().getGroupsRootPath();
315:
316:                    GROUP_SEPARATOR = IGroupConstants.NODE_SEPARATOR;
317:
318:                    // initialize composite service:
319:                    // GroupService.findGroup("local" + GROUP_SEPARATOR + "0");
320:
321:                    IPERSON_GROUPS_ROOT = GROUPS_ROOT + IPERSON_CLASS.getName();
322:                    iPersonGroupsRootDir = new File(IPERSON_GROUPS_ROOT);
323:                    if (!iPersonGroupsRootDir.exists()) {
324:                        iPersonGroupsRootDir.mkdir();
325:                        allFiles.add(iPersonGroupsRootDir);
326:                    }
327:                    int fileNameIdx = 0;
328:                    for (i = 0; i < numTestFiles; i++) {
329:                        fileName = iPersonGroupsRootDir.getPath() + sep
330:                                + testFileNames[fileNameIdx++];
331:                        f = new File(fileName);
332:                        f.mkdir();
333:                        allFiles.add(f);
334:                        directoryFiles.add(f);
335:                        for (j = numTestFiles; j < (numTestFiles * 2); j++) {
336:                            fileName = f.getPath() + sep
337:                                    + testFileNames[fileNameIdx++];
338:                            ff = new File(fileName);
339:                            ff.mkdir();
340:                            allFiles.add(ff);
341:                            directoryFiles.add(ff);
342:                            for (k = (numTestFiles * 2); k < (numTestFiles * 3); k++) {
343:                                fileName = ff.getPath() + sep
344:                                        + testFileNames[fileNameIdx++];
345:                                fff = new File(fileName);
346:                                fff.createNewFile();
347:                                addIdsToFile(fff);
348:                                allFiles.add(fff);
349:                                keyFiles.add(fff);
350:                            }
351:                        }
352:                    }
353:
354:                    this .testDataSource = new TransientDatasource();
355:                    Connection con = testDataSource.getConnection();
356:
357:                    con.prepareStatement(
358:                            "CREATE TABLE UP_ENTITY_TYPE "
359:                                    + "(ENTITY_TYPE_ID INTEGER, "
360:                                    + "ENTITY_TYPE_NAME VARCHAR, "
361:                                    + "DESCRIPTIVE_NAME VARCHAR)").execute();
362:
363:                    con
364:                            .prepareStatement(
365:                                    "INSERT INTO UP_ENTITY_TYPE "
366:                                            + "VALUES (1, 'java.lang.Object', 'Generic')")
367:                            .execute();
368:
369:                    con
370:                            .prepareStatement(
371:                                    "INSERT INTO UP_ENTITY_TYPE "
372:                                            + "VALUES (2, 'org.jasig.portal.security.IPerson', 'IPerson')")
373:                            .execute();
374:
375:                    con
376:                            .prepareStatement(
377:                                    "INSERT INTO UP_ENTITY_TYPE "
378:                                            + "VALUES (3, 'org.jasig.portal.groups.IEntityGroup', 'Group')")
379:                            .execute();
380:
381:                    con
382:                            .prepareStatement(
383:                                    "INSERT INTO UP_ENTITY_TYPE "
384:                                            + "VALUES (4, 'org.jasig.portal.ChannelDefinition', 'Channel')")
385:                            .execute();
386:
387:                    con
388:                            .prepareStatement(
389:                                    "INSERT INTO UP_ENTITY_TYPE "
390:                                            + "VALUES (5, 'org.jasig.portal.groups.IEntity', 'Grouped Entity')")
391:                            .execute();
392:
393:                    con.close();
394:
395:                    // initialize EntityTypes
396:                    EntityTypes.singleton(testDataSource);
397:
398:                } // end try
399:                catch (Exception ex) {
400:                    print("FileSystemGroupsTest.setUp(): " + ex.getMessage());
401:                }
402:
403:                //  print("Leaving FileSystemGroupsTest.setUp()" + CR);
404:
405:            }
406:
407:            /**
408:             * @return junit.framework.Test
409:             */
410:            public static junit.framework.Test suite() {
411:                TestSuite suite = new TestSuite();
412:
413:                suite.addTest(new FileSystemGroupsTest("testFind"));
414:                suite.addTest(new FileSystemGroupsTest(
415:                        "testFindContainingGroups"));
416:                suite.addTest(new FileSystemGroupsTest(
417:                        "testFindEntitiesForGroup"));
418:                suite.addTest(new FileSystemGroupsTest(
419:                        "testFindMemberGroupKeys"));
420:                suite.addTest(new FileSystemGroupsTest("testFindMemberGroups"));
421:                suite.addTest(new FileSystemGroupsTest("testSearchForGroups"));
422:                suite.addTest(new FileSystemGroupsTest(
423:                        "testFindEmbeddedMemberGroups"));
424:
425:                //	Add more tests here.
426:                //  NB: Order of tests is not guaranteed.
427:
428:                return suite;
429:            }
430:
431:            /**
432:             */
433:            protected void tearDown() {
434:                //    print("Entering FileSystemGroupsTest.tearDown()");
435:                try {
436:                    testEntityKeys = null;
437:                    testFileNames = null;
438:                    testGroupKeys = null;
439:
440:                    File[] oldFiles = (File[]) allFiles
441:                            .toArray(new File[allFiles.size()]);
442:
443:                    for (int i = oldFiles.length; i > 0; i--) {
444:                        oldFiles[i - 1].delete();
445:                    }
446:
447:                    getGroupsRoot().delete();
448:
449:                    allFiles = null;
450:                    directoryFiles = null;
451:                    keyFiles = null;
452:                    groupStore = null;
453:                    groupsRoot = null;
454:
455:                    Connection con = this .testDataSource.getConnection();
456:
457:                    con.prepareStatement("DROP TABLE UP_ENTITY_TYPE").execute();
458:                    con.prepareStatement("SHUTDOWN").execute();
459:
460:                    con.close();
461:
462:                    this .testDataSource = null;
463:
464:                } catch (Exception ex) {
465:                    print("FileSystemGroupsTest.tearDown()" + ex.getMessage());
466:                }
467:
468:                //    print("Leaving FilesystemGroupsTester.tearDown()");
469:
470:            }
471:
472:            /**
473:             * Tests IEntityGroupStore.find(), which returns an instance of IEntityGroup
474:             * given a key.
475:             */
476:            public void testFind() throws Exception {
477:                print("***** ENTERING FilesyStemGroupsTester.testFind() *****"
478:                        + CR);
479:
480:                String msg = null;
481:                Class type = IPERSON_CLASS;
482:                String existingKey = null, nonExistingKey = null;
483:                IEntityGroup existingGroup = null;
484:                File f = null;
485:
486:                msg = "Finding existing groups by key...";
487:                print(msg);
488:                for (Iterator itr = directoryFiles.iterator(); itr.hasNext();) {
489:                    f = (File) itr.next();
490:                    existingKey = getKeyFromFile(f);
491:                    msg = "Finding group key " + existingKey;
492:                    existingGroup = getGroupStore().find(existingKey);
493:                    assertNotNull(msg, existingGroup);
494:                }
495:
496:                nonExistingKey = existingKey + "x";
497:                msg = "Finding non-existing key: " + nonExistingKey;
498:                print(msg);
499:                existingGroup = getGroupStore().find(nonExistingKey);
500:                assertNull(msg, existingGroup);
501:
502:                print("Test completed successfully." + CR);
503:
504:                print("***** LEAVING FileSystemGroupsTest.testFind() *****"
505:                        + CR);
506:
507:            }
508:
509:            /**
510:             * Tests IEntityGroupStore.findContainingGroups() for both an IEntity and
511:             * an IEntityGroup.
512:             */
513:            public void testFindContainingGroups() throws Exception {
514:                print("***** ENTERING FileSystemGroupsTest.testFindContainingGroups() *****"
515:                        + CR);
516:
517:                String msg = null;
518:                Class type = IPERSON_CLASS;
519:                String ipersonKey = null, groupKey = null;
520:                IEntityGroup group = null, containingGroup = null;
521:                IEntity ent = null;
522:                File f = null;
523:                Iterator itr = null;
524:                List containingGroups = new ArrayList();
525:
526:                msg = "Finding containing groups for entity keys...";
527:                print(msg);
528:                for (int i = 0; i < testEntityKeys.length; i++) {
529:                    ent = getNewIPersonEntity(testEntityKeys[i]);
530:                    msg = "Finding containing groups for " + ent;
531:                    print(msg);
532:                    containingGroups.clear();
533:                    for (itr = getGroupStore().findContainingGroups(ent); itr
534:                            .hasNext();) {
535:                        group = (IEntityGroup) itr.next();
536:                        containingGroups.add(group);
537:                        assertTrue(msg, group instanceof  IEntityGroup);
538:                    }
539:                    assertEquals(msg, keyFiles.size(), containingGroups.size());
540:                }
541:
542:                ent = getNewIPersonEntity(NON_EXISTENT_ID);
543:                msg = "Finding containing groups for non-existent key: "
544:                        + NON_EXISTENT_ID;
545:                print(msg);
546:                containingGroups.clear();
547:                for (itr = getGroupStore().findContainingGroups(ent); itr
548:                        .hasNext();) {
549:                    containingGroups.add(itr.next());
550:                }
551:                assertEquals(msg, 0, containingGroups.size());
552:
553:                msg = "Finding containing groups for groups...";
554:                print(msg);
555:                // Each file that contains keys should have 1 and only 1 containing group.
556:                for (itr = keyFiles.iterator(); itr.hasNext();) {
557:                    f = (File) itr.next();
558:                    group = findGroup(f);
559:                    assertTrue(msg, group instanceof  IEntityGroup);
560:                    containingGroups.clear();
561:
562:                    for (Iterator cg = getGroupStore().findContainingGroups(
563:                            group); cg.hasNext();) {
564:                        containingGroup = (IEntityGroup) cg.next();
565:                        assertTrue(msg, containingGroup instanceof  IEntityGroup);
566:                        containingGroups.add(containingGroup);
567:                    }
568:                    assertEquals(msg, 1, containingGroups.size());
569:                }
570:
571:                msg = "Finding containing groups for a non-existent type...";
572:                print(msg);
573:
574:                ent = getNewEntity(new Object().getClass(), testEntityKeys[0]);
575:                itr = getGroupStore().findContainingGroups(ent);
576:                boolean hasContainingGroup = itr.hasNext();
577:                assertTrue(msg, !hasContainingGroup);
578:
579:                print("Test completed successfully." + CR);
580:
581:                print("***** LEAVING FileSystemGroupsTest.testFindContainingGroups() *****"
582:                        + CR);
583:
584:            }
585:
586:            /**
587:             * Tests IEntityGroupStore.findMemberGroups(), findContainingGroups
588:             */
589:            public void testFindEmbeddedMemberGroups() throws Exception {
590:                print("***** ENTERING FileSystemGroupsTest.testFindEmbeddedMemberGroups() *****"
591:                        + CR);
592:
593:                String msg = null;
594:                IEntityGroup group = null, memberGroup = null;
595:                File f = null, f2 = null;
596:                String memberKeys[] = null;
597:
598:                f = (File) keyFiles.get(keyFiles.size() - 1); // member
599:                f2 = (File) keyFiles.get(keyFiles.size() - 2); // group
600:                String memberKey = getKeyFromFile(f);
601:                String groupKey = getKeyFromFile(f2);
602:
603:                msg = "Now adding member group key " + memberKey + " to "
604:                        + groupKey;
605:                print(msg);
606:                BufferedWriter bw = new BufferedWriter(new FileWriter(f2
607:                        .getPath(), true));
608:                bw.write("group:" + memberKey);
609:                bw.newLine();
610:                bw.close();
611:
612:                msg = "Finding member group keys for key file " + groupKey;
613:                print(msg);
614:                group = findGroup(f2);
615:                assertTrue(msg, group instanceof  IEntityGroup);
616:                memberKeys = getGroupStore().findMemberGroupKeys(group);
617:                assertEquals(msg, 1, memberKeys.length);
618:                memberGroup = findGroup(memberKeys[0]);
619:                assertNotNull(msg, memberGroup);
620:                assertTrue(msg, getGroupStore().contains(group, memberGroup));
621:
622:                print("Test completed successfully." + CR);
623:
624:                print("***** LEAVING FileSystemGroupsTest.testFindEmbeddedMemberGroups() *****"
625:                        + CR);
626:
627:            }
628:
629:            /**
630:             * Tests IEntityGroupStore.findEntitiesForGroup().
631:             */
632:            public void testFindEntitiesForGroup() throws Exception {
633:                print("***** ENTERING FileSystemGroupsTest.testFindEntitiesForGroup() *****"
634:                        + CR);
635:
636:                String msg = null;
637:                IEntityGroup group = null;
638:                String entityKey = null;
639:                File f = null, f2 = null;
640:                Iterator itr = null;
641:                List memberEntities = new ArrayList();
642:
643:                msg = "Finding entities for files...";
644:                print(msg);
645:
646:                for (itr = keyFiles.iterator(); itr.hasNext();) {
647:                    f = (File) itr.next();
648:                    msg = "finding group: " + f;
649:                    group = findGroup(f);
650:                    assertTrue(msg, group instanceof  IEntityGroup);
651:                    memberEntities.clear();
652:
653:                    for (Iterator members = getGroupStore()
654:                            .getEntityIdsFromFile(f).iterator(); members
655:                            .hasNext();) {
656:                        entityKey = (String) members.next();
657:                        assertTrue(msg, entityKey != null);
658:                        assertTrue(msg, entityKey.length() > 0);
659:                        memberEntities.add(entityKey);
660:                    }
661:                    assertEquals(msg, numTestEntities, memberEntities.size());
662:                }
663:
664:                f = (File) keyFiles.get(0);
665:                f2 = f.getParentFile();
666:                msg = "Finding entities for " + f2 + " (should have none).";
667:                group = findGroup(f2);
668:                assertTrue(msg, group instanceof  IEntityGroup);
669:                boolean hasEntities = getGroupStore().findEntitiesForGroup(
670:                        group).hasNext();
671:                assertTrue(msg, !hasEntities);
672:
673:                print("Test completed successfully." + CR);
674:
675:                print("***** LEAVING FileSystemGroupsTest.testFindEntitiesForGroup() *****"
676:                        + CR);
677:
678:            }
679:
680:            /**
681:             * Tests IEntityGroupStore.findMemberGroupKeys().
682:             */
683:            public void testFindMemberGroupKeys() throws Exception {
684:                print("***** ENTERING FileSystemGroupsTest.testFindMemberGroupKeys() *****"
685:                        + CR);
686:
687:                String msg = null;
688:                IEntityGroup group = null, memberGroup = null;
689:                File f = null, f2 = null;
690:                Iterator itr = null;
691:                String memberKeys[] = null;
692:
693:                msg = "Finding member group keys for directory files...";
694:                print(msg);
695:
696:                for (itr = directoryFiles.iterator(); itr.hasNext();) {
697:                    f = (File) itr.next();
698:                    msg = "Finding member group keys for group: " + f;
699:                    group = findGroup(f);
700:                    assertTrue(msg, group instanceof  IEntityGroup);
701:                    memberKeys = getGroupStore().findMemberGroupKeys(group);
702:                    assertEquals(msg, numTestFiles, memberKeys.length);
703:                    for (int i = 0; i < memberKeys.length; i++) {
704:                        memberGroup = findGroup(memberKeys[i]);
705:                        assertNotNull(msg, memberGroup);
706:                        assertTrue(msg, getGroupStore().contains(group,
707:                                memberGroup));
708:                    }
709:                }
710:
711:                msg = "Finding member group keys for key files...";
712:                print(msg);
713:
714:                for (itr = keyFiles.iterator(); itr.hasNext();) {
715:                    f = (File) itr.next();
716:                    msg = "Finding member group keys for group: " + f;
717:                    group = findGroup(f);
718:                    assertTrue(msg, group instanceof  IEntityGroup);
719:                    memberKeys = getGroupStore().findMemberGroupKeys(group);
720:                    assertEquals(msg, 0, memberKeys.length);
721:                }
722:
723:                print("Test completed successfully." + CR);
724:
725:                print("***** LEAVING FileSystemGroupsTest.testFindMemberGroupKeys() *****"
726:                        + CR);
727:
728:            }
729:
730:            /**
731:             * Tests IEntityGroupStore.findMemberGroups().
732:             */
733:            public void testFindMemberGroups() throws Exception {
734:                print("***** ENTERING FileSystemGroupsTest.testFindMemberGroups() *****"
735:                        + CR);
736:
737:                String msg = null, groupKey = null;
738:                IEntityGroup group = null, memberGroup = null;
739:                File f = null, f2 = null;
740:                Iterator itr = null;
741:                Iterator memberGroups = null;
742:
743:                msg = "Finding member groups for directory files...";
744:                print(msg);
745:
746:                for (itr = directoryFiles.iterator(); itr.hasNext();) {
747:                    f = (File) itr.next();
748:                    msg = "Finding member groups for group: " + f;
749:                    group = findGroup(f);
750:                    assertTrue(msg, group instanceof  IEntityGroup);
751:                    memberGroups = getGroupStore().findMemberGroups(group);
752:                    while (memberGroups.hasNext()) {
753:                        memberGroup = (IEntityGroup) memberGroups.next();
754:                        assertNotNull(msg, memberGroup);
755:                        groupKey = memberGroup.getKey();
756:                        memberGroup = findGroup(groupKey);
757:                        assertTrue(msg, getGroupStore().contains(group,
758:                                memberGroup));
759:                    }
760:                }
761:
762:                msg = "Finding member groups for key files...";
763:                print(msg);
764:
765:                for (itr = keyFiles.iterator(); itr.hasNext();) {
766:                    f = (File) itr.next();
767:                    msg = "Finding member groups for group: " + f;
768:                    group = findGroup(f);
769:                    assertTrue(msg, group instanceof  IEntityGroup);
770:                    memberGroups = getGroupStore().findMemberGroups(group);
771:                    assertTrue(msg, !memberGroups.hasNext());
772:                }
773:
774:                print("Test completed successfully." + CR);
775:
776:                print("***** LEAVING FileSystemGroupsTest.testFindMemberGroups() *****"
777:                        + CR);
778:
779:            }
780:
781:            /**
782:             * Tests IEntityGroupStore.searchForGroups(), which returns EntityIdentifier[] given
783:             * a search string.
784:             */
785:            public void testSearchForGroups() throws Exception {
786:                print("***** ENTERING FileSystemGroupsTest.testSearchForGroups() *****"
787:                        + CR);
788:
789:                String msg = null;
790:                String is = null, startsWith = null, endsWith = null, contains = null, badQuery = null;
791:                Class type = IPERSON_CLASS;
792:                IEntityGroup existingGroup = null;
793:                IGroupMember member = null;
794:                EntityIdentifier[] ids = null;
795:
796:                msg = "Searching for existing groups...";
797:                print(msg);
798:                for (int i = 0; i < testFileNames.length; i++) {
799:                    is = testFileNames[i];
800:                    startsWith = is.substring(0, (is.length() - 1));
801:                    endsWith = is.substring(1);
802:                    contains = is.substring(1, (is.length() - 1));
803:                    badQuery = is + " a b c";
804:
805:                    msg = "Searching for IS " + is;
806:                    ids = getGroupStore().searchForGroups(is,
807:                            IGroupConstants.IS, type);
808:                    assertEquals(msg, ids.length, 1);
809:                    member = findGroup(ids[0].getKey());
810:                    assertTrue(msg, member.isGroup());
811:
812:                    msg = "Searching for STARTS WITH " + startsWith;
813:                    ids = getGroupStore().searchForGroups(startsWith,
814:                            IGroupConstants.STARTS_WITH, type);
815:                    assertTrue(msg, ids.length > 0);
816:
817:                    msg = "Searching for ENDS WITH " + endsWith;
818:                    ids = getGroupStore().searchForGroups(endsWith,
819:                            IGroupConstants.ENDS_WITH, type);
820:                    assertTrue(msg, ids.length > 0);
821:
822:                    msg = "Searching for CONTAINS " + contains;
823:                    ids = getGroupStore().searchForGroups(contains,
824:                            IGroupConstants.CONTAINS, type);
825:                    assertTrue(msg, ids.length > 0);
826:
827:                    msg = "Searching for IS " + badQuery;
828:                    ids = getGroupStore().searchForGroups(badQuery,
829:                            IGroupConstants.IS, type);
830:                    assertEquals(msg, ids.length, 0);
831:
832:                }
833:
834:                print("Test completed successfully." + CR);
835:
836:                print("***** LEAVING FileSystemGroupsTest.testSearchForGroups() *****"
837:                        + CR);
838:
839:            }
840:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.