Skip to content

Commit 0eb7381

Browse files
committed
Add miscellaneous test cases
1 parent 268ce78 commit 0eb7381

File tree

9 files changed

+578
-30
lines changed

9 files changed

+578
-30
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ public final class Example {
8080
var configFile = Paths.get("/tmp/config.yml");
8181
var config = new UserConfiguration();
8282

83-
// Save a new instance to the configuration file
83+
// Save an instance to the configuration file
8484
YamlConfigurations.save(configFile, UserConfiguration.class, config);
8585

8686
// Load a new instance from the configuration file
8787
config = YamlConfigurations.load(configFile, UserConfiguration.class);
8888
System.out.println(config.admin.username);
8989
System.out.println(config.blockedUsers);
9090

91-
// Modify and save the configuration file
91+
// Modify the configuration and save it again
9292
config.blockedUsers.add(new User("user3", "pass3"));
9393
YamlConfigurations.save(configFile, UserConfiguration.class, config);
9494
}
@@ -446,8 +446,8 @@ missing. That can happen, for example, when somebody deleted that value from the
446446
when you add configuration elements to your configuration type, or when the `NameFormatter` that
447447
was used to create that file is replaced.
448448

449-
In such cases, fields of configuration types keep the default value you assigned to them and record
450-
components are initialized with the default value of their corresponding type.
449+
In such cases, fields of configuration classes keep the default value you assigned to them and
450+
record components are initialized with the default value of their corresponding type.
451451

452452
#### Null values
453453

