View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.commons.crypto;
20  
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.File;
26  
27  import org.junit.Assume;
28  import org.junit.Ignore;
29  import org.junit.Test;
30  
31  public class NativeCodeLoaderTest {
32  
33      @Test
34      public void test() {
35          if (NativeCodeLoader.isNativeCodeLoaded()) {
36              // TODO display versions once available
37              System.out.println("** INFO: Native (JNI) code loaded successfully");
38          } else {
39              System.out.println("** WARN: Native (JNI) code was not loaded: " 
40                  + NativeCodeLoader.getLoadingError());
41          }
42      }
43  
44      @Test
45      public void testNativePresent() {
46          Assume.assumeTrue(NativeCodeLoader.isNativeCodeLoaded());
47          assertNull(NativeCodeLoader.getLoadingError());
48      }
49  
50      @Test
51      public void testNativeNotPresent() {
52          Assume.assumeTrue(!NativeCodeLoader.isNativeCodeLoaded());
53          assertNotNull(NativeCodeLoader.getLoadingError());
54      }
55  
56      @Test
57      public void testCanLoadIfPresent() {
58          Assume.assumeTrue(NativeCodeLoader.isNativeCodeLoaded());
59          // This will try to reload the library, so should work
60          assertNull(NativeCodeLoader.loadLibrary());
61      }
62  
63      @Test
64      @Ignore("Seems to cause issues with other tests on Linux; disable for now")
65      public void testUnSuccessfulLoad() throws Exception {
66          final String nameKey = System.getProperty(Crypto.LIB_NAME_KEY);
67          final String pathKey = System.getProperty(Crypto.LIB_PATH_KEY);
68          // An empty file should cause UnsatisfiedLinkError
69          File empty = File.createTempFile("NativeCodeLoaderTest", "tmp");
70          try {
71              System.setProperty(Crypto.LIB_PATH_KEY, empty.getParent());
72              System.setProperty(Crypto.LIB_NAME_KEY, empty.getName());
73              final Throwable result = NativeCodeLoader.loadLibrary();
74              assertNotNull(result);
75              assertTrue(result instanceof UnsatisfiedLinkError);
76          } finally {
77              empty.delete();
78              if (nameKey != null) {
79                  System.setProperty(Crypto.LIB_NAME_KEY, nameKey);
80              }
81              if (pathKey != null) {
82                  System.setProperty(Crypto.LIB_PATH_KEY, pathKey);
83              }
84          }
85      }
86  }