/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.imperius.javaspl.samples.bugfix;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import org.apache.imperius.javaspl.Java_SPLPolicyRuleProvider;
import org.apache.imperius.spl.parser.exceptions.SPLException;
/**
* @author xwang
*
*/
public class RunBugFixTest {
public static String policyFolder = "resources/samples/bugfix/policies/";
public static void testImperius20(){
String policyName = "Imperius20";
Map objMap = new Hashtable();
StringBuilder sb = new StringBuilder();
objMap.put("stringBuilder", sb);
testPolicy(policyName, objMap);
}
public static void testImperius22(){
String policyName = "Imperius22";
Map objMap = new Hashtable();
Properties pList = new Properties();
pList.put("bar", "abc");
pList.put("foo", "def");
objMap.put("props", pList);
testPolicy(policyName, objMap);
}
public static void testImperius23(){
String policyName = "Imperius23";
Map objMap = new Hashtable();
Properties pList = new Properties();
pList.put("bar", "abc");
pList.put("foo", "def");
objMap.put("props", pList);
testPolicy(policyName, objMap);
}
public static void testImperius24(){
String policyName = "Imperius24";
Map objMap = new Hashtable();
Properties pList = new Properties();
pList.put("bar", "abc");
pList.put("foo", "def");
objMap.put("props", pList);
SimpleBean1 sb1 = new SimpleBean1();
SimpleBean2 sb2 = new SimpleBean2();
objMap.put("b1", sb1);
objMap.put("b2", sb2);
testPolicy(policyName, objMap);
}
public static void testImperius25(){
String policyName = "Imperius25";
Map objMap = new Hashtable();
ArrayList pList = new MyList();
objMap.put("list1", pList);
testPolicy(policyName, objMap);
}
public static void testImperius26(){
String policyName = "Imperius26";
Map objMap = new Hashtable();
Properties props = new Properties();
Bar bar = new Bar();
props.put("Foo", bar);
objMap.put("list1", props);
testPolicy(policyName, objMap);
}
public static void testImperius27(){
String policyName = "Imperius27";
Map objMap = new Hashtable();
Properties pList = new Properties();
pList.put("bar", "abc");
objMap.put("list1", pList);
testPolicy(policyName, objMap);
}
public static void testImperius28(){
String policyName = "Imperius28";
Map objMap = new Hashtable();
byte array[] = {0, 1};
ByteArrayFactory baf = new ByteArrayFactory(array);
objMap.put("baf", baf);
testPolicy(policyName, objMap);
}
public static void testPolicy(String policyName, Map objMap)
{
try
{
Java_SPLPolicyRuleProvider jspl = Java_SPLPolicyRuleProvider.getInstance();
String aFile = policyFolder + policyName + ".spl";
StringBuffer contents = new StringBuffer();
BufferedReader input = null;
try
{
input = new BufferedReader( new FileReader(aFile) );
String line = null;
while (( line = input.readLine()) != null)
{
contents.append(line);
contents.append(System.getProperty("line.separator"));
}
input.close();
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
try
{
jspl.deletePolicy(policyName);
}
catch(Exception e)
{
}
boolean createReturn = jspl.createPolicy(policyName, contents.toString());
System.out.println("Policy Created : " + policyName + " " + createReturn);
System.out.println("");
Object result = jspl.executePolicy(policyName, objMap);
System.out.println("Result is " + result);
}
catch (SPLException e)
{
e.printStackTrace();
}
}
public static void test(String pFolder) {
policyFolder = pFolder;
testImperius20();
testImperius22();
testImperius23();
testImperius24();
testImperius25();
testImperius26();
testImperius28();
}
}