| 1 | package org.aeonbits.owner.reload; |
| 2 | |
| 3 | import org.aeonbits.owner.*; |
| 4 | import org.aeonbits.owner.Config.HotReload; |
| 5 | import org.aeonbits.owner.Config.Sources; |
| 6 | import org.aeonbits.owner.event.ReloadEvent; |
| 7 | import org.aeonbits.owner.event.ReloadListener; |
| 8 | import org.aeonbits.owner.util.SystemProviderForTest; |
| 9 | import org.aeonbits.owner.util.UtilTest; |
| 10 | import org.junit.After; |
| 11 | import org.junit.Before; |
| 12 | import org.junit.Test; |
| 13 | |
| 14 | import java.util.HashMap; |
| 15 | import java.util.Properties; |
| 16 | |
| 17 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 18 | import static org.aeonbits.owner.Config.HotReloadType.ASYNC; |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | |
| 21 | public class SystemPropertiesReloadTest extends AsyncReloadSupport { |
| 22 | private SystemProviderForTest systemForTest = new SystemProviderForTest(new Properties(), new HashMap<String, String>()); |
| 23 | private Object save; |
| 24 | |
| 25 | @Sources("system:properties") |
| 26 | @HotReload(value = 1, unit = MILLISECONDS, type = ASYNC) |
| 27 | interface AsyncAutoReloadConfig extends Config, Reloadable { |
| 28 | @DefaultValue("5") |
| 29 | Integer someValue(); |
| 30 | } |
| 31 | |
| 32 | @Before |
| 33 | public void before() { |
| 34 | this.save = UtilTest.setSystem(systemForTest); |
| 35 | } |
| 36 | |
| 37 | @After |
| 38 | public void after() { |
| 39 | UtilTest.setSystem(save); |
| 40 | } |
| 41 | |
| 42 | @Test |
| 43 | public void testReload() throws Throwable { |
| 44 | |
| 45 | AsyncAutoReloadConfig cfg = ConfigFactory.create(AsyncAutoReloadConfig.class); |
| 46 | |
| 47 | cfg.addReloadListener(new ReloadListener() { |
| 48 | public void reloadPerformed(ReloadEvent event) { |
| 49 | notifyReload(); |
| 50 | } |
| 51 | }); |
| 52 | |
| 53 | assertEquals(Integer.valueOf(5), cfg.someValue()); |
| 54 | |
| 55 | String propKey = "someValue"; |
| 56 | systemForTest.setProperty(propKey, "5"); |
| 57 | waitForReload(10); |
| 58 | assertEquals(Integer.valueOf(5), cfg.someValue()); |
| 59 | |
| 60 | systemForTest.setProperty(propKey, "20"); |
| 61 | waitForReload(10); |
| 62 | assertEquals(Integer.valueOf(20), cfg.someValue()); |
| 63 | |
| 64 | systemForTest.remove(propKey); |
| 65 | waitForReload(10); |
| 66 | assertEquals(Integer.valueOf(5), cfg.someValue()); |
| 67 | |
| 68 | systemForTest.setProperty(propKey, "30"); |
| 69 | waitForReload(10); |
| 70 | assertEquals(Integer.valueOf(30), cfg.someValue()); |
| 71 | } |
| 72 | } |