Source Code Cross Referenced for SecurityManagerTest.java in  » XML » xstream-1.3 » com » thoughtworks » acceptance » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML » xstream 1.3 » com.thoughtworks.acceptance 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) 2006, 2007 XStream Committers.
003:         * All rights reserved.
004:         *
005:         * The software in this package is published under the terms of the BSD
006:         * style license a copy of which has been included with this distribution in
007:         * the LICENSE.txt file.
008:         * 
009:         * Created on 24. March 2006 by Joerg Schaible
010:         */
011:        package com.thoughtworks.acceptance;
012:
013:        import com.thoughtworks.acceptance.objects.Software;
014:        import com.thoughtworks.xstream.XStream;
015:        import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
016:        import com.thoughtworks.xstream.core.JVM;
017:        import com.thoughtworks.xstream.io.xml.DomDriver;
018:        import com.thoughtworks.xstream.testutil.DynamicSecurityManager;
019:
020:        import junit.framework.TestCase;
021:
022:        import java.io.File;
023:        import java.io.FilePermission;
024:        import java.lang.reflect.ReflectPermission;
025:        import java.security.CodeSource;
026:        import java.security.Permission;
027:        import java.security.Policy;
028:        import java.security.cert.Certificate;
029:        import java.util.Iterator;
030:        import java.util.PropertyPermission;
031:
032:        /**
033:         * Test XStream with an active SecurityManager. Note, that it is intentional, that this test is
034:         * not derived from AbstractAcceptanceTest to avoid loaded classes before the SecurityManager is
035:         * in action. Also run each fixture in its own to avoid side-effects.
036:         * 
037:         * @author Jörg Schaible
038:         */
039:        public class SecurityManagerTest extends TestCase {
040:
041:            private XStream xstream;
042:            private DynamicSecurityManager securityManager;
043:            private CodeSource defaultCodeSource;
044:            private File mainClasses;
045:            private File testClasses;
046:            private File libs;
047:            private File libsJDK13;
048:
049:            protected void setUp() throws Exception {
050:                super .setUp();
051:                System.setSecurityManager(null);
052:                defaultCodeSource = new CodeSource(null, (Certificate[]) null);
053:                mainClasses = new File(new File(new File(System
054:                        .getProperty("user.dir"), "target"), "classes"), "-");
055:                testClasses = new File(new File(new File(System
056:                        .getProperty("user.dir"), "target"), "test-classes"),
057:                        "-");
058:                libs = new File(
059:                        new File(System.getProperty("user.dir"), "lib"), "*");
060:                if (!JVM.is14()) {
061:                    libsJDK13 = new File(new File(new File(System
062:                            .getProperty("user.dir"), "lib"), "jdk1.3"), "*");
063:                }
064:                securityManager = new DynamicSecurityManager();
065:                Policy policy = Policy.getPolicy();
066:                securityManager.setPermissions(defaultCodeSource, policy
067:                        .getPermissions(defaultCodeSource));
068:                securityManager.addPermission(defaultCodeSource,
069:                        new RuntimePermission("setSecurityManager"));
070:            }
071:
072:            protected void tearDown() throws Exception {
073:                System.setSecurityManager(null);
074:                super .tearDown();
075:            }
076:
077:            protected void runTest() throws Throwable {
078:                try {
079:                    super .runTest();
080:                } catch (Throwable e) {
081:                    for (final Iterator iter = securityManager
082:                            .getFailedPermissions().iterator(); iter.hasNext();) {
083:                        final Permission permission = (Permission) iter.next();
084:                        System.out.println("SecurityException: Permission "
085:                                + permission.toString());
086:                    }
087:                    throw e;
088:                }
089:            }
090:
091:            public void testSerializeWithXpp3DriverAndSun14ReflectionProviderAndActiveSecurityManager() {
092:                if (JVM.is14()) {
093:                    securityManager.addPermission(defaultCodeSource,
094:                            new FilePermission(mainClasses.toString(), "read"));
095:                    securityManager.addPermission(defaultCodeSource,
096:                            new FilePermission(testClasses.toString(), "read"));
097:                    securityManager.addPermission(defaultCodeSource,
098:                            new FilePermission(libs.toString(), "read"));
099:                    securityManager.addPermission(defaultCodeSource,
100:                            new RuntimePermission("accessDeclaredMembers"));
101:                    securityManager.addPermission(defaultCodeSource,
102:                            new RuntimePermission(
103:                                    "accessClassInPackage.sun.reflect"));
104:                    securityManager.addPermission(defaultCodeSource,
105:                            new RuntimePermission(
106:                                    "accessClassInPackage.sun.misc"));
107:                    securityManager.addPermission(defaultCodeSource,
108:                            new RuntimePermission("createClassLoader"));
109:                    securityManager.addPermission(defaultCodeSource,
110:                            new RuntimePermission("reflectionFactoryAccess"));
111:                    securityManager.addPermission(defaultCodeSource,
112:                            new ReflectPermission("suppressAccessChecks"));
113:                    // permissions necessary for CGLIBMapper
114:                    securityManager.addPermission(defaultCodeSource,
115:                            new PropertyPermission("cglib.debugLocation",
116:                                    "read"));
117:                    securityManager.addPermission(defaultCodeSource,
118:                            new RuntimePermission("getProtectionDomain"));
119:                    securityManager.setReadOnly();
120:                    System.setSecurityManager(securityManager);
121:
122:                    // uses implicit Sun14ReflectionProvider in JDK >= 1.4, since it has the appropriate
123:                    // rights
124:                    xstream = new XStream();
125:
126:                    assertBothWays();
127:                }
128:            }
129:
130:            public void testSerializeWithXpp3DriverAndPureJavaReflectionProviderAndActiveSecurityManager() {
131:                securityManager.addPermission(defaultCodeSource,
132:                        new FilePermission(mainClasses.toString(), "read"));
133:                securityManager.addPermission(defaultCodeSource,
134:                        new FilePermission(testClasses.toString(), "read"));
135:                securityManager.addPermission(defaultCodeSource,
136:                        new FilePermission(libs.toString(), "read"));
137:                if (libsJDK13 != null) {
138:                    securityManager.addPermission(defaultCodeSource,
139:                            new FilePermission(libsJDK13.toString(), "read"));
140:                }
141:                securityManager.addPermission(defaultCodeSource,
142:                        new RuntimePermission("accessDeclaredMembers"));
143:                securityManager.addPermission(defaultCodeSource,
144:                        new RuntimePermission("createClassLoader"));
145:                securityManager.addPermission(defaultCodeSource,
146:                        new ReflectPermission("suppressAccessChecks"));
147:                // permissions necessary for CGLIBMapper
148:                securityManager.addPermission(defaultCodeSource,
149:                        new PropertyPermission("cglib.debugLocation", "read"));
150:                securityManager.addPermission(defaultCodeSource,
151:                        new RuntimePermission("getProtectionDomain"));
152:                securityManager.setReadOnly();
153:                System.setSecurityManager(securityManager);
154:
155:                xstream = new XStream(new PureJavaReflectionProvider());
156:
157:                assertBothWays();
158:            }
159:
160:            public void testSerializeWithDomDriverAndPureJavaReflectionProviderAndActiveSecurityManager() {
161:                securityManager.addPermission(defaultCodeSource,
162:                        new FilePermission(mainClasses.toString(), "read"));
163:                securityManager.addPermission(defaultCodeSource,
164:                        new FilePermission(testClasses.toString(), "read"));
165:                securityManager.addPermission(defaultCodeSource,
166:                        new FilePermission(libs.toString(), "read"));
167:                if (libsJDK13 != null) {
168:                    securityManager.addPermission(defaultCodeSource,
169:                            new FilePermission(libsJDK13.toString(), "read"));
170:                }
171:                securityManager.addPermission(defaultCodeSource,
172:                        new RuntimePermission("accessDeclaredMembers"));
173:                securityManager.addPermission(defaultCodeSource,
174:                        new RuntimePermission("createClassLoader"));
175:                securityManager.addPermission(defaultCodeSource,
176:                        new ReflectPermission("suppressAccessChecks"));
177:                // permissions necessary for CGLIBMapper
178:                securityManager.addPermission(defaultCodeSource,
179:                        new PropertyPermission("cglib.debugLocation", "read"));
180:                securityManager.addPermission(defaultCodeSource,
181:                        new RuntimePermission("getProtectionDomain"));
182:                securityManager.setReadOnly();
183:                System.setSecurityManager(securityManager);
184:
185:                // uses implicit PureJavaReflectionProvider, since Sun14ReflectionProvider cannot be
186:                // loaded
187:                xstream = new XStream(new DomDriver());
188:
189:                assertBothWays();
190:            }
191:
192:            private void assertBothWays() {
193:
194:                xstream.alias("software", Software.class);
195:
196:                final Software sw = new Software("jw", "xstr");
197:                final String xml = "<software>\n" + "  <vendor>jw</vendor>\n"
198:                        + "  <name>xstr</name>\n" + "</software>";
199:
200:                String resultXml = xstream.toXML(sw);
201:                assertEquals(xml, resultXml);
202:                Object resultRoot = xstream.fromXML(resultXml);
203:                if (!sw.equals(resultRoot)) {
204:                    assertEquals("Object deserialization failed",
205:                            "DESERIALIZED OBJECT\n" + xstream.toXML(sw),
206:                            "DESERIALIZED OBJECT\n" + xstream.toXML(resultRoot));
207:                }
208:            }
209:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.