KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > application > ApplicationTest


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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.apache.myfaces.application;
17
18 import org.apache.myfaces.MyFacesBaseTest;
19
20 import javax.faces.application.Application;
21 import javax.faces.el.ValueBinding;
22 import java.util.*;
23
24
25 /**
26  * @author Anton Koinov (latest modification by $Author: matze $)
27  * @version $Revision: 1.13 $ $Date: 2004/10/13 11:50:59 $
28  */

29 public class ApplicationTest extends MyFacesBaseTest
30 {
31     //~ Static fields/initializers -----------------------------------------------------------------
32

33     private static final int THREAD_COUNT = 100;
34     private static final int VB_COUNT = 20000;
35
36     //~ Instance fields ----------------------------------------------------------------------------
37

38     final Map _bindingsMap = new HashMap(VB_COUNT * 2);
39
40     //~ Constructors -------------------------------------------------------------------------------
41

42     public ApplicationTest(String JavaDoc name)
43     {
44         super(name);
45     }
46
47     //~ Methods ------------------------------------------------------------------------------------
48

49     public void testValueBindingCaching()
50     {
51         ValueBinding vb;
52
53         vb = _application.createValueBinding("#{test}");
54         for (int i = 0; i < 1000; i++)
55         {
56             assertSame(vb, _application.createValueBinding("#{test}"));
57         }
58     }
59
60     public void testValueBindingMutithreadedCaching()
61     throws InterruptedException JavaDoc
62     {
63         final Random random = new Random();
64
65         Set varNames = new HashSet(VB_COUNT * 2);
66         for (int i = 0; i < VB_COUNT; i++)
67         {
68             String JavaDoc name = null;
69             do
70             {
71                 name =
72                     "#{t" + Math.abs(random.nextLong()) + ".t" + Math.abs(random.nextLong()) + '}';
73             }
74             while (varNames.contains(name));
75
76             varNames.add(name);
77         }
78
79         String JavaDoc[] names = toStringArray(varNames);
80         varNames = null; // free memory
81

82         Thread JavaDoc[] threads = new Thread JavaDoc[THREAD_COUNT];
83         for (int i = 0; i < THREAD_COUNT; i++)
84         {
85             threads[i] = new Thread JavaDoc(new ValueBindingCachingTesterThread((names = shuffle(names))));
86         }
87         names = null; // free memory
88

89         for (int i = 0; i < THREAD_COUNT; i++)
90         {
91             threads[i].start();
92         }
93
94         for (int i = 0; i < THREAD_COUNT; i++)
95         {
96             threads[i].join();
97         }
98     }
99
100     Application getApplication()
101     {
102         return _application;
103     }
104
105     private String JavaDoc[] shuffle(String JavaDoc[] strings)
106     {
107         strings = (String JavaDoc[]) strings.clone();
108
109         final Random random = new Random();
110
111         for (int i = strings.length; i > 0;)
112         {
113             int j = random.nextInt(i);
114             i--;
115             String JavaDoc temp = strings[i];
116             strings[i] = strings[j];
117             strings[j] = temp;
118         }
119
120         return strings;
121     }
122
123     private String JavaDoc[] toStringArray(Set set)
124     {
125         String JavaDoc[] strings = new String JavaDoc[set.size()];
126         int i = 0;
127         for (Iterator it = set.iterator(); it.hasNext();)
128         {
129             strings[i++] = (String JavaDoc) it.next();
130         }
131         return strings;
132     }
133
134     //~ Inner Classes ------------------------------------------------------------------------------
135

136     class ValueBindingCachingTesterThread implements Runnable JavaDoc
137     {
138         //~ Instance fields ------------------------------------------------------------------------
139

140         final String JavaDoc[] _names;
141
142         //~ Constructors ---------------------------------------------------------------------------
143

144         ValueBindingCachingTesterThread(String JavaDoc[] names)
145         {
146             _names = names;
147         }
148
149         //~ Methods --------------------------------------------------------------------------------
150

151         public void run()
152         {
153             try {
154                 // invoke to initialize FacesContext.currentInstance() for this thread
155
new MyFacesBaseTest("dummy") {
156                     public void setUp()
157                         throws Exception JavaDoc
158                     {
159                         super.setUp();
160                     }
161                 }.setUp();
162     
163                 final Application application = getApplication();
164                 final ValueBinding[] bindings = new ValueBinding[_names.length];
165                 for (int i = 0, len = _names.length; i < len; i++)
166                 {
167                     bindings[i] = application.createValueBinding(_names[i]);
168                 }
169     
170                 final Map bindingsMap = _bindingsMap;
171                 synchronized (bindingsMap)
172                 {
173                     boolean put = !bindingsMap.containsKey(_names[0]);
174                     for (int i = 0, len = _names.length; i < len; i++)
175                     {
176                         String JavaDoc name = _names[i];
177                         ValueBinding vb = bindings[i];
178                         if (put)
179                         {
180                             bindingsMap.put(name, vb);
181                         }
182                         else
183                         {
184                             assertSame(
185                                 "Probably serious mutli-threading issue, please report to MyFaces team",
186                                 vb, bindingsMap.get(name));
187                         }
188                     }
189                 }
190             }
191             catch (Exception JavaDoc e)
192             {
193                 assertTrue(false);
194             }
195         }
196     }
197 }
198
Popular Tags