Skip to content

Commit bf80397

Browse files
committed
Add test for java 8 closure serialization
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 #299
1 parent 6c402d9 commit bf80397

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<properties>
3939
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4040
<versions.reflectasm>1.10.1</versions.reflectasm>
41+
<test.exclude>**/Java8*Test.java</test.exclude>
4142
</properties>
4243

4344
<modules>
@@ -101,6 +102,9 @@
101102
<source>1.5</source>
102103
<target>1.5</target>
103104
<encoding>utf-8</encoding>
105+
<testExcludes>
106+
<testExclude>${test.exclude}</testExclude>
107+
</testExcludes>
104108
</configuration>
105109
</plugin>
106110
<plugin>
@@ -206,6 +210,18 @@
206210
</plugins>
207211
</build>
208212
</profile>
213+
<profile>
214+
<id>java8</id>
215+
<!-- To disable this profile run mvn with '-P !java8' (maybe escape the exclamation mark) -->
216+
<activation><jdk>[1.8,)</jdk></activation>
217+
<!-- use properties to change compiler configuration because overriding build/plugin config just did not work -->
218+
<properties>
219+
<!-- Setting an empty values does not override/set the property -->
220+
<test.exclude>someValueWhichDoesNotExist</test.exclude>
221+
<maven.compiler.testSource>1.8</maven.compiler.testSource>
222+
<maven.compiler.testTarget>1.8</maven.compiler.testTarget>
223+
</properties>
224+
</profile>
209225
</profiles>
210226

211227
<repositories>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)