KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > script > rhino > ScriptSelfTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.batik.script.rhino;
18
19 import org.apache.batik.test.*;
20 import org.apache.batik.util.ApplicationSecurityEnforcer;
21 import org.apache.batik.util.ParsedURL;
22 import org.apache.batik.test.svg.SelfContainedSVGOnLoadTest;
23 import org.apache.batik.bridge.UserAgent;
24 import org.apache.batik.bridge.UserAgentAdapter;
25 import org.apache.batik.bridge.ScriptSecurity;
26 import org.apache.batik.bridge.DefaultScriptSecurity;
27 import org.apache.batik.bridge.NoLoadScriptSecurity;
28 import org.apache.batik.bridge.RelaxedScriptSecurity;
29
30 /**
31  * Helper class to simplify writing the unitTesting.xml file for
32  * scripting. The "id" for the test needs to be the path of the
33  * selft contained SVG test, starting from:
34  * <xml-batik-dir>/test-resources/org/apache/batik/
35  *
36  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
37  * @version $Id: ScriptSelfTest.java,v 1.4 2005/04/01 02:28:16 deweese Exp $
38  */

39
40 public class ScriptSelfTest extends SelfContainedSVGOnLoadTest {
41     boolean secure = true;
42     boolean constrain = true;
43     String JavaDoc scripts = "text/ecmascript, application/java-archive";
44     TestUserAgent userAgent = new TestUserAgent();
45
46     public void setId(String JavaDoc id){
47         super.setId(id);
48         svgURL = resolveURL("test-resources/org/apache/batik/" + id + ".svg");
49     }
50
51     public void setSecure(Boolean JavaDoc secure){
52         this.secure = secure.booleanValue();
53     }
54
55     public Boolean JavaDoc getSecure(){
56         return new Boolean JavaDoc(this.secure);
57     }
58
59     public void setConstrain(Boolean JavaDoc constrain){
60         this.constrain = constrain.booleanValue();
61     }
62
63     public Boolean JavaDoc getConstrain(){
64         return new Boolean JavaDoc(this.constrain);
65     }
66
67     public void setScripts(String JavaDoc scripts){
68         this.scripts = scripts;
69     }
70
71     public String JavaDoc getScripts(){
72         return scripts;
73     }
74
75     public TestReport runImpl() throws Exception JavaDoc{
76         ApplicationSecurityEnforcer ase
77             = new ApplicationSecurityEnforcer(this.getClass(),
78                                               "org/apache/batik/apps/svgbrowser/resources/svgbrowser.policy");
79
80         if (secure) {
81             ase.enforceSecurity(true);
82         }
83
84         try {
85             return super.runImpl();
86         } finally {
87             ase.enforceSecurity(false);
88         }
89     }
90
91     protected UserAgent buildUserAgent(){
92         return userAgent;
93     }
94     
95     class TestUserAgent extends UserAgentAdapter {
96         public ScriptSecurity getScriptSecurity(String JavaDoc scriptType,
97                                                 ParsedURL scriptPURL,
98                                                 ParsedURL docPURL){
99             if (scripts.indexOf(scriptType) == -1){
100                 return new NoLoadScriptSecurity(scriptType);
101             } else {
102                 if (constrain){
103                     return new DefaultScriptSecurity
104                         (scriptType, scriptPURL, docPURL);
105                 } else {
106                     return new RelaxedScriptSecurity
107                         (scriptType, scriptPURL, docPURL);
108                 }
109             }
110         }
111     }
112
113 }
114
Popular Tags