Clover coverage report - Code Coverage for tapestry release 4.0-beta-12
Coverage timestamp: Sun Oct 30 2005 16:22:01 EST
file stats: LOC: 153   Methods: 9
NCLOC: 89   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ClientPropertyPersistenceStrategy.java 91.7% 100% 100% 98.2%
coverage 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.Collection;
 18    import java.util.Collections;
 19    import java.util.HashMap;
 20    import java.util.Iterator;
 21    import java.util.List;
 22    import java.util.Map;
 23   
 24    import org.apache.hivemind.util.Defense;
 25    import org.apache.tapestry.IRequestCycle;
 26    import org.apache.tapestry.engine.ServiceEncoding;
 27    import org.apache.tapestry.web.WebRequest;
 28   
 29    /**
 30    * Service tapestry.persist.ClientPropertyPersistenceStrategy. Encodes persistent page properties on
 31    * the client as query parameters.
 32    * <p>
 33    * Uses the threaded model.
 34    *
 35    * @author Howard M. Lewis Ship
 36    * @since 4.0
 37    * @see org.apache.tapestry.engine.ILink
 38    */
 39    public class ClientPropertyPersistenceStrategy implements PropertyPersistenceStrategy
 40    {
 41    /**
 42    * Keyed on page name (String), values are
 43    * {@link org.apache.tapestry.record.PersistentPropertyData}.
 44    */
 45    private final Map _data = new HashMap();
 46   
 47    private PersistentPropertyDataEncoder _encoder;
 48   
 49    private WebRequest _request;
 50   
 51    private ClientPropertyPersistenceScope _scope;
 52   
 53    /**
 54    * Initializer for this service, invoked every time a service instance is created. This
 55    * initializer pulls out of the request and query parameters whose prefix is "client:" and
 56    * expects them to be encoded {@link PersistentPropertyData}, which are stored internally.
 57    * Because the service model is threaded, this information is specific to a single request, and
 58    * will be discarded at the end of the request.
 59    */
 60   
 61  247 public void initializeService()
 62    {
 63  247 List names = _request.getParameterNames();
 64  247 Iterator i = names.iterator();
 65  247 while (i.hasNext())
 66    {
 67  711 String name = (String) i.next();
 68   
 69  711 if (!_scope.isParameterForScope(name))
 70  707 continue;
 71   
 72  4 String pageName = _scope.extractPageName(name);
 73   
 74  4 String encoded = _request.getParameterValue(name);
 75   
 76  4 PersistentPropertyData data = new PersistentPropertyData(_encoder);
 77  4 data.storeEncoded(encoded);
 78   
 79  4 _data.put(pageName, data);
 80    }
 81    }
 82   
 83  1 public void store(String pageName, String idPath, String propertyName, Object newValue)
 84    {
 85  1 PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
 86  1 if (data == null)
 87    {
 88  1 data = new PersistentPropertyData(_encoder);
 89  1 _data.put(pageName, data);
 90    }
 91   
 92  1 data.store(idPath, propertyName, newValue);
 93    }
 94   
 95  529 public Collection getStoredChanges(String pageName, IRequestCycle cycle)
 96    {
 97  529 PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
 98   
 99  529 if (data == null)
 100  527 return Collections.EMPTY_LIST;
 101   
 102  2 return data.getPageChanges();
 103    }
 104   
 105  1 public void discardStoredChanges(String pageName, IRequestCycle cycle)
 106    {
 107  1 _data.remove(pageName);
 108    }
 109   
 110  341 public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle,
 111    boolean post)
 112    {
 113  341 Defense.notNull(encoding, "encoding");
 114  341 Defense.notNull(cycle, "cycle");
 115   
 116  341 Iterator i = _data.entrySet().iterator();
 117  341 while (i.hasNext())
 118    {
 119  3 Map.Entry e = (Map.Entry) i.next();
 120   
 121  3 String pageName = (String) e.getKey();
 122  3 PersistentPropertyData data = (PersistentPropertyData) e.getValue();
 123   
 124  3 ClientPropertyPersistenceScope scope = getScope();
 125   
 126  3 if (scope.shouldEncodeState(encoding, cycle, pageName, data))
 127    {
 128  2 String parameterName = _scope.constructParameterName(pageName);
 129  2 encoding.setParameterValue(parameterName, data.getEncoded());
 130    }
 131    }
 132    }
 133   
 134  247 public void setRequest(WebRequest request)
 135    {
 136  247 _request = request;
 137    }
 138   
 139  3 public ClientPropertyPersistenceScope getScope()
 140    {
 141  3 return _scope;
 142    }
 143   
 144  247 public void setScope(ClientPropertyPersistenceScope scope)
 145    {
 146  247 _scope = scope;
 147    }
 148   
 149  248 public void setEncoder(PersistentPropertyDataEncoder encoder)
 150    {
 151  248 _encoder = encoder;
 152    }
 153    }