@Test public void testCalendar() { Date now = new Date(); StringDerivedProperty<Date> calendarProperty = new StringDerivedProperty<Date>( "myproperty", now, new Function<String, Date>() { @Override public Date apply(@Nullable String input) { Date d = new Date(); try { d.setTime(Long.parseLong(input)); } catch (Exception e) { return new Date(0); } return d; } }); assertEquals(now, calendarProperty.getValue()); Date newTime = new Date(); newTime.setTime(now.getTime() + 60000); ConfigurationManager.getConfigInstance() .setProperty("myproperty", String.valueOf(newTime.getTime())); assertEquals(newTime, calendarProperty.getValue()); }
@Test public void testPropertyChanged() { final AtomicBoolean derived = new AtomicBoolean(false); final String defaultVal = "hi"; StringDerivedProperty<String> p = new StringDerivedProperty<String>( "com.netflix.hello", defaultVal, new Function<String, String>() { @Override public String apply(String input) { derived.set(true); return String.format("%s/derived", input); } }); assertEquals(defaultVal, p.getValue()); ConfigurationManager.getConfigInstance().setProperty("com.netflix.hello", "archaius"); assertTrue("derive() was not called", derived.get()); assertEquals(String.format("%s/derived", "archaius"), p.getValue()); }
@Test public void testConfigurationClass() { TestConfiguration config = (TestConfiguration) ConfigurationManager.getConfigInstance(); assertTrue(ConfigurationManager.isConfigurationInstalled()); Object configSource = DynamicPropertyFactory.getInstance().getBackingConfigurationSource(); assertTrue(configSource == config); try { ConfigurationManager.install(new BaseConfiguration()); fail("IllegalStateException expected"); } catch (IllegalStateException e) { assertNotNull(e); } }
@Test public void testPropertyChangedWhenDeriveThrowsException() { final String defaultVal = "hi"; StringDerivedProperty<String> p = new StringDerivedProperty<String>( "com.netflix.test", defaultVal, new Function<String, String>() { @Override public String apply(String input) { throw new RuntimeException("oops"); } }); ConfigurationManager.getConfigInstance().setProperty("com.netflix.test", "xyz"); assertEquals("hi", p.getValue()); }