1
+ package me .itzg .helpers .properties ;
2
+
3
+ import static com .github .stefanbirkner .systemlambda .SystemLambda .tapSystemErr ;
4
+ import static org .assertj .core .api .Assertions .assertThat ;
5
+
6
+ import java .io .IOException ;
7
+ import java .io .InputStream ;
8
+ import java .net .URISyntaxException ;
9
+ import java .net .URL ;
10
+ import java .nio .file .Files ;
11
+ import java .nio .file .Path ;
12
+ import java .nio .file .Paths ;
13
+ import java .util .Arrays ;
14
+ import java .util .Collections ;
15
+ import java .util .HashSet ;
16
+ import java .util .Properties ;
17
+ import me .itzg .helpers .env .MappedEnvVarProvider ;
18
+ import org .apache .commons .lang3 .RandomStringUtils ;
19
+ import org .jetbrains .annotations .NotNull ;
20
+ import org .junit .jupiter .api .BeforeEach ;
21
+ import org .junit .jupiter .api .Test ;
22
+ import org .junit .jupiter .api .io .TempDir ;
23
+ import picocli .CommandLine ;
24
+ import picocli .CommandLine .ExitCode ;
25
+
26
+ class SetPropertiesCommandTest {
27
+
28
+ @ TempDir
29
+ Path tempDir ;
30
+
31
+ private Path propertiesFile ;
32
+ private Path definitionsFile ;
33
+ private Properties originalProperties ;
34
+
35
+ @ BeforeEach
36
+ void setUp () throws IOException , URISyntaxException {
37
+ propertiesFile = preparePropertiesFile ();
38
+
39
+ final URL definitionsResource = getClass ().getResource ("/properties/property-definitions.json" );
40
+ assertThat (definitionsResource ).isNotNull ();
41
+ definitionsFile = Paths .get (definitionsResource .toURI ());
42
+
43
+ originalProperties = loadProperties ();
44
+ }
45
+
46
+ @ Test
47
+ void simpleNeedsChange () throws Exception {
48
+ final String name = RandomStringUtils .randomAlphabetic (10 );
49
+
50
+ final int exitCode = new CommandLine (new SetPropertiesCommand ()
51
+ .setEnvironmentVariablesProvider (MappedEnvVarProvider .of (
52
+ "SERVER_NAME" , name
53
+ ))
54
+ )
55
+ .execute (
56
+ "--definitions" , definitionsFile .toString (),
57
+ propertiesFile .toString ()
58
+ );
59
+
60
+ assertThat (exitCode ).isEqualTo (ExitCode .OK );
61
+
62
+ final Properties properties = loadProperties ();
63
+
64
+ assertThat (properties ).containsEntry ("server-name" , name );
65
+ assertPropertiesEqualExcept (properties , "server-name" );
66
+ }
67
+
68
+ @ Test
69
+ void hasMapping () throws Exception {
70
+
71
+ final int exitCode = new CommandLine (new SetPropertiesCommand ()
72
+ .setEnvironmentVariablesProvider (MappedEnvVarProvider .of (
73
+ "GAMEMODE" , "1"
74
+ ))
75
+ )
76
+ .execute (
77
+ "--definitions" , definitionsFile .toString (),
78
+ propertiesFile .toString ()
79
+ );
80
+
81
+ assertThat (exitCode ).isEqualTo (ExitCode .OK );
82
+
83
+ final Properties properties = loadProperties ();
84
+
85
+ assertThat (properties ).containsEntry ("gamemode" , "creative" );
86
+ assertPropertiesEqualExcept (properties , "gamemode" );
87
+ }
88
+
89
+ @ Test
90
+ void disallowedValue () throws Exception {
91
+ final String err = tapSystemErr (() -> {
92
+ final int exitCode = new CommandLine (new SetPropertiesCommand ()
93
+ .setEnvironmentVariablesProvider (MappedEnvVarProvider .of (
94
+ "ONLINE_MODE" , "invalid"
95
+ ))
96
+ )
97
+ .execute (
98
+ "--definitions" , definitionsFile .toString (),
99
+ propertiesFile .toString ()
100
+ );
101
+
102
+ assertThat (exitCode ).isNotEqualTo (ExitCode .OK );
103
+ });
104
+
105
+ assertThat (err )
106
+ .contains ("ONLINE_MODE" )
107
+ .contains ("InvalidParameterException" );
108
+
109
+ }
110
+
111
+ @ Test
112
+ void removesMarkedForRemoval () throws IOException {
113
+ final Path hasWhiteList = Files .write (tempDir .resolve ("old.properties" ), Collections .singletonList ("white-list=true" ));
114
+ final int exitCode = new CommandLine (new SetPropertiesCommand ()
115
+ )
116
+ .execute (
117
+ "--definitions" , definitionsFile .toString (),
118
+ hasWhiteList .toString ()
119
+ );
120
+
121
+ assertThat (exitCode ).isEqualTo (ExitCode .OK );
122
+
123
+ final Properties properties = new Properties ();
124
+ try (InputStream propsIn = Files .newInputStream (hasWhiteList )) {
125
+ properties .load (propsIn );
126
+ }
127
+
128
+ assertThat (properties ).doesNotContainKey ("white-list" );
129
+ }
130
+
131
+ private void assertPropertiesEqualExcept (Properties properties , String ... propertiesToIgnore ) {
132
+ final HashSet <Object > actualKeys = new HashSet <>(properties .keySet ());
133
+ Arrays .asList (propertiesToIgnore ).forEach (actualKeys ::remove );
134
+ final HashSet <Object > originalKeys = new HashSet <>(originalProperties .keySet ());
135
+ Arrays .asList (propertiesToIgnore ).forEach (originalKeys ::remove );
136
+
137
+ assertThat (actualKeys ).isEqualTo (originalKeys );
138
+
139
+ for (final Object key : originalKeys ) {
140
+ assertThat (properties .get (key )).withFailMessage (() -> String .format ("Property %s does not equal" , key ))
141
+ .isEqualTo (originalProperties .get (key ));
142
+ }
143
+ }
144
+
145
+ @ NotNull
146
+ private Properties loadProperties () throws IOException {
147
+ final Properties properties = new Properties ();
148
+ try (InputStream propsIn = Files .newInputStream (propertiesFile )) {
149
+ properties .load (propsIn );
150
+ }
151
+ return properties ;
152
+ }
153
+
154
+ private Path preparePropertiesFile () throws IOException {
155
+ try (InputStream in = getClass ().getClassLoader ().getResourceAsStream ("properties/server.properties" )) {
156
+ assertThat (in ).isNotNull ();
157
+ final Path outFile = tempDir .resolve ("server.properties" );
158
+ Files .copy (in , outFile );
159
+ return outFile ;
160
+ }
161
+ }
162
+ }
0 commit comments