KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > velocity > VelocityConnectionTest


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 scriptella.driver.velocity;
17
18 import scriptella.AbstractTestCase;
19 import scriptella.configuration.MockConnectionEl;
20 import scriptella.configuration.StringResource;
21 import scriptella.spi.ConnectionParameters;
22 import scriptella.spi.MockDriverContext;
23 import scriptella.spi.MockParametersCallbacks;
24
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.URLConnection JavaDoc;
31 import java.net.URLStreamHandler JavaDoc;
32
33 /**
34  * Tests velocity connection class.
35  *
36  * @author Fyodor Kupolov
37  * @version 1.0
38  */

39 public class VelocityConnectionTest extends AbstractTestCase {
40
41     /**
42      * This test creates a velocity connection that produces output into memory.
43      * Context chaining is also tested.
44      */

45     public void test() {
46         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
47         VelocityConnection c = createConnection(out);
48         run(c);
49         c.close();
50         String JavaDoc s = out.toString();
51         assertEquals("*v1*///*v2*", s);
52
53
54     }
55     //Methods shared with performance test
56

57     static VelocityConnection createConnection(final OutputStream JavaDoc out) {
58         try {
59             final URL JavaDoc u = new URL JavaDoc("mem", "", 0, "memfile", new URLStreamHandler JavaDoc() {
60                 protected URLConnection JavaDoc openConnection(URL JavaDoc u) {
61                     return new URLConnection JavaDoc(u) {
62                         public void connect() {
63                         }
64
65                         public InputStream JavaDoc getInputStream() {
66                             throw new UnsupportedOperationException JavaDoc();
67                         }
68
69                         public OutputStream JavaDoc getOutputStream() {
70                             return out;
71                         }
72                     };
73                 }
74             });
75             ConnectionParameters cp = new ConnectionParameters(new MockConnectionEl(), MockDriverContext.INSTANCE) {
76                 @Override JavaDoc
77                 public URL JavaDoc getResolvedUrl() {
78                     return u;
79                 }
80             };
81             return new VelocityConnection(cp);
82         } catch (MalformedURLException JavaDoc e) {
83             throw new IllegalStateException JavaDoc(e);
84         }
85     }
86
87     static void run(VelocityConnection c) {
88         c.executeScript(new StringResource(("$v1///$v2")), MockParametersCallbacks.SIMPLE);
89     }
90 }
91
Popular Tags