1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
18 using System
.Collections
;
20 using System
.Text
.RegularExpressions
;
21 using System
.Threading
;
23 using NUnit
.Framework
;
26 [TestFixture
, Explicit
]
27 public class MultiThreadTestCase
29 private ManualResetEvent startEvent
= new ManualResetEvent(false);
30 private ManualResetEvent stopEvent
= new ManualResetEvent(false);
31 private ArrayList items
;
32 private VelocityEngine ve
;
37 items
= new ArrayList();
44 ve
= new VelocityEngine();
49 public void Multithreaded_ASTExecuteDoesNotShareParams()
51 const int threadCount
= 1;
53 Thread
[] threads
= new Thread
[threadCount
];
55 for (int i
= 0; i
< threadCount
; i
++)
57 threads
[i
] = new Thread(ExecuteMethodUntilSignal3
);
63 Thread
.CurrentThread
.Join(3*5000);
69 public void Multithreaded1()
71 const int threadCount
= 30;
73 Thread
[] threads
= new Thread
[threadCount
];
75 for (int i
= 0; i
< threadCount
; i
++)
77 threads
[i
] = new Thread(ExecuteMethodUntilSignal1
);
83 Thread
.CurrentThread
.Join(1*5000);
89 public void Multithreaded_ReuseVelocityEngineInstanceConcurrently()
91 const int threadCount
= 30;
93 Thread
[] threads
= new Thread
[threadCount
];
95 for (int i
= 0; i
< threadCount
; i
++)
97 threads
[i
] = new Thread(ExecuteMethodUntilSignal2
);
103 Thread
.CurrentThread
.Join(1*5000);
109 /// This test starts a VelocityEngine for each execution
111 public void ExecuteMethodUntilSignal1()
113 startEvent
.WaitOne(int.MaxValue
, false);
115 while (!stopEvent
.WaitOne(0, false))
117 VelocityEngine ve
= new VelocityEngine();
120 StringWriter sw
= new StringWriter();
122 VelocityContext c
= new VelocityContext();
123 c
.Put("x", new Something());
124 c
.Put("items", items
);
126 bool ok
= ve
.Evaluate(c
, sw
,
127 "ContextTest.CaseInsensitive",
129 #foreach( $item in $items )
134 Assert
.IsTrue(ok
, "Evalutation returned failure");
135 Assert
.AreEqual("a,b,c,d,", Normalize(sw
));
140 /// This test uses the previously created velocity engine
142 public void ExecuteMethodUntilSignal2()
144 startEvent
.WaitOne(int.MaxValue
, false);
146 while (!stopEvent
.WaitOne(0, false))
148 StringWriter sw
= new StringWriter();
150 VelocityContext c
= new VelocityContext();
151 c
.Put("x", new Something());
152 c
.Put("items", items
);
154 bool ok
= ve
.Evaluate(c
, sw
,
155 "ContextTest.CaseInsensitive",
157 #foreach($item in $items)
161 $x.Print('hey') $x.Contents('test', '1')
164 Assert
.IsTrue(ok
, "Evalutation returned failure");
165 Assert
.AreEqual("a,b,c,d,heytest,1", Normalize(sw
));
169 public void ExecuteMethodUntilSignal3()
171 startEvent
.WaitOne(int.MaxValue
, false);
175 while (!stopEvent
.WaitOne(0, false))
177 StringWriter sw
= new StringWriter();
179 VelocityContext c
= new VelocityContext();
180 c
.Put("x", new Urlhelper());
182 bool ok
= ve
.Evaluate(c
, sw
, "",
183 "#foreach($i in [1..3000]) \r\n" +
184 "#set($temp = $x.For(\"%{controller='Test',id=$i}\")) \r\n" +
187 Assert
.IsTrue(ok
, "Evalutation returned failure");
190 catch(System
.Exception ex
)
192 Console
.WriteLine(ex
.ToString());
196 private string Normalize(StringWriter sw
)
198 return Regex
.Replace(sw
.ToString(), "\\s+", "");
201 private class Urlhelper
203 public string For(IDictionary parameters
)
205 if (parameters
== null)
207 throw new ArgumentNullException("parameters", "parameters cannot be null");
212 string controller
= (string) parameters
["controller"];
213 int id
= (int) parameters
["id"];
215 parameters
.Remove("controller");
216 parameters
.Remove("id");
218 return controller
+ " " + id
;
220 catch (System
.Exception ex
)
222 Console
.WriteLine(ex
.ToString());