Skip to content

Commit c9ee543

Browse files
errandirslachiewicz
authored andcommitted
Add FeedbackingValueSource
1 parent 1d05c5f commit c9ee543

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.codehaus.plexus.interpolation;
2+
3+
/*
4+
* Copyright 2001-2008 Codehaus Foundation.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* Adds feedback on any {@link #getValue(String) getValue(String)} call.
21+
* <p>One of the obvious usages is to add FeedbackingValueSource as last value source to {@link Interpolator}
22+
* to add feedback messages indicating not resolved expressions.</p>
23+
*/
24+
public class FeedbackingValueSource extends AbstractValueSource
25+
{
26+
private final String messagePattern;
27+
28+
public FeedbackingValueSource()
29+
{
30+
this( "'${expression}' not resolved" );
31+
}
32+
33+
/**
34+
* @param messagePattern could contain <code>${expression}</code> placeholder
35+
*/
36+
public FeedbackingValueSource( String messagePattern )
37+
{
38+
super( true );
39+
this.messagePattern = messagePattern;
40+
}
41+
42+
@Override
43+
public Object getValue( String expression )
44+
{
45+
addFeedback( messagePattern.replace( "${expression}", expression ) );
46+
return null;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.codehaus.plexus.interpolation;
2+
3+
/*
4+
* Copyright 2001-2008 Codehaus Foundation.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import junit.framework.TestCase;
20+
21+
import static java.util.Collections.singletonMap;
22+
23+
public class FeedbackingValueSourceTest
24+
extends TestCase
25+
{
26+
27+
public void testStandalone()
28+
{
29+
ValueSource valueSource = new FeedbackingValueSource();
30+
assertNull( valueSource.getValue( "test" ) );
31+
assertEquals( 1, valueSource.getFeedback().size() );
32+
assertEquals( "'test' not resolved", valueSource.getFeedback().iterator().next() );
33+
}
34+
35+
public void testAfterResolvedExpression() throws InterpolationException
36+
{
37+
StringSearchInterpolator interpolator = new StringSearchInterpolator();
38+
interpolator.addValueSource( new MapBasedValueSource( singletonMap( "key", "val" ) ) );
39+
interpolator.addValueSource( new FeedbackingValueSource() );
40+
assertEquals( "val", interpolator.interpolate( "${key}" ) );
41+
assertTrue( interpolator.getFeedback().isEmpty() );
42+
}
43+
44+
public void testBeforeResolvedExpression() throws InterpolationException
45+
{
46+
StringSearchInterpolator interpolator = new StringSearchInterpolator();
47+
interpolator.addValueSource( new FeedbackingValueSource("Resolving ${expression}") );
48+
interpolator.addValueSource( new MapBasedValueSource( singletonMap( "key", "val" ) ) );
49+
assertEquals( "val", interpolator.interpolate( "${key}" ) );
50+
assertEquals( 1, interpolator.getFeedback().size() );
51+
assertEquals( "Resolving key", interpolator.getFeedback().iterator().next() );
52+
}
53+
54+
public void testAfterNotResolvedExpression() throws InterpolationException
55+
{
56+
StringSearchInterpolator interpolator = new StringSearchInterpolator();
57+
interpolator.addValueSource( new MapBasedValueSource( singletonMap( "key", "val" ) ) );
58+
interpolator.addValueSource( new FeedbackingValueSource() );
59+
assertEquals( "${other-key}", interpolator.interpolate( "${other-key}" ) );
60+
assertEquals( 1, interpolator.getFeedback().size() );
61+
}
62+
}

0 commit comments

Comments
 (0)