1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.portletbridge.mock;
17  
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import javax.portlet.PortletMode;
23  import javax.portlet.PortletModeException;
24  import javax.portlet.PortletSecurityException;
25  import javax.portlet.PortletURL;
26  import javax.portlet.WindowState;
27  import javax.portlet.WindowStateException;
28  
29  /***
30   * @author jmccrindle
31   */
32  public class MockPortletURL implements PortletURL {
33  
34      private Map parameters = new HashMap();
35      private WindowState windowState;
36      private PortletMode portletMode;
37      
38      /***
39       * 
40       */
41      public MockPortletURL() {
42          super();
43      }
44  
45      /* (non-Javadoc)
46       * @see javax.portlet.PortletURL#setWindowState(javax.portlet.WindowState)
47       */
48      public void setWindowState(WindowState windowState) throws WindowStateException {
49          this.windowState = windowState;
50      }
51  
52      /* (non-Javadoc)
53       * @see javax.portlet.PortletURL#setPortletMode(javax.portlet.PortletMode)
54       */
55      public void setPortletMode(PortletMode portletMode) throws PortletModeException {
56          this.portletMode = portletMode;
57      }
58  
59      /* (non-Javadoc)
60       * @see javax.portlet.PortletURL#setParameter(java.lang.String, java.lang.String)
61       */
62      public void setParameter(String name, String value) {
63          parameters.put(name, new String[] {value});
64      }
65  
66      /* (non-Javadoc)
67       * @see javax.portlet.PortletURL#setParameter(java.lang.String, java.lang.String[])
68       */
69      public void setParameter(String name, String[] values) {
70          parameters.put(name, values);
71      }
72  
73      /* (non-Javadoc)
74       * @see javax.portlet.PortletURL#setParameters(java.util.Map)
75       */
76      public void setParameters(Map parameters) {
77          this.parameters = parameters;
78      }
79  
80      /* (non-Javadoc)
81       * @see javax.portlet.PortletURL#setSecure(boolean)
82       */
83      public void setSecure(boolean secure) throws PortletSecurityException {
84      }
85      
86      /* (non-Javadoc)
87       * @see javax.portlet.PortletURL#toString()
88       */
89      public String toString() {
90          StringBuffer result = new StringBuffer("mock?");
91          if(windowState != null) {
92              result.append("ws=" + windowState + "&");
93          }
94          if(portletMode != null) {
95              result.append("pm=" + portletMode + "&");
96          }
97          if(parameters.size() > 0) {
98              for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();) {
99                  Map.Entry entry = (Map.Entry) iter.next();
100                 String name = (String) entry.getKey();
101                 String[] values = (String[]) entry.getValue();
102                 for (int i = 0; i < values.length; i++) {
103                     String value = values[i];
104                     result.append(name + "=" + value + "&");
105                 }
106             }
107         }
108         return result.toString().substring(0, result.length() - 1);
109     }
110 
111 }