forked from matteobaccan/owner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added pre processing feature for properties. Resolves matteobaccan#120
- Loading branch information
Luigi R. Viggiano
committed
Apr 16, 2015
1 parent
79124c2
commit 4d39966
Showing
14 changed files
with
219 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
TODO LIST | ||
========= | ||
|
||
Web site: | ||
- [ ] Write documentation for pre processing feature | ||
- [ ] Write documentation for JMX support | ||
- [ ] Update the release note. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (c) 2012-2015, 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; | ||
|
||
/** | ||
* Preprocessor interface specifies how to pre-process an input string coming from a property value before being used by | ||
* OWNER. | ||
* | ||
* @author Luigi R. Viggiano | ||
* @since 1.0.9 | ||
*/ | ||
public interface Preprocessor { | ||
String process(String input); | ||
} |
48 changes: 48 additions & 0 deletions
48
owner/src/main/java/org/aeonbits/owner/PreprocessorResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2012-2015, 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 org.aeonbits.owner.Config.PreprocessorClasses; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static org.aeonbits.owner.Util.newInstance; | ||
|
||
/** | ||
* @author Luigi R. Viggiano | ||
*/ | ||
final class PreprocessorResolver { | ||
|
||
/** Don't let anyone instantiate this class */ | ||
private PreprocessorResolver() {} | ||
|
||
public static List<Preprocessor> resolvePreprocessors(Method method) { | ||
List<Preprocessor> result = new ArrayList<Preprocessor>(); | ||
List<Preprocessor> preprocessorsOnMethod = getPreprocessor(method.getAnnotation(PreprocessorClasses.class)); | ||
result.addAll(preprocessorsOnMethod); | ||
|
||
List<Preprocessor> preprocessorsOnClass = getPreprocessor(method.getDeclaringClass().getAnnotation(PreprocessorClasses.class)); | ||
result.addAll(preprocessorsOnClass); | ||
|
||
return result; | ||
} | ||
|
||
private static List<Preprocessor> getPreprocessor(PreprocessorClasses preprocessorClassesAnnotation) { | ||
if (preprocessorClassesAnnotation == null) return emptyList(); | ||
Class<? extends Preprocessor>[] preprocessorClasses = preprocessorClassesAnnotation.value(); | ||
if (preprocessorClasses == null) return emptyList(); | ||
List<Preprocessor> result = new LinkedList<Preprocessor>(); | ||
return newInstance(preprocessorClassesAnnotation.value(), result); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
owner/src/test/java/org/aeonbits/owner/PreprocessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (c) 2012-2015, 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 org.aeonbits.owner.Config.PreprocessorClasses; | ||
import org.junit.Test; | ||
|
||
import static java.lang.Math.max; | ||
import static java.lang.Math.min; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
/** | ||
* Issue #120 | ||
* | ||
* @author Luigi R. Viggiano | ||
*/ | ||
public class PreprocessorTest { | ||
|
||
@PreprocessorClasses({ SkipInlineComments.class, Trim.class }) | ||
public interface ConfigWithPreprocessors extends Config { | ||
@DefaultValue(" 300 ") | ||
Integer pollingPeriod(); | ||
|
||
@PreprocessorClasses(ToLowerCase.class) | ||
@DefaultValue(" HelloWorld ") | ||
String helloWorld(); | ||
|
||
@DefaultValue(" This is the value ! this is an inline comment # this is the inline comment too") | ||
String skipsInlineComments(); | ||
|
||
@PreprocessorClasses(ToLowerCase.class) | ||
String nullValue(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnTrimmedValue() { | ||
ConfigWithPreprocessors cfg = ConfigFactory.create(ConfigWithPreprocessors.class); | ||
assertEquals(new Integer(300), cfg.pollingPeriod()); | ||
} | ||
|
||
@Test | ||
public void shouldReturnLowercasedTrimmedHelloWorld() { | ||
ConfigWithPreprocessors cfg = ConfigFactory.create(ConfigWithPreprocessors.class); | ||
assertEquals("helloworld", cfg.helloWorld()); | ||
} | ||
|
||
@Test | ||
public void shouldSkipInlineComments() { | ||
ConfigWithPreprocessors cfg = ConfigFactory.create(ConfigWithPreprocessors.class); | ||
assertEquals("This is the value", cfg.skipsInlineComments()); | ||
} | ||
|
||
@Test | ||
public void shouldNotThrowExceptions() { | ||
ConfigWithPreprocessors cfg = ConfigFactory.create(ConfigWithPreprocessors.class); | ||
assertNull(cfg.nullValue()); | ||
} | ||
|
||
|
||
// preprocessors implementation | ||
|
||
public static class Trim implements Preprocessor { | ||
public String process(String input) { | ||
return input.trim(); | ||
} | ||
} | ||
|
||
public static class ToLowerCase implements Preprocessor { | ||
public String process(String input) { | ||
return input.toLowerCase(); | ||
} | ||
} | ||
|
||
public static class SkipInlineComments implements Preprocessor { | ||
public String process(String input) { | ||
int hashTagIndex = input.indexOf('#'); | ||
int exclamationMarkIndex = input.indexOf("!"); | ||
if (hashTagIndex == -1 && exclamationMarkIndex == -1) | ||
return input; // comments not present. | ||
|
||
int commentIndex = min(hashTagIndex, exclamationMarkIndex); | ||
if (commentIndex == -1) | ||
commentIndex = max(hashTagIndex, exclamationMarkIndex); | ||
|
||
return input.substring(0, commentIndex); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters