001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.record; 016 017 import static org.easymock.EasyMock.expect; 018 019 import java.util.Collection; 020 import java.util.Collections; 021 import java.util.List; 022 023 import org.apache.hivemind.ApplicationRuntimeException; 024 import org.apache.tapestry.BaseComponentTestCase; 025 import org.apache.tapestry.engine.ServiceEncoding; 026 import org.testng.annotations.Test; 027 028 /** 029 * Tests for {@link org.apache.tapestry.record.PropertyPersistenceStrategySourceImpl}. 030 * 031 * @author Howard M. Lewis Ship 032 * @since 4.0 033 */ 034 @Test 035 public class PropertyPersistenceStrategySourceTest extends BaseComponentTestCase 036 { 037 private PropertyPersistenceStrategy newStrategy() 038 { 039 return newMock(PropertyPersistenceStrategy.class); 040 } 041 042 private List newContributions(String name, PropertyPersistenceStrategy strategy) 043 { 044 PropertyPersistenceStrategyContribution c = new PropertyPersistenceStrategyContribution(); 045 c.setName(name); 046 c.setStrategy(strategy); 047 048 return Collections.singletonList(c); 049 } 050 051 public void testGetKnownStrategy() 052 { 053 PropertyPersistenceStrategy strategy = newStrategy(); 054 055 replay(); 056 057 PropertyPersistenceStrategySourceImpl source = new PropertyPersistenceStrategySourceImpl(); 058 source.setContributions(newContributions("known", strategy)); 059 source.initializeService(); 060 061 assertSame(strategy, source.getStrategy("known")); 062 063 verify(); 064 } 065 066 public void testGetUnknownStrategy() 067 { 068 PropertyPersistenceStrategySourceImpl source = new PropertyPersistenceStrategySourceImpl(); 069 source.setContributions(Collections.EMPTY_LIST); 070 071 try 072 { 073 source.getStrategy("unknown"); 074 unreachable(); 075 } 076 catch (ApplicationRuntimeException ex) 077 { 078 assertEquals(RecordMessages.unknownPersistenceStrategy("unknown"), ex.getMessage()); 079 } 080 } 081 082 protected void trainGetStoredChanges(PropertyPersistenceStrategy strategy, String pageName, 083 Collection changes) 084 { 085 expect(strategy.getStoredChanges(pageName)).andReturn(changes); 086 } 087 088 public void testGetAllStoredChanges() 089 { 090 PropertyPersistenceStrategy strategy = newStrategy(); 091 092 PropertyChange change = newChange(); 093 094 trainGetStoredChanges(strategy, "MyPage", Collections.singleton(change)); 095 096 replay(); 097 098 PropertyPersistenceStrategySourceImpl source = new PropertyPersistenceStrategySourceImpl(); 099 source.setContributions(newContributions("whatever", strategy)); 100 source.initializeService(); 101 102 Collection result = source.getAllStoredChanges("MyPage"); 103 104 assertEquals(1, result.size()); 105 assertSame(change, result.iterator().next()); 106 107 verify(); 108 } 109 110 private PropertyChange newChange() 111 { 112 return newMock(PropertyChange.class); 113 } 114 115 public void testAddParameters() 116 { 117 PropertyPersistenceStrategy strategy = newStrategy(); 118 ServiceEncoding encoding = newEncoding(); 119 120 strategy.addParametersForPersistentProperties(encoding, false); 121 122 replay(); 123 124 PropertyPersistenceStrategySourceImpl source = new PropertyPersistenceStrategySourceImpl(); 125 source.setContributions(newContributions("whatever", strategy)); 126 source.initializeService(); 127 128 source.addParametersForPersistentProperties(encoding, false); 129 130 verify(); 131 132 strategy.addParametersForPersistentProperties(encoding, true); 133 134 replay(); 135 136 source.addParametersForPersistentProperties(encoding, true); 137 } 138 139 private ServiceEncoding newEncoding() 140 { 141 return newMock(ServiceEncoding.class); 142 } 143 144 public void testDiscardStoredChanges() 145 { 146 PropertyPersistenceStrategy strategy = newStrategy(); 147 148 strategy.discardStoredChanges("Home"); 149 150 replay(); 151 152 PropertyPersistenceStrategySourceImpl source = new PropertyPersistenceStrategySourceImpl(); 153 source.setContributions(newContributions("known", strategy)); 154 source.initializeService(); 155 156 source.discardAllStoredChanged("Home"); 157 158 verify(); 159 } 160 }