-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test project for GraalVM support
See freemarker-test-native/README.md
- Loading branch information
Showing
9 changed files
with
326 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
freemarker-test-graalvm-native/src/main/config/reflect-config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] } | ||
] | ||
} | ||
] |
44 changes: 44 additions & 0 deletions
44
freemarker-test-graalvm-native/src/main/java/org/freemarker/core/graal/HelloDataModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
freemarker-test-graalvm-native/src/main/java/org/freemarker/core/graal/HelloFreeMarker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
freemarker-test-graalvm-native/src/main/java/org/freemarker/core/graal/HelloHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = Configuration.getVersion(); // 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 ) ); | ||
} | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
freemarker-test-graalvm-native/src/main/resources/freemarker-templates/hello-world.ftl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<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> |
42 changes: 42 additions & 0 deletions
42
...rker-test-graalvm-native/src/test/java/org/freemarker/core/graal/HelloFreeMarkerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,5 @@ dependencyResolutionManagement { | |
} | ||
} | ||
} | ||
|
||
include("freemarker-test-graalvm-native") |