Source Code Cross Referenced for SerializationTest.java in  » Swing-Library » jgoodies-forms » com » jgoodies » forms » layout » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Swing Library » jgoodies forms » com.jgoodies.forms.layout 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003:         *
004:         * Redistribution and use in source and binary forms, with or without 
005:         * modification, are permitted provided that the following conditions are met:
006:         * 
007:         *  o Redistributions of source code must retain the above copyright notice, 
008:         *    this list of conditions and the following disclaimer. 
009:         *     
010:         *  o Redistributions in binary form must reproduce the above copyright notice, 
011:         *    this list of conditions and the following disclaimer in the documentation 
012:         *    and/or other materials provided with the distribution. 
013:         *     
014:         *  o Neither the name of JGoodies Karsten Lentzsch nor the names of 
015:         *    its contributors may be used to endorse or promote products derived 
016:         *    from this software without specific prior written permission. 
017:         *     
018:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
019:         * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
020:         * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
021:         * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
022:         * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
023:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
024:         * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
025:         * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026:         * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
027:         * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
028:         * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
029:         */
030:
031:        package com.jgoodies.forms.layout;
032:
033:        import java.io.*;
034:
035:        import javax.swing.JButton;
036:        import javax.swing.JLabel;
037:        import javax.swing.JPanel;
038:
039:        import junit.framework.TestCase;
040:
041:        /**
042:         * Tests the serialization and deserialization of instances of 
043:         * <code>FormLayout</code> and <code>JPanel</code>. 
044:         *
045:         * @author Karsten Lentzsch
046:         * @version $Revision: 1.5 $
047:         * 
048:         * @see java.io.Serializable
049:         */
050:
051:        public final class SerializationTest extends TestCase {
052:
053:            /**
054:             * Tests the serialization of a FormLayout that has just been constructed.
055:             * The layout contains no components and the layout algorithm has not
056:             * been performed.
057:             */
058:            public void testSerializeConstructedLayout() {
059:                FormLayout layout = createSampleLayout();
060:                OutputStream out = new ByteArrayOutputStream();
061:                serialize(out, layout);
062:            }
063:
064:            /**
065:             * Tests the serialization of an empty FormLayout that has computed 
066:             * a layout immediately after its construction.
067:             * The layout contains no components.
068:             */
069:            public void testSerializeEmptyLayout() {
070:                FormLayout layout = createSampleLayout();
071:                doLayout(layout);
072:                OutputStream out = new ByteArrayOutputStream();
073:                serialize(out, layout);
074:            }
075:
076:            /**
077:             * Tests the serialization of a panel that is layed out using
078:             * a FormLayout. The panel consists some sample components that in turn
079:             * uses some sample <code>CellConstraints</code>.
080:             */
081:            public void testSerializePanel() {
082:                JPanel panel = createSamplePanel();
083:                OutputStream out = new ByteArrayOutputStream();
084:                serialize(out, panel);
085:            }
086:
087:            /**
088:             * Tests the deserialization of a FormLayout that has just been constructed.
089:             * The layout contains no components and the layout algorithm has not
090:             * been performed.
091:             */
092:            public void testDeserializeConstructedLayout() {
093:                FormLayout layout = createSampleLayout();
094:                ByteArrayOutputStream out = new ByteArrayOutputStream();
095:                serialize(out, layout);
096:                byte[] bytes = out.toByteArray();
097:                InputStream in = new ByteArrayInputStream(bytes);
098:                deserialize(in);
099:            }
100:
101:            /**
102:             * Tests the deserialization of an empty FormLayout that has computed 
103:             * a layout immediately after its construction.
104:             * The layout contains no components.
105:             */
106:            public void testDeserializeEmptyLayout() {
107:                FormLayout layout = createSampleLayout();
108:                doLayout(layout);
109:                ByteArrayOutputStream out = new ByteArrayOutputStream();
110:                serialize(out, layout);
111:                byte[] bytes = out.toByteArray();
112:                InputStream in = new ByteArrayInputStream(bytes);
113:                deserialize(in);
114:            }
115:
116:            /**
117:             * Tests the deserialization of a panel that is layed out using
118:             * a FormLayout. The panel consists some sample components that in turn
119:             * uses some sample <code>CellConstraints</code>.
120:             */
121:            public void testDeserializePanel() {
122:                JPanel panel = createSamplePanel();
123:                ByteArrayOutputStream out = new ByteArrayOutputStream();
124:                serialize(out, panel);
125:                byte[] bytes = out.toByteArray();
126:                InputStream in = new ByteArrayInputStream(bytes);
127:                deserialize(in);
128:            }
129:
130:            /**
131:             * Tests that the a layout can be computed with a deserialized 
132:             * empty FormLayout.
133:             */
134:            public void testLayoutDeserializedEmptyLayout() {
135:                FormLayout layout = createSampleLayout();
136:                doLayout(layout);
137:                ByteArrayOutputStream out = new ByteArrayOutputStream();
138:                serialize(out, layout);
139:                byte[] bytes = out.toByteArray();
140:                InputStream in = new ByteArrayInputStream(bytes);
141:                FormLayout layout2 = (FormLayout) deserialize(in);
142:                doLayout(layout2);
143:            }
144:
145:            /**
146:             * Tests that the a panel can be layed out with a deserialized 
147:             * FormLayout.
148:             */
149:            public void testLayoutDeserializedPanel() {
150:                JPanel panel = createSamplePanel();
151:                ByteArrayOutputStream out = new ByteArrayOutputStream();
152:                serialize(out, panel);
153:                byte[] bytes = out.toByteArray();
154:                InputStream in = new ByteArrayInputStream(bytes);
155:                JPanel panel2 = (JPanel) deserialize(in);
156:                panel2.doLayout();
157:            }
158:
159:            // Helper Code *********************************************************
160:
161:            /**
162:             * Creates and returns a sample <code>FormLayout</code> instance
163:             * that uses all prebuilt alignments and <code>Size</code> implementations
164:             * so we can test their serialization and deserialization. 
165:             * 
166:             * @return a sample layout
167:             */
168:            private FormLayout createSampleLayout() {
169:                return new FormLayout(
170:                        "l:1px, c:2dlu, r:3mm, f:m, p, d, max(p;3dlu), min(p;7px)",
171:                        "t:1px, c:2dlu, b:3mm, f:m, p, d, max(p;3dlu), min(p;7px)");
172:            }
173:
174:            /**
175:             * Creates and returns a sample panel that uses the sample layout
176:             * created by <code>#createSampleLayout</code>. Useful to test the
177:             * FormLayout serialization in combination with a layout container
178:             * and some components managed by the FormLayout. Especially it tests
179:             * the serialization of <code>CellConstraints</code> objects.
180:             * 
181:             * @return a sample panel
182:             */
183:            private JPanel createSamplePanel() {
184:                JPanel panel = new JPanel(createSampleLayout());
185:                CellConstraints cc = new CellConstraints();
186:                panel.add(new JLabel("Test1"), cc.xy(1, 1, "l, t"));
187:                panel.add(new JButton("Test2"), cc.xy(2, 2, "c, c"));
188:                panel.add(new JButton("Test3"), cc.xy(3, 3, "r, b"));
189:                panel.add(new JButton("Test4"), cc.xy(4, 4, "f, f"));
190:                panel.doLayout();
191:                return panel;
192:            }
193:
194:            /**
195:             * Lays out a container using the given <code>FormLayout</code>
196:             * and returns the layout info object.
197:             * 
198:             * @param layout    the FormLayout used to lay out
199:             * @return the layout info after the container has been layed out
200:             */
201:            private FormLayout.LayoutInfo doLayout(FormLayout layout) {
202:                JPanel panel = new JPanel(layout);
203:                panel.doLayout();
204:                FormLayout.LayoutInfo info = layout.getLayoutInfo(panel);
205:                return info;
206:            }
207:
208:            /**
209:             * Serializes the given object and writes it to the given 
210:             * output stream.
211:             * 
212:             * @param out      the stream to write the serialized object
213:             * @param object   the object to be serialized
214:             */
215:            private void serialize(OutputStream out, Object object) {
216:                ObjectOutputStream objectOut = null;
217:                try {
218:                    objectOut = new ObjectOutputStream(out);
219:                    objectOut.writeObject(object);
220:                } catch (IOException e) {
221:                    e.printStackTrace();
222:                    fail("IO Exception");
223:                } finally {
224:                    if (objectOut != null) {
225:                        try {
226:                            objectOut.close();
227:                        } catch (IOException e1) {
228:                            e1.printStackTrace();
229:                        }
230:                    }
231:                }
232:            }
233:
234:            /**
235:             * Deserializes and returns an object that is contained in serialized form
236:             * in the given input stream.
237:             * 
238:             * @param in   the stream to read from
239:             * @return the deserialized object
240:             */
241:            private Object deserialize(InputStream in) {
242:                ObjectInputStream objectIn = null;
243:                try {
244:                    objectIn = new ObjectInputStream(in);
245:                    return objectIn.readObject();
246:                } catch (IOException e) {
247:                    e.printStackTrace();
248:                    fail("IO Exception");
249:                } catch (ClassNotFoundException e) {
250:                    e.printStackTrace();
251:                    fail("Class not found");
252:                } finally {
253:                    if (objectIn != null) {
254:                        try {
255:                            objectIn.close();
256:                        } catch (IOException e1) {
257:                            e1.printStackTrace();
258:                        }
259:                    }
260:                }
261:                return null;
262:            }
263:
264:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.