KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > ConcurrentBeanFactoryTests


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.beans.factory;
18
19 import java.text.ParseException JavaDoc;
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import junit.framework.TestCase;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import org.springframework.beans.factory.xml.XmlBeanFactory;
31 import org.springframework.beans.propertyeditors.CustomDateEditor;
32 import org.springframework.core.io.ClassPathResource;
33
34 /**
35  * @author Guillaume Poirier
36  * @author Juergen Hoeller
37  * @since 10.03.2004
38  */

39 public class ConcurrentBeanFactoryTests extends TestCase {
40
41     private static final Log logger = LogFactory.getLog(ConcurrentBeanFactoryTests.class);
42
43     private static final SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("yyyy/MM/dd");
44
45     private static final Date JavaDoc date1;
46
47     private static final Date JavaDoc date2;
48
49     static {
50         try {
51             date1 = df.parse("2004/08/08");
52             date2 = df.parse("2000/02/02");
53         }
54         catch (ParseException JavaDoc e) {
55             throw new RuntimeException JavaDoc(e);
56         }
57     }
58
59     private BeanFactory factory;
60
61     private Set JavaDoc set = Collections.synchronizedSet(new HashSet JavaDoc());
62
63     private Throwable JavaDoc ex = null;
64
65     protected void setUp() throws Exception JavaDoc {
66         XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("concurrent.xml", getClass()));
67         CustomDateEditor editor = new CustomDateEditor(df, false);
68         factory.registerCustomEditor(Date JavaDoc.class, editor);
69         this.factory = factory;
70     }
71
72     public void testSingleThread() {
73         for (int i = 0; i < 100; i++) {
74             performTest();
75         }
76     }
77
78     public void testConcurrent() {
79         for (int i = 0; i < 30; i++) {
80             TestRun run = new TestRun();
81             set.add(run);
82             Thread JavaDoc t = new Thread JavaDoc(run);
83             t.setDaemon(true);
84             t.start();
85         }
86         logger.info("Thread creation over, " + set.size() + " still active.");
87         synchronized (set) {
88             while (!set.isEmpty() && ex == null) {
89                 try {
90                     set.wait();
91                 }
92                 catch (InterruptedException JavaDoc e) {
93                     logger.info(e.toString());
94                 }
95                 logger.info(set.size() + " threads still active.");
96             }
97         }
98         if (ex != null) {
99             fail(ex.getMessage());
100         }
101     }
102
103     private void performTest() {
104         ConcurrentBean b1 = (ConcurrentBean) factory.getBean("bean1");
105         ConcurrentBean b2 = (ConcurrentBean) factory.getBean("bean2");
106
107         assertEquals(b1.getDate(), date1);
108         assertEquals(b2.getDate(), date2);
109     }
110
111
112     private class TestRun implements Runnable JavaDoc {
113
114         public void run() {
115             try {
116                 for (int i = 0; i < 100; i++) {
117                     performTest();
118                 }
119             }
120             catch (Throwable JavaDoc e) {
121                 ex = e;
122             }
123             finally {
124                 synchronized (set) {
125                     set.remove(this);
126                     set.notifyAll();
127                 }
128             }
129         }
130     }
131
132
133     public static class ConcurrentBean {
134
135         private Date JavaDoc date;
136
137         public Date JavaDoc getDate() {
138             return date;
139         }
140
141         public void setDate(Date JavaDoc date) {
142             this.date = date;
143         }
144     }
145
146 }
147
Popular Tags