/*
 * Copyright 2006-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.consol.citrus.util;

import javax.script.ScriptException;

import org.testng.Assert;
import org.testng.annotations.Test;

import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.testng.AbstractBaseTest;
import com.consol.citrus.variable.VariableUtils;

/**
 * @author Jan Lipphaus
 */
public class VariableUtilsTest extends AbstractBaseTest {
	
	private String validGroovyScript = "a = 1";
	private String groovyScriptResult = "1";
	private String invalidGroovyScript = "a";
	private String validScriptEngine = "groovy";
	private String invalidScriptEngine = "invalidScriptEngine";
	
	/**
	 * Test for correct return with valid script
	 */
	@Test
	public void testValidScript() {
		String result = VariableUtils.getValueFromScript(validScriptEngine, validGroovyScript);
		Assert.assertEquals(result, groovyScriptResult);
	}
	
	/**
	 * Test for correct exception with invalid script
	 */
	@Test
	public void testInvalidScript() {
		try {
		    VariableUtils.getValueFromScript(validScriptEngine, invalidGroovyScript);
		} catch (CitrusRuntimeException e) {
			Assert.assertTrue(e.getCause() instanceof ScriptException);
			return;
		}
		
		Assert.fail("Missing CitrusRuntimeException because of invalid groovy script");
	}
	
	/**
	 * Test for correct exception with invalid script engine
	 */
	@Test
	public void testInvalidScriptEngine() {
		try {
		    VariableUtils.getValueFromScript(invalidScriptEngine, validGroovyScript);
		} catch (CitrusRuntimeException e) {
			Assert.assertTrue(e.getMessage().contains(invalidScriptEngine));
			return;
		}
		
		Assert.fail("Missing CitrusRuntimeException because of invalid script engine");
	}
}

@@ -998,19 +998,20 @@
 eTest %7B%0A
-%09%0A%09
+    
 private 
@@ -1042,25 +1042,28 @@
  = %22a = 1%22;%0A
-%09
+    
 private Stri
@@ -1091,17 +1091,20 @@
  = %221%22;%0A
-%09
+    
 private 
@@ -1137,17 +1137,20 @@
  = %22a%22;%0A
-%09
+    
 private 
@@ -1186,17 +1186,20 @@
 roovy%22;%0A
-%09
+    
 private 
@@ -1250,24 +1250,29 @@
 ngine%22;%0A
-%09%0A%09/**%0A%09
+%0A    /**%0A    
  * Test 
@@ -1304,37 +1304,46 @@
 alid script%0A
-%09 */%0A%09
+     */%0A    
 @Test%0A
-%09
+    
 public void 
@@ -1362,18 +1362,24 @@
 ipt() %7B%0A
-%09%09
+        
 String r
@@ -1458,18 +1458,24 @@
 cript);%0A
-%09%09
+        
 Assert.a
@@ -1515,27 +1515,35 @@
 esult);%0A
-%09%7D%0A%09%0A%09/**%0A%09
+    %7D%0A%0A    /**%0A    
  * Test 
@@ -1584,29 +1584,38 @@
  script%0A
-%09 */%0A%09
+     */%0A    
 @Test%0A
-%09
+    
 public v
@@ -1636,34 +1636,46 @@
 dScript() %7B%0A
-%09%09try %7B%0A%09%09
+        try %7B%0A        
     Variable
@@ -1732,34 +1732,40 @@
 dGroovyScript);%0A
-%09%09
+        
 %7D catch (CitrusR
@@ -1777,35 +1777,44 @@
 eException e) %7B%0A
-%09%09%09
+            
 Assert.assertTru
@@ -1857,36 +1857,55 @@
 ption);%0A
-%09%09%09return;%0A%09%09%7D%0A%09%09%0A%09%09
+            return;%0A        %7D%0A%0A        
 Assert.f
@@ -1980,19 +1980,27 @@
 %22);%0A
-%09%7D%0A%09%0A%09/**%0A%09
+    %7D%0A%0A    /**%0A    
  * T
@@ -2056,21 +2056,30 @@
 ine%0A
-%09 */%0A%09
+     */%0A    
 @Test%0A
-%09
+    
 publ
@@ -2118,18 +2118,30 @@
 ) %7B%0A
-%09%09try %7B%0A%09%09
+        try %7B%0A        
     
@@ -2214,18 +2214,24 @@
 cript);%0A
-%09%09
+        
 %7D catch 
@@ -2259,19 +2259,28 @@
 on e) %7B%0A
-%09%09%09
+            
 Assert.a
@@ -2340,28 +2340,47 @@
 ));%0A
-%09%09%09return;%0A%09%09%7D%0A%09%09%0A%09%09
+            return;%0A        %7D%0A%0A        
 Asse
@@ -2459,9 +2459,12 @@
 %22);%0A
-%09
+    
 %7D%0A%7D%0A

Create variable with Groovy script