View Javadoc

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.view.facelets.el;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  
26  import javax.el.ExpressionFactory;
27  import javax.el.ValueExpression;
28  import javax.faces.view.Location;
29  
30  import org.apache.myfaces.config.RuntimeConfig;
31  import org.apache.myfaces.test.mock.MockExternalContext;
32  import org.apache.myfaces.view.facelets.FaceletTestCase;
33  import org.apache.myfaces.view.facelets.tag.TagAttributeImpl;
34  import org.junit.Assert;
35  import org.junit.Test;
36  
37  public class SerializableELExpressionsTestCase extends FaceletTestCase
38  {
39     
40      @Override
41      protected ExpressionFactory createExpressionFactory()
42      {
43          return new org.apache.el.ExpressionFactoryImpl();
44      }
45      
46      @Test
47      public void testSerializeLocationValueExpressionWrapper() throws Exception
48      {
49          ValueExpression ve = facesContext.getApplication().getExpressionFactory().createValueExpression(
50                  facesContext.getELContext(), "#{cc.attrs.value}", Object.class);
51          
52          TagAttributeImpl tai = new TagAttributeImpl(new Location("path",299, 12), 
53                  null, "value", "value", "#{cc.attrs.value}");
54          TagValueExpression tve = new TagValueExpression(tai, ve);
55          LocationValueExpression lve = new LocationValueExpression(
56                  new Location("path2",334, 22), tve);
57          
58          ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
59          ObjectOutputStream oos = new ObjectOutputStream(baos);
60          oos.writeObject(lve);
61          oos.flush();
62          baos.flush();
63          ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
64          ObjectInputStream ois = new ObjectInputStream(bais);
65          LocationValueExpression lve2 = (LocationValueExpression) ois.readObject();
66          Assert.assertEquals(lve.getExpressionString(), lve2.getExpressionString());
67          Assert.assertEquals(lve.getLocation().getPath(), lve2.getLocation().getPath());
68          Assert.assertEquals(lve.getLocation().getLine(), lve2.getLocation().getLine());
69          Assert.assertEquals(lve.getLocation().getColumn(), lve2.getLocation().getColumn());
70          oos.close();
71          ois.close();
72      }
73      
74      @Test
75      public void testSerializeLocationValueExpressionUELWrapper() throws Exception
76      {
77          ValueExpression ve = facesContext.getApplication().getExpressionFactory().createValueExpression(
78                  facesContext.getELContext(), "#{cc.attrs.value}", Object.class);
79          
80          TagAttributeImpl tai = new TagAttributeImpl(new Location("path",299, 12), 
81                  null, "value", "value", "#{cc.attrs.value}");
82          TagValueExpression tve = new TagValueExpression(tai, ve);
83          LocationValueExpressionUEL lve = new LocationValueExpressionUEL(
84                  new Location("path2",334, 22), tve);
85          
86          ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
87          ObjectOutputStream oos = new ObjectOutputStream(baos);
88          oos.writeObject(lve);
89          oos.flush();
90          baos.flush();
91          ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
92          ObjectInputStream ois = new ObjectInputStream(bais);
93          LocationValueExpressionUEL lve2 = (LocationValueExpressionUEL) ois.readObject();
94          Assert.assertEquals(lve.getExpressionString(), lve2.getExpressionString());
95          Assert.assertEquals(lve.getLocation().getPath(), lve2.getLocation().getPath());
96          Assert.assertEquals(lve.getLocation().getLine(), lve2.getLocation().getLine());
97          Assert.assertEquals(lve.getLocation().getColumn(), lve2.getLocation().getColumn());
98          oos.close();
99          ois.close();
100     }
101     
102 }