View Javadoc
1   package org.codehaus.plexus.context;
2   
3   /*
4    * Copyright 2001-2006 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  import org.codehaus.plexus.util.IOUtil;
21  import org.codehaus.plexus.util.InterpolationFilterReader;
22  
23  import java.io.StringReader;
24  import java.io.StringWriter;
25  
26  /**
27   * Generated by JUnitDoclet, a tool provided by ObjectFab GmbH under LGPL.
28   * Please see www.junitdoclet.org, www.gnu.org and www.objectfab.de for
29   * informations about the tool, the licence and the authors.
30   */
31  public class ContextMapAdapterTest
32      extends TestCase
33  {
34      public ContextMapAdapterTest( String name )
35      {
36          super( name );
37      }
38  
39      public void testInterpolation()
40          throws Exception
41      {
42          DefaultContext context = new DefaultContext();
43  
44          context.put( "name", "jason" );
45  
46          context.put( "occupation", "exotic dancer" );
47  
48          ContextMapAdapter adapter = new ContextMapAdapter( context );
49  
50          assertEquals( "jason", (String) adapter.get( "name" ) );
51  
52          assertEquals( "exotic dancer", (String) adapter.get( "occupation" ) );
53  
54          assertNull( adapter.get( "foo" ) );
55      }
56  
57      public void testInterpolationWithContext()
58          throws Exception
59      {
60          DefaultContext context = new DefaultContext();
61          context.put( "name", "jason" );
62          context.put( "noun", "asshole" );
63  
64          String foo = "${name} is an ${noun}. ${not.interpolated}";
65  
66          InterpolationFilterReader reader =
67              new InterpolationFilterReader( new StringReader( foo ), new ContextMapAdapter( context ) );
68  
69          StringWriter writer = new StringWriter();
70          IOUtil.copy( reader, writer );
71  
72          String bar = writer.toString();
73          assertEquals( "jason is an asshole. ${not.interpolated}", bar );
74      }
75  }