Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
ConcurrentBeanWrapperTests.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 3k
Category:
Java Develop
Development Platform:
Java
- /*
- * Copyright 2002-2004 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 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 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 TestBean {
- private Properties properties;
- public Properties getProperties() {
- return properties;
- }
- public void setProperties(Properties properties) {
- this.properties = properties;
- }
- }
- }