markap14 commented on a change in pull request #3894: NIFI-6884 - Native 
library loading fixed/improved
URL: https://github.com/apache/nifi/pull/3894#discussion_r349309000
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-nar-utils/src/test/java/org/apache/nifi/nar/NativeLibFinderTest.java
 ##########
 @@ -0,0 +1,566 @@
+/*
+ * 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.nifi.nar;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
+
+public class NativeLibFinderTest {
+    public static final String NATIVE_LIB_NAME = "native_lib";
+
+    @Mock
+    private NativeLibFinder testSubjectHelper;
+
+    private String javaLibraryPath;
+
+    private Path tempDirectory;
+    private List<File> nativeLibDirs;
+    private Map<String, Path> nativeLibNameToPath;
+
+    private boolean isOsWindows;
+    private boolean isOsMaxOsx;
+    private boolean isOsLinux;
+
+    @Before
+    public void setUp() throws Exception {
+        initMocks(this);
+        tempDirectory = 
Files.createTempDirectory(this.getClass().getSimpleName());
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        tempDirectory.toFile().deleteOnExit();
+
+        Files.walk(tempDirectory)
+                .sorted(Comparator.reverseOrder())
+                .map(Path::toFile)
+                .forEach(File::delete);
+
+    }
+
+    @Test
+    public void testFindLibraryShouldReturnNullOnWindowsWhenNoDLLAvailable() 
throws Exception {
+        // GIVEN
+        isOsWindows = true;
+
+        createTempFile("so");
+        createTempFile("lib", "so");
+        createTempFile("dylib");
+        createTempFile("lib", "dylib");
+
+        String expected = null;
+
+        // WHEN
+        // THEN
+        testFindLibrary(expected);
+    }
+
+    @Test
+    public void testFindLibraryShouldReturnDLLOnWindows() throws Exception {
+        // GIVEN
+        isOsWindows = true;
+
+        Path expectedNativeLib = createTempFile("dll");
+        createTempFile("so");
+        createTempFile("lib", "so");
+        createTempFile("dylib");
+        createTempFile("lib", "dylib");
+
+        String expected = expectedNativeLib.toFile().getAbsolutePath();
+
+        // WHEN
+        // THEN
+        testFindLibrary(expected);
+    }
+
+    @Test
+    public void testFindLibraryShouldReturnNullOnMacWhenNoDylibOrSoAvailable() 
throws Exception {
+        // GIVEN
+        isOsMaxOsx = true;
+
+        createTempFile("dll");
+
+        String expected = null;
+
+        // WHEN
+        // THEN
+        testFindLibrary(expected);
+    }
+
+    @Test
+    public void testFindLibraryShouldReturnDylibOnMac() throws Exception {
+        // GIVEN
+        isOsMaxOsx = true;
+
+        createTempFile("dll");
+        createTempFile("so");
+        createTempFile("lib", "so");
+        Path expectedNativeLib = createTempFile("dylib");
+        createTempFile("lib", "dylib");
+
+        String expected = expectedNativeLib.toFile().getAbsolutePath();
+
+        // WHEN
+        // THEN
+        testFindLibrary(expected);
+    }
+
+    @Test
+    public void testFindLibraryShouldReturnLibDylibOnMac() throws Exception {
+        // GIVEN
+        isOsMaxOsx = true;
+
+        createTempFile("dll");
+        createTempFile("so");
+        createTempFile("lib", "so");
+        Path expectedNativeLib = createTempFile("lib", "dylib");
+
+        String expected = expectedNativeLib.toFile().getAbsolutePath();
+
+        // WHEN
+        // THEN
+        testFindLibrary(expected);
+    }
+
+    @Test
+    public void testFindLibraryMayReturnSoOnMac() throws Exception {
+        // GIVEN
+        isOsMaxOsx = true;
+        NativeLibFinder testSubject = createTestSubject();
+
+        createTempFile("dll");
+        Path expectedNativeLib = createTempFile("so");
+        createTempFile("lib", "so");
+
+        String expected = expectedNativeLib.toFile().getAbsolutePath();
+
+        // WHEN
+        // THEN
+        testFindLibrary(expected);
+    }
+
+    @Test
+    public void testFindLibraryMayReturnLibSoOnMac() throws Exception {
+        // GIVEN
+        isOsMaxOsx = true;
+        NativeLibFinder testSubject = createTestSubject();
+
+        createTempFile("dll");
+        Path expectedNativeLib = createTempFile("lib", "so");
+
+        String expected = expectedNativeLib.toFile().getAbsolutePath();
+
+        // WHEN
+        // THEN
+        testFindLibrary(expected);
+    }
+
+    @Test
+    public void testFindLibraryShouldReturnNullOnLinuxWhenNoSoAvailable() 
throws Exception {
+        // GIVEN
+        isOsLinux = true;
+        NativeLibFinder testSubject = createTestSubject();
+
+        createTempFile("dll");
+        createTempFile("dylib");
+        createTempFile("lib", "dylib");
+
+        String expected = null;
+
+        // WHEN
+        // THEN
+        testFindLibrary(expected);
+    }
+
+    @Test
+    public void testFindLibraryShouldReturnSoOnLinux() throws Exception {
+        // GIVEN
+        isOsLinux = true;
+        NativeLibFinder testSubject = createTestSubject();
+
+        createTempFile("dll");
+        Path expectedNativeLib = createTempFile("so");
+        createTempFile("lib", "so");
+        createTempFile("dylib");
+        createTempFile("lib", "dylib");
+
+        String expected = expectedNativeLib.toFile().getAbsolutePath();
+
+        // WHEN
+        // THEN
+        testFindLibrary(expected);
+    }
+
+    @Test
+    public void testFindLibraryShouldReturnLibSoOnLinux() throws Exception {
+        // GIVEN
+        isOsLinux = true;
+        NativeLibFinder testSubject = createTestSubject();
+
+        createTempFile("dll");
+        Path expectedNativeLib = createTempFile("lib", "so");
+        createTempFile("dylib");
+        createTempFile("lib", "dylib");
+
+        String expected = expectedNativeLib.toFile().getAbsolutePath();
+
+        // WHEN
+        // THEN
+        testFindLibrary(expected);
+    }
+
+    private void testFindLibrary(String expected) {
+        String actual = createTestSubjectForOS().findLibrary(NATIVE_LIB_NAME, 
tempDirectory.toFile());
+
+        assertEquals(expected, actual);
+    }
+
+    @Test
+    public void testFindLibraryShouldReturnLibLocation() throws Exception {
+        // GIVEN
+        File nativeLibDir = mock(File.class);
+
+        nativeLibDirs = Arrays.asList(nativeLibDir);
+
+        nativeLibNameToPath = new HashMap<>();
+
+        Path libPath = createTempFile("mocked").toAbsolutePath();
+        when(testSubjectHelper.findLibrary("libName", 
nativeLibDir)).thenReturn("libLocation");
+        when(testSubjectHelper.createTempCopy("libName", 
"libLocation")).thenReturn(libPath);
+
+        String expected = libPath.toFile().getAbsolutePath();
+
+        NativeLibFinder testSubject = createTestSubject();
+
+        // WHEN
+        String actual = testSubject.findLibrary("libName");
+
+        // THEN
+        assertEquals(expected, actual);
+        verify(testSubjectHelper).findLibrary("libName", nativeLibDir);
+        verify(testSubjectHelper).createTempCopy("libName", "libLocation");
+        verifyNoMoreInteractions(testSubjectHelper);
+    }
+
+    @Test
+    public void testFindLibraryShouldReturnFirstFoundLibLocation() throws 
Exception {
+        // GIVEN
+        File nativeLibDir1 = mock(File.class);
+        File nativeLibDir2 = mock(File.class);
+        File nativeLibDir3 = mock(File.class);
+
+        nativeLibDirs = Arrays.asList(nativeLibDir1, nativeLibDir2, 
nativeLibDir3);
+
+        nativeLibNameToPath = new HashMap<>();
+
+        Path libPath = createTempFile("mocked").toAbsolutePath();
+        when(testSubjectHelper.findLibrary("libName", 
nativeLibDir1)).thenReturn(null);
+        when(testSubjectHelper.findLibrary("libName", 
nativeLibDir2)).thenReturn("firstFoundLibLocation");
+        when(testSubjectHelper.createTempCopy("libName", 
"firstFoundLibLocation")).thenReturn(libPath);
+
+        String expected = libPath.toFile().getAbsolutePath();
+
+        NativeLibFinder testSubject = createTestSubject();
+
+        // WHEN
+        String actual = testSubject.findLibrary("libName");
+
+        // THEN
+        assertEquals(expected, actual);
+        verify(testSubjectHelper).findLibrary("libName", nativeLibDir1);
+        verify(testSubjectHelper).findLibrary("libName", nativeLibDir2);
+        verify(testSubjectHelper).createTempCopy("libName", 
"firstFoundLibLocation");
+        verifyNoMoreInteractions(testSubjectHelper);
+    }
+
+    @Test
+    public void testFindLibraryShouldReturnCachedLibLocation() throws 
Exception {
+        // GIVEN
+        File nativeLibDir = mock(File.class);
+
+        nativeLibDirs = Arrays.asList(nativeLibDir);
+
+        Path cachedLibPath = createTempFile("cached", 
"mocked").toAbsolutePath();
+        nativeLibNameToPath = new HashMap<String, Path>() {{
 
 Review comment:
   Should avoid creating new subclass of HashMap and instead use 
`Collections.singletonMap`

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to