Java tutorial
/* * Copyright (c) 2012 NTT DATA Corporation * * 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 jp.terasoluna.fw.beans.jxpath; import static java.util.Arrays.asList; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import static uk.org.lidalia.slf4jtest.LoggingEvent.info; import static org.junit.Assert.assertTrue; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import org.apache.commons.jxpath.JXPathIntrospector; import org.junit.After; import org.junit.Before; import org.junit.Test; import uk.org.lidalia.slf4jtest.TestLogger; import uk.org.lidalia.slf4jtest.TestLoggerFactory; /** * {@link jp.terasoluna.fw.beans.jxpath.JXPATH152PatchActivator} ? * <p> * <h4>???</h4> commons-JXPath??(JXPATH-152)??<br> * ????<br> * ????JVM????????<br> * ?????JVM?????????<br> * (?????????????????? ????????? ?????????????JVM????????)<br> * ????JRE??<br> * (???JRE/lib/security/java.policy)???<br> * permission java.lang.reflect.ReflectPermission "suppressAccessChecks";<br> * ???????(??????????????)<br> * ??????class???class?????????? * <p> * @see jp.terasoluna.fw.beans.jxpath.JXPATH152PatchActivator */ public class JXPATH152PatchActivatorTest { private Map<?, ?> byClassBak = null; private Map<?, ?> byInterfaceBak = null; private TestLogger logger = TestLoggerFactory.getTestLogger(JXPATH152PatchActivator.class); /** * ??? * @throws Exception ????? */ @Before public void setUp() throws Exception { // ?????? // (?static) new JXPATH152PatchActivator(); // JXPATH152PatchActivator?static??? // (tearDown?static????????????) Field field = JXPathIntrospector.class.getDeclaredField("byClass"); field.setAccessible(true); byClassBak = (Map<?, ?>) field.get(JXPathIntrospector.class); field = JXPathIntrospector.class.getDeclaredField("byInterface"); field.setAccessible(true); byInterfaceBak = (Map<?, ?>) field.get(JXPathIntrospector.class); logger.clear(); } /** * ?? * @throws Exception ????? */ @After public void tearDown() throws Exception { logger.clear(); Field field = JXPathIntrospector.class.getDeclaredField("byClass"); field.setAccessible(true); field.set(JXPathIntrospector.class, byClassBak); field = JXPathIntrospector.class.getDeclaredField("byInterface"); field.setAccessible(true); field.set(JXPathIntrospector.class, byInterfaceBak); } /** * testActivate01() <br> * <br> * () <br> * A <br> * <br> * () ?<br> * () JXPathIntrospector.byClasst:HashMap<br> * () JXPathIntrospector.byInterface:HashMap<br> * <br> * () JXPathIntrospector.byClass?HashMapForJXPathIntrospector?????<br> * () JXPathIntrospector.byInterface?HashMapForJXPathIntrospector?????<br> * () :INFO<br> * JXPATH-152 Patch activation succeeded.<br> * <br> * ??????? JXPathIntrospector?byClass?byInterface?HashMapForJXPathIntrospector???????? <br> * @throws Exception ????? */ @Test public void testActivate01() throws Exception { // ?? Field field = JXPathIntrospector.class.getDeclaredField("byClass"); field.setAccessible(true); field.set(JXPathIntrospector.class, new HashMap<Object, Object>()); field = JXPathIntrospector.class.getDeclaredField("byInterface"); field.setAccessible(true); field.set(JXPathIntrospector.class, new HashMap<Object, Object>()); // Method method = JXPATH152PatchActivator.class.getDeclaredMethod("activate"); method.setAccessible(true); method.invoke(JXPATH152PatchActivator.class); // assertThat(logger.getLoggingEvents(), is(asList(info("JXPATH-152 Patch activation succeeded.")))); field = JXPathIntrospector.class.getDeclaredField("byClass"); field.setAccessible(true); assertTrue( ((Map<?, ?>) field.get(JXPathIntrospector.class)).getClass() == HashMapForJXPathIntrospector.class); field = JXPathIntrospector.class.getDeclaredField("byInterface"); field.setAccessible(true); assertTrue( ((Map<?, ?>) field.get(JXPathIntrospector.class)).getClass() == HashMapForJXPathIntrospector.class); } /** * testActivate01() <br> * <br> * () <br> * A <br> * <br> * () ?<br> * () JXPathIntrospector.byClasst:HashMap<br> * () JXPathIntrospector.byInterface:HashMap<br> * <br> * () JXPathIntrospector.byClass?HashMap???<br> * () JXPathIntrospector.byInterface?HashMap???<br> * () :FATAL<br> * JXPATH-152 Patch activation failed.<br> * <br> * ?????? FATAL???? <br> * @throws Exception ????? */ @Test public void testActivate02() throws Exception { // TODO // Java7??AccessController.doPrivileged()????AccessControlContext?DomainCombiner#combine()?????????????????? // // // final ProtectionDomain testTargetProtectionDomain = JXPATH152PatchActivator.class.getProtectionDomain(); // DomainCombiner domainCombiner = new DomainCombiner() { // public ProtectionDomain[] combine( // ProtectionDomain[] currentDomains, // ProtectionDomain[] assignedDomains) { // ProtectionDomain[] ret = new ProtectionDomain[currentDomains.length]; // for (int i = 0; i < currentDomains.length ;i++) { // // ??? // // (?????)??????? // if (currentDomains[i].getCodeSource() != testTargetProtectionDomain.getCodeSource()) { // Permissions permissions = new Permissions(); // permissions.add(new AllPermission()); // ProtectionDomain pd = new ProtectionDomain(currentDomains[i].getCodeSource(), permissions); // ret[i] = pd; // } else { // // (?????)??????? // // (Field#setAccessible???) // ret[i] = currentDomains[i]; // } // } // return ret; // } // }; // AccessControlContext acc = new AccessControlContext(AccessController.getContext(), domainCombiner); // System.setSecurityManager(new SecurityManager()); // // // ?DomainCombiner????? // AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { // // public Void run() throws Exception { // try { // // ?? // UTUtil.setPrivateField(JXPathIntrospector.class, "byClass", new HashMap()); // UTUtil.setPrivateField(JXPathIntrospector.class, "byInterface", new HashMap()); // // // // UTUtil.invokePrivate(JXPATH152PatchActivator.class, "activate"); // // // // assertTrue(LogUTUtil.checkFatal("JXPATH-152 Patch activation failed.", new AccessControlException(""))); // assertTrue(UTUtil.getPrivateField(JXPathIntrospector.class, "byClass").getClass() == HashMap.class); // assertTrue(UTUtil.getPrivateField(JXPathIntrospector.class, "byInterface").getClass() == HashMap.class); // } finally { // System.setSecurityManager(null); // } // // return null; // } // // }, acc); } }