@@ -572,4 +572,65 @@ void tryCreateParentDirectoriesDoesNotThrowIfParentIsNull(boolean createParentDi
572572 Path path = fs .getPath ("config.yml" );
573573 assertDoesNotThrow (() -> store .tryCreateParentDirectories (path ));
574574 }
575+
576+ private record ThrowingWhileSerializingSerializer ()
577+ implements Serializer <String , String > {
578+
579+ @ Override
580+ public String serialize (String element ) {
581+ throw new UnsupportedOperationException (element );
582+ }
583+
584+ @ Override
585+ public String deserialize (String element ) {
586+ return element ;
587+ }
588+ }
589+
590+ @ Configuration
591+ private static final class G {
592+ private String content = "-" ;
593+ }
594+
595+ @ Test
596+ void saveDoesNotOverwriteConfigurationFileContentsOnYamlDumpFailure () throws IOException {
597+ YamlConfigurationStore <G > store = new YamlConfigurationStore <>(
598+ G .class ,
599+ YamlConfigurationProperties .newBuilder ()
600+ .addSerializer (String .class , new ThrowingWhileSerializingSerializer ())
601+ .build ()
602+ );
603+
604+ String content = "content: abcde" ;
605+ Files .writeString (yamlFile , content );
606+
607+ G config = store .load (yamlFile );
608+ assertThat (config .content , is ("abcde" ));
609+ assertThrows (
610+ UnsupportedOperationException .class ,
611+ () -> store .save (config , yamlFile )
612+ );
613+ assertThat (readFile (yamlFile ), is (content ));
614+ }
615+
616+ @ Test
617+ void writeDoesNotOverwriteStreamContentsOnYamlDumpFailure () {
618+ YamlConfigurationStore <G > store = new YamlConfigurationStore <>(
619+ G .class ,
620+ YamlConfigurationProperties .newBuilder ()
621+ .addSerializer (String .class , new ThrowingWhileSerializingSerializer ())
622+ .build ()
623+ );
624+
625+ String content = "content: abcde" ;
626+ outputStream .writeBytes (content .getBytes ());
627+
628+ G config = store .read (inputFromOutput ());
629+ assertThat (config .content , is ("abcde" ));
630+ assertThrows (
631+ UnsupportedOperationException .class ,
632+ () -> store .write (config , outputStream )
633+ );
634+ assertThat (outputStream .toString (), is (content ));
635+ }
575636}
0 commit comments