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: }
|