Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage default value in filters #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -69,6 +69,8 @@ public class MultiDelimiterInterpolatorFilterReaderLineEnding
*/
public static final String DEFAULT_END_TOKEN = "}";

public static final char DEFAULT_DEFAULT_VALUE_TOKEN = ':';

/**
* true by default to preserve backward comp
*/
Expand Down Expand Up @@ -238,6 +240,7 @@ public int read()
boolean inEscape = useEscape && ch == getEscapeString().charAt( 0 );

StringBuilder key = new StringBuilder();
StringBuilder defaultValue = new StringBuilder();

// have we found an escape string?
if ( inEscape )
Expand Down Expand Up @@ -341,6 +344,7 @@ public int read()

int endTokenSize = endToken.length();
int end = endTokenSize;
boolean hasDefaultValue = false;
do
{
if ( ch == -1 )
Expand All @@ -353,9 +357,24 @@ else if ( ch == '\n' && !supportMultiLineFiltering )
key.append( (char) ch );
break;
}

key.append( (char) ch );


if (ch == DEFAULT_DEFAULT_VALUE_TOKEN)
{
// just ignore default value token
hasDefaultValue = true;
ch = in.read();
continue;
}

if (hasDefaultValue && ch != this.endToken.charAt( endTokenSize - end ))
{
defaultValue.append( (char) ch );
}
else
{
key.append( (char) ch );
}

if ( ch == this.endToken.charAt( endTokenSize - end ) )
{
end--;
Expand Down Expand Up @@ -386,10 +405,14 @@ else if ( ch == '\n' && !supportMultiLineFiltering )
if ( interpolateWithPrefixPattern )
{
value = interpolator.interpolate( key.toString(), "", recursionInterceptor );
if (value == null && hasDefaultValue)
value = defaultValue.toString();
}
else
{
value = interpolator.interpolate( key.toString(), recursionInterceptor );
if (value == null && hasDefaultValue)
value = defaultValue.toString();
}
}
catch ( InterpolationException e )
Expand Down
Expand Up @@ -102,6 +102,8 @@ public void testLineWithSingleAtAndExpression()
reader.setDelimiterSpecs( new HashSet<String>( Arrays.asList( "${*}", "@" ) ) );

assertEquals( "toto@titi.com bar", IOUtil.toString( reader ) );


}

// http://stackoverflow.com/questions/21786805/maven-war-plugin-customize-filter-delimitters-in-webresources/
Expand All @@ -121,4 +123,25 @@ public void testAtDollarExpression()

assertEquals( " url=\"jdbc:oracle:thin:@DB_SERVER:DB_PORT:DB_NAME\"", IOUtil.toString( reader ) );
}

@Test
public void testDefaultValue()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding tests to cover syntax error cases? Or empty string as default...

throws Exception
{

Reader in = new StringReader( "toto@titi.com ${noValue:defaultValue}" );
MultiDelimiterInterpolatorFilterReaderLineEnding reader = new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
reader.setDelimiterSpecs( new HashSet<String>( Arrays.asList( "${*}", "@" ) ) );

assertEquals( "defaultValue", IOUtil.toString( reader ) );


when( interpolator.interpolate( eq( "${hasAValue}" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "42" );

in = new StringReader( "toto@titi.com ${hasAValue:defaultvalue}" );
reader = new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
reader.setDelimiterSpecs( new HashSet<String>( Arrays.asList( "${*}", "@" ) ) );

assertEquals( "toto@titi.com 42", IOUtil.toString( reader ) );
}
}