Skip to content

Commit

Permalink
JEP-290 implementation for Helidon.
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
  • Loading branch information
tomas-langer committed Jul 19, 2021
1 parent f2acc19 commit c60d517
Show file tree
Hide file tree
Showing 48 changed files with 2,823 additions and 110 deletions.
632 changes: 527 additions & 105 deletions common/common/src/main/java/io/helidon/common/SerializationConfig.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.common;

import java.util.Properties;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* Make sure you never call configure or configureRuntime, as that modifies the shape of
* current JRE without possibility to reverse it.
*/
class SerializationConfigTest {
@Test
void testDefaults() throws Exception {
SerializationConfig serializationConfig = SerializationConfig.builder().build();

SerializationConfig.ConfigOptions options = serializationConfig.options();
assertThat(options.traceSerialization(), is(SerializationConfig.TraceOption.NONE));
// TODO this will change in 3.0.0
assertThat(options.onNoConfig(), is(SerializationConfig.Action.WARN));
// TODO this will change in 3.0.0
assertThat(options.onWrongConfig(), is(SerializationConfig.Action.WARN));
assertThat(options.filterPattern(), is("!*"));
}

@Test
void testBuilder() {
SerializationConfig serializationConfig = SerializationConfig.builder()
.ignoreFiles(true)
.filterPattern(SerializationConfigTest.class.getName())
.onNoConfig(SerializationConfig.Action.IGNORE)
.onWrongConfig(SerializationConfig.Action.CONFIGURE)
.traceSerialization(SerializationConfig.TraceOption.FULL)
.build();

SerializationConfig.ConfigOptions options = serializationConfig.options();
assertThat(options.traceSerialization(), is(SerializationConfig.TraceOption.FULL));
assertThat(options.onNoConfig(), is(SerializationConfig.Action.IGNORE));
assertThat(options.onWrongConfig(), is(SerializationConfig.Action.CONFIGURE));
assertThat(options.filterPattern(), is(SerializationConfigTest.class.getName() + ";!*"));
}

@Test
void testSysProps() {
try {
System.setProperty(SerializationConfig.PROP_PATTERN, SerializationConfigTest.class.getName());
System.setProperty(SerializationConfig.PROP_NO_CONFIG_ACTION, "ignore");
System.setProperty(SerializationConfig.PROP_WRONG_CONFIG_ACTION, "configure");
System.setProperty(SerializationConfig.PROP_TRACE, "full");
System.setProperty(SerializationConfig.PROP_IGNORE_FILES, "true");

SerializationConfig serializationConfig = SerializationConfig.builder()
.ignoreFiles(true)
.filterPattern(SerializationConfigTest.class.getName())
.onNoConfig(SerializationConfig.Action.IGNORE)
.onWrongConfig(SerializationConfig.Action.CONFIGURE)
.traceSerialization(SerializationConfig.TraceOption.FULL)
.build();

SerializationConfig.ConfigOptions options = serializationConfig.options();
assertThat(options.traceSerialization(), is(SerializationConfig.TraceOption.FULL));
assertThat(options.onNoConfig(), is(SerializationConfig.Action.IGNORE));
assertThat(options.onWrongConfig(), is(SerializationConfig.Action.CONFIGURE));
assertThat(options.filterPattern(), is(SerializationConfigTest.class.getName() + ";!*"));
} finally {
Properties properties = System.getProperties();

properties.remove(SerializationConfig.PROP_PATTERN);
properties.remove(SerializationConfig.PROP_NO_CONFIG_ACTION);
properties.remove(SerializationConfig.PROP_WRONG_CONFIG_ACTION);
properties.remove(SerializationConfig.PROP_TRACE);
properties.remove(SerializationConfig.PROP_IGNORE_FILES);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ grpc:
java:
enabled: true

mp.initializer:
allow: true
no-warn: true
52 changes: 52 additions & 0 deletions tests/integration/jep290/check_f_f_ok/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019, 2021 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.helidon.tests.integration.jep290</groupId>
<artifactId>helidon-tests-integration-jep290-project</artifactId>
<version>2.4.0-SNAPSHOT</version>
</parent>

<artifactId>helidon-tests-integration-jep290-check_f_f_ok</artifactId>
<name>Helidon Tests Integration JEP-290 check_f_f_ok</name>

<description>
Explicit pattern configured using java, fail on wrong config and pattern is ok.
</description>

<dependencies>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.tests.integration.jep290.checkffok;

import java.io.Serializable;

class Configured implements Serializable {
private static final long serialVersionUID = 12L;

private final String text;

Configured(String text) {
this.text = text;
}

String text() {
return text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.tests.integration.jep290.checkffok;

import java.io.Serializable;

class NotConfigured implements Serializable {
private static final long serialVersionUID = 12L;

private String text;

NotConfigured(String text) {
this.text = text;
}

String text() {
return text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.tests.integration.jep290.checkffok;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.io.ObjectInputFilter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Random;

import io.helidon.common.SerializationConfig;

import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class DeserializationTest {
private static final String TEST_STRING = "Hello_" + new Random().nextInt(10);
;

@BeforeAll
static void configureDeserialization() {

ObjectInputFilter myFilter = filterInfo -> {
if (filterInfo.serialClass() == null) {
return ObjectInputFilter.Status.UNDECIDED;
}
if (filterInfo.serialClass().equals(Configured.class)) {
return ObjectInputFilter.Status.ALLOWED;
}
return ObjectInputFilter.Status.REJECTED;
};
ObjectInputFilter.Config.setSerialFilter(myFilter);
SerializationConfig sc = SerializationConfig.builder()
.onWrongConfig(SerializationConfig.Action.FAIL)
.onNoConfig(SerializationConfig.Action.IGNORE)
.ignoreFiles(false)
.traceSerialization(SerializationConfig.TraceOption.NONE)
.build();

// should not fail, as we have a blacklist for all other
sc.configure();
}

@Test
void testConfigured() throws IOException, ClassNotFoundException {
Configured object = new Configured(TEST_STRING);

ByteArrayOutputStream ob = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ob);

oos.writeObject(object);
oos.close();

byte[] bytes = ob.toByteArray();

ByteArrayInputStream ib = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(ib);
Object o = ois.readObject();

assertThat(o, CoreMatchers.instanceOf(Configured.class));

object = (Configured) o;

assertThat(object.text(), is(TEST_STRING));
}

@Test
void testNotConfigured() throws IOException, ClassNotFoundException {
NotConfigured object = new NotConfigured(TEST_STRING);

ByteArrayOutputStream ob = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ob);

oos.writeObject(object);
oos.close();

byte[] bytes = ob.toByteArray();

ByteArrayInputStream ib = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(ib);

// should be excluded
assertThrows(InvalidClassException.class, ois::readObject);
}
}
52 changes: 52 additions & 0 deletions tests/integration/jep290/check_f_f_w/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019, 2021 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.helidon.tests.integration.jep290</groupId>
<artifactId>helidon-tests-integration-jep290-project</artifactId>
<version>2.4.0-SNAPSHOT</version>
</parent>

<artifactId>helidon-tests-integration-jep290-check_f_f_w</artifactId>
<name>Helidon Tests Integration JEP-290 check_f_f_w</name>

<description>
Explicit pattern configured using java, fail on wrong config and pattern is wrong.
</description>

<dependencies>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit c60d517

Please sign in to comment.