#!/bin/sh
# autopkgtest check: Run the example of the website, also in demo/
# (C) 2012-present the original author or authors.
#     2021 Pierre Gruet.
# Author: Pierre Gruet <pgt@debian.org>

set -e

WORKDIR=$(mktemp -d)
trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM
cd $WORKDIR

# Creating the interface marking an extension point.
cat << EOF >Greeting.java
import org.pf4j.ExtensionPoint;

public interface Greeting extends ExtensionPoint {

    String getGreeting();

}
EOF

# Creating the extension class.
cat <<EOF >WelcomeGreeting.java
import org.pf4j.Extension;
import org.pf4j.ExtensionPoint;

@Extension
public class WelcomeGreeting implements Greeting {

    @Override
    public String getGreeting() {
        return "Welcome";
    }

}
EOF

# Creating a class extending Plugin to manage startup or stopping phases.
cat <<EOF >WelcomePlugin.java
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;

public class WelcomePlugin extends Plugin {

    public WelcomePlugin(PluginWrapper wrapper) {
        super(wrapper);

        // you can use "wrapper" to have access to the plugin context (plugin manager, descriptor, ...)
    }

    @Override
    public void start() {
        System.out.println("WelcomePlugin.start()");
    }

    @Override
    public void stop() {
        System.out.println("WelcomePlugin.stop()");
    }
    
    @Override
    public void delete() {
        System.out.println("WelcomePlugin.delete()");
    }
}
EOF

# Creating the manifest, indicating the plugin class, the id and the version
# number.
mkdir META-INF/

cat <<EOF >META-INF/MANIFEST.MF
Manifest-Version: 1.0
Plugin-Class: WelcomePlugin
Plugin-Id: welcome-plugin
Plugin-Version: 0.0.1
EOF

# Compiling classes, then adding extensions.idx (created by pf4j) into
# META-INF/, then building the plugin jar and cleaning up.
javac -cp /usr/share/java/pf4j.jar Greeting.java WelcomeGreeting.java WelcomePlugin.java
mv extensions.idx META-INF/
zip -q -r plugin.jar META-INF/ Greeting.class WelcomeGreeting.class WelcomePlugin.class
rm -rf *.java *.class META-INF/

# Class with the test program.
cat <<EOF >Test.java
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.pf4j.JarPluginManager;
import org.pf4j.PluginManager;

public class Test {

    public static void main(String[] args) {
        // create the plugin manager
        Path[] pluginsDir = new Path[1];
        pluginsDir[0] = Paths.get(".");
        PluginManager pluginManager = new JarPluginManager(pluginsDir);

        // start and load all plugins of application
        pluginManager.loadPlugins();
        pluginManager.startPlugins();

        // retrieve all extensions for "Greeting" extension point
        List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
        for (Greeting greeting : greetings) {
            System.out.println(">>> " + greeting.getGreeting());
        }

        // stop and unload all plugins
        pluginManager.stopPlugins();
        pluginManager.unloadPlugins();
    }
}
EOF

# Compiling this class, before running it.
javac -cp /usr/share/java/pf4j.jar:plugin.jar Test.java

# See http://www.slf4j.org/codes.html#StaticLoggerBinder to understand why
# slf4j-simple.jar is included in this classpath.
java -cp /usr/share/java/pf4j.jar:/usr/share/java/slf4j-simple.jar:plugin.jar:. Test | grep -q ">>> Welcome"

if [ $? -ne 0 ]; then
  exit 1
fi
