KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > enhance > TestDeferredScript


1 // Copyright 2005 The Apache Software Foundation
2
//
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
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
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.
14

15 package org.apache.tapestry.enhance;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.Location;
19 import org.apache.hivemind.Resource;
20 import org.apache.hivemind.test.HiveMindTestCase;
21 import org.apache.tapestry.IScript;
22 import org.apache.tapestry.engine.IScriptSource;
23 import org.easymock.MockControl;
24
25 /**
26  * Tests for {@link org.apache.tapestry.enhance.DeferredScriptImpl}.
27  *
28  * @author Howard M. Lewis Ship
29  * @since 4.0
30  */

31 public class TestDeferredScript extends HiveMindTestCase
32 {
33     public void testSuccess()
34     {
35         MockControl control = newControl(IScriptSource.class);
36         IScriptSource source = (IScriptSource) control.getMock();
37
38         Resource r = (Resource) newMock(Resource.class);
39         IScript script = (IScript) newMock(IScript.class);
40
41         source.getScript(r);
42         control.setReturnValue(script);
43
44         replayControls();
45
46         DeferredScript ds = new DeferredScriptImpl(r, source, null);
47
48         assertSame(script, ds.getScript());
49
50         // Cheating a little for code coverage. The script's resource
51
// is part of ds's toString()
52

53         assertTrue(ds.toString().indexOf(r.toString()) > 0);
54
55         verifyControls();
56     }
57
58     public void testFailure()
59     {
60         MockControl control = newControl(IScriptSource.class);
61         IScriptSource source = (IScriptSource) control.getMock();
62
63         Resource newResource = (Resource) newMock(Resource.class);
64         Resource r = newResource;
65
66         Location l = newLocation();
67         Throwable JavaDoc t = new RuntimeException JavaDoc("Woops!");
68
69         source.getScript(r);
70         control.setThrowable(t);
71
72         replayControls();
73
74         DeferredScript ds = new DeferredScriptImpl(r, source, l);
75
76         try
77         {
78             ds.getScript();
79             unreachable();
80         }
81         catch (ApplicationRuntimeException ex)
82         {
83             assertEquals("Woops!", ex.getMessage());
84             assertSame(l, ex.getLocation());
85             assertSame(t, ex.getRootCause());
86         }
87
88         verifyControls();
89
90     }
91 }
92
Popular Tags