KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > record > TestPropertyPersistenceStrategySource


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

33 public class TestPropertyPersistenceStrategySource extends HiveMindTestCase
34 {
35     private PropertyPersistenceStrategy newStrategy()
36     {
37         return (PropertyPersistenceStrategy) newMock(PropertyPersistenceStrategy.class);
38     }
39
40     private IRequestCycle newCycle()
41     {
42         return (IRequestCycle) newMock(IRequestCycle.class);
43     }
44
45     private List JavaDoc newContributions(String JavaDoc name, PropertyPersistenceStrategy strategy)
46     {
47         PropertyPersistenceStrategyContribution c = new PropertyPersistenceStrategyContribution();
48         c.setName(name);
49         c.setStrategy(strategy);
50
51         return Collections.singletonList(c);
52     }
53
54     public void testGetKnownStrategy()
55     {
56         PropertyPersistenceStrategy strategy = newStrategy();
57
58         replayControls();
59
60         PropertyPersistenceStrategySourceImpl source = new PropertyPersistenceStrategySourceImpl();
61         source.setContributions(newContributions("known", strategy));
62         source.initializeService();
63
64         assertSame(strategy, source.getStrategy("known"));
65
66         verifyControls();
67     }
68
69     public void testGetUnknownStrategy()
70     {
71         PropertyPersistenceStrategySourceImpl source = new PropertyPersistenceStrategySourceImpl();
72         source.setContributions(Collections.EMPTY_LIST);
73
74         try
75         {
76             source.getStrategy("unknown");
77             unreachable();
78         }
79         catch (ApplicationRuntimeException ex)
80         {
81             assertEquals(RecordMessages.unknownPersistenceStrategy("unknown"), ex.getMessage());
82         }
83     }
84
85     public void testGetAllStoredChanges()
86     {
87         IRequestCycle cycle = newCycle();
88
89         MockControl control = newControl(PropertyPersistenceStrategy.class);
90         PropertyPersistenceStrategy strategy = (PropertyPersistenceStrategy) control.getMock();
91
92         PropertyChange change = (PropertyChange) newMock(PropertyChange.class);
93
94         strategy.getStoredChanges("MyPage", cycle);
95         control.setReturnValue(Collections.singleton(change));
96
97         replayControls();
98
99         PropertyPersistenceStrategySourceImpl source = new PropertyPersistenceStrategySourceImpl();
100         source.setContributions(newContributions("whatever", strategy));
101         source.initializeService();
102
103         Collection JavaDoc result = source.getAllStoredChanges("MyPage", cycle);
104
105         assertEquals(1, result.size());
106         assertSame(change, result.iterator().next());
107
108         verifyControls();
109     }
110
111     public void testAddParameters()
112     {
113         IRequestCycle cycle = newCycle();
114         PropertyPersistenceStrategy strategy = newStrategy();
115         ServiceEncoding encoding = (ServiceEncoding) newMock(ServiceEncoding.class);
116
117         strategy.addParametersForPersistentProperties(encoding, cycle);
118
119         replayControls();
120
121         PropertyPersistenceStrategySourceImpl source = new PropertyPersistenceStrategySourceImpl();
122         source.setContributions(newContributions("whatever", strategy));
123         source.initializeService();
124
125         source.addParametersForPersistentProperties(encoding, cycle);
126
127         verifyControls();
128     }
129
130     public void testDiscardStoredChanges()
131     {
132         IRequestCycle cycle = newCycle();
133         PropertyPersistenceStrategy strategy = newStrategy();
134
135         strategy.discardStoredChanges("Home", cycle);
136
137         replayControls();
138
139         PropertyPersistenceStrategySourceImpl source = new PropertyPersistenceStrategySourceImpl();
140         source.setContributions(newContributions("known", strategy));
141         source.initializeService();
142
143         source.discardAllStoredChanged("Home", cycle);
144
145         verifyControls();
146     }
147 }
Popular Tags