Skip to content

Commit

Permalink
Ensure Test Resources server is stopped, and cleanup properties files…
Browse files Browse the repository at this point in the history
… even if it died
  • Loading branch information
alvarosanchez committed Oct 25, 2024
1 parent fc91a28 commit 5e54777
Show file tree
Hide file tree
Showing 23 changed files with 569 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ try (ServerSocket s = new ServerSocket(port as int)) {
assert s != null
} catch (IOException e) {
assert false
}
}

assert !new File(basedir, ".micronaut/test-resources/test-resources.properties").exists()
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ File log = new File(basedir, 'build.log')
assert log.exists()
assert log.text.contains("BUILD SUCCESS")
assert !log.text.contains("test-resources-client")
assert !log.text.contains("Test Resources is configured in shared mode")
assert !log.text.contains("Test Resources is configured in shared mode")

assert !new File(basedir, ".micronaut/test-resources/test-resources.properties").exists()
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ try (ServerSocket s = new ServerSocket(port as int)) {
} catch (IOException e) {
assert false
}

assert !new File(basedir, ".micronaut/test-resources/test-resources.properties").exists()
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ try (ServerSocket s = new ServerSocket(port as int)) {
} catch (IOException e) {
assert false
}

assert !new File(basedir, ".micronaut/test-resources/test-resources.properties").exists()
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ try (ServerSocket s = new ServerSocket(port as int)) {
} catch (IOException e) {
assert false
}

assert !new File(basedir, ".micronaut/test-resources/test-resources.properties").exists()
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ try (ServerSocket s = new ServerSocket(port as int)) {
} catch (IOException e) {
assert false
}

assert !new File(basedir, ".micronaut/test-resources/test-resources.properties").exists()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = -ntp -e mn:stop-testresources-service
invoker.os.family = !windows
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.micronaut.build.examples</groupId>
<artifactId>test-resources-start-kill-stop</artifactId>
<version>0.1</version>
<packaging>${packaging}</packaging>
<parent>
<groupId>io.micronaut.platform</groupId>
<artifactId>micronaut-parent</artifactId>
<version>@it.micronaut.version@</version>
</parent>
<properties>
<packaging>jar</packaging>
<micronaut.version>@it.micronaut.version@</micronaut.version>
<micronaut.runtime>netty</micronaut.runtime>
<micronaut-maven-plugin.version>@project.version@</micronaut-maven-plugin.version>
<exec.mainClass>io.micronaut.build.examples.Application</exec.mainClass>
<micronaut.test.resources.version>@micronaut.test.resources.version@</micronaut.test.resources.version>
<micronaut.test.resources.enabled>true</micronaut.test.resources.enabled>
<maven.native.plugin.version>@native-maven-plugin.version@</maven.native.plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.validation</groupId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-jackson-databind</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.sql</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-jdbc</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.testresources</groupId>
<artifactId>micronaut-test-resources-client</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.micronaut.maven</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
<annotationProcessorPaths combine.self="append">
<path>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-processor</artifactId>
<version>${micronaut.data.version}</version>
<exclusions>
<exclusion>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
</exclusion>
</exclusions>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-validation</artifactId>
<version>${micronaut.core.version}</version>
</path>
<path>
<groupId>io.micronaut.validation</groupId>
<artifactId>micronaut-validation-processor</artifactId>
<version>${micronaut.validation.version}</version>
<exclusions>
<exclusion>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
</exclusion>
</exclusions>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amicronaut.processing.group=io.micronaut.build.examples</arg>
<arg>-Amicronaut.processing.module=test-resources-start-kill-stop</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>setup</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
<repository>
<id>sonatype-snapshots</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
File mvnw = new File(basedir, '../../../mvnw')
assert mvnw.exists()

String localRepo = (basedir as File).toPath().resolve("../../../target/local-repo").toFile().absolutePath

def processBuilder = new ProcessBuilder(mvnw.absolutePath, "-ntp", "-q", "-Dmaven.repo.local=${localRepo}", "-Psetup", "mn:start-testresources-service")
.directory(basedir as File)
.inheritIO()

Process p = processBuilder.start()
p.waitFor()

assert p.exitValue() == 0

def testResourcesProcess = ProcessHandle.allProcesses()
.filter(process -> process.info().commandLine().map(c -> c.contains("TestResourcesService")).orElse(false))
.findFirst();

if (testResourcesProcess.isPresent()) {
testResourcesProcess.get().destroyForcibly();
} else {
assert false : "Test Resources Service process not found"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.micronaut.build.examples;

import io.micronaut.context.ApplicationContextBuilder;
import io.micronaut.context.ApplicationContextConfigurer;
import io.micronaut.context.annotation.ContextConfigurer;
import io.micronaut.context.event.StartupEvent;
import io.micronaut.runtime.Micronaut;
import io.micronaut.runtime.event.annotation.EventListener;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Singleton
public class Application {

@Inject
private UserRepository userRepository;

@Inject
private BookRepository bookRepository;

@ContextConfigurer
public static class Configurer implements ApplicationContextConfigurer {
@Override
public void configure(ApplicationContextBuilder builder) {
builder.deduceEnvironment(false);
builder.banner(true);
}
}

public static void main(String[] args) {
Micronaut.run(Application.class, args);
}

@EventListener
public void onStart(StartupEvent event) {
if (userRepository.count() == 0) {
User admin = new User();
admin.setName("Administrator");
userRepository.save(admin);
}
if (bookRepository.count() == 0) {
Book book = new Book();
book.setTitle("The Hitchhiker's Guide to the Galaxy");
bookRepository.save(book);
}
if (System.getProperty("interrupt.on.startup") != null) {
System.exit(0);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2003-2021 the original author or authors.
*
* Licensed 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
*
* https://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 io.micronaut.build.examples;

import io.micronaut.data.annotation.GeneratedValue;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.MappedEntity;

@MappedEntity
public class Book {
@Id
@GeneratedValue(GeneratedValue.Type.AUTO)
private Long id;

private String title;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Loading

0 comments on commit 5e54777

Please sign in to comment.