kriegaex commented on code in PR #133:
URL: https://github.com/apache/xalan-java/pull/133#discussion_r1412340322


##########
xalan/src/test/java/org/apache/xalan/VersionTest.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the  "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.xalan;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+import java.io.ByteArrayInputStream;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class VersionTest {
+
+  @ParameterizedTest(name = "{0} -> {2}")
+  @MethodSource("testReadPropertiesArgs")
+  public void testReadProperties(String ignoredName, String properties, String 
version) {
+    try (MockedStatic<Version> versionMock = Mockito.mockStatic(Version.class, 
Mockito.CALLS_REAL_METHODS)) {
+      versionMock
+        .when(Version::getPropertiesStream)
+        .thenReturn(new ByteArrayInputStream(properties.getBytes()));
+      assertEquals(version, Version.readVersionNumber());
+    }
+  }
+
+  private static Stream<Arguments> testReadPropertiesArgs() {
+    return Stream.of(
+      Arguments.of("single line without line feed", "version=1.2.3", "1.2.3"),
+      Arguments.of("single line with line feed", "version=4.5.D6-SNAPSHOT\n", 
"4.5.D6-SNAPSHOT"),
+      Arguments.of("multiple lines with version number", 
"foo=bar\nversion=7.8.9\nbaz=zot\n", "7.8.9"),
+      Arguments.of("single line without version number", "verXion=7.8.9\n", 
"0.0.0"),
+      Arguments.of("multiple lines without version number", 
"foo=bar\nverXion=7.8.9\nbaz=zot\n", "0.0.0")
+    );
+  }
+
+  @Test
+  public void testCannotReadProperties() {
+    try (MockedStatic<Version> versionMock = Mockito.mockStatic(Version.class, 
Mockito.CALLS_REAL_METHODS)) {
+      versionMock
+        .when(Version::getPropertiesStream)
+        .thenThrow(NullPointerException.class);
+      assertEquals("0.0.0", Version.readVersionNumber());

Review Comment:
   > Just in case, here's an `InputStream` that throws
   
   Thanks, irrelevant again.
   
   > In other words, testing IO failure here with regular Java is not even 
harder than testing with Mockito.
   
   Wrong. I/O happens in method `Version::getPropertiesStream`, and there I 
cannot inject anything unless I factor out that one line of code again, which 
is a recursive problem. At the end, I need to stub that method if **I want to 
avoid real I/O to happen in the test,** which is exactly my objective: I do not 
want the test to read any test resource file but run the test in memory. The by 
far simplest method is to simply stub that one-line method. Is that so hard to 
understand? It is almost too trivial to explain IMO.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@xalan.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@xalan.apache.org
For additional commands, e-mail: dev-h...@xalan.apache.org

Reply via email to