Clover coverage report - Code Coverage for tapestry release 4.0-beta-10
Coverage timestamp: Sat Oct 8 2005 19:08:05 EDT
file stats: LOC: 158   Methods: 10
NCLOC: 92   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ClientPropertyPersistenceStrategy.java 91.7% 100% 100% 98.3%
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 final PersistentPropertyDataEncoder _encoder;
 48   
 49    private WebRequest _request;
 50   
 51    private ClientPropertyPersistenceScope _scope;
 52   
 53  256 public ClientPropertyPersistenceStrategy()
 54    {
 55  256 this(new PersistentPropertyDataEncoderImpl());
 56    }
 57   
 58    // Alternate constructor used for testing
 59  257 ClientPropertyPersistenceStrategy(PersistentPropertyDataEncoder encoder)
 60    {
 61  257 _encoder = encoder;
 62    }
 63   
 64    /**
 65    * Initializer for this service, invoked every time a service instance is created. This
 66    * initializer pulls out of the request and query parameters whose prefix is "client:" and
 67    * expects them to be encoded {@link PersistentPropertyData}, which are stored internally.
 68    * Because the service model is threaded, this information is specific to a single request, and
 69    * will be discarded at the end of the request.
 70    */
 71   
 72  255 public void initializeService()
 73    {
 74  255 List names = _request.getParameterNames();
 75  255 Iterator i = names.iterator();
 76  255 while (i.hasNext())
 77    {
 78  723 String name = (String) i.next();
 79   
 80  723 if (!_scope.isParameterForScope(name))
 81  719 continue;
 82   
 83  4 String pageName = _scope.extractPageName(name);
 84   
 85  4 String encoded = _request.getParameterValue(name);
 86   
 87  4 PersistentPropertyData data = new PersistentPropertyData(_encoder);
 88  4 data.storeEncoded(encoded);
 89   
 90  4 _data.put(pageName, data);
 91    }
 92    }
 93   
 94  1 public void store(String pageName, String idPath, String propertyName, Object newValue)
 95    {
 96  1 PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
 97  1 if (data == null)
 98    {
 99  1 data = new PersistentPropertyData(_encoder);
 100  1 _data.put(pageName, data);
 101    }
 102   
 103  1 data.store(idPath, propertyName, newValue);
 104    }
 105   
 106  547 public Collection getStoredChanges(String pageName, IRequestCycle cycle)
 107    {
 108  547 PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
 109   
 110  547 if (data == null)
 111  545 return Collections.EMPTY_LIST;
 112   
 113  2 return data.getPageChanges();
 114    }
 115   
 116  1 public void discardStoredChanges(String pageName, IRequestCycle cycle)
 117    {
 118  1 _data.remove(pageName);
 119    }
 120   
 121  374 public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle, boolean post)
 122    {
 123  374 Defense.notNull(encoding, "encoding");
 124  374 Defense.notNull(cycle, "cycle");
 125   
 126  374 Iterator i = _data.entrySet().iterator();
 127  374 while (i.hasNext())
 128    {
 129  3 Map.Entry e = (Map.Entry) i.next();
 130   
 131  3 String pageName = (String) e.getKey();
 132  3 PersistentPropertyData data = (PersistentPropertyData) e.getValue();
 133   
 134  3 ClientPropertyPersistenceScope scope = getScope();
 135   
 136  3 if (scope.shouldEncodeState(encoding, cycle, pageName, data))
 137    {
 138  2 String parameterName = _scope.constructParameterName(pageName);
 139  2 encoding.setParameterValue(parameterName, data.getEncoded());
 140    }
 141    }
 142    }
 143   
 144  255 public void setRequest(WebRequest request)
 145    {
 146  255 _request = request;
 147    }
 148   
 149  3 public ClientPropertyPersistenceScope getScope()
 150    {
 151  3 return _scope;
 152    }
 153   
 154  255 public void setScope(ClientPropertyPersistenceScope scope)
 155    {
 156  255 _scope = scope;
 157    }
 158    }