S++Core systems, human-reviewable
SDK DOCUMENTATION ยท 1.0-SNAPSHOTStart the tutorial

TransactionCompositionTest.java

sdk-tools/samples/custom-services/src/test/java/example/technical/TransactionCompositionTest.java

1package example.technical;23import com.servpp.generator.GeneratorUtil;4import com.servpp.sdk.JavaProjectWriter;5import java.io.File;6import java.nio.file.Files;7import java.nio.file.Path;8import java.util.ArrayList;9import java.util.List;10import javax.tools.ToolProvider;11import org.junit.Rule;12import org.junit.Test;13import org.junit.rules.TemporaryFolder;14import org.junit.runner.RunWith;15import org.junit.runners.Parameterized;16import static org.junit.Assert.assertEquals;1718@RunWith(Parameterized.class)19public class TransactionCompositionTest {20    @Parameterized.Parameters(name = "{0}")21    public static List<String> variants() {22        return List.of("flat", "interleaved", "nested", "serial");23    }2425    @Rule public TemporaryFolder temporary = new TemporaryFolder();2627    private final String variant;2829    public TransactionCompositionTest(String variant) {30        this.variant = variant;31    }3233    @Test34    public void generatedScenarioCommitsAndRollsBackOnH2() throws Exception {35        Path fixtures = Path.of("src/test/composition");36        Path project = temporary.newFolder().toPath();37        Path sources = project.resolve("src/main/java");38        Path openApi = project.resolve("openapi");39        String previousEnvironment = System.getProperty("spp.environment");40        System.setProperty("spp.environment", "testing");41        try {42            GeneratorUtil.execute(fixtures.resolve(variant).toFile(), openApi.toFile(), sources.toFile(), "com.example.cv1");43            new JavaProjectWriter().write(project, "com.example.cv1");44        } finally {45            if (previousEnvironment == null) System.clearProperty("spp.environment");46            else System.setProperty("spp.environment", previousEnvironment);47        }4849        Path classes = Files.createDirectory(project.resolve("classes"));50        String testClasspath = System.getProperty("surefire.test.class.path");51        var options = new ArrayList<>(List.of("-classpath", testClasspath,52                "-d", classes.toString(), fixtures.resolve("ComposeTest.java").toString()));53        try (var files = Files.walk(sources)) {54            files.filter(path -> path.toString().endsWith(".java")).map(Path::toString).sorted().forEach(options::add);55        }56        assertEquals("Generated Java compilation for " + variant, 0, ToolProvider.getSystemJavaCompiler()57                .run(null, null, null, options.toArray(String[]::new)));58        // Each generated application owns the APP catalogs and the runtime singletons.59        String classpath = String.join(File.pathSeparator, classes.toString(),60                project.resolve("src/main/resources").toString(), testClasspath);61        var process = new ProcessBuilder(Path.of(System.getProperty("java.home"), "bin", "java").toString(),62                "-classpath", classpath, "com.example.cv1.ComposeTest", variant).redirectErrorStream(true).start();63        String output = new String(process.getInputStream().readAllBytes(), java.nio.charset.StandardCharsets.UTF_8);64        assertEquals("Generated H2 application for " + variant + "\n" + output, 0, process.waitFor());65    }66}