Skip to content

Commit

Permalink
Add test for java 8 closure serialization
Browse files Browse the repository at this point in the history
For this test the compiler source/target level for tests must be set to 1.8.
This is done via the "java8" profile which is activated if built with
java 8+. Java8*Tests are excluded by default from tests compilation, this
profile removes this exclude. Because overriding the compiler plugin in the
profile did not really work build properties are used.

To deactivate the java8 profile (e.g. to compile tests for jdks < 1.8)
mvn can be invoked with `-P !java8` (or `-P \!java8` on linux).

Refs EsotericSoftware#299
  • Loading branch information
magro committed Mar 29, 2016
1 parent 6c402d9 commit bf80397
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<versions.reflectasm>1.10.1</versions.reflectasm>
<test.exclude>**/Java8*Test.java</test.exclude>
</properties>

<modules>
Expand Down Expand Up @@ -101,6 +102,9 @@
<source>1.5</source>
<target>1.5</target>
<encoding>utf-8</encoding>
<testExcludes>
<testExclude>${test.exclude}</testExclude>
</testExcludes>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -206,6 +210,18 @@
</plugins>
</build>
</profile>
<profile>
<id>java8</id>
<!-- To disable this profile run mvn with '-P !java8' (maybe escape the exclamation mark) -->
<activation><jdk>[1.8,)</jdk></activation>
<!-- use properties to change compiler configuration because overriding build/plugin config just did not work -->
<properties>
<!-- Setting an empty values does not override/set the property -->
<test.exclude>someValueWhichDoesNotExist</test.exclude>
<maven.compiler.testSource>1.8</maven.compiler.testSource>
<maven.compiler.testTarget>1.8</maven.compiler.testTarget>
</properties>
</profile>
</profiles>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright (c) 2016, Martin Grotzke
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.esotericsoftware.kryo.serializers;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoTestCase;
import org.junit.Assert;
import org.junit.Test;
import org.objenesis.strategy.StdInstantiatorStrategy;

import java.lang.invoke.SerializedLambda;
import java.util.Arrays;
import java.util.concurrent.Callable;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* Test for java 8 closures.
*
* For jdk < 1.8 excluded from surefire tests via the "until-java8" profile in pom.xml which excludes "Java8*Tests".
*/
public class Java8ClosureSerializerTest extends KryoTestCase {

public void setUp() throws Exception {
super.setUp();
kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
// the following registrations are needed because registration is required
kryo.register(Object[].class);
kryo.register(java.lang.Class.class);
kryo.register(getClass()); // closure capturing class (in this test `this`), it would usually already be registered
kryo.register(SerializedLambda.class);
// always needed for closure serialization, also if registrationRequired=false
kryo.register(ClosureSerializer.Closure.class, new ClosureSerializer());
}

public void testSerializeSerializableLambdaWithKryo() throws Exception {
Callable<Boolean> doNothing = (Callable<Boolean> & java.io.Serializable)(() -> true);
roundTrip(222, 225, doNothing);
}

// we must override equals as lambdas have no equals check built in...
@Override
protected void doAssertEquals(Object object1, Object object2) {
try {
Assert.assertEquals(((Callable<?>)object1).call(), ((Callable<?>)object2).call());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}

}

0 comments on commit bf80397

Please sign in to comment.