KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > 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.bridge;
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
24 import java.security.AccessController JavaDoc;
25 import java.security.AccessControlContext JavaDoc;
26 import java.security.CodeSource JavaDoc;
27 import java.security.PrivilegedExceptionAction JavaDoc;
28 import java.security.PrivilegedActionException JavaDoc;
29 import java.security.ProtectionDomain JavaDoc;
30 import java.security.Permission JavaDoc;
31 import java.security.PermissionCollection JavaDoc;
32 import java.security.Permissions JavaDoc;
33 import java.security.Policy JavaDoc;
34
35 import java.io.FilePermission JavaDoc;
36 import java.io.File JavaDoc;
37
38 import java.net.URL JavaDoc;
39
40 import java.util.Enumeration JavaDoc;
41 /**
42  * Helper class to simplify writing the unitTesting.xml file for
43  * the bridge.
44  *
45  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
46  * @version $Id: ScriptSelfTest.java,v 1.8 2005/04/01 02:28:16 deweese Exp $
47  */

48
49 public class ScriptSelfTest extends SelfContainedSVGOnLoadTest {
50     String JavaDoc scripts = "text/ecmascript, application/java-archive";
51     boolean secure = true;
52     String JavaDoc scriptOrigin = "any";
53     String JavaDoc fileName;
54
55     TestUserAgent userAgent = new TestUserAgent();
56
57     public void setId(String JavaDoc id){
58         super.setId(id);
59
60         if (id != null) {
61             int i = id.indexOf("(");
62             if (i != -1) {
63                 id = id.substring(0, i);
64             }
65             fileName = "test-resources/org/apache/batik/bridge/" + id + ".svg";
66             svgURL = resolveURL(fileName);
67         }
68     }
69
70     public void setSecure(boolean secure){
71         this.secure = secure;
72     }
73
74     public boolean getSecure(){
75         return secure;
76     }
77
78     public String JavaDoc getScriptOrigin() {
79         return scriptOrigin;
80     }
81
82     public void setScriptOrigin(String JavaDoc scriptOrigin) {
83         this.scriptOrigin = scriptOrigin;
84     }
85
86     public void setScripts(String JavaDoc scripts){
87         this.scripts = scripts;
88     }
89
90     public String JavaDoc getScripts(){
91         return scripts;
92     }
93
94     public TestReport runImpl() throws Exception JavaDoc {
95         ApplicationSecurityEnforcer ase
96             = new ApplicationSecurityEnforcer(this.getClass(),
97                                               "org/apache/batik/apps/svgbrowser/resources/svgbrowser.policy");
98
99         if (secure) {
100             ase.enforceSecurity(true);
101         }
102
103         try {
104             return super.runImpl();
105         } catch (ExceptionInInitializerError JavaDoc e) {
106             e.printStackTrace();
107             throw e;
108         } catch (NoClassDefFoundError JavaDoc e) {
109             // e.printStackTrace();
110
throw new Exception JavaDoc(e.getMessage());
111         } finally {
112             ase.enforceSecurity(false);
113         }
114     }
115
116     protected UserAgent buildUserAgent(){
117         return userAgent;
118     }
119     
120     class TestUserAgent extends UserAgentAdapter {
121         public ScriptSecurity getScriptSecurity(String JavaDoc scriptType,
122                                                 ParsedURL scriptPURL,
123                                                 ParsedURL docPURL){
124             ScriptSecurity scriptSecurity = null;
125             if (scripts.indexOf(scriptType) == -1){
126                 scriptSecurity = new NoLoadScriptSecurity(scriptType);
127             } else {
128                 if ("any".equals(scriptOrigin)) {
129                      scriptSecurity = new RelaxedScriptSecurity
130                         (scriptType, scriptPURL, docPURL);
131                 } else if ("document".equals(scriptOrigin)) {
132                     scriptSecurity = new DefaultScriptSecurity
133                         (scriptType, scriptPURL, docPURL);
134                 } else if ("embeded".equals(scriptOrigin)) {
135                     scriptSecurity = new EmbededScriptSecurity
136                         (scriptType, scriptPURL, docPURL);
137                 } else if ("none".equals(scriptOrigin)) {
138                     scriptSecurity = new NoLoadScriptSecurity(scriptType);
139                 } else {
140                     throw new Error JavaDoc("Wrong scriptOrigin : " + scriptOrigin);
141                 }
142             }
143
144             return scriptSecurity;
145         }
146     }
147
148 }
149
Popular Tags