Skip to content

Commit

Permalink
Add test project for GraalVM support
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Jan 17, 2025
1 parent 4489899 commit 8083405
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 0 deletions.
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 = 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 ) );
}
}

}
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>
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 );
}
}

}
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ dependencyResolutionManagement {
}
}
}

include("freemarker-test-graalvm-native")

0 comments on commit 8083405

Please sign in to comment.