Source Code Cross Referenced for TestUsers.java in  » J2EE » fleXive » com » flexive » tests » embedded » 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 » J2EE » fleXive » com.flexive.tests.embedded 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /***************************************************************
002:         *  This file is part of the [fleXive](R) project.
003:         *
004:         *  Copyright (c) 1999-2008
005:         *  UCS - unique computing solutions gmbh (http://www.ucs.at)
006:         *  All rights reserved
007:         *
008:         *  The [fleXive](R) project is free software; you can redistribute
009:         *  it and/or modify it under the terms of the GNU General Public
010:         *  License as published by the Free Software Foundation;
011:         *  either version 2 of the License, or (at your option) any
012:         *  later version.
013:         *
014:         *  The GNU General Public License can be found at
015:         *  http://www.gnu.org/copyleft/gpl.html.
016:         *  A copy is found in the textfile GPL.txt and important notices to the
017:         *  license from the author are found in LICENSE.txt distributed with
018:         *  these libraries.
019:         *
020:         *  This library is distributed in the hope that it will be useful,
021:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
022:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023:         *  GNU General Public License for more details.
024:         *
025:         *  For further information about UCS - unique computing solutions gmbh,
026:         *  please see the company website: http://www.ucs.at
027:         *
028:         *  For further information about [fleXive](R), please see the
029:         *  project website: http://www.flexive.org
030:         *
031:         *
032:         *  This copyright notice MUST APPEAR in all copies of the file!
033:         ***************************************************************/package com.flexive.tests.embedded;
034:
035:        import com.flexive.shared.CacheAdmin;
036:        import com.flexive.shared.EJBLookup;
037:        import com.flexive.shared.FxContext;
038:        import com.flexive.shared.exceptions.FxApplicationException;
039:        import com.flexive.shared.interfaces.ACLEngine;
040:        import com.flexive.shared.interfaces.AccountEngine;
041:        import com.flexive.shared.interfaces.LanguageEngine;
042:        import com.flexive.shared.interfaces.MandatorEngine;
043:        import com.flexive.shared.security.ACL;
044:        import com.flexive.shared.security.Account;
045:        import com.flexive.shared.security.Role;
046:        import com.flexive.shared.value.FxString;
047:        import org.apache.commons.lang.RandomStringUtils;
048:        import org.apache.commons.lang.StringUtils;
049:
050:        import java.util.ArrayList;
051:        import java.util.Arrays;
052:        import java.util.Collections;
053:        import java.util.List;
054:
055:        /**
056:         * Test user creation and management.
057:         *
058:         * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
059:         */
060:
061:        public class TestUsers {
062:            /**
063:             * System property that contains the enabled test users (or "all" for all users)
064:             */
065:            public final static String ENABLE_USERS_PROPERTY = "tests.users";
066:
067:            public final static TestUser SUPERVISOR = new TestUser("SUPERVISOR");
068:            public final static TestUser MANDATOR_SUPERVISOR = new TestUser(
069:                    "MANDATOR-SUPERVISOR");
070:            public final static TestUser GUEST = new TestUser("GUEST");
071:
072:            public final static List<TestUser> ALL_USERS = Collections
073:                    .unmodifiableList(Arrays.asList(new TestUser[] {
074:                            SUPERVISOR, MANDATOR_SUPERVISOR, GUEST }));
075:            /**
076:             * Keeps track of user defined users
077:             */
078:            private static List<TestUser> USER_DEFINED_USERS = new ArrayList<TestUser>();
079:
080:            private static final String MANDATOR_NAME = "UNIT_TEST_MANDATOR (automatically created)";
081:
082:            private static boolean initialized = false;
083:            private static long mandatorId;
084:            private static long languageId;
085:
086:            private static AccountEngine accounts;
087:            private static MandatorEngine mandators;
088:            private static LanguageEngine languages;
089:            private static ACLEngine acl;
090:
091:            public static void initializeUsers() throws FxApplicationException {
092:                if (initialized) {
093:                    return;
094:                }
095:                synchronized (TestUsers.class) {
096:                    accounts = EJBLookup.getAccountEngine();
097:                    mandators = EJBLookup.getMandatorEngine();
098:                    languages = EJBLookup.getLanguageEngine();
099:                    acl = EJBLookup.getACLEngine();
100:
101:                    initialized = true;
102:
103:                    try {
104:                        FxContext.get().runAsSystem();
105:                        languageId = languages.load("en").getId();
106:                        mandatorId = getTestMandator();
107:                        removeOrphanedAccounts();
108:
109:                        // setup supervisor
110:                        SUPERVISOR.createUser(mandatorId, languageId);
111:                        accounts.setRoleList(SUPERVISOR.getUserId(), Role
112:                                .getList());
113:
114:                        // setup mandator supervisor
115:                        MANDATOR_SUPERVISOR.createUser(mandatorId, languageId);
116:                        accounts.setRoles(MANDATOR_SUPERVISOR.getUserId(),
117:                                Role.MandatorSupervisor.getId());
118:
119:                        // setup guest user
120:                        GUEST.createUser(mandatorId, languageId);
121:
122:                        // create user-defined users
123:                        for (TestUser user : USER_DEFINED_USERS) {
124:                            user.createUser(mandatorId, languageId);
125:                        }
126:                        getTestMandator(); //create mandator
127:                    } catch (Exception e) {
128:                        deleteUsers(); // cleanup
129:                        e.printStackTrace();
130:                        throw new RuntimeException(
131:                                "Failed to initialize test users: "
132:                                        + e.getMessage());
133:                    } finally {
134:                        FxContext.get().stopRunAsSystem();
135:                    }
136:                }
137:            }
138:
139:            /**
140:             * Get id of english
141:             *
142:             * @return english id
143:             */
144:            public static long getEnglishLanguageId() {
145:                return languageId;
146:            }
147:
148:            public static TestUser createUser(String informalName,
149:                    Role... roles) throws FxApplicationException {
150:                TestUser user = new TestUser(informalName);
151:                if (roles != null && roles.length > 0) {
152:                    user.setRoles(roles);
153:                }
154:                USER_DEFINED_USERS.add(user);
155:                return user;
156:            }
157:
158:            /**
159:             * Assign the requested acl and permissions to the given user
160:             *
161:             * @param user        TestUser
162:             * @param aclId       ACL to assign
163:             * @param permissions permissions to assign for the acl
164:             * @throws FxApplicationException on errors
165:             */
166:            public static void assignACL(TestUser user, long aclId,
167:                    ACL.Permission... permissions)
168:                    throws FxApplicationException {
169:                acl.assign(aclId, user.getUserGroupId(), permissions);
170:            }
171:
172:            public static long getTestMandator() throws FxApplicationException {
173:                try {
174:                    return CacheAdmin.getEnvironment().getMandator(
175:                            MANDATOR_NAME).getId();
176:                } catch (Exception e) {
177:                    // ignore - try to create
178:                    return mandators.create(MANDATOR_NAME, true);
179:                }
180:            }
181:
182:            private static void removeOrphanedAccounts()
183:                    throws FxApplicationException {
184:                // first remove all orphaned accounts created in previous runs
185:                for (Account account : accounts.loadAll(mandatorId)) {
186:                    if (account.getEmail().startsWith("TESTEMAIL_")) {
187:                        System.out.println("Removing orphaned test account: "
188:                                + account.getLoginName());
189:                        accounts.remove(account.getId());
190:                    }
191:                }
192:            }
193:
194:            public static synchronized void deleteUsers()
195:                    throws FxApplicationException {
196:                try {
197:                    FxContext.get().runAsSystem();
198:                    List<TestUser> users = new ArrayList<TestUser>(ALL_USERS);
199:                    users.addAll(USER_DEFINED_USERS);
200:                    for (TestUser user : users) {
201:                        try {
202:                            user.deleteUser();
203:                        } catch (FxApplicationException e) {
204:                            System.out
205:                                    .println("Failed to remove test user from database: "
206:                                            + e.getMessage());
207:                            e.printStackTrace();
208:                        }
209:                    }
210:                    // remove test mandator
211:                    mandators.remove(mandatorId);
212:                } finally {
213:                    FxContext.get().stopRunAsSystem();
214:                }
215:            }
216:
217:            /**
218:             * Return the list of all test users as configured in the test environment.
219:             *
220:             * @return all test users as configured in the test environment.
221:             */
222:            public static List<TestUser> getConfiguredTestUsers() {
223:                String enableUsers = System.getProperty(ENABLE_USERS_PROPERTY);
224:                if (StringUtils.isBlank(enableUsers)) {
225:                    return Collections.unmodifiableList(Arrays
226:                            .asList(new TestUser[] { SUPERVISOR }));
227:                } else if ("all".equalsIgnoreCase(enableUsers)) {
228:                    return ALL_USERS;
229:                } else {
230:                    throw new RuntimeException("Invalid value for property "
231:                            + ENABLE_USERS_PROPERTY + ": " + enableUsers);
232:                }
233:            }
234:
235:            /**
236:             * Create a new ACL of category CONTENT
237:             *
238:             * @param name proposed name
239:             * @return id of the new ACL
240:             * @throws FxApplicationException on errors
241:             */
242:            public static long newContentACL(String name)
243:                    throws FxApplicationException {
244:                return acl.create("TEST_" + name
245:                        + RandomStringUtils.random(16, true, true),
246:                        new FxString(name + " (content)"), TestUsers
247:                                .getTestMandator(), "#112233", "",
248:                        ACL.Category.INSTANCE);
249:            }
250:
251:            /**
252:             * Create a new ACL of category STRUCTURE
253:             *
254:             * @param name proposed name
255:             * @return id of the new ACL
256:             * @throws FxApplicationException on errors
257:             */
258:            public static long newStructureACL(String name)
259:                    throws FxApplicationException {
260:                return acl.create("TEST_" + name
261:                        + RandomStringUtils.random(16, true, true),
262:                        new FxString(name + " (structure)"), TestUsers
263:                                .getTestMandator(), "#112233", "",
264:                        ACL.Category.STRUCTURE);
265:            }
266:
267:            /**
268:             * Create a new ACL of category WORKFLOW
269:             *
270:             * @param name proposed name
271:             * @return id of the new ACL
272:             * @throws FxApplicationException on errors
273:             */
274:            public static long newWorkflowACL(String name)
275:                    throws FxApplicationException {
276:                return acl.create("TEST_" + name
277:                        + RandomStringUtils.random(16, true, true),
278:                        new FxString(name + " (workflow)"), TestUsers
279:                                .getTestMandator(), "#112233", "",
280:                        ACL.Category.WORKFLOW);
281:            }
282:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.