felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clem...@apache.org
Subject svn commit: r1523619 - in /felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src: main/java/org/apache/felix/ipojo/runtime/core/components/nativ/ main/resources/libs/ main/resources/libs/linux32/ main/resources/libs/linux...
Date Mon, 16 Sep 2013 13:03:55 GMT
Author: clement
Date: Mon Sep 16 13:03:54 2013
New Revision: 1523619

URL: http://svn.apache.org/r1523619
Log:
Check that native methods are not manipulated.

Added:
    felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/nativ/
    felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/nativ/NativeComponent.java
    felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/libs/
    felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/libs/linux32/
    felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/libs/linux64/
    felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/libs/mac/
    felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/libs/mac/libfoo.jnilib
  (with props)
    felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestNativeMethod.java

Added: felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/nativ/NativeComponent.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/nativ/NativeComponent.java?rev=1523619&view=auto
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/nativ/NativeComponent.java
(added)
+++ felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/nativ/NativeComponent.java
Mon Sep 16 13:03:54 2013
@@ -0,0 +1,28 @@
+package org.apache.felix.ipojo.runtime.core.components.nativ;
+
+import org.apache.felix.ipojo.annotations.Component;
+import org.apache.felix.ipojo.annotations.Provides;
+import org.apache.felix.ipojo.runtime.core.services.BazService;
+
+/**
+ * Component using a native method.
+ */
+@Component
+@Provides
+public class NativeComponent implements BazService {
+
+    static {
+        System.loadLibrary("foo");
+    }
+
+    /**
+     * Provided by the native library.
+     * @return <code>foo: Test program of JNI.</code>
+     */
+    public native String nativeFoo();
+
+    @Override
+    public String hello(String name) {
+        return nativeFoo();
+    }
+}

Added: felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/libs/mac/libfoo.jnilib
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/libs/mac/libfoo.jnilib?rev=1523619&view=auto
==============================================================================
Binary file - no diff available.

Propchange: felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/libs/mac/libfoo.jnilib
------------------------------------------------------------------------------
    svn:executable = *

Propchange: felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/libs/mac/libfoo.jnilib
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestNativeMethod.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestNativeMethod.java?rev=1523619&view=auto
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestNativeMethod.java
(added)
+++ felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestNativeMethod.java
Mon Sep 16 13:03:54 2013
@@ -0,0 +1,127 @@
+package org.apache.felix.ipojo.runtime.core;
+
+import aQute.lib.osgi.Constants;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.filefilter.TrueFileFilter;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.runtime.core.services.BazService;
+import org.junit.Test;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.tinybundles.core.TinyBundle;
+import org.ops4j.pax.tinybundles.core.TinyBundles;
+import org.ow2.chameleon.testing.helpers.BaseTest;
+import org.ow2.chameleon.testing.tinybundles.ipojo.IPOJOStrategy;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import static junit.framework.Assert.assertEquals;
+import static org.apache.commons.io.FileUtils.copyInputStreamToFile;
+import static org.ops4j.pax.exam.CoreOptions.bundle;
+
+/**
+ * Checks that native methods can still be used in components.
+ */
+public class TestNativeMethod extends BaseTest {
+
+    private static final String NATIVE_CLAUSE = "" +
+            //Mac
+            "libs/mac/libfoo.jnilib;osname=MacOSX;osname=MacOS;processor=x86;processor=x86_64;processor=PowerPC,"
+
+            // Linux 32 bits
+            "libs/linux64/libfoo.so;processor=x86_64;osname=Linux," +
+            // Linux 64 bits
+            "libs/linux32/libfoo.so;processor=x86;osname=Linux";
+
+    /**
+     * We don't deploy the test bundle, a specific one will be built.
+     */
+    @Override
+    public boolean deployTestBundle() {
+        return false;
+    }
+
+    @Override
+    protected Option[] getCustomOptions() {
+        return new Option[] {
+                buildBundleWithNativeLibraries()
+        };
+    }
+
+    @Test
+    public void testComponentWithNativeMethod() {
+        ComponentInstance ci = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.components"
+
+                ".nativ.NativeComponent");
+
+        BazService baz = osgiHelper.getServiceObject(BazService.class, "(instance.name="
+ ci.getInstanceName() +")");
+        assertEquals("foo: Test program of JNI.", baz.hello(""));
+    }
+
+
+    public static Option buildBundleWithNativeLibraries() {
+        File out = new File("target/tested/test-bundle-with-native.jar");
+        if (out.exists()) {
+            try {
+                return bundle(out.toURI().toURL().toExternalForm());
+            } catch (MalformedURLException e) {
+                // Ignore it.
+            }
+        }
+
+        TinyBundle tested = TinyBundles.bundle();
+
+        // We look inside target/classes to find the class and resources
+        File classes = new File("target/classes");
+        Collection<File> files = FileUtils.listFilesAndDirs(classes, TrueFileFilter.INSTANCE,
TrueFileFilter.INSTANCE);
+        List<String> exports = new ArrayList<String>();
+        for (File file : files) {
+            if (file.isDirectory()) {
+                // By convention we export of .services and .service package
+                if (file.getAbsolutePath().contains("/services")  || file.getAbsolutePath().contains("/service"))
{
+                    String path = file.getAbsolutePath().substring(classes.getAbsolutePath().length()
+1);
+                    String packageName = path.replace('/', '.');
+                    exports.add(packageName);
+                }
+            } else {
+                // We need to compute the path
+                String path = file.getAbsolutePath().substring(classes.getAbsolutePath().length()
+1);
+                try {
+                    tested.add(path, file.toURI().toURL());
+                } catch (MalformedURLException e) {
+                    // Ignore it.
+                }
+                System.out.println(file.getName() + " added to " + path);
+            }
+        }
+
+        String clause = "";
+        for (String export : exports) {
+            if (export.length() > 0) { export += ", "; }
+            clause += export;
+        }
+
+        System.out.println("Exported packages : " + clause);
+
+        InputStream inputStream = tested
+                .set(Constants.BUNDLE_SYMBOLICNAME, BaseTest.TEST_BUNDLE_SYMBOLIC_NAME)
+                .set(Constants.IMPORT_PACKAGE, "*")
+                .set(Constants.EXPORT_PACKAGE, clause)
+                .set(Constants.BUNDLE_NATIVECODE, NATIVE_CLAUSE)
+                .build(IPOJOStrategy.withiPOJO(new File("src/main/resources")));
+
+        try {
+            copyInputStreamToFile(inputStream, out);
+            return bundle(out.toURI().toURL().toExternalForm());
+        } catch (MalformedURLException e) {
+            throw new RuntimeException("Cannot compute the url of the manipulated bundle");
+        } catch (IOException e) {
+            throw new RuntimeException("Cannot write of the manipulated bundle");
+        }
+    }
+
+
+}



Mime
View raw message