@@ -119,14 +119,22 @@ public void testMultipleContainersCanBePassed() throws Exception {
119119 when (scmClient .getContainerReplicas (anyLong ())).thenReturn (getReplicas (true ));
120120 cmd = new InfoSubcommand ();
121121 CommandLine c = new CommandLine (cmd );
122- c .parseArgs ("1" , "123" , "456" , "invalid" , " 789" );
122+ c .parseArgs ("1" , "123" , "456" , "789" );
123123 cmd .execute (scmClient );
124124 validateMultiOutput ();
125125 }
126126
127+ @ Test
128+ public void testMultipleInvalidContainerIdFails () throws Exception {
129+ cmd = new InfoSubcommand ();
130+ CommandLine c = new CommandLine (cmd );
131+ c .parseArgs ("1" , "invalid" , "-2" , "0.5" );
132+ validateInvalidContainerIDOutput ();
133+ }
134+
127135 @ Test
128136 public void testContainersCanBeReadFromStdin () throws IOException {
129- String input = "1\n 123\n 456\n invalid \ n 789\n " ;
137+ String input = "1\n 123\n 456\n 789\n " ;
130138 ByteArrayInputStream inContent = new ByteArrayInputStream (input .getBytes (DEFAULT_ENCODING ));
131139 System .setIn (inContent );
132140 cmd = new InfoSubcommand ();
@@ -137,23 +145,38 @@ public void testContainersCanBeReadFromStdin() throws IOException {
137145 validateMultiOutput ();
138146 }
139147
148+ @ Test
149+ public void testInvalidContainerIdFromStdinFails () throws Exception {
150+ String input = "1\n invalid\n -2\n 0.5\n " ;
151+ ByteArrayInputStream inContent = new ByteArrayInputStream (input .getBytes (DEFAULT_ENCODING ));
152+ System .setIn (inContent );
153+ cmd = new InfoSubcommand ();
154+ CommandLine c = new CommandLine (cmd );
155+ c .parseArgs ("-" );
156+ validateInvalidContainerIDOutput ();
157+ }
158+
140159 private void validateMultiOutput () throws UnsupportedEncodingException {
141160 // Ensure we have a log line for each containerID
142161 List <String > replica = Arrays .stream (outContent .toString (DEFAULT_ENCODING ).split ("\n " ))
143162 .filter (m -> m .matches ("(?s)^Container id: (1|123|456|789).*" ))
144163 .collect (Collectors .toList ());
145164 assertEquals (4 , replica .size ());
165+ }
146166
147- Pattern p = Pattern .compile (
148- "^Invalid\\ scontainer\\ sID:\\ sinvalid.*" , Pattern .MULTILINE );
149- Matcher m = p .matcher (errContent .toString (DEFAULT_ENCODING ));
150- assertTrue (m .find ());
167+ private void validateInvalidContainerIDOutput () throws Exception {
168+ CommandLine .ParameterException ex = assertThrows (
169+ CommandLine .ParameterException .class , () -> cmd .execute (scmClient ));
170+
171+ assertThat (ex .getMessage ())
172+ .isEqualTo ("Container IDs must be positive integers. Invalid container IDs: invalid -2 0.5" );
173+ assertThat (outContent .toString (DEFAULT_ENCODING )).isEmpty ();
151174 }
152175
153176 @ Test
154177 public void testContainersCanBeReadFromStdinJson ()
155178 throws IOException {
156- String input = "1\n 123\n 456\n invalid \ n 789\n " ;
179+ String input = "1\n 123\n 456\n 789\n " ;
157180 ByteArrayInputStream inContent = new ByteArrayInputStream (input .getBytes (DEFAULT_ENCODING ));
158181 System .setIn (inContent );
159182 cmd = new InfoSubcommand ();
@@ -164,12 +187,23 @@ public void testContainersCanBeReadFromStdinJson()
164187 validateJsonMultiOutput ();
165188 }
166189
190+ @ Test
191+ public void testInvalidContainerIdFromStdinJsonFails () throws Exception {
192+ String input = "1\n invalid\n -2\n 0.5\n " ;
193+ ByteArrayInputStream inContent = new ByteArrayInputStream (input .getBytes (DEFAULT_ENCODING ));
194+ System .setIn (inContent );
195+ cmd = new InfoSubcommand ();
196+ CommandLine c = new CommandLine (cmd );
197+ c .parseArgs ("-" , "--json" );
198+ validateInvalidContainerIDOutput ();
199+ }
200+
167201 @ Test
168202 public void testMultipleContainersCanBePassedJson () throws Exception {
169203 when (scmClient .getContainerReplicas (anyLong ())).thenReturn (getReplicas (true ));
170204 cmd = new InfoSubcommand ();
171205 CommandLine c = new CommandLine (cmd );
172- c .parseArgs ("1" , "123" , "456" , "invalid" , " 789" , "--json" );
206+ c .parseArgs ("1" , "123" , "456" , "789" , "--json" );
173207 cmd .execute (scmClient );
174208
175209 validateJsonMultiOutput ();
@@ -181,11 +215,6 @@ private void validateJsonMultiOutput() throws UnsupportedEncodingException {
181215 .filter (m -> m .matches ("(?s)^.*\" containerInfo\" .*" ))
182216 .collect (Collectors .toList ());
183217 assertEquals (4 , replica .size ());
184-
185- Pattern p = Pattern .compile (
186- "^Invalid\\ scontainer\\ sID:\\ sinvalid.*" , Pattern .MULTILINE );
187- Matcher m = p .matcher (errContent .toString (DEFAULT_ENCODING ));
188- assertTrue (m .find ());
189218 }
190219
191220 private void testReplicaIncludedInOutput (boolean includeIndex )
0 commit comments