View Javadoc

1   /*
2    * Copyright 2013 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 1.0 (the
5    * "License"); 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.opensource.org/licenses/ecl1.php
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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.kuali.student.common.spring;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  
25  import org.apache.commons.io.FileUtils;
26  import org.junit.Assert;
27  import org.kuali.student.r2.common.messages.service.MessageService;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   *
33   * @author Kuali Student Team 
34   *
35   */
36  public final class SpringProxyTestUtils {
37      private static final Logger log = LoggerFactory
38              .getLogger(SpringProxyTestUtils.class);
39  
40      /**
41       * 
42       */
43      private SpringProxyTestUtils() {
44          // TODO Auto-generated constructor stub
45      }
46      
47      public static Object testBeanSerialization(Object bean, long expectedSize) throws IOException, ClassNotFoundException {
48          
49          File tempFile = File.createTempFile("proxy", "dat");
50  
51          ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tempFile));
52          
53          oos.writeObject(bean);
54          oos.flush();
55          oos.close();
56          
57          // check the size of that file
58          
59          long serializedSizeInBytes = FileUtils.sizeOf(tempFile);
60          
61          Assert.assertEquals(expectedSize, serializedSizeInBytes);
62          
63          ObjectInputStream iis = new ObjectInputStream(new FileInputStream(tempFile));
64          
65          Object deserialized =  iis.readObject();
66          
67          iis.close();
68          
69          return deserialized;
70          
71      }
72      
73   public static Object testBeanSerialization(Object bean, String expectedToStringValue, long expectedSize) throws IOException, ClassNotFoundException {
74          
75          File tempFile = File.createTempFile("proxy", "dat");
76  
77          ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tempFile));
78          
79          oos.writeObject(bean);
80          oos.flush();
81          oos.close();
82          
83          // check the size of that file
84          
85          long serializedSizeInBytes = FileUtils.sizeOf(tempFile);
86          
87          Assert.assertEquals(expectedSize, serializedSizeInBytes);
88          
89          ObjectInputStream iis = new ObjectInputStream(new FileInputStream(tempFile));
90          
91          Object generated =  iis.readObject();
92          
93          iis.close();
94          
95          Assert.assertEquals(expectedToStringValue, generated.toString());
96          
97          return generated;
98      }
99  }