Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
file stats: LOC: 133   Methods: 8
NCLOC: 79   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SessionPropertyPersistenceStrategy.java 100% 100% 100% 100%
coverage
 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.ArrayList;
 18    import java.util.Collection;
 19    import java.util.Collections;
 20   
 21    import org.apache.hivemind.util.Defense;
 22    import org.apache.tapestry.engine.ServiceEncoding;
 23    import org.apache.tapestry.web.WebRequest;
 24    import org.apache.tapestry.web.WebSession;
 25   
 26    /**
 27    * The most basic {@link org.apache.tapestry.record.PropertyPersistenceStrategy}, which stores
 28    * properties in the HttpSession as attributes.
 29    *
 30    * @author Howard M. Lewis Ship
 31    * @since 4.0
 32    */
 33    public class SessionPropertyPersistenceStrategy implements PropertyPersistenceStrategy
 34    {
 35    public static final String STRATEGY_ID = "session";
 36   
 37    // Really, the name of the servlet; used as a prefix on all HttpSessionAttribute keys
 38    // to keep things straight if multiple Tapestry apps are deployed
 39    // in the same WAR.
 40   
 41    private String _applicationId;
 42   
 43    private WebRequest _request;
 44   
 45  46 public void store(String pageName, String idPath, String propertyName, Object newValue)
 46    {
 47  46 Defense.notNull(pageName, "pageName");
 48  46 Defense.notNull(propertyName, "propertyName");
 49   
 50  46 WebSession session = _request.getSession(true);
 51   
 52  46 String attributeName = RecordUtils.buildChangeKey(
 53    STRATEGY_ID,
 54    _applicationId,
 55    pageName,
 56    idPath,
 57    propertyName);
 58   
 59  46 session.setAttribute(attributeName, newValue);
 60    }
 61   
 62  358 public Collection getStoredChanges(String pageName)
 63    {
 64  358 Defense.notNull(pageName, "pageName");
 65   
 66  358 WebSession session = _request.getSession(false);
 67   
 68  358 if (session == null)
 69  262 return Collections.EMPTY_LIST;
 70   
 71  96 final Collection result = new ArrayList();
 72   
 73  96 WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
 74    {
 75  26 public void handleAttribute(WebSession session, String name)
 76    {
 77  26 PropertyChange change = RecordUtils.buildChange(name, session.getAttribute(name));
 78   
 79  26 result.add(change);
 80    }
 81    };
 82   
 83  96 RecordUtils.iterateOverMatchingAttributes(
 84    STRATEGY_ID,
 85    _applicationId,
 86    pageName,
 87    session,
 88    callback);
 89   
 90  96 return result;
 91    }
 92   
 93  6 public void discardStoredChanges(String pageName)
 94    {
 95  6 WebSession session = _request.getSession(false);
 96   
 97  6 if (session == null)
 98  2 return;
 99   
 100  4 WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
 101    {
 102  2 public void handleAttribute(WebSession session, String name)
 103    {
 104  2 session.setAttribute(name, null);
 105    }
 106    };
 107   
 108  4 RecordUtils.iterateOverMatchingAttributes(
 109    STRATEGY_ID,
 110    _applicationId,
 111    pageName,
 112    session,
 113    callback);
 114    }
 115   
 116    /**
 117    * Does nothing; session persistence does not make use of query parameters.
 118    */
 119   
 120  226 public void addParametersForPersistentProperties(ServiceEncoding encoding, boolean post)
 121    {
 122    }
 123   
 124  94 public void setApplicationId(String applicationName)
 125    {
 126  94 _applicationId = applicationName;
 127    }
 128   
 129  98 public void setRequest(WebRequest request)
 130    {
 131  98 _request = request;
 132    }
 133    }