001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.chain.web.portlet; 018 019import java.io.InputStream; 020import java.net.MalformedURLException; 021import java.net.URL; 022import java.util.Enumeration; 023import java.util.HashMap; 024import java.util.Map; 025import java.util.Set; 026 027import javax.portlet.PortletContext; 028import javax.portlet.PortletRequestDispatcher; 029 030import org.apache.commons.chain.web.MockEnumeration; 031 032/** 033 * Mock Object for {@code PortletContext} 034 */ 035public class MockPortletContext implements PortletContext { 036 private int majorVersion = 3; 037 private int minorVersion = 1; 038 private String portletContextName = "MockPortletContext"; 039 private String serverInfo = portletContextName; 040 private Map<String, String> parameters = new HashMap<>(); 041 private Map<String, Object> attributes = new HashMap<>(); 042 043 // ----------------------------------------------------------- Constructors 044 045 /** 046 * The Default-Constructor for this class. 047 */ 048 public MockPortletContext() { 049 } 050 051 // --------------------------------------------------------- Public Methods 052 053 public void setPortletContextName(String portletContextName) { 054 this.portletContextName = portletContextName; 055 } 056 057 public void setServerInfo(String serverInfo) { 058 this.serverInfo = serverInfo; 059 } 060 061 public void addInitParameter(String name, String value) { 062 parameters.put(name, value); 063 } 064 065 // ------------------------------------------------- PortletContext Methods 066 067 @Override 068 public Object getAttribute(String name) { 069 return attributes.get(name); 070 } 071 072 @Override 073 public Enumeration<String> getAttributeNames() { 074 return new MockEnumeration<>(attributes.keySet().iterator()); 075 } 076 077 @Override 078 public String getInitParameter(String name) { 079 return parameters.get(name); 080 } 081 082 @Override 083 public Enumeration<String> getInitParameterNames() { 084 return new MockEnumeration<>(parameters.keySet().iterator()); 085 } 086 087 @Override 088 public int getMajorVersion() { 089 return majorVersion; 090 } 091 092 @Override 093 public String getMimeType(String path) { 094 throw new UnsupportedOperationException(); 095 } 096 097 @Override 098 public int getMinorVersion() { 099 return minorVersion; 100 } 101 102 @Override 103 public PortletRequestDispatcher getNamedDispatcher(String name) { 104 throw new UnsupportedOperationException(); 105 } 106 107 @Override 108 public String getPortletContextName() { 109 return portletContextName; 110 } 111 112 @Override 113 public String getRealPath(String path) { 114 throw new UnsupportedOperationException(); 115 } 116 117 @Override 118 public PortletRequestDispatcher getRequestDispatcher(String path) { 119 throw new UnsupportedOperationException(); 120 } 121 122 @Override 123 public URL getResource(String path) throws MalformedURLException { 124 throw new UnsupportedOperationException(); 125 } 126 127 @Override 128 public InputStream getResourceAsStream(String path) { 129 throw new UnsupportedOperationException(); 130 } 131 132 @Override 133 public Set<String> getResourcePaths(String path) { 134 throw new UnsupportedOperationException(); 135 } 136 137 @Override 138 public String getServerInfo() { 139 return serverInfo; 140 } 141 142 @Override 143 public void log(String message) { 144 throw new UnsupportedOperationException(); 145 } 146 147 @Override 148 public void log(String message, Throwable exception) { 149 throw new UnsupportedOperationException(); 150 } 151 152 @Override 153 public void removeAttribute(String name) { 154 attributes.remove(name); 155 } 156 157 @Override 158 public void setAttribute(String name, Object value) { 159 attributes.put(name, value); 160 } 161 162 @Override 163 public Enumeration<String> getContainerRuntimeOptions() { 164 throw new UnsupportedOperationException(); 165 } 166 167 @Override 168 public int getEffectiveMajorVersion() { 169 return majorVersion; 170 } 171 172 @Override 173 public int getEffectiveMinorVersion() { 174 return minorVersion; 175 } 176 177 @Override 178 public String getContextPath() { 179 throw new UnsupportedOperationException(); 180 } 181 182 @Override 183 public ClassLoader getClassLoader() { 184 return MockPortletContext.class.getClassLoader(); 185 } 186}