|
| 1 | +/* Copyright (c) 2016, Martin Grotzke |
| 2 | + * All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following |
| 5 | + * conditions are met: |
| 6 | + * |
| 7 | + * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 8 | + * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following |
| 9 | + * disclaimer in the documentation and/or other materials provided with the distribution. |
| 10 | + * - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived |
| 11 | + * from this software without specific prior written permission. |
| 12 | + * |
| 13 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
| 14 | + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 15 | + * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 16 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 17 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 18 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
| 19 | + |
| 20 | +package com.esotericsoftware.kryo.serializers; |
| 21 | + |
| 22 | +import com.esotericsoftware.kryo.Kryo; |
| 23 | +import com.esotericsoftware.kryo.KryoTestCase; |
| 24 | +import org.junit.Assert; |
| 25 | +import org.junit.Test; |
| 26 | +import org.objenesis.strategy.StdInstantiatorStrategy; |
| 27 | + |
| 28 | +import java.lang.invoke.SerializedLambda; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.concurrent.Callable; |
| 31 | + |
| 32 | +import static org.hamcrest.CoreMatchers.is; |
| 33 | +import static org.junit.Assert.assertThat; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test for java 8 closures. |
| 37 | + * |
| 38 | + * For jdk < 1.8 excluded from surefire tests via the "until-java8" profile in pom.xml which excludes "Java8*Tests". |
| 39 | + */ |
| 40 | +public class Java8ClosureSerializerTest extends KryoTestCase { |
| 41 | + |
| 42 | + public void setUp() throws Exception { |
| 43 | + super.setUp(); |
| 44 | + kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); |
| 45 | + // the following registrations are needed because registration is required |
| 46 | + kryo.register(Object[].class); |
| 47 | + kryo.register(java.lang.Class.class); |
| 48 | + kryo.register(getClass()); // closure capturing class (in this test `this`), it would usually already be registered |
| 49 | + kryo.register(SerializedLambda.class); |
| 50 | + // always needed for closure serialization, also if registrationRequired=false |
| 51 | + kryo.register(ClosureSerializer.Closure.class, new ClosureSerializer()); |
| 52 | + } |
| 53 | + |
| 54 | + public void testSerializeSerializableLambdaWithKryo() throws Exception { |
| 55 | + Callable<Boolean> doNothing = (Callable<Boolean> & java.io.Serializable)(() -> true); |
| 56 | + roundTrip(222, 225, doNothing); |
| 57 | + } |
| 58 | + |
| 59 | + // we must override equals as lambdas have no equals check built in... |
| 60 | + @Override |
| 61 | + protected void doAssertEquals(Object object1, Object object2) { |
| 62 | + try { |
| 63 | + Assert.assertEquals(((Callable<?>)object1).call(), ((Callable<?>)object2).call()); |
| 64 | + } catch (Exception e) { |
| 65 | + throw new RuntimeException(e.getMessage()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments