|
| 1 | +package com.fasterxml.jackson.module.blackbird.ser; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.lang.reflect.Field; |
| 5 | +import java.net.URL; |
| 6 | +import java.net.URLClassLoader; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.Arrays; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | +import com.fasterxml.jackson.module.blackbird.BlackbirdTestBase; |
| 16 | + |
| 17 | +import javax.tools.*; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +public class TestNoPackageSerialization extends BlackbirdTestBase |
| 23 | +{ |
| 24 | + @Test |
| 25 | + public void testSerializeDeserializeDefaultPackageClass() throws Exception { |
| 26 | + // Define the source code for a class in the default package (no package |
| 27 | + // declaration) |
| 28 | + String source = "package dynamicClassTest;" + |
| 29 | + "public class Person {" + |
| 30 | + " public String name;" + |
| 31 | + " public int age;" + |
| 32 | + " public Person() {}" + |
| 33 | + " public Person(String name, int age) {" + |
| 34 | + " this.name = name;" + |
| 35 | + " this.age = age;" + |
| 36 | + " }" + |
| 37 | + "}"; |
| 38 | + |
| 39 | + // Create a temporary directory for the compiled class |
| 40 | + Path tempDir = Files.createTempDirectory("dynamicClassTest"); |
| 41 | + File sourceFile = new File(tempDir.toFile(), "Person.java"); |
| 42 | + //System.out.println(sourceFile.getAbsolutePath()); |
| 43 | + Files.write(sourceFile.toPath(), source.getBytes(StandardCharsets.UTF_8)); |
| 44 | + |
| 45 | + // Compile the source file using the JDK compiler |
| 46 | + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 47 | + StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); |
| 48 | + Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(sourceFile); |
| 49 | + JavaCompiler.CompilationTask task = compiler.getTask( |
| 50 | + null, fileManager, null, |
| 51 | + Arrays.asList("-d", tempDir.toString()), |
| 52 | + null, compilationUnits); |
| 53 | + assertTrue(task.call(), "Compilation failed"); |
| 54 | + fileManager.close(); |
| 55 | + |
| 56 | + // Load the compiled class using a URLClassLoader |
| 57 | + try (URLClassLoader classLoader = new URLClassLoader(new URL[] { tempDir.toUri().toURL() })) { |
| 58 | + Class<?> personClass = classLoader.loadClass("dynamicClassTest.Person"); |
| 59 | + |
| 60 | + // Instantiate the Person object using reflection |
| 61 | + Object personInstance = personClass.getConstructor(String.class, int.class) |
| 62 | + .newInstance("John Doe", 42); |
| 63 | + final ObjectMapper mapper = newObjectMapper(); |
| 64 | + |
| 65 | + // Perform serialization and deserialization |
| 66 | + String json = mapper.writeValueAsString(personInstance); |
| 67 | + Object deserialized = mapper.readValue(json, personClass); |
| 68 | + |
| 69 | + // Verify that the deserialized object has the expected field values via |
| 70 | + // reflection |
| 71 | + Field nameField = personClass.getField("name"); |
| 72 | + Field ageField = personClass.getField("age"); |
| 73 | + |
| 74 | + assertEquals("John Doe", nameField.get(deserialized)); |
| 75 | + assertEquals(42, ageField.get(deserialized)); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments