|
| 1 | +/* |
| 2 | + * BioJava development code |
| 3 | + * |
| 4 | + * This code may be freely distributed and modified under the |
| 5 | + * terms of the GNU Lesser General Public Licence. This should |
| 6 | + * be distributed with the code. If you do not have a copy, |
| 7 | + * see: |
| 8 | + * |
| 9 | + * http://www.gnu.org/copyleft/lesser.html |
| 10 | + * |
| 11 | + * Copyright for this code is held jointly by the individual |
| 12 | + * authors. These should be listed in @author doc comments. |
| 13 | + * |
| 14 | + * For more information on the BioJava project and its aims, |
| 15 | + * or to join the biojava-l mailing list, visit the home page |
| 16 | + * at: |
| 17 | + * |
| 18 | + * http://www.biojava.org/ |
| 19 | + * |
| 20 | + */ |
| 21 | +package org.biojava.nbio.structure; |
| 22 | + |
| 23 | +import org.biojava.nbio.structure.io.PDBFileReader; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import java.io.ByteArrayInputStream; |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.ObjectInputStream; |
| 30 | +import java.io.ObjectOutputStream; |
| 31 | + |
| 32 | +import static org.junit.Assert.*; |
| 33 | + |
| 34 | +/** |
| 35 | + * Test the serialization of BioJava structure objects. |
| 36 | + * |
| 37 | + * @author Aleix Lafita |
| 38 | + * |
| 39 | + */ |
| 40 | +public class TestStructureSerialization { |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testSerializeStructure() throws IOException, StructureException, ClassNotFoundException { |
| 44 | + |
| 45 | + PDBFileReader reader = new PDBFileReader(); |
| 46 | + Structure sin = reader.getStructure("src/test/resources/2gox.pdb"); |
| 47 | + |
| 48 | + // Serialize the structure object and keep it in memory |
| 49 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 50 | + ObjectOutputStream objectOut = new ObjectOutputStream(baos); |
| 51 | + objectOut.writeObject(sin); |
| 52 | + objectOut.close(); |
| 53 | + byte[] bytes = baos.toByteArray(); |
| 54 | + |
| 55 | + // Deserialize the bytes back into a structure object |
| 56 | + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
| 57 | + ObjectInputStream objectIn = new ObjectInputStream(bais); |
| 58 | + Structure sout = (Structure) objectIn.readObject(); |
| 59 | + objectIn.close(); |
| 60 | + |
| 61 | + // Test some properties of the structures |
| 62 | + assertEquals(sin.nrModels(), sout.nrModels()); |
| 63 | + assertEquals(sin.getChains().size(), sout.getChains().size()); |
| 64 | + assertEquals(sin.getName(), sout.getName()); |
| 65 | + |
| 66 | + } |
| 67 | +} |
0 commit comments