KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > asset > TestAssetSource


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.asset;
16
17 import java.util.Collections JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import org.apache.hivemind.Location;
22 import org.apache.hivemind.Resource;
23 import org.apache.hivemind.test.HiveMindTestCase;
24 import org.apache.tapestry.IAsset;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.tapestry.asset.AssetSourceImpl}.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class TestAssetSource extends HiveMindTestCase
34 {
35     private AssetFactoryContribution newContribution(String JavaDoc prefix, AssetFactory factory)
36     {
37         AssetFactoryContribution c = new AssetFactoryContribution();
38         c.setPrefix(prefix);
39         c.setFactory(factory);
40
41         return c;
42     }
43
44     private List JavaDoc newContributions(String JavaDoc prefix, AssetFactory factory)
45     {
46         return Collections.singletonList(newContribution(prefix, factory));
47     }
48
49     private Resource newResource()
50     {
51         return (Resource) newMock(Resource.class);
52     }
53
54     private AssetFactory newAssetFactory(Resource base, String JavaDoc path, Locale JavaDoc locale,
55             Location location, IAsset asset)
56     {
57         MockControl control = newControl(AssetFactory.class);
58         AssetFactory f = (AssetFactory) control.getMock();
59
60         f.createAsset(base, path, locale, location);
61         control.setReturnValue(asset);
62
63         return f;
64     }
65
66     private IAsset newAsset()
67     {
68         return (IAsset) newMock(IAsset.class);
69     }
70
71     public void testKnownPrefix()
72     {
73         Location l = fabricateLocation(17);
74
75         Resource r = newResource();
76         IAsset asset = newAsset();
77
78         List JavaDoc contributions = newContributions("known", newAssetFactory(
79                 r,
80                 "path/to/asset",
81                 Locale.ENGLISH,
82                 l,
83                 asset));
84
85         replayControls();
86
87         AssetSourceImpl as = new AssetSourceImpl();
88         as.setContributions(contributions);
89
90         as.initializeService();
91
92         IAsset actual = as.findAsset(r, "known:path/to/asset", Locale.ENGLISH, l);
93
94         assertSame(actual, asset);
95
96         verifyControls();
97     }
98
99     public void testUnknownPrefix()
100     {
101         Location l = fabricateLocation(17);
102
103         Resource r = newResource();
104         IAsset asset = newAsset();
105
106         AssetFactory f = newAssetFactory(r, "unknown:path/to/asset", Locale.ENGLISH, l, asset);
107
108         replayControls();
109
110         AssetSourceImpl as = new AssetSourceImpl();
111         as.setDefaultAssetFactory(f);
112
113         IAsset actual = as.findAsset(r, "unknown:path/to/asset", Locale.ENGLISH, l);
114
115         assertSame(actual, asset);
116
117         verifyControls();
118     }
119
120     public void testNoPrefix()
121     {
122         Location l = fabricateLocation(17);
123
124         Resource r = newResource();
125         IAsset asset = newAsset();
126
127         AssetFactory f = newAssetFactory(r, "path/to/asset", Locale.ENGLISH, l, asset);
128
129         replayControls();
130
131         AssetSourceImpl as = new AssetSourceImpl();
132         as.setLookupAssetFactory(f);
133
134         IAsset actual = as.findAsset(r, "path/to/asset", Locale.ENGLISH, l);
135
136         assertSame(actual, asset);
137
138         verifyControls();
139     }
140 }
Popular Tags