Skip to content

Commit 5d73d6c

Browse files
committed
Add test project for GraalVM support
See freemarker-test-native/README.md
1 parent 821ca75 commit 5d73d6c

File tree

10 files changed

+331
-0
lines changed

10 files changed

+331
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# freemarker-test-graalvm-natice
2+
3+
Test project for GraalVM support
4+
5+
Requirements :
6+
7+
- GraalVM 21+
8+
9+
## Quickstart
10+
11+
1. Build the main Apache FreeMarker proejct :
12+
13+
```shell
14+
./gradlew "-Pfreemarker.signMethod=none" "-Pfreemarker.allowUnsignedReleaseBuild=true" clean build
15+
```
16+
17+
2. Build the test module native image with GraalVM :
18+
19+
```shell
20+
./gradlew :freemarker-test-graalvm-native:nativeCompile
21+
```
22+
23+
3. Run the project :
24+
25+
```shell
26+
./freemarker-test-graalvm-native/build/native/nativeCompile/freemarker-test-graalvm-native
27+
```
28+
29+
Output should be similar to :
30+
31+
```txt
32+
INFO: name : FreeMarker Native Demo, version : 2.3.35-nightly
33+
Jan 15, 2025 4:28:19 PM freemarker.log._JULLoggerFactory$JULLogger info
34+
INFO: result :
35+
<html>
36+
<head>
37+
<title>Hello : FreeMarker GraalVM Native Demo</title>
38+
</head>
39+
<body>
40+
<h1>Hello : FreeMarker GraalVM Native Demo</h1>
41+
<p>Test template for Apache FreeMarker GraalVM native support (2.3.35-nightly)</p>
42+
</body>
43+
</html>
44+
```
45+
46+
## CI (GitHub workflow)
47+
48+
GraalVM native test for this module is included in the GitHub [CI](../.github/workflows/ci.yml) worflow.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask
2+
import java.util.Arrays
3+
4+
/*
5+
* Licensed to the Apache Software Foundation (ASF) under one
6+
* or more contributor license agreements. See the NOTICE file
7+
* distributed with this work for additional information
8+
* regarding copyright ownership. The ASF licenses this file
9+
* to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance
11+
* with the License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing,
16+
* software distributed under the License is distributed on an
17+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18+
* KIND, either express or implied. See the License for the
19+
* specific language governing permissions and limitations
20+
* under the License.
21+
*/
22+
23+
plugins {
24+
java
25+
application
26+
id("org.graalvm.buildtools.native") version "0.10.3"
27+
}
28+
29+
group = "org.freemarker"
30+
version = rootProject.version
31+
32+
val graalSdkVersion = "24.1.1"
33+
val junitJupiterVersion = "5.11.4"
34+
35+
val profile = findProperty("profile") as String? ?: "default"
36+
37+
dependencies {
38+
implementation(project(":"))
39+
implementation("org.python:jython:2.5.0")
40+
compileOnly("org.graalvm.sdk:graal-sdk:$graalSdkVersion")
41+
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion")
42+
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion")
43+
}
44+
45+
java {
46+
sourceCompatibility = JavaVersion.VERSION_17
47+
targetCompatibility = JavaVersion.VERSION_17
48+
withSourcesJar()
49+
withJavadocJar()
50+
}
51+
52+
application {
53+
mainClass.set("org.freemarker.core.graal.HelloFreeMarker")
54+
}
55+
56+
tasks.test {
57+
useJUnitPlatform()
58+
testLogging {
59+
events("PASSED", "FAILED", "SKIPPED")
60+
}
61+
}
62+
63+
graalvmNative {
64+
binaries.all {
65+
fallback.set(false)
66+
verbose.set(true)
67+
resources.autodetect()
68+
buildArgs.add( "-H:ReflectionConfigurationFiles=$projectDir/src/main/config/reflect-config.json" )
69+
jvmArgs()
70+
}
71+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"name": "org.freemarker.core.graal.HelloDataModel",
4+
"methods": [
5+
{ "name": "getName", "parameterTypes": [] },
6+
{ "name": "getVersion", "parameterTypes": [] }
7+
]
8+
}
9+
]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.freemarker.core.graal;
21+
22+
public class HelloDataModel {
23+
24+
private String name;
25+
26+
private String version;
27+
28+
public String getVersion() {
29+
return version;
30+
}
31+
32+
public void setVersion(String version) {
33+
this.version = version;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public void setName(String name) {
41+
this.name = name;
42+
}
43+
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.freemarker.core.graal;
21+
22+
import freemarker.log.Logger;
23+
24+
public class HelloFreeMarker {
25+
26+
private final static Logger log = Logger.getLogger(HelloFreeMarker.class.getName());
27+
28+
public static void main( String[] args ) throws Exception {
29+
HelloHandler helloHandler = new HelloHandler();
30+
helloHandler.sayHello();
31+
}
32+
33+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.freemarker.core.graal;
21+
22+
import freemarker.log.Logger;
23+
import freemarker.template.Configuration;
24+
import freemarker.template.Template;
25+
import freemarker.template.Version;
26+
27+
import java.io.StringWriter;
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
/**
32+
* Simple test class, able to say hello.
33+
*/
34+
public class HelloHandler {
35+
36+
private final static Logger log = Logger.getLogger(HelloHandler.class.getName());
37+
38+
public HelloHandler() throws ClassNotFoundException {
39+
// test native configuration
40+
Class.forName("freemarker.ext.jython.JythonModel");
41+
}
42+
43+
/**
44+
* This method will say hello by printing an Apache FreeMarker template
45+
*
46+
* @throws Exception if any unexpected situation occurs
47+
*/
48+
public void sayHello() throws Exception {
49+
try (StringWriter buffer = new StringWriter()) {
50+
Version version = Configuration.getVersion(); // using latest version
51+
// creates FreeMarker configuration
52+
Configuration cfg = new Configuration( version );
53+
cfg.setClassForTemplateLoading(HelloDataModel.class, "/freemarker-templates/");
54+
// creates data model
55+
HelloDataModel data = new HelloDataModel();
56+
data.setName( "FreeMarker GraalVM Native Demo" );
57+
data.setVersion( version.toString() );
58+
log.info( String.format( "name : %s, version : %s", data.getName(), data.getVersion() ) );
59+
Map<String, Object> input = new HashMap<>();
60+
input.put( "data", data );
61+
// process template
62+
Template template = cfg.getTemplate( "hello-world.ftl" );
63+
template.process( input, buffer );
64+
log.info( String.format( "result :\n%s", buffer ) );
65+
}
66+
}
67+
68+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>Hello : ${data.name}</title>
4+
</head>
5+
<body>
6+
<h1>Hello : ${data.name}</h1>
7+
<p>Test template for Apache FreeMarker GraalVM native support (${data.version})</p>
8+
</body>
9+
</html>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.freemarker.core.graal;
21+
22+
import freemarker.log.Logger;
23+
import org.junit.jupiter.api.Assertions;
24+
import org.junit.jupiter.api.Test;
25+
26+
class HelloFreeMarkerTest {
27+
28+
private final static Logger log = Logger.getLogger(HelloFreeMarker.class.getName());
29+
30+
@Test
31+
public void testMain() {
32+
try {
33+
HelloFreeMarker.main(new String[0]);
34+
Assertions.assertTrue( Boolean.TRUE );
35+
} catch (Exception e) {
36+
String message = String.format( "Error : %s", e );
37+
log.error( message , e );
38+
Assertions.fail( message );
39+
}
40+
}
41+
42+
}

rat-excludes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ gradle/**
6868
.directory
6969
.Trash*
7070

71+
# ignore for freemarker-test-graalvm-native module (only used for GraalVM native support compliance)
72+
freemarker-test-graalvm-native/build/**
73+
freemarker-test-graalvm-native/README.md
74+
freemarker-test-graalvm-native/src/main/resources/freemarker-templates/**
75+
7176
# Well known files that need no license note:
7277
# -------------------------------------------
7378

settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ dependencyResolutionManagement {
3434
}
3535
}
3636
}
37+
38+
include("freemarker-test-graalvm-native")

0 commit comments

Comments
 (0)