/**
 * Copyright 2013 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.netflix.config;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicBoolean;

import com.google.common.base.Function;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class StringDerivedPropertyTest {

    @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 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());
    }

}

@@ -644,16 +644,128 @@
 .Test;%0A%0A
+import java.text.DateFormat;%0Aimport java.text.ParseException;%0Aimport java.util.Calendar;%0Aimport java.util.Date;%0A
 import j
@@ -803,24 +803,59 @@
 icBoolean;%0A%0A
+import javax.annotation.Nullable;%0A%0A
 import com.g
@@ -2394,12 +2394,915 @@
 );%0A    %7D
+%0A    %0A    @Test%0A    public void testCalendar() %7B%0A        Date now = new Date();%0A        StringDerivedProperty%3CDate%3E calendarProperty = new StringDerivedProperty%3CDate%3E(%22myproperty%22, now, new Function%3CString, Date%3E() %7B%0A            @Override%0A            public Date apply(@Nullable String input) %7B%0A                Date d = new Date();%0A                try %7B%0A                    d.setTime(Long.parseLong(input));%0A                %7D catch (Exception e) %7B%0A                    return new Date(0);%0A                %7D%0A                return d;%0A            %7D            %0A        %7D);%0A        assertEquals(now, calendarProperty.getValue());%0A        %0A        Date newTime = new Date();%0A        newTime.setTime(now.getTime() + 60000);%0A        %0A        ConfigurationManager.getConfigInstance().setProperty(%22myproperty%22, String.valueOf(newTime.getTime()));%0A        assertEquals(newTime, calendarProperty.getValue());%0A    %7D
 %0A%0A%7D%0A

Add one more test for StringDerivedProperty.