Coverage Report - org.seasar.teeda.core.mock.MockFacesContextImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
MockFacesContextImpl
88%
65/74
100%
12/12
1.286
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.mock;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Iterator;
 21  
 import java.util.LinkedHashMap;
 22  
 import java.util.List;
 23  
 import java.util.Locale;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.faces.application.Application;
 27  
 import javax.faces.application.FacesMessage;
 28  
 import javax.faces.application.FacesMessage.Severity;
 29  
 import javax.faces.component.UIViewRoot;
 30  
 import javax.faces.context.ExternalContext;
 31  
 import javax.faces.context.ResponseStream;
 32  
 import javax.faces.context.ResponseWriter;
 33  
 import javax.faces.internal.FactoryFinderUtil;
 34  
 import javax.faces.render.RenderKit;
 35  
 import javax.servlet.ServletResponse;
 36  
 
 37  
 import org.seasar.framework.exception.IORuntimeException;
 38  
 import org.seasar.teeda.core.context.html.HtmlResponseWriter;
 39  
 
 40  
 /**
 41  
  * @author shot
 42  
  * @author manhole
 43  
  */
 44  
 public class MockFacesContextImpl extends MockFacesContext {
 45  
 
 46  6745
     private UIViewRoot viewRoot = null;
 47  
 
 48  
     private MockExternalContext externalContext;
 49  
 
 50  
     private Application application;
 51  
 
 52  6745
     private Map messages = new LinkedHashMap();
 53  
 
 54  
     private ResponseWriter responseWriter;
 55  
 
 56  
     private boolean renderResponse;
 57  
 
 58  
     private boolean responseComplete;
 59  
 
 60  5405
     public MockFacesContextImpl() {
 61  5405
         setCurrentInstance(this);
 62  5405
     }
 63  
 
 64  1
     public MockFacesContextImpl(final MockExternalContext externalContext) {
 65  1
         this.externalContext = externalContext;
 66  1
         application = FactoryFinderUtil.getApplicationFactory()
 67  
                 .getApplication();
 68  1
         setCurrentInstance(this);
 69  1
     }
 70  
 
 71  
     public MockFacesContextImpl(final MockExternalContext context,
 72  1339
             final Application application) {
 73  1339
         externalContext = context;
 74  1339
         this.application = application;
 75  1339
         setCurrentInstance(this);
 76  1339
     }
 77  
 
 78  
     public Application getApplication() {
 79  850
         if (application == null) {
 80  414
             application = new MockApplicationImpl();
 81  
         }
 82  850
         return application;
 83  
     }
 84  
 
 85  
     public Iterator getClientIdsWithMessages() {
 86  0
         return messages.keySet().iterator();
 87  
     }
 88  
 
 89  
     public ExternalContext getExternalContext() {
 90  1424
         return getMockExternalContext();
 91  
     }
 92  
 
 93  
     public Severity getMaximumSeverity() {
 94  0
         return null;
 95  
     }
 96  
 
 97  
     public void removeMessages() {
 98  0
         messages = new LinkedHashMap();
 99  0
     }
 100  
 
 101  
     public Iterator getMessages() {
 102  143
         final List all = new ArrayList();
 103  143
         for (final Iterator it = messages.values().iterator(); it.hasNext();) {
 104  132
             final List messages = (List) it.next();
 105  132
             all.addAll(messages);
 106  
         }
 107  143
         return all.iterator();
 108  
     }
 109  
 
 110  
     public Iterator getMessages(final String clientId) {
 111  32
         return getMessagesList(clientId).iterator();
 112  
     }
 113  
 
 114  
     public RenderKit getRenderKit() {
 115  0
         return null;
 116  
     }
 117  
 
 118  
     public boolean getRenderResponse() {
 119  25
         return renderResponse;
 120  
     }
 121  
 
 122  
     public boolean getResponseComplete() {
 123  38
         return responseComplete;
 124  
     }
 125  
 
 126  
     public ResponseStream getResponseStream() {
 127  0
         return null;
 128  
     }
 129  
 
 130  
     public void setResponseStream(final ResponseStream responseStream) {
 131  0
     }
 132  
 
 133  
     public ResponseWriter getResponseWriter() {
 134  932
         if (responseWriter == null) {
 135  367
             final HtmlResponseWriter responseWriter = new HtmlResponseWriter();
 136  367
             final ServletResponse response = getMockExternalContext()
 137  
                     .getMockHttpServletResponse();
 138  
             try {
 139  367
                 responseWriter.setWriter(response.getWriter());
 140  367
                 this.responseWriter = responseWriter;
 141  0
             } catch (final IOException e) {
 142  0
                 throw new IORuntimeException(e);
 143  367
             }
 144  
         }
 145  932
         return responseWriter;
 146  
     }
 147  
 
 148  
     public void setResponseWriter(final ResponseWriter responseWriter) {
 149  9
         this.responseWriter = responseWriter;
 150  9
     }
 151  
 
 152  
     public UIViewRoot getViewRoot() {
 153  1266
         if (viewRoot == null) {
 154  230
             viewRoot = new UIViewRoot();
 155  230
             viewRoot.setLocale(Locale.getDefault());
 156  
         }
 157  1266
         return viewRoot;
 158  
     }
 159  
 
 160  
     public void setViewRoot(final UIViewRoot root) {
 161  1461
         viewRoot = root;
 162  1461
     }
 163  
 
 164  
     public void addMessage(final String clientId, final FacesMessage message) {
 165  152
         final List l = getMessagesList(clientId);
 166  152
         l.add(message);
 167  152
     }
 168  
 
 169  
     private List getMessagesList(final String clientId) {
 170  184
         List l = (List) messages.get(clientId);
 171  184
         if (l == null) {
 172  144
             l = new ArrayList();
 173  144
             messages.put(clientId, l);
 174  
         }
 175  184
         return l;
 176  
     }
 177  
 
 178  
     public void release() {
 179  6619
         setCurrentInstance(null);
 180  6619
     }
 181  
 
 182  
     public void renderResponse() {
 183  20
         renderResponse = true;
 184  20
     }
 185  
 
 186  
     public void responseComplete() {
 187  9
         responseComplete = true;
 188  9
     }
 189  
 
 190  
     public void setExternalContext(final ExternalContext context) {
 191  7
         setMockExternalContext((MockExternalContext) context);
 192  7
     }
 193  
 
 194  
     public void setMockExternalContext(final MockExternalContext context) {
 195  7
         externalContext = context;
 196  7
     }
 197  
 
 198  
     public void setApplication(final Application application) {
 199  81
         this.application = application;
 200  81
     }
 201  
 
 202  
     public MockExternalContext getMockExternalContext() {
 203  1795
         if (externalContext == null) {
 204  401
             externalContext = new MockExternalContextImpl();
 205  
         }
 206  1795
         return externalContext;
 207  
     }
 208  
 
 209  
 }