1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   */
19  package org.apache.myfaces.trinidad.component;
20  
21  import javax.faces.context.FacesContext;
22  import javax.faces.el.MethodBinding;
23  
24  import org.apache.myfaces.trinidad.event.AttributeChangeEvent;
25  import org.apache.myfaces.trinidad.event.AttributeChangeListener;
26  
27  /**
28   * Utility class for testing out AttributeChange events.
29   */
30  public class AttributeChangeTester extends MethodBinding
31   implements AttributeChangeListener
32  {
33    public AttributeChangeTester()
34    {
35    }
36  
37    public void processAttributeChange(AttributeChangeEvent event)
38    {
39      if (_methodBindingCalled)
40        throw new IllegalStateException("Method binding called before listener");
41      _listenerCalled = true;
42    }
43  
44    @Override
45    public Object invoke(FacesContext context, Object params[])
46    {
47      if (params.length != 1)
48        throw new IllegalStateException("Params not of length 1");
49      if (params[0] == null)
50        throw new IllegalStateException("Event is null");
51      if (!(params[0] instanceof AttributeChangeEvent))
52        throw new IllegalStateException("Event isn't an AttributeChangeEvent");
53  
54      _methodBindingCalled = true;
55      return null;
56    }
57  
58    @Override
59    public Class<?> getType(FacesContext context)
60    {
61      return null;
62    }
63  
64    public void verify()
65    {
66      if (!_methodBindingCalled)
67        throw new IllegalStateException("Method binding never called");
68  
69      if (!_listenerCalled)
70        throw new IllegalStateException("Listener never called");
71    }
72  
73    private boolean _methodBindingCalled;
74    private boolean _listenerCalled;
75  }