|
Spring Framework example source code file (ConcurrentBeanWrapperTests.java)
The Spring Framework ConcurrentBeanWrapperTests.java source code/* * Copyright 2002-2005 the original author or authors. * * 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.springframework.beans; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Properties; import java.util.Set; import junit.framework.TestCase; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * @author Guillaume Poirier * @author Juergen Hoeller * @since 08.03.2004 */ public class ConcurrentBeanWrapperTests extends TestCase { private final Log logger = LogFactory.getLog(getClass()); private Set set = Collections.synchronizedSet(new HashSet()); private Throwable ex = null; public void testSingleThread() { for (int i = 0; i < 100; i++) { performSet(); } } public void testConcurrent() { for (int i = 0; i < 10; i++) { TestRun run = new TestRun(this); set.add(run); Thread t = new Thread(run); t.setDaemon(true); t.start(); } logger.info("Thread creation over, " + set.size() + " still active."); synchronized (this) { while (!set.isEmpty() && ex == null) { try { wait(); } catch (InterruptedException e) { logger.info(e.toString()); } logger.info(set.size() + " threads still active."); } } if (ex != null) { fail(ex.getMessage()); } } private static void performSet() { TestBean bean = new TestBean(); Properties p = (Properties) System.getProperties().clone(); assertTrue("The System properties must not be empty", p.size() != 0); for (Iterator i = p.entrySet().iterator(); i.hasNext();) { i.next(); if (Math.random() > 0.9) { i.remove(); } } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { p.store(buffer, null); } catch (IOException e) { // ByteArrayOutputStream does not throw // any IOException } String value = new String(buffer.toByteArray()); BeanWrapperImpl wrapper = new BeanWrapperImpl(bean); wrapper.setPropertyValue("properties", value); assertEquals(p, bean.getProperties()); } private static class TestRun implements Runnable { private ConcurrentBeanWrapperTests test; public TestRun(ConcurrentBeanWrapperTests test) { this.test = test; } public void run() { try { for (int i = 0; i < 100; i++) { performSet(); } } catch (Throwable e) { test.ex = e; } finally { synchronized (test) { test.set.remove(this); test.notifyAll(); } } } } private static class TestBean { private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } } } Other Spring Framework examples (source code examples)Here is a short list of links related to this Spring Framework ConcurrentBeanWrapperTests.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.