1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.layout;
18  
19  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
20  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
21  import org.apache.pluto.om.common.Parameter;
22  import org.apache.pluto.om.common.ParameterSet;
23  import org.jmock.Mock;
24  import org.jmock.core.Constraint;
25  import org.jmock.core.InvocationMatcher;
26  import org.jmock.core.constraint.IsEqual;
27  import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;
28  import org.jmock.core.stub.ReturnStub;
29  
30  /***
31   * @version $Id: MockPortletRegistryFactory.java 598481 2007-11-27 00:47:29Z ate $
32   *
33   */
34  public class MockPortletRegistryFactory
35  {
36      public static PortletRegistry createMockPortletRegistry()
37      {
38          Mock portletRegistryMock;
39          PortletRegistry portletRegistry;
40          Mock portletDefMock;
41          PortletDefinitionComposite portletDef;
42          Mock portletDefInitParamsMock;
43          ParameterSet portletDefInitParams;
44  
45          Mock portletSizesParamMock;
46          Parameter portletSizesParam;
47          
48          portletRegistryMock = new Mock(PortletRegistry.class);
49          portletRegistry = (PortletRegistry) portletRegistryMock.proxy();
50          
51          portletDefMock = new Mock(PortletDefinitionComposite.class);
52          portletDef = (PortletDefinitionComposite) portletDefMock.proxy();
53  
54          portletDefInitParamsMock = new Mock(ParameterSet.class);
55          portletDefInitParams = (ParameterSet) portletDefInitParamsMock.proxy();
56  
57          portletSizesParamMock = new Mock(Parameter.class);
58          portletSizesParam = (Parameter) portletSizesParamMock.proxy();
59  
60          expectAndReturn(new InvokeAtLeastOnceMatcher(), portletSizesParamMock, "getValue", "33%,66%");
61          expectAndReturn(new InvokeAtLeastOnceMatcher(), portletDefInitParamsMock, "get",new Constraint[] {new IsEqual("sizes")}, portletSizesParam);
62          expectAndReturn(new InvokeAtLeastOnceMatcher(), portletDefMock, "getInitParameterSet", portletDefInitParams);
63          expectAndReturn(new InvokeAtLeastOnceMatcher(), portletRegistryMock, "getPortletDefinitionByUniqueName",new Constraint[] {new IsEqual("layout")}, portletDef);
64          return portletRegistry;
65      }
66      
67      protected static void expectAndReturn(InvocationMatcher matcher, Mock mock, String methodName, Constraint[] constraints, Object returnValue)
68      {
69          mock.expects(matcher).method(methodName)
70                              .with(constraints)
71                              .will(new ReturnStub(returnValue));
72      }
73      
74      protected static void expectAndReturn(InvocationMatcher matcher, Mock mock, String methodName, Object returnValue)
75      {
76          mock.expects(matcher).method(methodName)
77                              .withNoArguments()
78                              .will(new ReturnStub(returnValue));
79      }
80  }