GryoMapper implements Mapper<Kryo> { @Override public Kryo createMapper() { final Kryo kryo = new Kryo(classResolver.get(), new MapReferenceResolver(), new DefaultStreamFactory()); kryo.addDefaultSerializer(Map.Entry.class, new UtilSerializers.EntrySerializer()); kryo.setRegistrationRequired(registrationRequired); kryo.setReferences(referenceTracking); for (TypeRegistration tr : typeRegistrations) tr.registerWith(kryo); return kryo; } private  GryoMapper(final Builder builder); @Override Kryo createMapper(); GryoVersion getVersion(); List<Class> getRegisteredClasses(); List<TypeRegistration<?>> getTypeRegistrations(); static Builder build(); static final byte[] GIO; static final byte[] HEADER; }
@Test public void shouldSerializeDeserialize() throws Exception { final GryoMapper mapper = builder.get().create(); final Kryo kryo = mapper.createMapper(); try (final OutputStream stream = new ByteArrayOutputStream()) { final Output out = new Output(stream); final Map<String,Object> props = new HashMap<>(); final List<Map<String, Object>> propertyNames = new ArrayList<>(1); final Map<String,Object> propertyName = new HashMap<>(); propertyName.put(GraphSONTokens.ID, "x"); propertyName.put(GraphSONTokens.KEY, "x"); propertyName.put(GraphSONTokens.VALUE, "no-way-this-will-ever-work"); propertyNames.add(propertyName); props.put("x", propertyNames); final DetachedVertex v = new DetachedVertex(100, Vertex.DEFAULT_LABEL, props); kryo.writeClassAndObject(out, v); try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) { final Input input = new Input(inputStream); final DetachedVertex readX = (DetachedVertex) kryo.readClassAndObject(input); assertEquals("no-way-this-will-ever-work", readX.value("x")); } } }
@Test public void shouldSerializeWithCustomClassResolverToDetachedVertex() throws Exception { final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier(); final GryoMapper mapper = builder.get().classResolver(classResolver).create(); final Kryo kryo = mapper.createMapper(); try (final OutputStream stream = new ByteArrayOutputStream()) { final Output out = new Output(stream); final IoX x = new IoX("no-way-this-will-ever-work"); kryo.writeClassAndObject(out, x); final GryoMapper mapperWithoutKnowledgeOfIox = builder.get().create(); final Kryo kryoWithoutKnowledgeOfIox = mapperWithoutKnowledgeOfIox.createMapper(); try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) { final Input input = new Input(inputStream); final DetachedVertex readX = (DetachedVertex) kryoWithoutKnowledgeOfIox.readClassAndObject(input); assertEquals("no-way-this-will-ever-work", readX.value("x")); } } }
@Test public void shouldSerializeWithCustomClassResolverToHashMap() throws Exception { final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier(); final GryoMapper mapper = builder.get().classResolver(classResolver).create(); final Kryo kryo = mapper.createMapper(); try (final OutputStream stream = new ByteArrayOutputStream()) { final Output out = new Output(stream); final IoY y = new IoY(100, 200); kryo.writeClassAndObject(out, y); final GryoMapper mapperWithoutKnowledgeOfIoy = builder.get().create(); final Kryo kryoWithoutKnowledgeOfIox = mapperWithoutKnowledgeOfIoy.createMapper(); try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) { final Input input = new Input(inputStream); final Map readY = (HashMap) kryoWithoutKnowledgeOfIox.readClassAndObject(input); assertEquals("100-200", readY.get("y")); } } }
@Test public void shouldSerializeWithoutRegistration() throws Exception { final GryoMapper mapper = builder.get().registrationRequired(false).create(); final Kryo kryo = mapper.createMapper(); try (final OutputStream stream = new ByteArrayOutputStream()) { final Output out = new Output(stream); final IoX x = new IoX("x"); final IoY y = new IoY(100, 200); kryo.writeClassAndObject(out, x); kryo.writeClassAndObject(out, y); try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) { final Input input = new Input(inputStream); final IoX readX = (IoX) kryo.readClassAndObject(input); final IoY readY = (IoY) kryo.readClassAndObject(input); assertEquals(x, readX); assertEquals(y, readY); } } }
@Test public void shouldRegisterMultipleIoRegistryToSerialize() throws Exception { final GryoMapper mapper = builder.get().addRegistry(IoXIoRegistry.InstanceBased.instance()) .addRegistry(IoYIoRegistry.InstanceBased.instance()).create(); final Kryo kryo = mapper.createMapper(); try (final OutputStream stream = new ByteArrayOutputStream()) { final Output out = new Output(stream); final IoX x = new IoX("x"); final IoY y = new IoY(100, 200); kryo.writeClassAndObject(out, x); kryo.writeClassAndObject(out, y); try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) { final Input input = new Input(inputStream); final IoX readX = (IoX) kryo.readClassAndObject(input); final IoY readY = (IoY) kryo.readClassAndObject(input); assertEquals(x, readX); assertEquals(y, readY); } } }
@Test public void shouldOverrideExistingSerializer() throws Exception { final GryoMapper mapper = builder.get().addCustom(Duration.class, new OverrideDurationSerializer()).create(); try (final OutputStream stream = new ByteArrayOutputStream()) { final Output out = new Output(stream); mapper.createMapper().writeObject(out, Duration.ZERO); fail("The OverrideDurationSerializer throws exceptions so this should not have worked"); } catch (Exception ex) { assertThat(ex, instanceOf(UnsupportedOperationException.class)); assertEquals("I don't do anything", ex.getMessage()); } }