|
| 1 | +package net.sf.jsqlparser.statement; |
| 2 | + |
| 3 | +import net.sf.jsqlparser.JSQLParserException; |
| 4 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 5 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 6 | +import org.junit.jupiter.api.Assertions; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.ObjectInputStream; |
| 13 | +import java.io.ObjectOutputStream; |
| 14 | + |
| 15 | +public class SerializationTest { |
| 16 | + @Test |
| 17 | + void serializeWithItem() throws JSQLParserException, IOException, ClassNotFoundException { |
| 18 | + String sqlStr = |
| 19 | + "with sample_data(day, value) as (values ((0, 13), (1, 12), (2, 15), (3, 4), (4, 8), (5, 16))), test2 as (values (1,2,3)) \n" |
| 20 | + + "select day, value from sample_data as a"; |
| 21 | + |
| 22 | + // Parse the SQL string into a PlainSelect object |
| 23 | + PlainSelect originalSelect = (PlainSelect) CCJSqlParserUtil.parse(sqlStr); |
| 24 | + |
| 25 | + // Serialize the object to a byte array |
| 26 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 27 | + try (ObjectOutputStream out = new ObjectOutputStream(byteArrayOutputStream)) { |
| 28 | + out.writeObject(originalSelect); |
| 29 | + } |
| 30 | + |
| 31 | + // Deserialize the object from the byte array |
| 32 | + PlainSelect deserializedSelect; |
| 33 | + try (ObjectInputStream in = new ObjectInputStream( |
| 34 | + new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))) { |
| 35 | + deserializedSelect = (PlainSelect) in.readObject(); |
| 36 | + } |
| 37 | + |
| 38 | + // Verify that the original and deserialized objects are equal |
| 39 | + Assertions.assertEquals(originalSelect.toString(), deserializedSelect.toString(), |
| 40 | + "The deserialized object should be equal to the original"); |
| 41 | + } |
| 42 | + |
| 43 | +} |
0 commit comments