Skip to content

Commit

Permalink
[BEAM-2530] Compile and run tests on java 11 for Precommit portabilit…
Browse files Browse the repository at this point in the history
…y api
  • Loading branch information
pawelpasterz authored and iemejia committed May 8, 2020
1 parent b0a0f9c commit cecc4db
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .test-infra/jenkins/CommonJobProperties.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
class CommonJobProperties {

static String checkoutDir = 'src'
final static String JAVA_8_HOME = '/usr/lib/jvm/java-8-openjdk-amd64'
final static String JAVA_11_HOME = '/usr/lib/jvm/java-11-openjdk-amd64'

// Sets common top-level job properties for main repository jobs.
static void setTopLevelMainJobProperties(def context,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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.
*/

import PrecommitJobBuilder
import CommonJobProperties as properties

PrecommitJobBuilder builder = new PrecommitJobBuilder(
scope: this,
nameBase: 'JavaPortabilityApiJava11',
gradleTask: ':clean', // Do nothing here
gradleSwitches: ['-PdisableSpotlessCheck=true'], // spotless checked in separate pre-commit
triggerPathPatterns: [
'^model/.*$',
'^sdks/java/.*$',
'^runners/google-cloud-dataflow-java/worker.*$',
'^examples/java/.*$',
'^examples/kotlin/.*$',
'^release/.*$',
]
)
builder.build {
publishers {
archiveJunit('**/build/test-results/**/*.xml')
}

steps {
gradle {
rootBuildScriptDir(properties.checkoutDir)
tasks 'javaPreCommitPortabilityApi'
switches '-Pdockerfile=Dockerfile-java11'
switches '-PdisableSpotlessCheck=true'
switches '-PcompileAndRunTestsWithJava11'
switches "-Pjava11Home=${properties.JAVA_11_HOME}"
properties.setGradleSwitches(delegate, 3 * Runtime.runtime.availableProcessors())
}
}
}
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,12 @@ if (project.hasProperty('javaLinkageArtifactIds')) {
}
}
}
if (project.hasProperty('compileAndRunTestsWithJava11')) {
project.javaPreCommitPortabilityApi.dependsOn ':sdks:java:testing:test-utils:verifyJavaVersion'
} else {
allprojects {
tasks.withType(Test) {
exclude '**/JvmVerification.class'
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package org.apache.beam.gradle
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.gradle.api.JavaVersion

import java.util.concurrent.atomic.AtomicInteger
import org.gradle.api.GradleException
import org.gradle.api.Plugin
Expand Down Expand Up @@ -698,6 +700,20 @@ class BeamModulePlugin implements Plugin<Project> {
+ (defaultLintSuppressions + configuration.disableLintWarnings).collect { "-Xlint:-${it}" })
}

if (project.hasProperty("compileAndRunTestsWithJava11")) {
def java11Home = project.findProperty("java11Home")
project.tasks.compileTestJava {
options.fork = true
options.forkOptions.javaHome = java11Home as File
options.compilerArgs += ['-Xlint:-path']
options.compilerArgs.addAll(['--release', '11'])
}
project.tasks.withType(Test) {
useJUnit()
executable = "${java11Home}/bin/java"
}
}

// Configure the default test tasks set of tests executed
// to match the equivalent set that is executed by the maven-surefire-plugin.
// See https://maven.apache.org/components/surefire/maven-surefire-plugin/test-mojo.html
Expand Down
11 changes: 11 additions & 0 deletions sdks/java/testing/test-utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ dependencies {
testCompile library.java.hamcrest_library
testRuntimeOnly project(path: ":runners:direct-java", configuration: "shadowTest")
}

task verifyJavaVersion(type: Test) {
filter {
includeTestsMatching 'org.apache.beam.sdk.testutils.jvmverification.JvmVerification.verifyCodeIsCompiledWithJava8'
includeTestsMatching 'org.apache.beam.sdk.testutils.jvmverification.JvmVerification.verifyTestCodeIsCompiledWithJava11'
includeTestsMatching 'org.apache.beam.sdk.testutils.jvmverification.JvmVerification.verifyRunningJVMVersionIs11'
}
doLast {
println 'Java verified'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.apache.beam.sdk.testutils.jvmverification;

import static org.apache.beam.sdk.testutils.jvmverification.JvmVerification.Java.v11;
import static org.apache.beam.sdk.testutils.jvmverification.JvmVerification.Java.v1_8;
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.beam.repackaged.core.org.apache.commons.compress.utils.IOUtils;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;

public class JvmVerification {

private static final Map<String, Java> versionMapping = new HashMap<>();

static {
versionMapping.put("0034", v1_8);
versionMapping.put("0037", v11);
}

// bytecode
@Test
public void verifyCodeIsCompiledWithJava8() throws IOException {
assertEquals(v1_8, getByteCodeVersion(DoFn.class));
}

@Test
public void verifyTestCodeIsCompiledWithJava8() throws IOException {
assertEquals(v1_8, getByteCodeVersion(JvmVerification.class));
}

@Test
public void verifyTestCodeIsCompiledWithJava11() throws IOException {
assertEquals(v11, getByteCodeVersion(JvmVerification.class));
}

// jvm
@Test
public void verifyRunningJVMVersionIs11() {
final String version = getJavaSpecification();
assertEquals(v11.name, version);
}

private static <T> Java getByteCodeVersion(final Class<T> clazz) throws IOException {
final InputStream stream =
clazz.getClassLoader().getResourceAsStream(clazz.getName().replace(".", "/") + ".class");
final byte[] classBytes = IOUtils.toByteArray(stream);
final String versionInHexString =
Hex.encodeHexString(new byte[] {classBytes[6], classBytes[7]});
return versionMapping.get(versionInHexString);
}

private static String getJavaSpecification() {
return System.getProperty("java.specification.version");
}

enum Java {
v1_8("1.8"),
v11("11");

final String name;

Java(final String name) {
this.name = name;
}
}
}

0 comments on commit cecc4db

Please sign in to comment.