33import com .alessiodp .core .common .addons .external .simpleyaml .configuration .file .YamlFile ;
44import com .alessiodp .core .common .configuration .ConfigOption ;
55import com .alessiodp .core .common .configuration .ConfigurationFile ;
6+ import com .alessiodp .core .common .logging .logger .ADPLogger ;
67import com .alessiodp .lastloginapi .bungeecord .configuration .data .BungeeConfigMain ;
78import com .alessiodp .lastloginapi .bungeecord .configuration .data .BungeeMessages ;
89import com .alessiodp .lastloginapi .common .LastLoginPlugin ;
10+ import org .junit .jupiter .api .BeforeAll ;
911import org .junit .jupiter .api .Test ;
12+ import org .junit .jupiter .api .io .TempDir ;
1013
11- import java .io .InputStreamReader ;
14+ import java .io .IOException ;
1215import java .lang .reflect .Field ;
16+ import java .nio .file .Path ;
1317import java .util .Collections ;
1418import java .util .List ;
1519
1620import static org .junit .jupiter .api .Assertions .assertNotNull ;
21+ import static org .mockito .ArgumentMatchers .anyString ;
22+ import static org .mockito .Mockito .doAnswer ;
1723import static org .mockito .Mockito .mock ;
24+ import static org .mockito .Mockito .when ;
1825
1926public class BungeeConfigurationTest {
2027 private static final LastLoginPlugin mockPlugin = mock (LastLoginPlugin .class );
2128
29+ @ BeforeAll
30+ public static void setUp () {
31+ ADPLogger testLogger = mock (ADPLogger .class );
32+ doAnswer ((params ) -> {
33+ //System.out.println((String) params.getArgument(0));
34+ return null ;
35+ }).when (testLogger ).info (anyString ());
36+ doAnswer ((params ) -> {
37+ System .out .println ((String ) params .getArgument (0 ));
38+ return null ;
39+ }).when (testLogger ).error (anyString ());
40+ when (mockPlugin .getLogger ()).thenReturn (testLogger );
41+
42+ when (mockPlugin .getResource (anyString ())).thenAnswer ((a ) -> ClassLoader .getSystemResourceAsStream (a .getArgument (0 )));
43+ }
44+
2245 @ Test
23- public void testConfigMain () throws IllegalAccessException {
46+ public void testConfigMain (@ TempDir Path tempDir ) throws IllegalAccessException , IOException {
2447 BungeeConfigMain configMain = new BungeeConfigMain (mockPlugin );
2548
2649 List <String > skipPaths = Collections .emptyList ();
2750
28- testConfiguration (configMain , skipPaths );
51+ testConfiguration (configMain , skipPaths , tempDir );
52+ testAutomaticUpgrade (configMain );
2953 }
3054
3155 @ Test
32- public void testMessages () throws IllegalAccessException {
56+ public void testMessages (@ TempDir Path tempDir ) throws IllegalAccessException , IOException {
3357 BungeeMessages messages = new BungeeMessages (mockPlugin );
3458
3559 List <String > skipPaths = Collections .emptyList ();
3660
37- testConfiguration (messages , skipPaths );
61+ testConfiguration (messages , skipPaths , tempDir );
62+ testAutomaticUpgrade (messages );
3863 }
3964
40- private void testConfiguration (ConfigurationFile configurationFile , List <String > skipPaths ) throws IllegalAccessException {
65+ private void testConfiguration (ConfigurationFile configurationFile , List <String > skipPaths , Path tempDir ) throws IllegalAccessException {
4166 Field [] fields = configurationFile .getClass ().getFields ();
4267
4368 // Initialize YAML
44- YamlFile yf = YamlFile .loadConfiguration (new InputStreamReader (getClass ().getResourceAsStream ("/" + configurationFile .getResourceName ())));
69+ configurationFile .initializeConfiguration (tempDir );
70+ YamlFile yf = configurationFile .getConfiguration ();
4571
4672 // Check fields
4773 for (Field f : fields ) {
@@ -54,6 +80,13 @@ private void testConfiguration(ConfigurationFile configurationFile, List<String>
5480 }
5581 }
5682
83+ private void testAutomaticUpgrade (ConfigurationFile configurationFile ) throws IOException {
84+ configurationFile .getConfiguration ().set ("dont-edit-this.version" , -1 );
85+ configurationFile .getConfiguration ().save ();
86+
87+ configurationFile .checkVersion (true );
88+ }
89+
5790 private boolean skippablePath (String path , List <String > skipPaths ) {
5891 for (String sp : skipPaths ) {
5992 if (path .startsWith (sp ))
0 commit comments