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 java.io.StringReader;
20  import java.io.StringWriter;
21  
22  import junit.framework.TestCase;
23  import org.codehaus.plexus.util.IOUtil;
24  import org.codehaus.plexus.util.InterpolationFilterReader;
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 extends TestCase {
32      public ContextMapAdapterTest(String name) {
33          super(name);
34      }
35  
36      public void testInterpolation() throws Exception {
37          DefaultContext context = new DefaultContext();
38  
39          context.put("name", "jason");
40  
41          context.put("occupation", "exotic dancer");
42  
43          ContextMapAdapter adapter = new ContextMapAdapter(context);
44  
45          assertEquals("jason", (String) adapter.get("name"));
46  
47          assertEquals("exotic dancer", (String) adapter.get("occupation"));
48  
49          assertNull(adapter.get("foo"));
50      }
51  
52      public void testInterpolationWithContext() throws Exception {
53          DefaultContext context = new DefaultContext();
54          context.put("name", "jason");
55          context.put("noun", "asshole");
56  
57          String foo = "${name} is an ${noun}. ${not.interpolated}";
58  
59          InterpolationFilterReader reader =
60                  new InterpolationFilterReader(new StringReader(foo), new ContextMapAdapter(context));
61  
62          StringWriter writer = new StringWriter();
63          IOUtil.copy(reader, writer);
64  
65          String bar = writer.toString();
66          assertEquals("jason is an asshole. ${not.interpolated}", bar);
67      }
68  }