You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2022/04/15 06:48:38 UTC

[GitHub] [netbeans] vieiro commented on a diff in pull request #3988: Added more tests to BaseUtilities, Pai and MapFormat classes in org.netbeans.util package.

vieiro commented on code in PR #3988:
URL: https://github.com/apache/netbeans/pull/3988#discussion_r851091388


##########
platform/openide.util/src/org/openide/util/MapFormat.java:
##########
@@ -188,9 +189,14 @@ public String processPattern(String newPattern) throws IllegalArgumentException
 
             if (ridx < 0) {
                 if (exactmatch) {
-                    throw new IllegalArgumentException(
-                        NbBundle.getBundle(MapFormat.class).getString("MSG_UnmatchedBraces") + " " + lidx
-                    );
+                    throw new IllegalArgumentException() {
+                        @Override
+                        public String getMessage() {
+                            // this is ugly- but it allows unit testing

Review Comment:
   Why?



##########
platform/openide.util/src/org/openide/util/MapFormat.java:
##########
@@ -276,12 +282,16 @@ public StringBuffer format(Object pat, StringBuffer result, FieldPosition fpos)
 
             if (obj == null) {
                 if (throwex) {
-                    throw new IllegalArgumentException(
-                        MessageFormat.format(
-                            NbBundle.getBundle(MapFormat.class).getString("MSG_FMT_ObjectForKey"),
-                            new Object[] { new Integer(key) }
-                        )
-                    );
+                    throw new IllegalArgumentException() {
+                        @Override
+                        public String getMessage() {
+                            // this is ugly- but it allows unit testing

Review Comment:
   Why? 



##########
platform/openide.util/test/unit/src/org/openide/util/PairTest.java:
##########
@@ -19,39 +19,84 @@
 package org.openide.util;
 
 import org.netbeans.junit.NbTestCase;
-import org.openide.util.Pair;
 
 /**
  *
- * @author Tomas Zezula
+ * @author Tomas Zezula, Lukasz Bownik
  */
 public class PairTest extends NbTestCase {
 
-    public PairTest(String name) {
+    //--------------------------------------------------------------------------
+    public PairTest(final String name) {
+        
         super(name);
     }
 
+    //-------------------------------------------------------------------------- 
+    public void test_pairEquality() {
 
-    public void testPairs() {
-        final Pair<Integer,Integer> p1a = Pair.of(1, 1);
-        final Pair<Integer,Integer> p1b = Pair.of(1, 1);
-        final Pair<Integer,Integer> p2 = Pair.of(1, 2);
-        final Pair<Integer,Integer> p3 = Pair.of(2, 1);
-        final Pair<Integer,Integer> p4 = Pair.of(null, 1);
-        final Pair<Integer,Integer> p5 = Pair.of(1, null);
-        final Pair<Integer,Integer> p6 = Pair.of(null, null);
-        assertTrue(p1a.equals(p1a));
-        assertTrue(p1a.equals(p1b));
-        assertFalse(p1a.equals(p2));
-        assertFalse(p1a.equals(p3));
-        assertFalse(p1a.equals(p4));
-        assertFalse(p1a.equals(p5));
-        assertFalse(p1a.equals(p6));
-        assertEquals(p1a.hashCode(), p1b.hashCode());
-        assertEquals(p4.hashCode(), p4.hashCode());
-        assertEquals(p5.hashCode(), p5.hashCode());
-        assertEquals(p6.hashCode(), p6.hashCode());
-        assertFalse(p4.hashCode() == p5.hashCode());
+        assertTrue(pair11a.equals(pair11a));
+        assertEquals(pair11a.hashCode(), pair11a.hashCode());
+        assertEquals(new Integer(1), pair11a.first());
+        assertEquals(new Integer(1), pair11a.second());
+        
+        assertTrue(pair12.equals(pair12));
+        assertEquals(pair12.hashCode(), pair12.hashCode());
+        assertEquals(new Integer(1), pair12.first());
+        assertEquals(new Integer(2), pair12.second());
+
+        assertTrue(pair11a.equals(pair11b));
+        assertEquals(pair11a.hashCode(), pair11b.hashCode());
+        assertEquals(new Integer(1), pair11b.first());
+        assertEquals(new Integer(1), pair11b.second());
+
+        assertTrue(pair1Null.equals(pair1Null));
+        assertEquals(pair1Null.hashCode(), pair1Null.hashCode());
+        assertEquals(new Integer(1), pair1Null.first());
+        assertEquals(null, pair1Null.second());
+
+        assertTrue(pairNul1.equals(pairNul1));
+        assertEquals(pairNul1.hashCode(), pairNul1.hashCode());
+        assertEquals(null, pairNul1.first());
+        assertEquals(new Integer(1), pairNul1.second());
+
+        assertTrue(pairNullNull.equals(pairNullNull));
+        assertEquals(pairNullNull.hashCode(), pairNullNull.hashCode());
+        assertEquals(null, pairNullNull.first());
+        assertEquals(null, pairNullNull.second());
     }
 
+    //-------------------------------------------------------------------------- 
+    public void test_pairInequality() {
+
+        assertFalse(pair11a.equals(pair12));
+        assertFalse(pair11a.equals(pair21));
+        assertFalse(pair11a.equals(pairNul1));
+        assertFalse(pair11a.equals(pair1Null));
+        assertFalse(pair11a.equals(pairNullNull));
+        assertFalse(pairNul1.equals(pair11a));
+        assertFalse(pair1Null.equals(pair11a));
+        assertFalse(pairNullNull.equals(pair11a));
+        
+        assertFalse(pair11a.equals(null));
+        assertFalse(pair11a.equals(""));
+    }
+    //--------------------------------------------------------------------------
+    public void test_pair_toString() {
+        
+        assertEquals("Pair[1,1]", pair11a.toString());
+        assertEquals("Pair[1,1]", pair11b.toString());
+        assertEquals("Pair[1,2]", pair12.toString());
+        assertEquals("Pair[1,null]", pair1Null.toString());
+        assertEquals("Pair[null,1]", pairNul1.toString());
+        assertEquals("Pair[null,null]", pairNullNull.toString());
+    }
+    //-------------------------------------------------------------------------- 
+    private final Pair<Integer, Integer> pair11a = Pair.of(1, 1);

Review Comment:
   If we're testing "Pair" here, we should instantiate Pairs in test methods/setup methods, not as class members. What happens if the constructors themselves fail? 



##########
platform/openide.util/src/org/openide/util/MapFormat.java:
##########
@@ -221,7 +227,7 @@ private String formatObject(Object obj) {
         if (obj instanceof Number) {
             return NumberFormat.getInstance(locale).format(obj); // fix
         } else if (obj instanceof Date) {
-            return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale).format(obj); //fix
+            return DateFormat.getDateTimeInstance(SHORT, SHORT, locale).format(obj); //fix

Review Comment:
   DateFormat.SHORT is more legible to me



##########
platform/openide.util/test/unit/src/org/openide/util/MapFormatTest.java:
##########
@@ -18,68 +18,178 @@
  */
 package org.openide.util;
 
+import static java.util.Collections.emptyMap;
+import java.util.Date;
 import java.util.HashMap;
-import junit.framework.*;
-import org.openide.util.MapFormat;
+import java.util.Map;
+import static junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertTrue;
+import static junit.framework.TestCase.fail;
+import org.junit.Before;
+import org.junit.Test;
+import static org.openide.util.MapFormat.format;
 
 /**
  *
- * @author Jaroslav Tulach
+ * @author Jaroslav Tulach, Lukasz Bownik
  */
-public class MapFormatTest extends TestCase {
+public class MapFormatTest {

Review Comment:
   Why remove TestCase?



##########
platform/openide.util/test/unit/src/org/openide/util/BaseUtilitiesTest.java:
##########
@@ -309,66 +311,74 @@ public void test_toURI_producesURI_wchichResolvesAndWrapsProperly()
         URI wrappedResolvedURI = new URI("jar:" + resolvedURI + "!/");
         assertEquals(wrappedURI, wrappedResolvedURI);
     }
+    
+    //--------------------------------------------------------------------------
+    public void test_parseParameters_throwsNullPointer_whenGivenNullArgument() {
 
-    public void testParseParameters1() {
-        String[] args = BaseUtilities.parseParameters("\"c:\\program files\\jdk\\bin\\java\" -Dmessage=\"Hello /\\\\/\\\\ there!\" -Xmx128m");
+        try {
+            parseParameters(null);
+            fail();
+        } catch (final NullPointerException e) {
+            //good
+        }
+    }
+
+    //--------------------------------------------------------------------------
+    public void test_parseParameters_returnsEmptyArray_whenGivenEmptyString() {
+
+        assertEquals(0, parseParameters("").length);
+        assertEquals(0, parseParameters("   \t\r\n").length);
+    }
+
+    //--------------------------------------------------------------------------
+    public void test_parseParameters_returnsProperArray_forProperInput() {
+
+        String[] args;
+
+        args = parseParameters("\"c:\\program files\\jdk\\bin\\java\" -Dmessage=\"Hello /\\\\/\\\\ there!\" -Xmx128m");

Review Comment:
   Why to lines here?



-- 
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: notifications-unsubscribe@netbeans.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists