This is an automated email from the ASF dual-hosted git repository.

vieiro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ac27d23f1 PR comments taken into account - removed bogus "return" 
statements - separated "os-dependent" and "os-independent" tests - added cases 
to "toObjectArray" and "toPrimitiveArray" - fixed Travis issues (I hope)
     new 5a49cd36ba Merge pull request #3902 from lbownik/lbownik-dev
0ac27d23f1 is described below

commit 0ac27d23f12b4973a7ab56a6fb0020c71f06c450
Author: Lukasz Bownik <[email protected]>
AuthorDate: Wed Mar 30 18:55:01 2022 -0700

    PR comments taken into account
    - removed bogus "return" statements
    - separated "os-dependent" and "os-independent" tests
    - added cases to "toObjectArray" and "toPrimitiveArray"
    - fixed Travis issues (I hope)
---
 .../src/org/openide/util/BaseUtilitiesTest.java    | 448 ++++++++++++++++++---
 1 file changed, 396 insertions(+), 52 deletions(-)

diff --git 
a/platform/openide.util/test/unit/src/org/openide/util/BaseUtilitiesTest.java 
b/platform/openide.util/test/unit/src/org/openide/util/BaseUtilitiesTest.java
index f82a429526..afcdeb0f07 100644
--- 
a/platform/openide.util/test/unit/src/org/openide/util/BaseUtilitiesTest.java
+++ 
b/platform/openide.util/test/unit/src/org/openide/util/BaseUtilitiesTest.java
@@ -23,6 +23,8 @@ import java.io.File;
 import java.net.URI;
 import java.util.Locale;
 import org.netbeans.junit.NbTestCase;
+import static org.openide.util.BaseUtilities.*;
+import static java.lang.Boolean.*;
 
 /**
  *
@@ -41,7 +43,7 @@ public class BaseUtilitiesTest extends NbTestCase {
         BaseUtilities.resetOperatingSystem ();
         originalOsName = System.getProperty("os.name");
     }
-    
+
     @Override
     protected void tearDown() throws Exception {
         System.setProperty("os.name", originalOsName);
@@ -98,49 +100,214 @@ public class BaseUtilitiesTest extends NbTestCase {
         assertFalse ("freebsd is not isWindows", BaseUtilities.isWindows ());
         assertTrue ("freebsd isUnix", BaseUtilities.isUnix ());
     }
+    
//--------------------------------------------------------------------------
+    public void test_IsJavaIdentifier_returnsTrue_whenGivenJavaIdentifier()
+            throws Exception {
 
-    public void testIsJavaIdentifier() throws Exception {
-        assertTrue(BaseUtilities.isJavaIdentifier("whatever"));
-        assertTrue(BaseUtilities.isJavaIdentifier("Ю"));
-        assertTrue(BaseUtilities.isJavaIdentifier("_someThing$99"));
-        assertFalse(BaseUtilities.isJavaIdentifier("99z"));
-        assertFalse(BaseUtilities.isJavaIdentifier("assert"));
-        assertFalse(BaseUtilities.isJavaIdentifier("null"));
-        assertFalse(BaseUtilities.isJavaIdentifier(""));
-        assertFalse(BaseUtilities.isJavaIdentifier(null));
-        assertFalse(BaseUtilities.isJavaIdentifier("some.thing"));
-    }
-
-    public void testFileURI() throws Exception {
-        if (BaseUtilities.isWindows()) {
-            assertFileURI("C:\\some\\path #1", "file:/C:/some/path%20%231");
-            assertEquals(new File("C:\\some\\path"), BaseUtilities.toFile(new 
URI("file:/C:/some/path")));
-            assertEquals(new File("C:\\some\\path"), BaseUtilities.toFile(new 
URI("file:///C:/some/path")));
-            assertEquals(new File("C:\\some\\path"), BaseUtilities.toFile(new 
URI("file:/C:/some/path/")));
-            assertFileURI("\\\\server\\share\\path", 
"file://server/share/path");
-            assertEquals(new File("\\\\server\\share\\path"), 
BaseUtilities.toFile(new URI("file:////server/share/path")));
-            assertEquals(new File("\\\\server\\share\\path #1"), 
BaseUtilities.toFile(new URI("file:////server/share/path%20%231")));
-        } else {
-            assertFileURI("/some/path #1", "file:/some/path%20%231");
-            assertEquals(new File("/some/path"), BaseUtilities.toFile(new 
URI("file:/some/path")));
-            assertEquals(new File("/some/path"), BaseUtilities.toFile(new 
URI("file:///some/path")));
-            assertEquals(new File("/some/path"), BaseUtilities.toFile(new 
URI("file:/some/path/")));
-        }
-        String s = BaseUtilities.toURI(getWorkDir()).toString();
-        assertTrue(s, s.endsWith("/"));
-        URI jar = BaseUtilities.toURI(new File(getWorkDir(), "some.jar"));
-        URI jarN = jar.resolve("some.jar");
-        assertEquals(jar, jarN);
-        URI jarR = new URI("jar:" + jar + "!/");
-        URI jarNR = new URI("jar:" + jarN + "!/");
-        assertEquals("#214131: equal even when wrapped", jarR, jarNR);
-        // XXX test that IllegalArgumentException is thrown where appropriate
-    }
-    private static void assertFileURI(String file, String uri) throws 
Exception {
-        URI u = new URI(uri);
-        File f = new File(file);
-        assertEquals(u, BaseUtilities.toURI(f));
-        assertEquals(f, BaseUtilities.toFile(u));
+        assertTrue(isJavaIdentifier("whatever"));
+        assertTrue(isJavaIdentifier("Ю"));
+        assertTrue(isJavaIdentifier("_someThing$99"));
+    }
+    
+    
//--------------------------------------------------------------------------
+    public void test_IsJavaIdentifier_returnsFalse_whenImproperArgument()
+            throws Exception {
+
+        assertFalse(isJavaIdentifier("99z"));
+        assertFalse(isJavaIdentifier("assert"));
+        assertFalse(isJavaIdentifier("null"));
+        assertFalse(isJavaIdentifier(""));
+        assertFalse(isJavaIdentifier(null));
+        assertFalse(isJavaIdentifier("some.thing"));
+    }
+
+    
//--------------------------------------------------------------------------
+    public void test_toFile_throwsNullPonter_whenArgumentIsNull()
+            throws Exception {
+
+        try {
+            toFile(null);
+            fail();
+        } catch (final NullPointerException e) {
+            //good
+        }
+    }
+    
+    
//--------------------------------------------------------------------------
+    public void test_toFile_throwsIllegalArgument_whenGivenNonFileURI()
+            throws Exception {
+
+        try {
+            toFile(new URI("http://example.com";));
+            fail();
+        } catch (final IllegalArgumentException e) {
+            //good
+        }
+        try {
+            toFile(new URI("mailto:[email protected]";));
+            fail();
+        } catch (final IllegalArgumentException e) {
+            //good
+        }
+        try {
+            toFile(new URI("isbn:12345"));
+            fail();
+        } catch (final IllegalArgumentException e) {
+            //good
+        }
+    }
+    
//--------------------------------------------------------------------------
+    public void 
test_toFile_throwsIllegalArgument_whenGivenFileURIWithIllegalCharacter()
+            throws Exception {
+
+        try {
+            toFile(new URI("file:/C:/some/?"));
+            fail();
+        } catch (final IllegalArgumentException e) {
+            //good
+        }
+    }
+    
+    
//--------------------------------------------------------------------------
+    public void test_toFile_returnsFile_whenGivenCorrectURI()
+          throws Exception {
+
+        if (isUnix()) {
+            assertEquals(new File("/some/path"), toFile(new 
URI("file:/some/path")));
+            assertEquals(new File("/some/path"), toFile(new 
URI("file:///some/path")));
+            assertEquals(new File("/some/path"), toFile(new 
URI("file:/some/path/")));
+            assertEquals(new File("/some/path #1"), toFile(new 
URI("file:/some/path%20%231")));
+            assertEquals(new File("/tmp/hezky česky"), toFile(new 
URI("file:/tmp/hezky%20česky")));
+
+            assertEquals(new File("/a/b/c"), toFile(new URI("file", null, 
"/a/b/c", null)));
+            assertEquals(new File("/a/b/c"), toFile(new URI("file", "", 
"/a/b/c", null)));
+        }
+        if (isWindows()) {
+            assertEquals(new File("C:\\some\\path #1"), toFile(new 
URI("file:/C:/some/path%20%231")));
+            assertEquals(new File("C:\\some\\path"), toFile(new 
URI("file:/C:/some/path")));
+            assertEquals(new File("C:\\some\\path"), toFile(new 
URI("file:///C:/some/path")));
+            assertEquals(new File("C:\\some\\path"), toFile(new 
URI("file:/C:/some/path/")));
+            assertEquals(new File("\\\\server\\share\\path"), toFile(new 
URI("file://server/share/path")));
+            assertEquals(new File("\\\\server\\share\\path"), toFile(new 
URI("file:////server/share/path")));
+            assertEquals(new File("\\\\server\\share\\path #1"), toFile(new 
URI("file:////server/share/path%20%231")));
+            assertEquals(new File("C:\\tmp\\hezky česky"), toFile(new 
URI("file:/C:/tmp/hezky%20česky")));
+
+            assertEquals(new File("\\a\\b\\c"), toFile(new URI("file", null, 
"/a/b/c", null)));
+            assertEquals(new File("\\a\\b\\c"), toFile(new URI("file", "", 
"/a/b/c", null)));
+        }
+    }
+    
+    
//--------------------------------------------------------------------------
+    public void test_toFile_acceptsURIsCreatedBy_toURI()
+            throws Exception {
+
+        if (isUnix()) {
+            assertThatFileEqualsItself(new File("/some/path"));
+            assertThatFileEqualsItself(new File("/some/path"));
+            assertThatFileEqualsItself(new File("/some/path/"));
+            assertThatFileEqualsItself(new File("/some/path #1"));
+            assertThatFileEqualsItself(new File("/tmp/hezky česky"));
+        }
+        if (isWindows()) {
+            assertThatFileEqualsItself(new File("C:\\some\\path #1"));
+            assertThatFileEqualsItself(new File("C:\\some\\path"));
+            assertThatFileEqualsItself(new File("C:\\some\\path"));
+            assertThatFileEqualsItself(new File("C:\\some\\path\\"));
+            assertThatFileEqualsItself(new File("C:\\tmp\\hezky česky"));
+            assertThatFileEqualsItself(new File("\\\\server\\share\\path"));
+            assertThatFileEqualsItself(new File("\\\\server\\share\\path\\"));
+            assertThatFileEqualsItself(new File("\\\\server\\share\\path #1"));
+        }
+    }
+    
+    
//--------------------------------------------------------------------------
+    private void assertThatFileEqualsItself(final File file) {
+        
+        assertEquals(file, toFile(toURI(file)));
+    }
+    
+    
//--------------------------------------------------------------------------
+    public void test_toURI_retunrURI_containingTrailingSlash()
+            throws Exception {
+
+        assertTrue(toURI(getWorkDir()).toString().endsWith("/"));
+    }
+    
+    
//--------------------------------------------------------------------------
+    public void test_toURI_throwsNullPonter_whenArgumentIsNull()
+            throws Exception {
+
+        try {
+            toURI(null);
+            fail();
+        } catch (final NullPointerException e) {
+            //good
+        }
+    }
+
+    
//--------------------------------------------------------------------------
+    public void test_toURI_returnsURI_whenGivenCorrectPath()
+          throws Exception {
+
+        if (isUnix()) {
+            assertEquals(new URI("file:/some/path"), toURI(new 
File("/some/path")));
+            assertEquals(new URI("file:///some/path"), toURI(new 
File("/some/path")));
+            assertEquals(new URI("file:/some/path"), toURI(new 
File("/some/path/")));
+            assertEquals(new URI("file:/some/path%20%231"), toURI(new 
File("/some/path #1")));
+            assertEquals(new URI("file:/tmp/hezky%20česky"), toURI(new 
File("/tmp/hezky česky")));
+        }
+        if (isWindows()) {
+            assertEquals(new URI("file:/C:/some/path%20%231"), toURI(new 
File("C:\\some\\path #1")));
+            assertEquals(new URI("file:/C:/some/path"), toURI(new 
File("C:\\some\\path")));
+            assertEquals(new URI("file:///C:/some/path"), toURI(new 
File("C:\\some\\path")));
+            assertEquals(new URI("file:/C:/some/path"), toURI(new 
File("C:\\some\\path\\")));
+            assertEquals(new URI("file:/C:/tmp/hezky%20česky"), toURI(new 
File("C:\\tmp\\hezky česky")));
+            assertEquals(new URI("file://server/share/path"), toURI(new 
File("\\\\server\\share\\path")));
+            assertEquals(new URI("file://server/share/path"), toURI(new 
File("\\\\server\\share\\path\\")));
+            assertEquals(new URI("file://server/share/path%20%231"), toURI(new 
File("\\\\server\\share\\path #1")));
+        }
+    }
+    
//--------------------------------------------------------------------------
+    public void test_toURI_acceptsFilesCreatedBy_toFile()
+            throws Exception {
+
+        if (isUnix()) {
+            assertThatURIEqualsItself(new URI("file:/some/path"));
+            assertThatURIEqualsItself(new URI("file:///some/path"));
+            assertEquals(new URI("file:/some/path"), toURI(toFile(new 
URI("file:/some/path/"))));
+            assertThatURIEqualsItself(new URI("file:/some/path%20%231"));
+            assertThatURIEqualsItself(new URI("file:/tmp/hezky%20česky"));
+        }
+        if (isWindows()) {
+            assertThatURIEqualsItself(new URI("file:/C:/some/path"));
+            assertThatURIEqualsItself(new URI("file:///C:/some/path"));
+            assertEquals(new URI("file:/C:/some/path"), toURI(toFile(new 
URI("file:/C:/some/path/"))));
+            assertThatURIEqualsItself(new URI("file:/C:/some/path%20%231"));
+            assertThatURIEqualsItself(new URI("file:/C:/tmp/hezky%20česky"));
+            assertThatURIEqualsItself(new URI("file://server/share/path"));
+            assertEquals(new URI("file://server/share/path"), 
toURI(toFile((new URI("file:////server/share/path")))));
+            assertEquals(new URI("file://server/share/path%20%231"), 
toURI(toFile(new URI("file:////server/share/path%20%231"))));
+        }
+    }
+    
+    
//--------------------------------------------------------------------------
+    private void assertThatURIEqualsItself(final URI uri) {
+        
+        assertEquals(uri, toURI(toFile(uri)));
+    }
+    
+    
//--------------------------------------------------------------------------
+    public void test_toURI_producesURI_wchichResolvesAndWrapsProperly()
+          throws Exception {
+
+        URI uri = toURI(new File("c:\\dir", "some.jar"));
+        URI resolvedURI = uri.resolve("some.jar");
+        assertEquals(uri, resolvedURI);
+
+        // these are supposed to test bugfix 
https://bz.apache.org/netbeans/show_bug.cgi?id=214131
+        URI wrappedURI = new URI("jar:" + uri + "!/");
+        URI wrappedResolvedURI = new URI("jar:" + resolvedURI + "!/");
+        assertEquals(wrappedURI, wrappedResolvedURI);
     }
 
     public void testParseParameters1() {
@@ -200,17 +367,194 @@ public class BaseUtilitiesTest extends NbTestCase {
         assertEquals("", args[1]);
         assertEquals("third\\narg", args[2]);
     }
+    
//--------------------------------------------------------------------------
+
+    public void test_normalizeURI_ThrowsNullPointer_whenGivenNull()
+            throws Exception {
+
+        try {
+            normalizeURI(null);
+            fail();
+        } catch (final NullPointerException e) {
+            //good
+        }
+    }
+    
//--------------------------------------------------------------------------
+    public void 
test_normalizeURI_ReturnsTheSameURI_whenNormalizationIsNotNeeded()
+            throws Exception {
+
+        assertEquals("file:////wsl$/Target/",
+                normalizeURI(new URI("file:////wsl$/Target/")).toString());
+    }
+    
//--------------------------------------------------------------------------
+    public void test_normalizeURI_ReturnsNorMalizadURI()
+            throws Exception {
+
+        assertEquals("file:////wsl$/Target/",
+                normalizeURI(new 
URI("file:////wsl$/Target/home/./../")).toString());
+    }
+    
//--------------------------------------------------------------------------
+    public void 
test_toObjectArray_throwsIllegalArgumentException_whenGivenNullArgument() {
 
-    public void testNormalizeURI() throws Exception {
-        {
-            URI uri = new URI("file:////wsl$/Target/");
-            URI normalized = BaseUtilities.normalizeURI(uri);
-            assertEquals("file:////wsl$/Target/", normalized.toString());
+        try {
+            toObjectArray(null);
+            fail();
+        } catch (final IllegalArgumentException e) {
+            //good
         }
-        {
-            URI uri = new URI("file:////wsl$/Target/home/./../");
-            URI normalized = BaseUtilities.normalizeURI(uri);
-            assertEquals("file:////wsl$/Target/", normalized.toString());
+    }
+
+    
//--------------------------------------------------------------------------
+    public void test_toObjectArray_returnsEmptyArray_whenGivenEmptyArray() {
+
+        assertEquals(0, toObjectArray(new boolean[0]).length);
+
+        assertEquals(0, toObjectArray(new char[0]).length);
+
+        assertEquals(0, toObjectArray(new byte[0]).length);
+        assertEquals(0, toObjectArray(new short[0]).length);
+        assertEquals(0, toObjectArray(new int[0]).length);
+        assertEquals(0, toObjectArray(new long[0]).length);
+
+        assertEquals(0, toObjectArray(new float[0]).length);
+        assertEquals(0, toObjectArray(new double[0]).length);
+    }
+    
//--------------------------------------------------------------------------
+    public void 
test_toObjectArray_returnsArrayOfObject_whenGivenArrayOfPrimitives() {
+
+        Object[] result;
+        
+        result = toObjectArray(new boolean[]{true, false});
+        assertTrue(result instanceof Boolean[]);
+        assertEquals(2, result.length);
+        assertEquals(TRUE, result[0]);
+        assertEquals(FALSE, result[1]);
+
+        result = toObjectArray(new char[]{'a', 'b'});
+        assertTrue(result instanceof Character[]);
+        assertEquals(2, result.length);
+        assertEquals(new Character('a'), result[0]);
+        assertEquals(new Character('b'), result[1]);
+        
+        result = toObjectArray(new byte[]{(byte)0, (byte)1});
+        assertTrue(result instanceof Byte[]);
+        assertEquals(2, result.length);
+        assertEquals(new Byte((byte)0), result[0]);
+        assertEquals(new Byte((byte)1), result[1]);
+        
+        result = toObjectArray(new short[]{(short)0, (short)1});
+        assertTrue(result instanceof Short[]);
+        assertEquals(2, result.length);
+        assertEquals(new Short((short)0), result[0]);
+        assertEquals(new Short((short)1), result[1]);
+        
+        result = toObjectArray(new int[]{0, 1});
+        assertTrue(result instanceof Integer[]);
+        assertEquals(2, result.length);
+        assertEquals(new Integer(0), result[0]);
+        assertEquals(new Integer(1), result[1]);
+        
+        result = toObjectArray(new long[]{(long)0, (long)1});
+        assertTrue(result instanceof Long[]);
+        assertEquals(2, result.length);
+        assertEquals(new Long(0), result[0]);
+        assertEquals(new Long(1), result[1]);
+        
+        result = toObjectArray(new float[]{(float)0.0, (float)1.0});
+        assertTrue(result instanceof Float[]);
+        assertEquals(2, result.length);
+        assertEquals(new Float(0.0), result[0]);
+        assertEquals(new Float(1.0), result[1]);
+        
+        result = toObjectArray(new double[]{0.0, 1.0});
+        assertTrue(result instanceof Double[]);
+        assertEquals(2, result.length);
+        assertEquals(new Double(0.0), result[0]);
+        assertEquals(new Double(1.0), result[1]);
+    }
+    
+    
//--------------------------------------------------------------------------
+    public void 
test_toObjectArray_returnsTheArgument_whenGivenArrayOfObjects() {
+        
+        Object[] array = new Object[2];
+        assertTrue(array == toObjectArray(array));
+    }
+    
//--------------------------------------------------------------------------
+    public void 
test_toPrimitiveArray_throwsIllegalArgumentException_whenGivenNullArgument() {
+
+        try {
+            toPrimitiveArray(null);
+            fail();
+        } catch (final IllegalArgumentException e) {
+            //good
         }
     }
+    
//--------------------------------------------------------------------------
+    public void test_toPrimitiveArray_returnsEmptyArray_whenGivenEmptyArray() {
+
+        assertEquals(0, toObjectArray(new Boolean[0]).length);
+
+        assertEquals(0, toObjectArray(new Character[0]).length);
+
+        assertEquals(0, toObjectArray(new Byte[0]).length);
+        assertEquals(0, toObjectArray(new Short[0]).length);
+        assertEquals(0, toObjectArray(new Integer[0]).length);
+        assertEquals(0, toObjectArray(new Long[0]).length);
+
+        assertEquals(0, toObjectArray(new Float[0]).length);
+        assertEquals(0, toObjectArray(new Double[0]).length);
+    }
+    
//--------------------------------------------------------------------------
+    public void 
test_toPrimitiveArray_returnsArrayOfPrimitives_whenGivenArrayOfObjects() {
+
+        Object result;
+        
+        result = toPrimitiveArray(new Boolean[]{TRUE, null});
+        assertTrue(result instanceof boolean[]);
+        assertEquals(2, ((boolean[])result).length);
+        assertEquals(true, ((boolean[])result)[0]);
+        assertEquals(false, ((boolean[])result)[1]);
+
+        result = toPrimitiveArray(new Character[]{new Character('a'), null});
+        assertTrue(result instanceof char[]);
+        assertEquals(2, ((char[])result).length);
+        assertEquals('a', ((char[])result)[0]);
+        assertEquals('\0', ((char[])result)[1]);
+        
+        result = toPrimitiveArray(new Byte[]{null, new Byte((byte)1)});
+        assertTrue(result instanceof byte[]);
+        assertEquals(2, ((byte[])result).length);
+        assertEquals((byte)0, ((byte[])result)[0]);
+        assertEquals((byte)1, ((byte[])result)[1]);
+        
+        result = toPrimitiveArray(new Short[]{null, new Short((short)1)});
+        assertTrue(result instanceof short[]);
+        assertEquals(2, ((short[])result).length);
+        assertEquals((short)0, ((short[])result)[0]);
+        assertEquals((short)1, ((short[])result)[1]);
+        
+        result = toPrimitiveArray(new Integer[]{null, new Integer(1)});
+        assertTrue(result instanceof int[]);
+        assertEquals(2, ((int[])result).length);
+        assertEquals(0, ((int[])result)[0]);
+        assertEquals(1, ((int[])result)[1]);
+        
+        result = toPrimitiveArray(new Long[]{null, new Long(1)});
+        assertTrue(result instanceof long[]);
+        assertEquals(2, ((long[])result).length);
+        assertEquals(0, ((long[])result)[0]);
+        assertEquals(1, ((long[])result)[1]);
+        
+        result = toPrimitiveArray(new Float[]{null, new Float(1.0)});
+        assertTrue(result instanceof float[]);
+        assertEquals(2, ((float[])result).length);
+        assertEquals((float)0.0, ((float[])result)[0], 0.00001);
+        assertEquals((float)1.0, ((float[])result)[1], 0.00001);
+        
+        result = toPrimitiveArray(new Double[]{null, new Double(1.0)});
+        assertTrue(result instanceof double[]);
+        assertEquals(2, ((double[])result).length);
+        assertEquals(0.0, ((double[])result)[0], 0.00001);
+        assertEquals(1.0, ((double[])result)[1], 0.00001);
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

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

Reply via email to