Source Code Cross Referenced for ConfigurationTest.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.EJBLookup;
036:        import com.flexive.shared.FxContext;
037:        import com.flexive.shared.configuration.*;
038:        import com.flexive.shared.configuration.parameters.IntegerParameter;
039:        import com.flexive.shared.configuration.parameters.ObjectParameter;
040:        import com.flexive.shared.content.FxPK;
041:        import com.flexive.shared.exceptions.*;
042:        import com.flexive.shared.interfaces.*;
043:        import com.flexive.shared.security.UserTicket;
044:        import static com.flexive.tests.embedded.FxTestUtils.login;
045:        import static com.flexive.tests.embedded.FxTestUtils.logout;
046:        import com.flexive.tests.shared.TestParameters;
047:        import org.apache.commons.lang.ArrayUtils;
048:        import org.apache.commons.lang.StringUtils;
049:        import org.testng.annotations.*;
050:
051:        import java.io.Serializable;
052:        import java.util.*;
053:
054:        /**
055:         * Configuration test suite
056:         *
057:         * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
058:         */
059:        @Test(groups={"ejb","configuration"})
060:        public class ConfigurationTest {
061:
062:            private TestUser user;
063:            private GlobalConfigurationEngine globalConfiguration;
064:            private DivisionConfigurationEngine divisionConfiguration;
065:            private UserConfigurationEngine userConfiguration;
066:            private ConfigurationEngine configuration;
067:
068:            public ConfigurationTest() {
069:            }
070:
071:            public ConfigurationTest(TestUser user) {
072:                this .user = user;
073:            }
074:
075:            @Factory
076:            public Object[] createTestInstances() throws FxApplicationException {
077:                List<Object> result = new ArrayList<Object>();
078:                for (TestUser user : TestUsers.getConfiguredTestUsers()) {
079:                    result.add(new ConfigurationTest(user));
080:                }
081:                if (result.size() > 1) {
082:                    // TODO add configuration management role
083:                    //result.add(new ConfigurationTest(TestUsers.createUser("CONFIGROLE", Role.GlobalSupervisor)));
084:                }
085:                return result.toArray();
086:            }
087:
088:            @BeforeClass
089:            public void beforeClass() throws Exception {
090:                globalConfiguration = EJBLookup.getGlobalConfigurationEngine();
091:                divisionConfiguration = EJBLookup
092:                        .getDivisionConfigurationEngine();
093:                userConfiguration = EJBLookup.getUserConfigurationEngine();
094:                configuration = EJBLookup.getConfigurationEngine();
095:            }
096:
097:            @BeforeMethod
098:            public void beforeTestMethod() throws FxLoginFailedException,
099:                    FxAccountInUseException {
100:                System.out.println("Login: " + user.getUserName());
101:                login(user);
102:            }
103:
104:            @AfterMethod
105:            public void afterTestMethod() throws Exception {
106:                System.out.println("Logout: " + user.getUserName());
107:                logout();
108:            }
109:
110:            /**
111:             * Generic test class
112:             *
113:             * @param <T>   type parameter
114:             */
115:            public static class GenericConfigurationTest<T extends Serializable> {
116:                private GenericConfigurationEngine configuration;
117:                private Parameter<T> parameter;
118:                private ParameterDataEditBean<T> data;
119:                private T value;
120:
121:                /**
122:                 * Create a new test class for the given parameter.
123:                 *
124:                 * @param configuration the configuration instance to be tested
125:                 * @param parameter     the parameter to be used for unit tests
126:                 * @param value         the value to be stored in the given parameter
127:                 */
128:                public GenericConfigurationTest(
129:                        GenericConfigurationEngine configuration,
130:                        Parameter<T> parameter, T value) {
131:                    this .configuration = configuration;
132:                    this .parameter = parameter;
133:                    this .data = (ParameterDataEditBean<T>) parameter.getData();
134:                    this .value = value;
135:                }
136:
137:                /**
138:                 * Run all tests for this instance
139:                 *
140:                 * @throws Exception if an exception is thrown
141:                 */
142:                public void runTests() throws Exception {
143:                    ParameterPath oldPath = data.getPath();
144:                    try {
145:                        for (SystemParameterPaths path : SystemParameterPaths
146:                                .getTestPaths()) {
147:                            if (!path.getScope()
148:                                    .hasScope(ParameterScope.GLOBAL)) {
149:                                continue;
150:                            }
151:                            // cycle through all available scopes
152:                            data.setPath(path);
153:                            testDefaultValue();
154:                            testPut();
155:                            testDelete();
156:                            testUpdate();
157:                            testGetAll();
158:                        }
159:                    } finally {
160:                        // restore values
161:                        data.setPath(oldPath);
162:                    }
163:                }
164:
165:                /**
166:                 * Check if the default parameter value is returned properly.
167:                 *
168:                 * @throws Exception if an error occured
169:                 */
170:                private void testDefaultValue() throws Exception {
171:                    try {
172:                        configuration.remove(parameter);
173:                        if (!mayUpdateConfig())
174:                            assert false : "Should not be permitted to delete parameter "
175:                                    + parameter + " in " + configuration;
176:                        assert parameter.getDefaultValue().equals(
177:                                configuration.get(parameter)) : "Default value incorrect.";
178:                    } catch (FxNoAccessException e) {
179:                        if (mayUpdateConfig())
180:                            assert false : "Failed to delete parameter although privileges exist.";
181:                    }
182:                }
183:
184:                /**
185:                 * Test storing a parameter in the configuration
186:                 *
187:                 * @throws Exception if an error occured
188:                 */
189:                private void testPut() throws Exception {
190:                    try {
191:                        configuration.put(parameter, value);
192:                        if (!mayUpdateConfig())
193:                            assert false : "Should not be permitted to set parameter "
194:                                    + parameter + " in " + configuration;
195:
196:                        T dbValue = configuration.get(parameter);
197:                        assert !((dbValue == null && value != null) || (dbValue != null && value == null));
198:                        assert dbValue == null || dbValue.equals(value) : "Parameter value not stored correctly or incorrect implementation of \"Object#equals\"";
199:                    } catch (FxNoAccessException e) {
200:                        if (mayUpdateConfig())
201:                            assert false : "Failed to put parameter although privileges exist for parameter "
202:                                    + parameter + " in " + configuration;
203:
204:                    } finally {
205:                        safeDelete();
206:                    }
207:                }
208:
209:                /**
210:                 * Test deleting a parameter
211:                 *
212:                 * @throws Exception if an error occured
213:                 */
214:                private void testDelete() throws Exception {
215:                    try {
216:                        configuration.put(parameter, value);
217:                        if (!mayUpdateConfig())
218:                            assert false : "Should not be permitted to set parameter "
219:                                    + parameter + " in " + configuration;
220:
221:                        configuration.get(parameter);
222:                        configuration.remove(parameter);
223:                        if (!mayUpdateConfig())
224:                            assert false : "Should not be permitted to delete parameter "
225:                                    + parameter + " in " + configuration;
226:
227:                        try {
228:                            configuration.get(parameter, parameter.getKey(),
229:                                    true);
230:                            assert false : "Parameter deleted but still retrievable with get(): "
231:                                    + parameter;
232:                        } catch (FxNotFoundException e) {
233:                            // succeed
234:                        }
235:                    } catch (FxNoAccessException e) {
236:                        if (mayUpdateConfig())
237:                            assert false : "Failed to update/delete parameter although privileges exist "
238:                                    + "for parameter "
239:                                    + parameter
240:                                    + " in "
241:                                    + configuration;
242:                    } finally {
243:                        safeDelete();
244:                    }
245:                }
246:
247:                /**
248:                 * Test updating a parameter
249:                 *
250:                 * @throws Exception if an error occured
251:                 */
252:                private void testUpdate() throws Exception {
253:                    try {
254:                        assert !parameter.getDefaultValue().equals(value) : "Default value and value must not be the same!";
255:                        configuration.put(parameter, parameter
256:                                .getDefaultValue());
257:                        assert parameter.getDefaultValue().equals(
258:                                configuration.get(parameter)) : "Failed to load value.";
259:                        configuration.put(parameter, value);
260:                        if (!mayUpdateConfig())
261:                            assert false : "Should not be permitted to set parameter "
262:                                    + parameter + " in " + configuration;
263:                        assert (configuration.get(parameter) == null && value == null)
264:                                || configuration.get(parameter).equals(value) : "Failed to load updated value.";
265:                    } catch (FxNoAccessException e) {
266:                        if (mayUpdateConfig())
267:                            assert false : "Failed to update parameter although privileges exist "
268:                                    + "for parameter "
269:                                    + parameter
270:                                    + " in "
271:                                    + configuration;
272:                    } finally {
273:                        safeDelete();
274:                    }
275:                }
276:
277:                /**
278:                 * Test retrieving a group of parameters stored in a path.
279:                 *
280:                 * @throws Exception if an error occured
281:                 */
282:                private void testGetAll() throws Exception {
283:                    try {
284:                        configuration.put(parameter, "key1", parameter
285:                                .getDefaultValue());
286:                        configuration.put(parameter, "key2", value);
287:                        configuration.put(parameter, "key3", value);
288:                        configuration.put(parameter, "key4", value);
289:                        if (!mayUpdateConfig())
290:                            assert false : "Should not be permitted to set parameter "
291:                                    + parameter + " in " + configuration;
292:
293:                        Map<String, T> params = configuration.getAll(parameter);
294:                        assert 4 == params.entrySet().size() : "Should have retrieved four parameters.";
295:                        int ctr = 1;
296:                        final String[] keyValues = { "key1", "key2", "key3",
297:                                "key4" };
298:                        for (String key : keyValues) {
299:                            if (params.get(key) != null) {
300:                                assert params.get(key).equals(
301:                                        ctr == 1 ? parameter.getDefaultValue()
302:                                                : value) : "Invalid parameter value";
303:                            }
304:                            ctr++;
305:                        }
306:
307:                        final Collection<String> keys = configuration
308:                                .getKeys(parameter);
309:                        assert 4 == keys.size() : "Should have retrieved for parameters.";
310:                        for (String key : keyValues) {
311:                            assert keys.contains(key) : "Key "
312:                                    + key
313:                                    + " not found in result returned by getKeys.";
314:                        }
315:                    } catch (FxNoAccessException e) {
316:                        if (mayUpdateConfig()) {
317:                            assert false : "Failed to update/delete parameter although privileges exist "
318:                                    + "for parameter "
319:                                    + parameter
320:                                    + " in "
321:                                    + configuration;
322:                        }
323:                    } finally {
324:                        safeDelete();
325:                    }
326:                }
327:
328:                /**
329:                 * Security-aware "cleanup" for parameters assuming that one can
330:                 * only delete parameters if one can set them...
331:                 *
332:                 * @throws FxApplicationException
333:                 */
334:                private void safeDelete() throws FxApplicationException {
335:                    try {
336:                        configuration.removeAll(parameter);
337:                    } catch (FxNoAccessException e) {
338:                        if (mayUpdateConfig()) {
339:                            assert false : "Failed to delete parameter although privileges exist.";
340:                        }
341:                    }
342:                }
343:
344:                /**
345:                 * Returns true if the current user may update this test's config
346:                 *
347:                 * @return true if the current user may update this test's config
348:                 * @throws FxLookupException if a lookup error occured
349:                 */
350:                private boolean mayUpdateConfig() throws FxLookupException {
351:                    final UserTicket ticket = FxContext.get().getTicket();
352:                    final ParameterScope scope = parameter.getScope();
353:                    GenericConfigurationEngine checkConfiguration = configuration;
354:                    if (configuration instanceof  ConfigurationEngine) {
355:                        // get "primary" config for current parameter for security checks
356:                        if (scope == ParameterScope.GLOBAL) {
357:                            checkConfiguration = EJBLookup
358:                                    .getGlobalConfigurationEngine();
359:                        } else if (scope == ParameterScope.DIVISION
360:                                || scope == ParameterScope.DIVISION_ONLY) {
361:                            checkConfiguration = EJBLookup
362:                                    .getDivisionConfigurationEngine();
363:                        } else if (scope == ParameterScope.USER
364:                                || scope == ParameterScope.USER_ONLY) {
365:                            checkConfiguration = EJBLookup
366:                                    .getUserConfigurationEngine();
367:                        }
368:                    }
369:                    return (checkConfiguration instanceof  GlobalConfigurationEngine && FxContext
370:                            .get().isGlobalAuthenticated())
371:                            || (checkConfiguration instanceof  DivisionConfigurationEngine && ticket
372:                                    .isGlobalSupervisor())
373:                            || (checkConfiguration instanceof  UserConfigurationEngine);
374:                }
375:
376:            }
377:
378:            /**
379:             * Test the global configuration
380:             *
381:             * @throws Exception if an error occured
382:             */
383:            @Test
384:            public void globalConfiguration() throws Exception {
385:                try {
386:                    FxContext.get().setGlobalAuthenticated(false);
387:                    globalConfiguration.put(TestParameters.CACTUS_TEST, "test");
388:                    assert false : "Global configuration modifiable without global admin login!";
389:                } catch (FxNoAccessException e) {
390:                    // pass
391:                }
392:                final GlobalConfigurationEngine config = EJBLookup
393:                        .getGlobalConfigurationEngine();
394:                // check if privileges are respected for all config actions
395:                testGenericConfiguration(config);
396:                try {
397:                    FxContext.get().setGlobalAuthenticated(true);
398:                    testGenericConfiguration(config);
399:                } finally {
400:                    FxContext.get().setGlobalAuthenticated(false);
401:                }
402:                // test division infos
403:                int[] divisionIds = config.getDivisionIds();
404:                int ctr = 0;
405:                for (DivisionData data : config.getDivisions()) {
406:                    assert ArrayUtils.contains(divisionIds, data.getId()) : "Division ID not returned by getDivisionIds().";
407:                    assert StringUtils.isNotBlank(data.getDataSource()) : "Division data source not returned";
408:                    assert StringUtils.isNotBlank(data.getDomainRegEx()) : "Domain regexp missing";
409:                    if (data.isAvailable()) {
410:                        assert StringUtils.isNotBlank(data.getDbVersion()) : "DB version missing";
411:                        assert data.getDbVendor().getId() >= 0 : "DB vendor missing";
412:                    }
413:                    config.clearDivisionCache();
414:                    assert config.getDivisionData(data.getId()).equals(data);
415:                    ctr++;
416:                }
417:                assert ctr == divisionIds.length : "getDivisions() and getDivisionIds() array length don't match";
418:
419:                // test division table update
420:                final DivisionData[] orig = config.getDivisions();
421:                FxContext.get().setGlobalAuthenticated(true);
422:                try {
423:                    final DivisionData newDivision = new DivisionData(1, false,
424:                            "test", "xxx", DBVendor.Unknown, "1.2");
425:                    config.saveDivisions(Arrays.asList(newDivision));
426:                    assert config.getDivisions().length == 1 : "Division table not updated";
427:                    assert config.getDivisions()[0].equals(newDivision) : "New division not written properly";
428:                } finally {
429:                    config.saveDivisions(Arrays.asList(orig));
430:                    assert Arrays.equals(config.getDivisions(), orig);
431:                    FxContext.get().setGlobalAuthenticated(false);
432:                }
433:            }
434:
435:            /**
436:             * Test the per-division configuration
437:             *
438:             * @throws Exception if an error occured
439:             */
440:            @Test
441:            public void divisionConfiguration() throws Exception {
442:                testGenericConfiguration(divisionConfiguration);
443:            }
444:
445:            /**
446:             * Test the per-user configuration
447:             *
448:             * @throws Exception if an error occured
449:             */
450:            @Test
451:            public void userConfiguration() throws Exception {
452:                testGenericConfiguration(userConfiguration);
453:            }
454:
455:            /**
456:             * Test the FxConfiguration wrapper
457:             *
458:             * @throws Exception if an error occured
459:             */
460:            @Test
461:            public void fxConfiguration() throws Exception {
462:                try {
463:                    FxContext.get().setGlobalAuthenticated(false);
464:                    configuration.put(TestParameters.CACTUS_TEST, "test");
465:                    assert false : "User allowed to set global config parameters without auth.";
466:                } catch (FxNoAccessException e) {
467:                    // pass
468:                }
469:                // test fallbacks
470:                try {
471:                    FxContext.get().setGlobalAuthenticated(true);
472:                    testFallbacks(TestParameters.CACTUS_TEST, "test");
473:                    testFallbacks(TestParameters.CACTUS_TEST, null);
474:                } finally {
475:                    FxContext.get().setGlobalAuthenticated(false);
476:                }
477:                // run generic config tests
478:                try {
479:                    FxContext.get().setGlobalAuthenticated(true);
480:                    testGenericConfiguration(configuration);
481:                } finally {
482:                    FxContext.get().setGlobalAuthenticated(false);
483:                }
484:            }
485:
486:            /**
487:             * Generic configuration test. Tests all known types of parameters
488:             * for the given configuration.
489:             *
490:             * @param configuration The configuration to be tested
491:             * @throws Exception if an error occurs
492:             */
493:            private void testGenericConfiguration(
494:                    GenericConfigurationEngine configuration) throws Exception {
495:                // test string parameters
496:                new GenericConfigurationTest<String>(configuration,
497:                        TestParameters.CACTUS_TEST, "test").runTests();
498:                new GenericConfigurationTest<String>(configuration,
499:                        TestParameters.CACTUS_TEST, "").runTests();
500:                new GenericConfigurationTest<String>(configuration,
501:                        TestParameters.CACTUS_TEST, null).runTests();
502:                // test int parameters
503:                new GenericConfigurationTest<Integer>(configuration,
504:                        IntegerParameter.CACTUS_TEST_INT, 255329).runTests();
505:                new GenericConfigurationTest<Integer>(configuration,
506:                        IntegerParameter.CACTUS_TEST_INT, -1032412).runTests();
507:                try {
508:                    new GenericConfigurationTest<Integer>(configuration,
509:                            IntegerParameter.CACTUS_TEST_INT, null).runTests();
510:                    assert false : "Should not be able to put null values in int parameters.";
511:                } catch (Exception e) {
512:                    // pass
513:                }
514:                // test long parameters
515:                new GenericConfigurationTest<Long>(configuration,
516:                        TestParameters.CACTUS_TEST_LONG, 255329L).runTests();
517:                new GenericConfigurationTest<Long>(configuration,
518:                        TestParameters.CACTUS_TEST_LONG, -1032412L).runTests();
519:                try {
520:                    new GenericConfigurationTest<Long>(configuration,
521:                            TestParameters.CACTUS_TEST_LONG, null).runTests();
522:                    assert false : "Should not be able to put null values in long parameters.";
523:                } catch (Exception e) {
524:                    // pass
525:                }
526:                // test boolean parameters
527:                ParameterDataEditBean<Boolean> data = (ParameterDataEditBean<Boolean>) TestParameters.CACTUS_TEST_BOOL
528:                        .getData();
529:                data.setDefaultValue(false);
530:                new GenericConfigurationTest<Boolean>(configuration,
531:                        TestParameters.CACTUS_TEST_BOOL, true).runTests();
532:                data.setDefaultValue(true);
533:                new GenericConfigurationTest<Boolean>(configuration,
534:                        TestParameters.CACTUS_TEST_BOOL, false).runTests();
535:                try {
536:                    new GenericConfigurationTest<Boolean>(configuration,
537:                            TestParameters.CACTUS_TEST_BOOL, null).runTests();
538:                    assert false : "Should not be able to put null values in boolean parameters.";
539:                } catch (Exception e) {
540:                    // pass
541:                }
542:                // test object parameters with a simple POJO
543:                new GenericConfigurationTest<FxPK>(configuration,
544:                        ObjectParameter.TEST_OBJ, new FxPK(5, 1)).runTests();
545:                try {
546:                    new GenericConfigurationTest<FxPK>(configuration,
547:                            ObjectParameter.TEST_OBJ, null).runTests();
548:                    assert false : "Should not be able to put null values in object parameters.";
549:                } catch (Exception e) {
550:                    // pass
551:                }
552:            }
553:
554:            /**
555:             * Test all kinds of fallbacks of FxConfiguration
556:             *
557:             * @param <T>       parameter value type to be tested
558:             * @param parameter parameter to be tested
559:             * @param value     the value to be tested
560:             * @throws Exception if an error occured
561:             */
562:            private <T extends Serializable> void testFallbacks(
563:                    Parameter<T> parameter, T value) throws Exception {
564:                cleanup(parameter);
565:                for (ParameterPath path : SystemParameterPaths.getTestPaths()) {
566:                    ParameterPath origPath = updatePath(parameter, path);
567:                    try {
568:                        configuration.put(parameter, value);
569:                        assertAccess(parameter);
570:                        T dbValue = configuration.get(parameter);
571:                        checkFallbacks(parameter, dbValue);
572:                    } catch (FxNoAccessException e) {
573:                        assertNoAccess(parameter);
574:                    } finally {
575:                        cleanup(parameter);
576:                        updatePath(parameter, origPath);
577:                    }
578:                }
579:            }
580:
581:            /**
582:             * Purges the given test parameter from all known configurations.
583:             *
584:             * @param parameter the parameter to be deleted
585:             */
586:            private void cleanup(Parameter<? extends Serializable> parameter) {
587:                try {
588:                    globalConfiguration.remove(parameter);
589:                    assertAccess(parameter, ParameterScope.GLOBAL);
590:                } catch (FxNoAccessException e) {
591:                    assertNoAccess(parameter, ParameterScope.GLOBAL);
592:                } catch (Exception e) {
593:                    System.out
594:                            .println("Failed to delete parameter from global config: "
595:                                    + e.getMessage());
596:                }
597:                try {
598:                    divisionConfiguration.remove(parameter);
599:                    assertAccess(parameter, ParameterScope.DIVISION);
600:                } catch (FxNoAccessException e) {
601:                    assertNoAccess(parameter, ParameterScope.DIVISION);
602:                } catch (Exception e) {
603:                    System.out
604:                            .println("Failed to delete parameter from division config: "
605:                                    + e.getMessage());
606:                }
607:                try {
608:                    userConfiguration.remove(parameter);
609:                    assertAccess(parameter, ParameterScope.USER);
610:                } catch (FxNoAccessException e) {
611:                    assertNoAccess(parameter, ParameterScope.USER);
612:                } catch (Exception e) {
613:                    System.out
614:                            .println("Failed to delete parameter from user config: "
615:                                    + e.getMessage());
616:                }
617:            }
618:
619:            /**
620:             * Helper method to manually check the fallback value
621:             *
622:             * @param parameter the parameter to be checked
623:             * @param expected  expected parameter value
624:             * @param <T>       value type
625:             * @throws FxApplicationException
626:             */
627:            private <T extends Serializable> void checkFallbacks(
628:                    Parameter<T> parameter, T expected)
629:                    throws FxApplicationException {
630:                ArrayList<ParameterScope> fallbacks = new ArrayList<ParameterScope>();
631:                fallbacks.add(0, parameter.getScope());
632:                fallbacks.addAll(parameter.getScope().getFallbacks());
633:                for (ParameterScope scope : fallbacks) {
634:                    try {
635:                        T value = null;
636:                        if (scope == ParameterScope.GLOBAL) {
637:                            value = globalConfiguration.get(parameter,
638:                                    parameter.getData().getKey(), true);
639:                        } else if (scope == ParameterScope.DIVISION
640:                                || scope == ParameterScope.DIVISION_ONLY) {
641:                            value = divisionConfiguration.get(parameter,
642:                                    parameter.getData().getKey(), true);
643:                        } else if (scope == ParameterScope.USER
644:                                || scope == ParameterScope.USER_ONLY) {
645:                            value = userConfiguration.get(parameter, parameter
646:                                    .getData().getKey(), true);
647:                        } else {
648:                            assert false : "Unexpected parameter scope: "
649:                                    + scope;
650:                        }
651:                        // parameter exists in config, check value and return
652:                        assert (expected == null && value == null)
653:                                || expected.equals(value) : "Unexpected parameter value";
654:                        return;
655:                    } catch (FxNotFoundException e) {
656:                        // continue with next configuration
657:                    }
658:                }
659:                // parameter does not exist in db - check default value
660:                if (parameter.getDefaultValue() != null) {
661:                    assert parameter.getDefaultValue().equals(expected) : "Configuration should have returned the default value.";
662:                } else {
663:                    assert false : "Parameter does not found: " + parameter;
664:                }
665:            }
666:
667:            /**
668:             * Update the path of test paraemeters.
669:             *
670:             * @param parameter the parameter to be updated
671:             * @param path      the path to be set
672:             * @return the previously set path
673:             */
674:            private ParameterPath updatePath(Parameter<?> parameter,
675:                    ParameterPath path) {
676:                ParameterPath oldPath = parameter.getPath();
677:                ((ParameterDataEditBean<?>) parameter.getData()).setPath(path);
678:                return oldPath;
679:            }
680:
681:            /**
682:             * Assert that the current user may not access (update) the given parameter.
683:             *
684:             * @param parameter the parameter to be checked
685:             */
686:            private void assertNoAccess(Parameter<?> parameter,
687:                    ParameterScope scope) {
688:                UserTicket ticket = FxContext.get().getTicket();
689:                if (scope == ParameterScope.USER
690:                        || scope == ParameterScope.USER_ONLY) {
691:                    assert false : "User parameters should always be writable for the user: "
692:                            + parameter;
693:                }
694:                if ((scope == ParameterScope.DIVISION || scope == ParameterScope.DIVISION_ONLY)
695:                        && ticket.isGlobalSupervisor()) {
696:                    assert false : "User is global supervisor, but cannot update division parameter: "
697:                            + parameter;
698:                }
699:                if (scope == ParameterScope.GLOBAL
700:                        && FxContext.get().isGlobalAuthenticated()) {
701:                    assert false : "User is authenticated for global config, but may not update parameter: "
702:                            + parameter;
703:                }
704:            }
705:
706:            private void assertNoAccess(Parameter<?> parameter) {
707:                assertNoAccess(parameter, parameter.getScope());
708:            }
709:
710:            /**
711:             * Assert that the current user may access the given parameter.
712:             *
713:             * @param parameter the parameter to be checked
714:             */
715:            private void assertAccess(Parameter<?> parameter,
716:                    ParameterScope scope) {
717:                UserTicket ticket = FxContext.get().getTicket();
718:                if ((scope == ParameterScope.DIVISION || scope == ParameterScope.DIVISION_ONLY)
719:                        && !ticket.isGlobalSupervisor()) {
720:                    assert false : "User is NOT global supervisor, but can update division parameter: "
721:                            + parameter;
722:                }
723:                if (scope == ParameterScope.GLOBAL
724:                        && !FxContext.get().isGlobalAuthenticated()) {
725:                    assert false : "User is NOT authenticated for global config, but may update global parameter: "
726:                            + parameter;
727:                }
728:            }
729:
730:            private void assertAccess(Parameter<?> parameter) {
731:                assertAccess(parameter, parameter.getScope());
732:            }
733:
734:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.