Skip to content

Commit

Permalink
Add test project for GraalVM native support + CI updated
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Jan 22, 2025
1 parent 24d1e6e commit c545b0a
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ jobs:
- name: Run Build
id: build_step
run: './gradlew "-Pfreemarker.signMethod=none" "-Pfreemarker.allowUnsignedReleaseBuild=true" --continue clean build'
- name: Set up GraalVM 21
uses: actions/setup-java@v4
with:
java-version: 21
distribution: graalvm
# test pipeline to check native support
#
# - GraalVM is added to the runner
# - A simple native project is build and run
#
# At the something like that should be found in the log :
#
# INFO: name : FreeMarker Native Demo, version : 2.3.35-nightly
# Jan 15, 2025 4:28:19 PM freemarker.log._JULLoggerFactory$JULLogger info
# INFO: result :
# <html>
# <head>
# <title>Hello : FreeMarker GraalVM Native Demo</title>
# </head>
# <body>
# <h1>Hello : FreeMarker GraalVM Native Demo</h1>
# <p>Test template for Apache FreeMarker GraalVM native support (2.3.35-nightly)</p>
# </body>
# </html>
- name: Test GraalVM native support (build and run)
id: native_test
run: './gradlew :freemarker-test-graalvm-native:nativeCompile;./freemarker-test-graalvm-native/build/native/nativeCompile/freemarker-test-graalvm-native'
- name: Upload Failed Report
uses: actions/upload-artifact@v4
if: failure() && steps.build_step.outcome == 'failure'
Expand Down
48 changes: 48 additions & 0 deletions freemarker-test-graalvm-native/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# freemarker-test-graalvm-natice

Test project for GraalVM support

Requirements :

- GraalVM 21+

## Quickstart

1. Build the main Apache FreeMarker proejct :

```shell
./gradlew "-Pfreemarker.signMethod=none" "-Pfreemarker.allowUnsignedReleaseBuild=true" clean build
```

2. Build the test module native image with GraalVM :

```shell
./gradlew :freemarker-test-graalvm-native:nativeCompile
```

3. Run the project :

```shell
./freemarker-test-graalvm-native/build/native/nativeCompile/freemarker-test-graalvm-native
```

Output should be similar to :

```txt
INFO: name : FreeMarker Native Demo, version : 2.3.35-nightly
Jan 15, 2025 4:28:19 PM freemarker.log._JULLoggerFactory$JULLogger info
INFO: result :
<html>
<head>
<title>Hello : FreeMarker GraalVM Native Demo</title>
</head>
<body>
<h1>Hello : FreeMarker GraalVM Native Demo</h1>
<p>Test template for Apache FreeMarker GraalVM native support (2.3.35-nightly)</p>
</body>
</html>
```

## CI (GitHub workflow)

GraalVM native test for this module is included in the GitHub [CI](../.github/workflows/ci.yml) worflow.
71 changes: 71 additions & 0 deletions freemarker-test-graalvm-native/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask
import java.util.Arrays

/*
* 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
*
* 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.
*/

plugins {
java
application
id("org.graalvm.buildtools.native") version "0.10.3"
}

group = "org.freemarker"
version = rootProject.version

val graalSdkVersion = "24.1.1"
val junitJupiterVersion = "5.11.4"

val profile = findProperty("profile") as String? ?: "default"

dependencies {
implementation(project(":"))
implementation("org.python:jython:2.5.0")
compileOnly("org.graalvm.sdk:graal-sdk:$graalSdkVersion")
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion")
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
withSourcesJar()
withJavadocJar()
}

application {
mainClass.set("org.freemarker.core.graal.HelloFreeMarker")
}

tasks.test {
useJUnitPlatform()
testLogging {
events("PASSED", "FAILED", "SKIPPED")
}
}

graalvmNative {
binaries.all {
fallback.set(false)
verbose.set(true)
resources.autodetect()
buildArgs.add( "-H:ReflectionConfigurationFiles=$projectDir/src/main/config/reflect-config.json" )
jvmArgs()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"name": "org.freemarker.core.graal.HelloDataModel",
"methods": [
{ "name": "getName", "parameterTypes": [] },
{ "name": "getVersion", "parameterTypes": [] }
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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
*
* 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 org.freemarker.core.graal;

public class HelloDataModel {

private String name;

private String version;

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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
*
* 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 org.freemarker.core.graal;

import freemarker.log.Logger;

public class HelloFreeMarker {

private final static Logger log = Logger.getLogger(HelloFreeMarker.class.getName());

public static void main( String[] args ) throws Exception {
HelloHandler helloHandler = new HelloHandler();
helloHandler.sayHello();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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
*
* 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 org.freemarker.core.graal;

import freemarker.log.Logger;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
* Simple test class, able to say hello.
*/
public class HelloHandler {

private final static Logger log = Logger.getLogger(HelloHandler.class.getName());

public HelloHandler() throws ClassNotFoundException {
// test native configuration
Class.forName("freemarker.ext.jython.JythonModel");
}

/**
* This method will say hello by printing an Apache FreeMarker template
*
* @throws Exception if any unexpected situation occurs
*/
public void sayHello() throws Exception {
try (StringWriter buffer = new StringWriter()) {
Version version = new Version(Configuration.getVersion().toString()); // using latest version
// creates FreeMarker configuration
Configuration cfg = new Configuration( version );
cfg.setClassForTemplateLoading(HelloDataModel.class, "/freemarker-templates/");
// creates data model
HelloDataModel data = new HelloDataModel();
data.setName( "FreeMarker GraalVM Native Demo" );
data.setVersion( version.toString() );
log.info( String.format( "name : %s, version : %s", data.getName(), data.getVersion() ) );
Map<String, Object> input = new HashMap<>();
input.put( "data", data );
// process template
Template template = cfg.getTemplate( "hello-world.ftl" );
template.process( input, buffer );
log.info( String.format( "result :\n%s", buffer ) );
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<#--
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
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.
-->
<html>
<head>
<title>Hello : ${data.name}</title>
</head>
<body>
<h1>Hello : ${data.name}</h1>
<p>Test template for Apache FreeMarker GraalVM native support (${data.version})</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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
*
* 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 org.freemarker.core.graal;

import freemarker.log.Logger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class HelloFreeMarkerTest {

private final static Logger log = Logger.getLogger(HelloFreeMarker.class.getName());

@Test
public void testMain() {
try {
HelloFreeMarker.main(new String[0]);
Assertions.assertTrue( Boolean.TRUE );
} catch (Exception e) {
String message = String.format( "Error : %s", e );
log.error( message , e );
Assertions.fail( message );
}
}

}
4 changes: 4 additions & 0 deletions rat-excludes
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ gradle/**
.directory
.Trash*

# ignore for freemarker-test-graalvm-native module (only used for GraalVM native support compliance)
freemarker-test-graalvm-native/build/**
freemarker-test-graalvm-native/README.md

# Well known files that need no license note:
# -------------------------------------------

Expand Down
Loading

0 comments on commit c545b0a

Please sign in to comment.