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.Collections;
020    import java.util.List;
021    
022    import org.apache.tapestry.BaseComponentTestCase;
023    import org.testng.annotations.Test;
024    
025    /**
026     * Tests for {@link org.apache.tapestry.record.PersistentPropertyData}.
027     * 
028     * @author Howard M. Lewis Ship
029     * @since 4.0
030     */
031    @Test
032    public class TestPersistentPropertyData extends BaseComponentTestCase
033    {
034    
035        public void testStoreChange()
036        {
037            Object newObject1 = new Object();
038            Object newObject2 = new Object();
039    
040            PersistentPropertyDataEncoder encoder = newMock(PersistentPropertyDataEncoder.class);
041    
042            replay();
043    
044            PersistentPropertyData ppd = new PersistentPropertyData(encoder);
045    
046            ppd.store("foo", "bar", newObject1);
047            
048            assertListEquals(new Object[] { new PropertyChangeImpl("foo", "bar", newObject1) }, ppd.getPageChanges());
049    
050            // Check for overwriting.
051    
052            ppd.store("foo", "bar", newObject2);
053    
054            assertListEquals(new Object[]
055            { new PropertyChangeImpl("foo", "bar", newObject2) }, ppd.getPageChanges());
056    
057            // We only add the one value, because the output order
058            // is indeterminate.
059    
060            verify();
061        }
062    
063        public void testDecode()
064        {
065            Object newObject = new Object();
066            String encoded = "ENCODED";
067            List decoded = Collections.singletonList(new PropertyChangeImpl("foo", "bar", newObject));
068            
069            PersistentPropertyDataEncoder encoder = newMock(PersistentPropertyDataEncoder.class);
070            
071            expect(encoder.decodePageChanges(encoded)).andReturn(decoded);
072            
073            replay();
074            
075            PersistentPropertyData ppd = new PersistentPropertyData(encoder);
076            
077            ppd.storeEncoded(encoded);
078            
079            List l1 = ppd.getPageChanges();
080            
081            assertListEquals(new Object[] { new PropertyChangeImpl("foo", "bar", newObject) }, l1);
082    
083            List l2 = ppd.getPageChanges();
084    
085            assertNotSame(l1, l2);
086    
087            assertListEquals(new Object[]
088            { new PropertyChangeImpl("foo", "bar", newObject) }, l2);
089    
090            verify();
091        }
092    
093        public void testEncode()
094        {
095            String encoded = "ENCODED";
096            Object newObject = new Object();
097            List changes = Collections.singletonList(new PropertyChangeImpl("foo", "bar", newObject));
098    
099            PersistentPropertyDataEncoder encoder = newMock(PersistentPropertyDataEncoder.class);
100    
101            expect(encoder.encodePageChanges(changes)).andReturn(encoded);
102    
103            replay();
104    
105            PersistentPropertyData ppd = new PersistentPropertyData(encoder);
106    
107            ppd.store("foo", "bar", newObject);
108    
109            assertSame(encoded, ppd.getEncoded());
110            assertSame(encoded, ppd.getEncoded());
111    
112            verify();
113        }
114    }