Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wavesoftware/sampler

A typesafe engine for your project examples
https://github.com/wavesoftware/sampler

developer-tools samples testing typesafe

Last synced: about 13 hours ago
JSON representation

A typesafe engine for your project examples

Awesome Lists containing this project

README

        

# Sampler for Java

A typesafe engine for your project examples

## Installation

```xml

pl.wavesoftware.sampler
sampler-spring
1.1.0
test

```

## Usage

### Basic usage

You can create a sample classes by implementing `Sampler` interface.

```java
@Sample
public final class Jsr305Artifact implements Sampler {
private final SamplerContext context;

Jsr305Artifact(SamplerContext context) {
this.context = context;
}

@Override
public Artifact create() {
return new MavenlikeArtifact(
context,
"jsr305",
"com.google.code.findbugs",
new Semver("3.0.2")
);
}
}
```

The `SampleContext` can be used to get instances of other samples, to achieve consistency across complex sample graphs.

```java
@Sample
public final class SimpleProject implements Sampler {

private final SamplerContext context;

SimpleProject(SamplerContext context) {
this.context = context;
}

@Override
public Project create() {
Path root = context.get(ProjectRoot.class);
return new AbstractProject(root, "simple") {
@Override
public Set dependencies() {
return HashSet
.of(HibernateArtifact.class, Jsr305Artifact.class)
.map(context::get);
}
};
}
}
```

### Vanilla Java

To use Sampler in vanilla Java, you need to create a `SamplerContext` instance,
and register all samples in it.

```java
class SampleTests {
@Test
void samples() {
var samplers = HashMap.of(JohnDoe.class, (Sampler) () -> User.builder()
.id(43L)
.name("John Doe")
.build()
);
try (var ctx = new StaticSamplerContext(samplers)) {
User user = ctx.get(JohnDoe.class);
assertThat(user).isNotNull();
}
}
}
```

### Spring integration

To use Sampler in Spring, you need to autowire a `SamplerContext` instance. If
you are using the Spring Boot, the `SamplerContext` is already available as a
bean. You need to register all samples using `@Sample` annotation.

```java
@Configuration
class Samples {
@Bean
Sampler johnDoe() {
return () -> User.builder()
.id(43L)
.name("John Doe")
.build();
}
}
```

Using it is also simple

```java
@SpringBootTest
class SampleTests {
@Autowired
private SamplerContext ctx;

@Test
void samples() {
User user = ctx.get(JohnDoe.class);
assertThat(user).isNotNull();
}
}
```

### Random samples

If you like to randomize your samples, you can use user the
`SamplerContext#controller()` method to get a `SamplerController` instance.
Using sample controller you can fetch a `Random` instance, and use it to create
random samples.

If you happen to encounter a situation where you need to re-create a given
randomized execution, like a failure on CI system, you can use the
`sampler.seed` system property, or `SAMPLER_SEED` environment variable to
recreate same execution. The seed used by `DefaultRandomSource` is printed on
console during the execution.

```java
@Sample
@RequiredArgsConstructor
class JohnDoe implements Sampler {
private final SamplerContext ctx;

@Override
public User create() {
return User.builder()
.id(ctx.controller().random().nextLong())
.name("John Doe")
.build();
}
}
```

## Contributing

Contributions are welcome! To contribute, file a PR.

Even if you can't contribute code, if you have an idea for an improvement
please open an [issue](https://github.com/wavesoftware/sampler/issues).

## Requirements

* Java 11+ (Tested on JDK 11, and 17)
* Spring module requires any modern Spring, tested against latest 6.x, but
should work in Spring 3+ as well.

## Releases

* `1.1.0` - codename: *FriarySparkle*
* Migrate to Spring Boot 3.x

* `1.0.0` - codename: *BananaBow*
* First publicly available release