root/trunk/components/client/test/ome/client/itests/ConfigTest.java
| Revision 2135, 5.3 kB (checked in by jmoore, 11 months ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | /* |
| 2 | * $Id$ |
| 3 | * |
| 4 | * Copyright 2006 University of Dundee. All rights reserved. |
| 5 | * Use is subject to license terms supplied in LICENSE.txt |
| 6 | */ |
| 7 | package ome.client.itests; |
| 8 | |
| 9 | import java.util.UUID; |
| 10 | |
| 11 | import junit.framework.TestCase; |
| 12 | import ome.api.IAdmin; |
| 13 | import ome.api.IConfig; |
| 14 | import ome.model.meta.Experimenter; |
| 15 | import ome.model.meta.ExperimenterGroup; |
| 16 | import ome.system.Login; |
| 17 | import ome.system.ServiceFactory; |
| 18 | |
| 19 | import org.testng.annotations.Configuration; |
| 20 | import org.testng.annotations.Test; |
| 21 | |
| 22 | /** |
| 23 | * simple client-side test of the ome.api.IConfig service. |
| 24 | * |
| 25 | * Also used as the main developer example for developing |
| 26 | * (stateless/client-side) tests. See source code documentation for more. |
| 27 | * |
| 28 | * @author Josh Moore, josh.moore at gmx.de |
| 29 | * @version $Revision$, $Date$ |
| 30 | * @since 3.0-M3 |
| 31 | */ |
| 32 | /* |
| 33 | * Developer's note: Like with the server-side test, the "integration" group is |
| 34 | * important for having tests run properly. Here, however, it's needed because |
| 35 | * there is no super-class. |
| 36 | * |
| 37 | * Client-side tests test the full spectrum, including login, security |
| 38 | * permissions, transactions and other interceptors, and the logic itself. |
| 39 | * |
| 40 | * As with the method names (mentioned in the server-side version of this test), |
| 41 | * we subclass from junit.framework.TestCase for some semblance of supporting |
| 42 | * both testing platforms. |
| 43 | */ |
| 44 | @Test(groups = { "ticket:306", "config", "integration" }) |
| 45 | public class ConfigTest extends TestCase { |
| 46 | |
| 47 | /* |
| 48 | * Developer notes: --------------- In general, there is no superclass on |
| 49 | * the client-side, though in some cases (like for security) one has been |
| 50 | * created. There is no particular reason for this and all classes could be |
| 51 | * made to inherit from a single class like AbstractManagedContextTest on |
| 52 | * the server-side. |
| 53 | */ |
| 54 | IConfig iConfig; |
| 55 | |
| 56 | /* |
| 57 | * Developer notes: --------------- Here we override the setUp() method from |
| 58 | * TestCase and add the @Configuration annotation TestNG so as to have both |
| 59 | * frameworks properly initialize the test. (@Configuration has actually |
| 60 | * been deprecated in TestNG 5. All our test classes will need to be |
| 61 | * updated.) |
| 62 | */ |
| 63 | @Override |
| 64 | @Configuration(beforeTestClass = true) |
| 65 | protected void setUp() throws Exception { |
| 66 | /* |
| 67 | * Developer notes: --------------- without further configuration, |
| 68 | * ServiceFactory() uses your default (development) user for logging in. |
| 69 | * This is to ensure a login as far as possible. |
| 70 | */ |
| 71 | ServiceFactory sf = new ServiceFactory(); |
| 72 | iConfig = sf.getConfigService(); |
| 73 | |
| 74 | /* |
| 75 | * Developer notes: --------------- Due to |
| 76 | * http://bugs.openmicroscopy.org.uk/show_bug.cgi?id=649 which causes |
| 77 | * the first DB access after a new start or redeploy to fail, all tests |
| 78 | * should attempt to access the DB during setup in a try/catch block |
| 79 | */ |
| 80 | try { |
| 81 | iConfig.getServerTime(); |
| 82 | } catch (Exception ex) { |
| 83 | // ok. |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void testConfigGetServerTime() throws Exception { |
| 90 | assertNotNull(iConfig.getServerTime()); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testConfigGetDBTime() throws Exception { |
| 95 | assertNotNull(iConfig.getDatabaseTime()); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testGetMissingConfigValue() throws Exception { |
| 100 | // HIGHLY unlikely that this will be available |
| 101 | String value = iConfig.getConfigValue(UUID.randomUUID().toString()); |
| 102 | assertNull(value); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | @Test(groups = "ignore") |
| 107 | public void testThisTestDoesntWork() throws Exception { |
| 108 | fail("Should not be called."); |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Developer notes: --------------- We can also test the ability for root or |
| 113 | * a new user to perform the same task. To do this, we'll need to obtain or |
| 114 | * create a Login for root. Here we look up the value from the OmeroContext. |
| 115 | * (This is marked as deprecated, but that may change.) |
| 116 | */ |
| 117 | @Test |
| 118 | public void testAsOtherUsers() throws Exception { |
| 119 | // get default service factory |
| 120 | ServiceFactory sf = new ServiceFactory("ome.client.test"); |
| 121 | Login rootLogin = (Login) sf.getContext().getBean("rootLogin"); |
| 122 | |
| 123 | ServiceFactory rootSf = new ServiceFactory(rootLogin); |
| 124 | IConfig rootConfig = rootSf.getConfigService(); |
| 125 | rootConfig.getConfigValue("foo"); |
| 126 | rootConfig.setConfigValue("foo", "bar"); |
| 127 | |
| 128 | // Now let's create another user. |
| 129 | final IAdmin rootAdmin = rootSf.getAdminService(); |
| 130 | ExperimenterGroup g = new ExperimenterGroup(); |
| 131 | g.setName(UUID.randomUUID().toString()); |
| 132 | rootAdmin.createGroup(g); |
| 133 | Experimenter e = new Experimenter(); |
| 134 | e.setOmeName(UUID.randomUUID().toString()); |
| 135 | e.setFirstName("Config"); |
| 136 | e.setLastName("Test"); |
| 137 | rootAdmin.createUser(e, g.getName()); // Not an admin or system user |
| 138 | rootAdmin.changeUserPassword(e.getOmeName(), "bar"); |
| 139 | |
| 140 | // And use it to login |
| 141 | Login l = new Login(e.getOmeName(), "bar"); |
| 142 | ServiceFactory userSf = new ServiceFactory(l); |
| 143 | assertNotNull(userSf.getConfigService().getServerTime()); |
| 144 | try { |
| 145 | userSf.getConfigService().setConfigValue("foo", "bax"); |
| 146 | fail("Non-system users should not be able to configure the server."); |
| 147 | } catch (Exception ex) { |
| 148 | // ok |
| 149 | } |
| 150 | } |
| 151 | } |
Note: See TracBrowser
for help on using the browser.