@@ -792,7 +792,7 @@ public final class RecursiveTypDefinitions {
792792

793793
</details>
794794

795-
## Project structure
795+
## Project and repository structure
796796

797797
This project contains three classes of modules:
798798

@@ -811,6 +811,13 @@ This project contains three classes of modules:
811811
object which adds support for the serialization of Bukkit classes like `ItemStack` as
812812
described [here](#support-for-bukkit-classes-like-itemstack).
813813

814+
The GitHub repository of this project uses two branches:
815+
816+
* The `master` branch contains the functionality of the latest release version.
817+
* The `dev` branch contains the newest, possibly unstable features and refactorings.
818+
819+
If you plan to contribute to this project, please base your commits on the `dev` branch.
820+
814821
## Import
815822

816823
To use this library, import it into your project with Maven or Gradle. Examples of how to do that
@@ -876,7 +883,9 @@ dependencies { implementation("com.github.Exlll.ConfigLib:configlib-yaml:v4.1.0"
876883
</details>
877884

878885
<details>
879-
<summary>Import via GitHub</summary>
886+
<summary>
887+
Import via GitHub
888+
</summary>
880889

881890
Importing via GitHub requires authentication. Check
882891
this [issue](https://github.com/Exlll/ConfigLib/issues/12) if you have any trouble with that.

configlib-core/src/main/java/de/exlll/configlib/SerializeWith.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
*
7676
* @return the type of serializer to use
7777
*/
78-
Class<? extends Serializer<?, ?>> serializer();
78+
@SuppressWarnings("rawtypes")
79+
Class<? extends Serializer> serializer();
7980

8081
/**
8182
* Returns the nesting level at which to apply the serializer.

configlib-core/src/test/java/de/exlll/configlib/ExampleConfigurationTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,25 @@ void deserializeExampleRecord2() {
254254
ExampleRecord2 deserialize2 = serializer.deserialize(EXAMPLE_RECORD2_2);
255255
assertExampleRecord2Equal(ExampleInitializer.EXAMPLE_RECORD2_2, deserialize2);
256256
}
257+
258+
@Test
259+
void serializeExampleConfigurationCustom() {
260+
ConfigurationSerializer<ExampleConfigurationCustom> serializer =
261+
new ConfigurationSerializer<>(ExampleConfigurationCustom.class, PROPERTIES_ALLOW_NULL);
262+
263+
ExampleConfigurationCustom config = new ExampleConfigurationCustom();
264+
Map<?, ?> serialized = serializer.serialize(config);
265+
assertEquals(EXAMPLE_CONFIGURATION_CUSTOM, serialized);
266+
}
267+
268+
@Test
269+
void deserializeExampleConfigurationCustom() {
270+
ConfigurationSerializer<ExampleConfigurationCustom> serializer =
271+
new ConfigurationSerializer<>(ExampleConfigurationCustom.class, PROPERTIES_ALLOW_NULL);
272+
273+
assertExampleConfigurationsCustomEqual(
274+
serializer.deserialize(EXAMPLE_CONFIGURATION_CUSTOM),
275+
new ExampleConfigurationCustom()
276+
);
277+
}
257278
}

configlib-core/src/test/java/de/exlll/configlib/SerializerSelectorTest.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ static class Z {
539539
}
540540

541541
@Test
542-
void selectCustomSerializerfieldAsElement() {
542+
void selectCustomSerializerForField() {
543543
var serializer = SELECTOR.select(fieldAsElement(Z.class, "string"));
544544
assertThat(serializer, instanceOf(IdentitySerializer.class));
545545
}
@@ -690,6 +690,63 @@ private record SerializerWithContext(SerializerContext ctx)
690690
@Override
691691
public String deserialize(String element) {return null;}
692692
}
693+
694+
@Test
695+
void selectCustomSerializerWithOnUnsupportedTypes() {
696+
record Box<T>(T element) {}
697+
698+
@SuppressWarnings("rawtypes")
699+
final class UnsupportedTypes<T> {
700+
@SerializeWith(serializer = IdentitySerializer.class)
701+
Map<Point, String> mapPointString1;
702+
@SerializeWith(serializer = IdentitySerializer.class)
703+
Map<List<String>, String> mapListStringString;
704+
@SerializeWith(serializer = IdentitySerializer.class)
705+
Box<String> boxString;
706+
@SerializeWith(serializer = IdentitySerializer.class)
707+
List<? extends String> listWildcardExtendsString;
708+
@SerializeWith(serializer = IdentitySerializer.class)
709+
List<?> listWildcard;
710+
@SerializeWith(serializer = IdentitySerializer.class)
711+
List<?>[] arrayListWildcard;
712+
@SerializeWith(serializer = IdentitySerializer.class)
713+
T typeVariable;
714+
@SerializeWith(serializer = IdentitySerializer.class)
715+
List listRaw;
716+
@SerializeWith(serializer = IdentitySerializer.class)
717+
List[] arrayListRaw;
718+
@SerializeWith(serializer = IdentitySerializer.class)
719+
List<String>[] arrayListString;
720+
@SerializeWith(serializer = IdentitySerializer.class)
721+
Set<Integer>[] arraySetInteger;
722+
@SerializeWith(serializer = IdentitySerializer.class)
723+
Map<Byte, Byte>[] arrayMapByteByte;
724+
}
725+
726+
final class Config {
727+
@SerializeWith(serializer = IdentitySerializer.class)
728+
UnsupportedTypes<Point> unsupportedTypes;
729+
}
730+
731+
assertInstanceOfIdentitySerializer(Config.class, "unsupportedTypes");
732+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "mapPointString1");
733+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "mapListStringString");
734+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "boxString");
735+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "listWildcardExtendsString");
736+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "listWildcard");
737+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "arrayListWildcard");
738+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "typeVariable");
739+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "listRaw");
740+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "arrayListRaw");
741+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "arrayListString");
742+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "arraySetInteger");
743+
assertInstanceOfIdentitySerializer(UnsupportedTypes.class, "arrayMapByteByte");
744+
}
745+
746+
static void assertInstanceOfIdentitySerializer(Class<?> type, String fieldName) {
747+
var serializer = SELECTOR.select(fieldAsElement(type, fieldName));
748+
assertThat(serializer, instanceOf(IdentitySerializer.class));
749+
}
693750
}
694751

695752
static final class SerializeWithOnTypesTest {

configlib-core/src/testFixtures/java/de/exlll/configlib/TestUtils.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,38 @@ public record IdentitySerializer(SerializerContext context)
131131
implements Serializer<Object, Object> {
132132

133133
@Override
134-
public Object serialize(Object element) {
135-
return element;
136-
}
137-
138-
@Override
139-
public Object deserialize(Object element) {
140-
return element;
141-
}
134+
public Object serialize(Object element) {
135+
return element;
136+
}
137+
138+
@Override
139+
public Object deserialize(Object element) {
140+
return element;
141+
}
142+
}
143+
144+
public record ThrowingSerializer(SerializerContext context)
145+
implements Serializer<Object, Object> {
146+
147+
public ThrowingSerializer {
148+
throw new UnsupportedOperationException(context.toString());
142149
}
143150

151+
@Override
152+
public Object serialize(Object element) {
153+
throw new UnsupportedOperationException(element.toString());
154+
}
155+
156+
@Override
157+
public Object deserialize(Object element) {
158+
throw new UnsupportedOperationException(element.toString());
159+
}
160+
}
161+
162+
@SafeVarargs
163+
public static <E> List<E> asList(E... elements) {
164+
return new ArrayList<>(Arrays.asList(elements));
165+
}
144166

145167
@SafeVarargs
146168
public static <E> Set<E> asSet(E... elements) {

0 commit comments

Comments
 (0)