ConcurrentBeanWrapperTests.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 3k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.beans;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.util.Collections;
  20. import java.util.HashSet;
  21. import java.util.Iterator;
  22. import java.util.Properties;
  23. import java.util.Set;
  24. import junit.framework.TestCase;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. /**
  28.  * @author Guillaume Poirier
  29.  * @author Juergen Hoeller
  30.  * @since 08.03.2004
  31.  */
  32. public class ConcurrentBeanWrapperTests extends TestCase {
  33. private final Log logger = LogFactory.getLog(getClass());
  34. private Set set = Collections.synchronizedSet(new HashSet());
  35. private Throwable ex = null;
  36. public void testSingleThread() {
  37. for (int i = 0; i < 100; i++) {
  38. performSet();
  39. }
  40. }
  41. public void testConcurrent() {
  42. for (int i = 0; i < 10; i++) {
  43. TestRun run = new TestRun(this);
  44. set.add(run);
  45. Thread t = new Thread(run);
  46. t.setDaemon(true);
  47. t.start();
  48. }
  49. logger.info("Thread creation over, " + set.size() + " still active.");
  50. synchronized (this) {
  51. while (!set.isEmpty() && ex == null) {
  52. try {
  53. wait();
  54. }
  55. catch (InterruptedException e) {
  56. logger.info(e.toString());
  57. }
  58. logger.info(set.size() + " threads still active.");
  59. }
  60. }
  61. if (ex != null) {
  62. fail(ex.getMessage());
  63. }
  64. }
  65. private static class TestRun implements Runnable {
  66. private ConcurrentBeanWrapperTests test;
  67. public TestRun(ConcurrentBeanWrapperTests test) {
  68. this.test = test;
  69. }
  70. public void run() {
  71. try {
  72. for (int i = 0; i < 100; i++) {
  73. performSet();
  74. }
  75. }
  76. catch (Throwable e) {
  77. test.ex = e;
  78. }
  79. finally {
  80. synchronized (test) {
  81. test.set.remove(this);
  82. test.notifyAll();
  83. }
  84. }
  85. };
  86. }
  87. private static void performSet() {
  88. TestBean bean = new TestBean();
  89. Properties p = (Properties) System.getProperties().clone();
  90. assertTrue("The System properties must not be empty", p.size() != 0);
  91. for (Iterator i = p.entrySet().iterator(); i.hasNext();) {
  92. i.next();
  93. if (Math.random() > 0.9) {
  94. i.remove();
  95. }
  96. }
  97. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  98. try {
  99. p.store(buffer, null);
  100. }
  101. catch (IOException e) {
  102. // ByteArrayOutputStream does not throw
  103. // any IOException
  104. }
  105. String value = new String(buffer.toByteArray());
  106. BeanWrapperImpl wrapper = new BeanWrapperImpl(bean);
  107. wrapper.setPropertyValue("properties", value);
  108. assertEquals(p, bean.getProperties());
  109. }
  110. private static class TestBean {
  111. private Properties properties;
  112. public Properties getProperties() {
  113. return properties;
  114. }
  115. public void setProperties(Properties properties) {
  116. this.properties = properties;
  117. }
  118. }
  119. }