JAXBCoder extends CustomCoder<T> { public static <T> JAXBCoder<T> of(Class<T> jaxbClass) { return new JAXBCoder<>(jaxbClass); } private  JAXBCoder(Class<T> jaxbClass); Class<T> getJAXBClass(); static JAXBCoder<T> of(Class<T> jaxbClass); @Override void encode(T value, OutputStream outStream); @Override void encode(T value, OutputStream outStream, Context context); @Override T decode(InputStream inStream); @Override T decode(InputStream inStream, Context context); @Override TypeDescriptor<T> getEncodedTypeDescriptor(); @Override boolean equals(@Nullable Object other); @Override int hashCode();  }
@Test public void testEncodeDecodeOuter() throws Exception { JAXBCoder<TestType> coder = JAXBCoder.of(TestType.class); byte[] encoded = CoderUtils.encodeToByteArray(coder, new TestType("abc", 9999)); assertEquals(new TestType("abc", 9999), CoderUtils.decodeFromByteArray(coder, encoded)); }
@Test public void testEncodeDecodeAfterClone() throws Exception { JAXBCoder<TestType> coder = SerializableUtils.clone(JAXBCoder.of(TestType.class)); byte[] encoded = CoderUtils.encodeToByteArray(coder, new TestType("abc", 9999)); assertEquals(new TestType("abc", 9999), CoderUtils.decodeFromByteArray(coder, encoded)); }
@Test public void testEncodeDecodeNested() throws Exception { JAXBCoder<TestType> jaxbCoder = JAXBCoder.of(TestType.class); TestCoder nesting = new TestCoder(jaxbCoder); byte[] encoded = CoderUtils.encodeToByteArray(nesting, new TestType("abc", 9999)); assertEquals(new TestType("abc", 9999), CoderUtils.decodeFromByteArray(nesting, encoded)); }
@Test @SuppressWarnings("AssertionFailureIgnored") public void testEncodeDecodeMultithreaded() throws Throwable { final JAXBCoder<TestType> coder = JAXBCoder.of(TestType.class); int numThreads = 100; final CountDownLatch ready = new CountDownLatch(numThreads); final CountDownLatch start = new CountDownLatch(1); final CountDownLatch done = new CountDownLatch(numThreads); final AtomicReference<Throwable> thrown = new AtomicReference<>(); Executor executor = Executors.newCachedThreadPool(); for (int i = 0; i < numThreads; i++) { final TestType elem = new TestType("abc", i); final int index = i; executor.execute( () -> { ready.countDown(); try { start.await(); } catch (InterruptedException ignored) { } try { byte[] encoded = CoderUtils.encodeToByteArray(coder, elem); assertEquals( new TestType("abc", index), CoderUtils.decodeFromByteArray(coder, encoded)); } catch (Throwable e) { thrown.compareAndSet(null, e); } done.countDown(); }); } ready.await(); start.countDown(); done.await(); Throwable actuallyThrown = thrown.get(); if (actuallyThrown != null) { throw actuallyThrown; } }
@Test public void testEncodable() { CoderProperties.coderSerializable(JAXBCoder.of(TestType.class)); }