View Javadoc

1   package org.apache.maven.shared.scriptinterpreter;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import junit.framework.TestCase;
22  import org.apache.maven.plugin.logging.SystemStreamLog;
23  import org.codehaus.plexus.util.FileUtils;
24  
25  import java.io.File;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  /**
30   * @author Olivier Lamy
31   */
32  public class ScriptRunnerTest
33      extends TestCase
34  {
35      public void testBeanshell()
36          throws Exception
37      {
38          File logFile = new File( "target/build.log" );
39          if ( logFile.exists() )
40          {
41              logFile.delete();
42          }
43          SystemStreamLog systemStreamLog = new SystemStreamLog();
44  
45          ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog );
46          scriptRunner.setGlobalVariable( "globalVar", "Yeah baby it's rocks" );
47          scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ), "verify", buildContext(),
48                            new FileLogger( logFile ), "foo", true );
49  
50          String logContent = FileUtils.fileRead( logFile );
51          assertTrue( logContent.contains( new File( "src/test/resources/bsh-test/verify.bsh" ).getPath() ) );
52          assertTrue( logContent.contains( "foo=bar" ) );
53          assertTrue( logContent.contains( "globalVar=Yeah baby it's rocks"));
54  
55      }
56  
57      public void testBeanshellWithFile()
58          throws Exception
59      {
60          File logFile = new File( "target/build.log" );
61          if ( logFile.exists() )
62          {
63              logFile.delete();
64          }
65          SystemStreamLog systemStreamLog = new SystemStreamLog();
66  
67          ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog );
68          scriptRunner.setGlobalVariable( "globalVar", "Yeah baby it's rocks" );
69          scriptRunner.run( "test", new File( "src/test/resources/bsh-test/verify.bsh" ), buildContext(),
70                            new FileLogger( logFile ), "foo", true );
71  
72          String logContent = FileUtils.fileRead( logFile );
73          assertTrue( logContent.contains( new File( "src/test/resources/bsh-test/verify.bsh" ).getPath() ) );
74          assertTrue( logContent.contains( "foo=bar" ) );
75  
76  
77      }
78  
79      public void testGroovy()
80          throws Exception
81      {
82          File logFile = new File( "target/build.log" );
83          if ( logFile.exists() )
84          {
85              logFile.delete();
86          }
87          SystemStreamLog systemStreamLog = new SystemStreamLog();
88  
89          ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog );
90          scriptRunner.setGlobalVariable( "globalVar", "Yeah baby it's rocks" );
91          scriptRunner.run( "test", new File( "src/test/resources/groovy-test" ), "verify", buildContext(),
92                            new FileLogger( logFile ), "foo", true );
93  
94          String logContent = FileUtils.fileRead( logFile );
95          assertTrue( logContent.contains( new File( "src/test/resources/groovy-test/verify.groovy" ).getPath() ) );
96          assertTrue( logContent.contains( "foo=bar" ) );
97          assertTrue( logContent.contains( "globalVar=Yeah baby it's rocks"));
98  
99      }
100 
101     public void testGroovyWithFile()
102         throws Exception
103     {
104         File logFile = new File( "target/build.log" );
105         if ( logFile.exists() )
106         {
107             logFile.delete();
108         }
109         SystemStreamLog systemStreamLog = new SystemStreamLog();
110 
111         ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog );
112         scriptRunner.run( "test", new File( "src/test/resources/groovy-test/verify.groovy" ), buildContext(),
113                           new FileLogger( logFile ), "foo", true );
114 
115         String logContent = FileUtils.fileRead( logFile );
116         assertTrue( logContent.contains( new File( "src/test/resources/groovy-test/verify.groovy" ).getPath() ) );
117         assertTrue( logContent.contains( "foo=bar" ) );
118 
119 
120     }
121 
122 
123     private Map<String, ? extends Object> buildContext()
124     {
125         Map<String, Object> context = new HashMap<String, Object>();
126         context.put( "foo", "bar" );
127         return context;
128     }
129 
130 }