1 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 29 public class ApplicationTest extends MyFacesBaseTest 30 { 31 33 private static final int THREAD_COUNT = 100; 34 private static final int VB_COUNT = 20000; 35 36 38 final Map _bindingsMap = new HashMap(VB_COUNT * 2); 39 40 42 public ApplicationTest(String name) 43 { 44 super(name); 45 } 46 47 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 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 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 [] names = toStringArray(varNames); 80 varNames = null; 82 Thread [] threads = new Thread [THREAD_COUNT]; 83 for (int i = 0; i < THREAD_COUNT; i++) 84 { 85 threads[i] = new Thread (new ValueBindingCachingTesterThread((names = shuffle(names)))); 86 } 87 names = null; 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 [] shuffle(String [] strings) 106 { 107 strings = (String []) 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 temp = strings[i]; 116 strings[i] = strings[j]; 117 strings[j] = temp; 118 } 119 120 return strings; 121 } 122 123 private String [] toStringArray(Set set) 124 { 125 String [] strings = new String [set.size()]; 126 int i = 0; 127 for (Iterator it = set.iterator(); it.hasNext();) 128 { 129 strings[i++] = (String ) it.next(); 130 } 131 return strings; 132 } 133 134 136 class ValueBindingCachingTesterThread implements Runnable 137 { 138 140 final String [] _names; 141 142 144 ValueBindingCachingTesterThread(String [] names) 145 { 146 _names = names; 147 } 148 149 151 public void run() 152 { 153 try { 154 new MyFacesBaseTest("dummy") { 156 public void setUp() 157 throws Exception 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 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 e) 192 { 193 assertTrue(false); 194 } 195 } 196 } 197 } 198 | Popular Tags |