Skip to content

Commit

Permalink
added test to reproduce issue #50
Browse files Browse the repository at this point in the history
  • Loading branch information
Luigi R. Viggiano committed Sep 18, 2013
1 parent 13ed0ff commit fdd29cb
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/test/java/org/aeonbits/owner/VariablesExpanderForTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2013, Luigi R. Viggiano
* All rights reserved.
*
* This software is distributable under the BSD license.
* See the terms of the BSD license in the documentation provided with this software.
*/

package org.aeonbits.owner;

import java.util.Properties;

/**
* @author Luigi R. Viggiano
*/
public class VariablesExpanderForTest extends VariablesExpander {
public VariablesExpanderForTest(Properties properties) {
super(properties);
}

@Override
public String expand(String path) {
return super.expand(path);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2013, Luigi R. Viggiano
* All rights reserved.
*
* This software is distributable under the BSD license.
* See the terms of the BSD license in the documentation provided with this software.
*/

package org.aeonbits.owner.reload;

import org.aeonbits.owner.Config;
import org.aeonbits.owner.Config.HotReload;
import org.aeonbits.owner.Config.Sources;
import org.aeonbits.owner.ConfigFactory;
import org.aeonbits.owner.Reloadable;
import org.aeonbits.owner.VariablesExpanderForTest;
import org.aeonbits.owner.event.ReloadEvent;
import org.aeonbits.owner.event.ReloadListener;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.net.URL;
import java.util.Properties;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.aeonbits.owner.Config.HotReloadType.ASYNC;
import static org.aeonbits.owner.UtilTest.save;
import static org.junit.Assert.assertEquals;

/**
* @author Luigi R. Viggiano
*/
public class HotReloadWhenURLContainsVariablesTest {
private static final String RESOURCES_DIR = "target/test-generated-resources";
private static final String SPEC = "file:${user.dir}/" + RESOURCES_DIR + "/AutoReloadExample.properties";
private static File target;
final Object reloadLock = new Object();

@Sources(SPEC)
@HotReload(value=5, unit = MILLISECONDS, type = ASYNC)
interface AutoReloadConfig extends Config, Reloadable {
@DefaultValue("5")
Integer someValue();
}

@Before
public void before() throws Throwable {

// here I need to expand SPEC manually to create the file for the test
String spec = new VariablesExpanderForTest(new Properties()).expand(SPEC);

target = new File(new URL(spec).getFile());
save(target, new Properties() {{
setProperty("someValue", "10");
}});

// 1-Jan-1970 (so, the file it's old enough to need reload
target.setLastModified(0);
}

@Test
public void testReloadWorksWhenURLContainsVariablesToExpand()
throws Throwable {

AutoReloadConfig cfg = ConfigFactory.create(AutoReloadConfig.class);
cfg.addReloadListener(new ReloadListener() {
public void reloadPerformed(ReloadEvent event) {
notifyReload();
}
});

assertEquals(new Integer(10), cfg.someValue());

save(target, new Properties() {{
setProperty("someValue", "20");
}});

waitForReload();

assertEquals(new Integer(20), cfg.someValue());

}

@After
public void after() throws Throwable {
target.delete();
}

private void waitForReload() throws InterruptedException {
System.out.println("waitForReload");
synchronized (reloadLock) {
reloadLock.wait(1000);
}
}

private void notifyReload() {
System.out.println("notifyReload()");
synchronized (reloadLock) {
reloadLock.notifyAll();
}
}

}

0 comments on commit fdd29cb

Please sign in to comment.