View Javadoc
1   package org.codehaus.plexus.velocity;
2   
3   /*
4    * Copyright 2001-2016 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 javax.inject.Inject;
20  
21  import java.io.StringWriter;
22  
23  import org.apache.velocity.Template;
24  import org.apache.velocity.VelocityContext;
25  import org.codehaus.plexus.testing.PlexusTest;
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.assertNotNull;
30  
31  @PlexusTest
32  public class DefaultVelocityComponentTest {
33  
34      @Inject
35      private VelocityComponent velocity;
36  
37      @Test
38      public void testBasic() {
39  
40          // test the properties
41          String value = (String) velocity.getEngine().getProperty("hello");
42  
43          assertNotNull(value);
44          assertEquals("world", value);
45  
46          // test the rendering
47          VelocityContext context = new VelocityContext();
48  
49          context.put("variable", "Value from context");
50  
51          Template template =
52                  velocity.getEngine().getTemplate("org/codehaus/plexus/velocity/DefaultVelocityComponentTest.vm");
53  
54          StringWriter writer = new StringWriter();
55  
56          template.merge(context, writer);
57  
58          assertEquals("Static text -- Value from context -- More static text", writer.toString());
59      }
60  }