Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@
<packaging>jar</packaging>
<name>MapStruct Tools Gem API</name>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -71,6 +84,10 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
Expand Down
11 changes: 11 additions & 0 deletions api/src/main/java/org/mapstruct/tools/gem/GemValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.AnnotationValueVisitor;
Expand Down Expand Up @@ -113,6 +114,16 @@ public T getValue() {
return value;
}

/**
* Returns the value set by the user, if present, otherwise invoke other and return the result of that invocation.
*
* @param other a Supplier whose result is returned if no value is present
* @return the value set by the user, if present, otherwise the result of {@code other.get()}
*/
public T getValueOrElseGet(Supplier<T> other) {
return value != null ? value : other.get();
}

/**
* The default value, as declared in the annotation
*
Expand Down
195 changes: 195 additions & 0 deletions api/src/test/java/org/mapstruct/tools/gem/GemValueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.tools.gem;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.AnnotationValueVisitor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class GemValueTest {

static class SimpleAnnotationValue implements AnnotationValue {

private final int value;

SimpleAnnotationValue(int value) {
this.value = value;
}

@Override
public Object getValue() {
return value;
}

@Override
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
return v.visitInt( value, p );
}
}

@Nested
class CreateSimpleValueTest {

@Test
void createSimpleValue() {
SimpleAnnotationValue annotationValue = new SimpleAnnotationValue(1);
GemValue<Integer> gemValue = GemValue.create( annotationValue, new SimpleAnnotationValue(2),
Integer.class );
assertThat( gemValue ).isNotNull();
assertThat( gemValue.isValid() ).isTrue();
assertThat( gemValue.hasValue() ).isTrue();
assertThat( gemValue.getValue() ).isEqualTo( 1 );
assertThat( gemValue.getDefaultValue() ).isEqualTo( 2 );
assertThat( gemValue.get() ).as( "get should return value" ).isEqualTo( 1 );
assertThat( gemValue.getAnnotationValue() ).isEqualTo( annotationValue );
assertThat( gemValue.getValueOrElseGet( () -> 3 ) )
.as( "getValueOrElseGet should return value" ).isEqualTo( 1 );
}

@Test
void createSimpleValueWithoutAnnotationValue() {
GemValue<Integer> gemValue = GemValue.create( null, new SimpleAnnotationValue(2),
Integer.class );
assertThat( gemValue ).isNotNull();
assertThat( gemValue.isValid() ).isTrue();
assertThat( gemValue.hasValue() ).isFalse();
assertThat( gemValue.getValue() ).isNull();
assertThat( gemValue.getDefaultValue() ).isEqualTo( 2 );
assertThat( gemValue.get() ).as( "get should return defaultValue" ).isEqualTo( 2 );
assertThat( gemValue.getAnnotationValue() ).isNull();
assertThat( gemValue.getValueOrElseGet( () -> 3 ) )
.as( "getValueOrElseGet should return other" ).isEqualTo( 3 );
}

@Test
void createSimpleValueWithoutAnnotationDefaultValue() {
SimpleAnnotationValue annotationValue = new SimpleAnnotationValue(1);
GemValue<Integer> gemValue = GemValue.create( annotationValue, null, Integer.class );
assertThat( gemValue ).isNotNull();
assertThat( gemValue.isValid() ).isTrue();
assertThat( gemValue.hasValue() ).isTrue();
assertThat( gemValue.getValue() ).isEqualTo( 1 );
assertThat( gemValue.getDefaultValue() ).isNull();
assertThat( gemValue.get() ).as( "get should return value" ).isEqualTo( 1 );
assertThat( gemValue.getAnnotationValue() ).isEqualTo( annotationValue );
assertThat( gemValue.getValueOrElseGet( () -> 3 ) )
.as( "getValueOrElseGet should return value" ).isEqualTo( 1 );
}

@Test
void createSimpleValueInvalid() {
GemValue<Integer> gemValue = GemValue.create( null, null, Integer.class );
assertThat( gemValue ).isNotNull();
assertThat( gemValue.isValid() ).isFalse();
assertThat( gemValue.hasValue() ).isFalse();
assertThat( gemValue.getValue() ).isNull();
assertThat( gemValue.getDefaultValue() ).isNull();
assertThat( gemValue.get() ).as( "get should return null" ).isNull();
assertThat( gemValue.getAnnotationValue() ).isNull();
assertThat( gemValue.getValueOrElseGet( () -> 3 ) )
.as( "getValueOrElseGet should return other" ).isEqualTo( 3 );
}
}

static class SimpleArrayAnnotationValue implements AnnotationValue {

private final List<SimpleAnnotationValue> value;

SimpleArrayAnnotationValue(int... intValues) {
this.value = new ArrayList<>( intValues.length );
for ( int v : intValues ) {
value.add( new SimpleAnnotationValue( v ) );
}
}

@Override
public Object getValue() {
return value;
}

@Override
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
return v.visitArray( value, p );
}
}

@Nested
class CreateArrayTest {

@Test
void createArrayValue() {
SimpleArrayAnnotationValue annotationValue = new SimpleArrayAnnotationValue(1);
GemValue<List<Integer>> gemValue = GemValue.createArray( annotationValue,
new SimpleArrayAnnotationValue(2), Integer.class );
assertThat( gemValue ).isNotNull();
assertThat( gemValue.isValid() ).isTrue();
assertThat( gemValue.hasValue() ).isTrue();
assertThat( gemValue.getValue() ).isEqualTo( Collections.singletonList( 1 ) );
assertThat( gemValue.getDefaultValue() ).isEqualTo( Collections.singletonList( 2 ) );
assertThat( gemValue.get() ).as( "get should return value" ).isEqualTo(
Collections.singletonList( 1 ) );
assertThat( gemValue.getAnnotationValue() ).isEqualTo( annotationValue );
assertThat( gemValue.getValueOrElseGet( Collections::emptyList ) )
.as( "getValueOrElseGet should return value" )
.isEqualTo( Collections.singletonList( 1 ) );
}

@Test
void createArrayValueWithoutAnnotationValue() {
GemValue<List<Integer>> gemValue = GemValue.createArray( null,
new SimpleArrayAnnotationValue(2), Integer.class );
assertThat( gemValue ).isNotNull();
assertThat( gemValue.isValid() ).isTrue();
assertThat( gemValue.hasValue() ).isFalse();
assertThat( gemValue.getValue() ).isNull();
assertThat( gemValue.getDefaultValue() ).isEqualTo( Collections.singletonList( 2 ) );
assertThat( gemValue.get() ).as( "get should return defaultValue" )
.isEqualTo( Collections.singletonList( 2 ) );
assertThat( gemValue.getAnnotationValue() ).isNull();
assertThat( gemValue.getValueOrElseGet( Collections::emptyList ) )
.as( "getValueOrElseGet should return other" ).isEqualTo( Collections.emptyList() );
}

@Test
void createArrayValueWithoutAnnotationDefaultValue() {
SimpleArrayAnnotationValue annotationValue = new SimpleArrayAnnotationValue(1);
GemValue<List<Integer>> gemValue = GemValue.createArray( annotationValue, null, Integer.class );
assertThat( gemValue ).isNotNull();
assertThat( gemValue.isValid() ).isTrue();
assertThat( gemValue.hasValue() ).isTrue();
assertThat( gemValue.getValue() ).isEqualTo( Collections.singletonList( 1 ) );
assertThat( gemValue.getDefaultValue() ).isNull();
assertThat( gemValue.get() ).as( "get should return value" )
.isEqualTo( Collections.singletonList( 1 ) );
assertThat( gemValue.getAnnotationValue() ).isEqualTo( annotationValue );
assertThat( gemValue.getValueOrElseGet( Collections::emptyList ) )
.as( "getValueOrElseGet should return value" )
.isEqualTo( Collections.singletonList( 1 ) );
}

@Test
void createArrayValueInvalid() {
GemValue<List<Integer>> gemValue = GemValue.createArray( null, null, Integer.class );
assertThat( gemValue ).isNotNull();
assertThat( gemValue.isValid() ).isFalse();
assertThat( gemValue.hasValue() ).isFalse();
assertThat( gemValue.getValue() ).isNull();
assertThat( gemValue.getDefaultValue() ).isNull();
assertThat( gemValue.get() ).as( "get should return null" ).isNull();
assertThat( gemValue.getAnnotationValue() ).isNull();
assertThat( gemValue.getValueOrElseGet( () -> Collections.singletonList( 3 ) ) )
.as( "getValueOrElseGet should return other" ).isEqualTo( Collections.singletonList( 3 ) );
}
}
}