[weaver-pojo-attributes-ext] Test and some javadocs

This commit is contained in:
2025-10-26 21:24:31 +01:00
parent 406a433db9
commit eb728df704
3 changed files with 101 additions and 0 deletions

View File

@@ -2,6 +2,10 @@ package de.siphalor.tweed5.weaver.pojoext.attributes.api;
import java.lang.annotation.*;
/**
* Defines an attribute on a config entry.
* The attribute will not be inherited by subentries.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.TYPE_USE})
@Repeatable(Attributes.class)

View File

@@ -2,6 +2,10 @@ package de.siphalor.tweed5.weaver.pojoext.attributes.api;
import java.lang.annotation.*;
/**
* Defines a default value for an attribute.
* Default values will be inherited by subentries, but can be overridden individually using {@link Attribute}.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.TYPE_USE})
@Repeatable(AttributeDefaults.class)

View File

@@ -0,0 +1,93 @@
package de.siphalor.tweed5.weaver.pojoext.attributes.api;
import de.siphalor.tweed5.attributesextension.api.AttributesExtension;
import de.siphalor.tweed5.core.api.container.ConfigContainer;
import de.siphalor.tweed5.core.api.entry.CompoundConfigEntry;
import de.siphalor.tweed5.core.api.entry.ConfigEntry;
import de.siphalor.tweed5.weaver.pojo.api.annotation.CompoundWeaving;
import de.siphalor.tweed5.weaver.pojo.api.annotation.DefaultWeavingExtensions;
import de.siphalor.tweed5.weaver.pojo.api.annotation.PojoWeaving;
import de.siphalor.tweed5.weaver.pojo.api.annotation.PojoWeavingExtension;
import de.siphalor.tweed5.weaver.pojo.impl.weaving.TweedPojoWeaverBootstrapper;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.type;
class AttributesPojoWeavingProcessorTest {
@Test
void test() {
ConfigContainer<Config> configContainer = TweedPojoWeaverBootstrapper.create(Config.class).weave();
configContainer.initialize();
AttributesExtension attributesExtension = configContainer.extension(AttributesExtension.class).orElseThrow();
assertThat(configContainer.rootEntry()).asInstanceOf(type(CompoundConfigEntry.class)).satisfies(
compoundEntry -> assertThat(compoundEntry.subEntries().get("string"))
.asInstanceOf(type(ConfigEntry.class)).satisfies(
entry -> assertThat(attributesExtension.getAttributeValues(entry, "scope"))
.isEqualTo(List.of("game"))
),
compoundEntry -> assertThat(compoundEntry.subEntries().get("sub1"))
.asInstanceOf(type(CompoundConfigEntry.class)).satisfies(
subEntry -> assertThat(attributesExtension.getAttributeValues(subEntry, "color"))
.isEqualTo(List.of("green")),
subEntry -> assertThat(subEntry.subEntries().get("a"))
.asInstanceOf(type(ConfigEntry.class)).satisfies(
entry -> assertThat(attributesExtension.getAttributeValues(entry, "color"))
.isEqualTo(List.of("red"))
),
subEntry -> assertThat(subEntry.subEntries().get("b"))
.asInstanceOf(type(ConfigEntry.class)).satisfies(
entry -> assertThat(attributesExtension.getAttributeValues(entry, "color"))
.isNullOrEmpty()
)
),
compoundEntry -> assertThat(compoundEntry.subEntries().get("sub2"))
.asInstanceOf(type(CompoundConfigEntry.class)).satisfies(
subEntry -> assertThat(attributesExtension.getAttributeValues(subEntry, "color"))
.isEqualTo(List.of("green")),
subEntry -> assertThat(attributesExtension.getAttributeValues(subEntry, "scope"))
.isEqualTo(List.of("world")),
subEntry -> assertThat(subEntry.subEntries().get("a"))
.asInstanceOf(type(ConfigEntry.class)).satisfies(
entry -> assertThat(attributesExtension.getAttributeValues(entry, "color"))
.isEqualTo(List.of("red")),
entry -> assertThat(attributesExtension.getAttributeValues(entry, "scope"))
.isEqualTo(List.of("game"))
),
subEntry -> assertThat(subEntry.subEntries().get("b"))
.asInstanceOf(type(ConfigEntry.class)).satisfies(
entry -> assertThat(attributesExtension.getAttributeValues(entry, "color"))
.isEqualTo(List.of("green")),
entry -> assertThat(attributesExtension.getAttributeValues(entry, "scope"))
.isEqualTo(List.of("game"))
)
)
);
}
@PojoWeaving(extensions = AttributesExtension.class)
@DefaultWeavingExtensions
@PojoWeavingExtension(AttributesPojoWeavingProcessor.class)
@CompoundWeaving
public static class Config {
@Attribute(key = "scope", values = "game")
public String string;
@Attribute(key = "color", values = "green")
public SubConfig sub1;
@AttributeDefault(key = "color", defaultValue = "green")
@AttributeDefault(key = "scope", defaultValue = "game")
@Attribute(key = "scope", values = "world")
public SubConfig sub2;
}
@CompoundWeaving
public static class SubConfig {
@Attribute(key = "color", values = "red")
public String a;
public String b;
}
}