📄 localebeanificationtestcase.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * 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 org.apache.commons.beanutils.locale;import java.util.*;import java.lang.ref.WeakReference;import java.lang.ref.ReferenceQueue;import junit.framework.TestCase;import junit.framework.Test;import junit.framework.TestSuite;import org.apache.commons.collections.ReferenceMap;import org.apache.commons.logging.LogFactory;import org.apache.commons.beanutils.ContextClassLoaderLocal;import org.apache.commons.beanutils.PrimitiveBean;/** * <p> * Test Case for changes made during LocaleBeanutils Beanification. * This is basically a cut-and-correct version of the beanutils beanifications tests. * </p> * * @author Robert Burrell Donkin * @author Juozas Baliuka * @version $Revision: 1.2 $ $Date: 2004/02/28 13:18:37 $ */public class LocaleBeanificationTestCase extends TestCase { // ---------------------------------------------------- Constants /** Maximum number of iterations before our test fails */ public static final int MAX_GC_ITERATIONS = 50; // ---------------------------------------------------- Instance Variables // ---------------------------------------------------------- Constructors /** * Construct a new instance of this test case. * * @param name Name of the test case */ public LocaleBeanificationTestCase(String name) { super(name); } // -------------------------------------------------- Overall Test Methods /** * Set up instance variables required by this test case. */ public void setUp() { LocaleConvertUtils.deregister(); } /** * Return the tests included in this test suite. */ public static Test suite() { return (new TestSuite(LocaleBeanificationTestCase.class)); } /** * Tear down instance variables required by this test case. */ public void tearDown() { ; // No action required } // ------------------------------------------------ Individual Test Methods /** Test of the methodology we'll use for some of the later tests */ public void testMemoryTestMethodology() throws Exception { // test methodology // many thanks to Juozas Baliuka for suggesting this method ClassLoader loader = new ClassLoader() {}; WeakReference reference = new WeakReference(loader); Class myClass = loader.loadClass("org.apache.commons.beanutils.BetaBean"); assertNotNull("Weak reference released early", reference.get()); // dereference class loader and class: loader = null; myClass = null; int iterations = 0; int bytz = 2; while(true) { System.gc(); if(iterations++ > MAX_GC_ITERATIONS){ fail("Max iterations reached before resource released."); } if( reference.get() == null ) { break; } else { // create garbage: byte[] b = new byte[bytz]; bytz = bytz * 2; } } } /** Tests whether classloaders and beans are released from memory by the map used by beanutils */ public void testMemoryLeak2() throws Exception { // tests when the map used by beanutils has the right behaviour if (isPre14JVM()) { System.out.println("WARNING: CANNOT TEST MEMORY LEAK ON PRE1.4 JVM"); return; } // many thanks to Juozas Baliuka for suggesting this methodology TestClassLoader loader = new TestClassLoader(); ReferenceQueue queue = new ReferenceQueue(); WeakReference loaderReference = new WeakReference(loader, queue); Integer test = new Integer(1); WeakReference testReference = new WeakReference(test, queue); //Map map = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.HARD, true); Map map = new WeakHashMap(); map.put(loader, test); assertEquals("In map", test, map.get(loader)); assertNotNull("Weak reference released early (1)", loaderReference.get()); assertNotNull("Weak reference released early (2)", testReference.get()); // dereference strong references loader = null; test = null; int iterations = 0; int bytz = 2; while(true) { System.gc(); if(iterations++ > MAX_GC_ITERATIONS){ fail("Max iterations reached before resource released."); } map.isEmpty(); if( loaderReference.get() == null && testReference.get() == null) { break; } else { // create garbage: byte[] b = new byte[bytz]; bytz = bytz * 2; } } } /** Tests whether classloaders and beans are released from memory */ public void testMemoryLeak() throws Exception { if (isPre14JVM()) { System.out.println("WARNING: CANNOT TEST MEMORY LEAK ON PRE1.4 JVM"); return; } // many thanks to Juozas Baliuka for suggesting this methodology TestClassLoader loader = new TestClassLoader(); WeakReference loaderReference = new WeakReference(loader); LocaleBeanUtilsBean.getLocaleBeanUtilsInstance(); class GetBeanUtilsBeanThread extends Thread { LocaleBeanUtilsBean beanUtils; LocaleConvertUtilsBean convertUtils; GetBeanUtilsBeanThread() {} public void run() { beanUtils = LocaleBeanUtilsBean.getLocaleBeanUtilsInstance(); convertUtils = LocaleConvertUtilsBean.getInstance(); // XXX Log keeps a reference around! LogFactory.releaseAll(); } public String toString() { return "GetBeanUtilsBeanThread"; } } GetBeanUtilsBeanThread thread = new GetBeanUtilsBeanThread(); WeakReference threadWeakReference = new WeakReference(thread); thread.setContextClassLoader(loader); thread.start(); thread.join(); WeakReference beanUtilsReference = new WeakReference(thread.beanUtils); WeakReference convertUtilsReference = new WeakReference(thread.convertUtils); assertNotNull("Weak reference released early (1)", loaderReference.get()); assertNotNull("Weak reference released early (2)", beanUtilsReference.get()); assertNotNull("Weak reference released early (4)", convertUtilsReference.get()); // dereference strong references loader = null; thread.setContextClassLoader(null); thread = null; int iterations = 0; int bytz = 2; while(true) { LocaleBeanUtilsBean.getLocaleBeanUtilsInstance(); System.gc(); if(iterations++ > MAX_GC_ITERATIONS){ fail("Max iterations reached before resource released."); } if( loaderReference.get() == null && beanUtilsReference.get() == null && convertUtilsReference.get() == null) { break; } else { // create garbage: byte[] b = new byte[bytz]; bytz = bytz * 2; } } } /** * Tests whether difference instances are loaded by different * context classloaders. */ public void testGetByContextClassLoader() throws Exception { class GetBeanUtilsBeanThread extends Thread { private Signal signal; GetBeanUtilsBeanThread(Signal signal) { this.signal = signal; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -