ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r1468612 - in /ace/trunk/org.apache.ace.client.repository: src/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperImpl.java test/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperTest.java
Date Tue, 16 Apr 2013 21:13:41 GMT
Author: bramk
Date: Tue Apr 16 21:13:41 2013
New Revision: 1468612

URL: http://svn.apache.org/r1468612
Log:
Added support for bundle manifest header localization and extended the tests

Modified:
    ace/trunk/org.apache.ace.client.repository/src/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperImpl.java
    ace/trunk/org.apache.ace.client.repository/test/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperTest.java

Modified: ace/trunk/org.apache.ace.client.repository/src/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.client.repository/src/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperImpl.java?rev=1468612&r1=1468611&r2=1468612&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.client.repository/src/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperImpl.java
(original)
+++ ace/trunk/org.apache.ace.client.repository/src/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperImpl.java
Tue Apr 16 21:13:41 2013
@@ -18,11 +18,20 @@
  */
 package org.apache.ace.client.repository.helper.bundle.impl;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
 import java.util.Map;
+import java.util.Properties;
 import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
 import java.util.jar.JarInputStream;
 import java.util.jar.Manifest;
 
@@ -33,14 +42,22 @@ import org.apache.ace.client.repository.
 import org.apache.ace.client.repository.helper.ArtifactResource;
 import org.apache.ace.client.repository.helper.bundle.BundleHelper;
 import org.apache.ace.client.repository.object.ArtifactObject;
+import org.osgi.framework.Constants;
 import org.osgi.framework.Version;
 
 /**
  * BundleHelperImpl provides the Artifact Repository with Helper and Recognizer services.
  */
 public class BundleHelperImpl implements ArtifactRecognizer, BundleHelper {
+
+    // manifest headers that will be extracted as metadata
+    private static final String[] MANIFEST_HEADERS = new String[] { KEY_NAME, KEY_SYMBOLICNAME,
KEY_VERSION, KEY_VENDOR, KEY_RESOURCE_PROCESSOR_PID };
+
+    // supported locales for manifest header localization in order of interest
+    private static final Locale[] MANIFEST_LOCALIZATION_LOCALES = new Locale[] { Locale.US,
Locale.ENGLISH, new Locale("nl") };
+
     /** A custom <code>Comparator</code>, used to sort bundles in increasing
version */
-    private static final Comparator <ArtifactObject> BUNDLE_COMPARATOR = new Comparator<ArtifactObject>()
{
+    private static final Comparator<ArtifactObject> BUNDLE_COMPARATOR = new Comparator<ArtifactObject>()
{
         public int compare(ArtifactObject left, ArtifactObject right) {
             Version vLeft = new Version(left.getAttribute(BundleHelper.KEY_VERSION));
             Version vRight = new Version(right.getAttribute(BundleHelper.KEY_VERSION));
@@ -60,8 +77,8 @@ public class BundleHelperImpl implements
 
     public <TYPE extends ArtifactObject> String getAssociationFilter(TYPE obj, Map<String,
String> properties) {
         /*
-         * Creates an endpoint filter for an association. If there is a KEY_ASSOCIATION_VERSIONSTATEMENT,
a filter
-         * will be created that matches exactly the given range.
+         * Creates an endpoint filter for an association. If there is a KEY_ASSOCIATION_VERSIONSTATEMENT,
a filter will
+         * be created that matches exactly the given range.
          */
         if ((properties != null) && properties.containsKey(KEY_ASSOCIATION_VERSIONSTATEMENT))
{
             String versions = properties.get(KEY_ASSOCIATION_VERSIONSTATEMENT);
@@ -103,8 +120,10 @@ public class BundleHelperImpl implements
     }
 
     public <TYPE extends ArtifactObject> int getCardinality(TYPE obj, Map<String,
String> properties) {
-        /* Normally, all objects that match the filter given by the previous version should
be part of the
-         * association. However, when a version statement has been given, only one should
be used. */
+        /*
+         * Normally, all objects that match the filter given by the previous version should
be part of the association.
+         * However, when a version statement has been given, only one should be used.
+         */
         if ((properties != null) && properties.containsKey(BundleHelper.KEY_ASSOCIATION_VERSIONSTATEMENT))
{
             return 1;
         }
@@ -122,8 +141,7 @@ public class BundleHelperImpl implements
     }
 
     /**
-     * For the filter to work correctly, we need to make sure the version statement is an
-     * OSGi version.
+     * For the filter to work correctly, we need to make sure the version statement is an
OSGi version.
      */
     private static Map<String, String> normalizeVersion(Map<String, String> input)
{
         String version = input.get(KEY_VERSION);
@@ -144,11 +162,11 @@ public class BundleHelperImpl implements
      * From BundleHelper
      */
     public String[] getDefiningKeys() {
-        return new String[] {KEY_SYMBOLICNAME, KEY_VERSION};
+        return new String[] { KEY_SYMBOLICNAME, KEY_VERSION };
     }
 
     public String[] getMandatoryAttributes() {
-        return new String[] {KEY_SYMBOLICNAME};
+        return new String[] { KEY_SYMBOLICNAME };
     }
 
     public String getResourceProcessorPIDs(ArtifactObject object) {
@@ -195,58 +213,32 @@ public class BundleHelperImpl implements
     }
 
     public Map<String, String> extractMetaData(ArtifactResource artifact) throws IllegalArgumentException
{
-        /*
-         * Opens the URL as a Jar input stream, gets the manifest, and extracts headers from
there.
-         */
-        JarInputStream jis = null;
-        try {
-            jis = new JarInputStream(artifact.openStream());
 
-            Attributes manifestAttributes = jis.getManifest().getMainAttributes();
-            Map<String, String> result = new HashMap<String, String>();
-
-            for (String key : new String[] {KEY_NAME, KEY_SYMBOLICNAME, KEY_VERSION, KEY_VENDOR,
KEY_RESOURCE_PROCESSOR_PID}) {
-                String value = manifestAttributes.getValue(key);
-                if (value != null) {
-                    result.put(key, value);
-                }
-            }
+        try {
+            Map<String, String> metadata = extractLocalizedHeaders(artifact);
 
-            if (result.get(KEY_VERSION) == null) {
-                result.put(KEY_VERSION, "0.0.0");
+            if (metadata.get(KEY_VERSION) == null) {
+                metadata.put(KEY_VERSION, "0.0.0");
             }
-
-            result.put(ArtifactHelper.KEY_MIMETYPE, MIMETYPE);
-            result.put(ArtifactObject.KEY_PROCESSOR_PID, "");
-            String name = manifestAttributes.getValue(KEY_NAME);
-            String version = manifestAttributes.getValue(KEY_VERSION);
+            metadata.put(ArtifactHelper.KEY_MIMETYPE, MIMETYPE);
+            metadata.put(ArtifactObject.KEY_PROCESSOR_PID, "");
+            String name = metadata.get(KEY_NAME);
+            String version = metadata.get(KEY_VERSION);
             if (name == null) {
-                name = manifestAttributes.getValue(KEY_SYMBOLICNAME);
+                name = metadata.get(KEY_SYMBOLICNAME);
             }
-            result.put(ArtifactObject.KEY_ARTIFACT_NAME, name + (version == null ? "" : "-"
+ version));
-
-            return result;
+            metadata.put(ArtifactObject.KEY_ARTIFACT_NAME, name + "-" + version);
+            return metadata;
         }
         catch (Exception e) {
             throw new IllegalArgumentException("Error extracting metadata from artifact.",
e);
         }
-        finally {
-            try {
-                if (jis != null) {
-                    jis.close();
-                }
-            }
-            catch (IOException e) {
-                // Too bad.
-            }
-        }
     }
 
     public String recognize(ArtifactResource artifact) {
         /*
-         * Tries to find out whether this artifact is a bundle by (a) trying to open it as
a
-         * jar, (b) trying to extract the manifest, and (c) checking whether that manifest
-         * contains a Bundle-SymbolicName header.
+         * Tries to find out whether this artifact is a bundle by (a) trying to open it as
a jar, (b) trying to extract
+         * the manifest, and (c) checking whether that manifest contains a Bundle-SymbolicName
header.
          */
         JarInputStream jis = null;
         try {
@@ -278,8 +270,120 @@ public class BundleHelperImpl implements
     public ArtifactPreprocessor getPreprocessor() {
         return null;
     }
-    
+
     public String getExtension(ArtifactResource artifact) {
         return ".jar";
     }
-}
\ No newline at end of file
+
+    /**
+     * Extracts the {@link MANIFEST_HEADERS} from the manifest with support for localization
(see OSGi core 3.11.2).
+     * 
+     * @param artifact
+     *            the artifact
+     * @return a map of localized headers
+     * @throws IOException
+     *             if reading from the stream fails
+     */
+    private Map<String, String> extractLocalizedHeaders(ArtifactResource artifact)
throws IOException {
+
+        Map<String, String> localizedHeaders = new HashMap<String, String>();
+        JarInputStream jarInputStream = null;
+        try {
+            jarInputStream = new JarInputStream(artifact.openStream());
+
+            Manifest manifest = jarInputStream.getManifest();
+            if (manifest == null) {
+                throw new IOException("Failed to extract bundle manifest");
+            }
+            Attributes attributes = manifest.getMainAttributes();
+            Properties localizationProperties = null;
+
+            for (String key : MANIFEST_HEADERS) {
+                String value = attributes.getValue(key);
+                if (value == null) {
+                    continue;
+                }
+                if (value.startsWith("%")) {
+                    if (localizationProperties == null) {
+                        // lazily instantiated because this is expensive and not widely used
+                        localizationProperties = loadLocalizationProperties(jarInputStream,
manifest);
+                    }
+                    value = localizationProperties.getProperty(value.substring(1), value);
+                }
+                localizedHeaders.put(key, value);
+            }
+            return localizedHeaders;
+        }
+        finally {
+            try {
+                if (jarInputStream != null) {
+                    jarInputStream.close();
+                }
+            }
+            catch (IOException e) {
+            }
+        }
+    }
+
+    /**
+     * Searches a JarInputStream for localization entries considering {@link #MANIFEST_LOCALIZATION_LOCALES}
and the
+     * default.
+     * 
+     * @param jarInputStream
+     *            the input, will not be closed
+     * @param manifest
+     *            the manifest
+     * @return the matching localized values
+     * @throws IOException
+     *             if reading from the stream fails
+     */
+    private Properties loadLocalizationProperties(JarInputStream jarInputStream, Manifest
manifest) throws IOException {
+
+        Attributes attributes = manifest.getMainAttributes();
+
+        String localizationBaseName = attributes.getValue(Constants.BUNDLE_LOCALIZATION);
+        if (localizationBaseName == null) {
+            localizationBaseName = Constants.BUNDLE_LOCALIZATION_DEFAULT_BASENAME;
+        }
+
+        List<String> localeEntryNames = new ArrayList<String>();
+        for (Locale locale : MANIFEST_LOCALIZATION_LOCALES) {
+            localeEntryNames.add(localizationBaseName + "_" + locale.toString() + ".properties");
+        }
+        localeEntryNames.add(localizationBaseName + ".properties");
+
+        Properties localeProperties = new Properties();
+        int currentLocaleEntryNameIndex = -1;
+
+        JarEntry entry;
+        while ((entry = jarInputStream.getNextJarEntry()) != null) {
+
+            String entryName = entry.getName();
+            int localeEntryNameIndex = localeEntryNames.indexOf(entryName);
+            if (localeEntryNameIndex == -1 || (currentLocaleEntryNameIndex > -1 &&
localeEntryNameIndex > currentLocaleEntryNameIndex)) {
+                // not a locale resource or we have already found a better matching one
+                continue;
+            }
+
+            int b;
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            while ((b = jarInputStream.read()) != -1) {
+                baos.write(b);
+            }
+            byte[] bytes = baos.toByteArray();
+            baos.close();
+
+            Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes));
+            localeProperties.clear();
+            localeProperties.load(reader);
+            reader.close();
+
+            currentLocaleEntryNameIndex = localeEntryNameIndex;
+            if (localeEntryNameIndex == 0) {
+                // found best match!
+                break;
+            }
+        }
+        return localeProperties;
+    }
+}

Modified: ace/trunk/org.apache.ace.client.repository/test/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.client.repository/test/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperTest.java?rev=1468612&r1=1468611&r2=1468612&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.client.repository/test/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperTest.java
(original)
+++ ace/trunk/org.apache.ace.client.repository/test/org/apache/ace/client/repository/helper/bundle/impl/BundleHelperTest.java
Tue Apr 16 21:13:41 2013
@@ -1,14 +1,31 @@
+/*
+ * 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.ace.client.repository.helper.bundle.impl;
 
 import static org.apache.ace.test.utils.TestUtils.UNIT;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.Locale;
 import java.util.Map;
 import java.util.jar.Attributes;
 import java.util.jar.JarOutputStream;
@@ -17,89 +34,203 @@ import java.util.zip.ZipEntry;
 
 import org.apache.ace.client.repository.helper.ArtifactResource;
 import org.apache.ace.client.repository.object.ArtifactObject;
+import org.osgi.framework.Constants;
 import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Test;
 
 public class BundleHelperTest {
     private BundleHelperImpl m_helper;
-    
+
     @BeforeTest
     public void setUp() throws Exception {
-    	m_helper = new BundleHelperImpl();
+        m_helper = new BundleHelperImpl();
     }
 
     @Test(groups = { UNIT })
     public void testMimetype() {
-    	assert m_helper.canHandle("application/vnd.osgi.bundle") : "Should be able to handle
bundle mimetype.";
-    	assert !m_helper.canHandle("somecrazy/mimetype") : "Should not be able to handle crazy
mimetype.";
+        assert m_helper.canHandle("application/vnd.osgi.bundle") : "Should be able to handle
bundle mimetype.";
+        assert !m_helper.canHandle("somecrazy/mimetype") : "Should not be able to handle
crazy mimetype.";
     }
-    
+
     @Test(groups = { UNIT })
     public void testManifestExtraction() {
-    	ArtifactResource artifact = new ArtifactResource() {
-			
-			@Override
-			public InputStream openStream() throws IOException {
-				ByteArrayOutputStream baos = new ByteArrayOutputStream();
-				Manifest manifest = new Manifest();
-				Attributes attrs = manifest.getMainAttributes();
-		        attrs.putValue("Manifest-Version", "1");
-				attrs.putValue("Bundle-SymbolicName", "mybundle");
-				attrs.putValue("Bundle-Version", "1.0.0");
-				attrs.putValue("Bundle-Name", "My Cool Bundle");
-				JarOutputStream jos = new JarOutputStream(baos, manifest);
-				jos.close();
-				return new ByteArrayInputStream(baos.toByteArray());
-			}
-			
-			@Override
-			public URL getURL() {
-				return null;
-			}
-		};
-    	Map<String, String> map = m_helper.extractMetaData(artifact);
-    	assert "mybundle".equals(map.get("Bundle-SymbolicName")) : "Symbolic name should have
been 'mybundle', was " + map.get("Bundle-SymbolicName");
-    	assert "1.0.0".equals(map.get("Bundle-Version")) : "Version should have been '1.0.0',
was " + map.get("Bundle-Version");
-    	assert "My Cool Bundle-1.0.0".equals(map.get(ArtifactObject.KEY_ARTIFACT_NAME)) : "Artifact
name should have been 'My Cool Bundle-1.0.0', was " + map.get(ArtifactObject.KEY_ARTIFACT_NAME);
+        ArtifactResource artifact = new ArtifactResource() {
+
+            @Override
+            public InputStream openStream() throws IOException {
+                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                Manifest manifest = new Manifest();
+                Attributes attrs = manifest.getMainAttributes();
+                attrs.putValue("Manifest-Version", "1");
+                attrs.putValue("Bundle-SymbolicName", "mybundle");
+                attrs.putValue("Bundle-Version", "1.0.0");
+                attrs.putValue("Bundle-Name", "My Cool Bundle");
+                JarOutputStream jos = new JarOutputStream(baos, manifest);
+                jos.close();
+                return new ByteArrayInputStream(baos.toByteArray());
+            }
+
+            @Override
+            public URL getURL() {
+                return null;
+            }
+        };
+        Map<String, String> map = m_helper.extractMetaData(artifact);
+        assert "mybundle".equals(map.get("Bundle-SymbolicName")) : "Symbolic name should
have been 'mybundle', was " + map.get("Bundle-SymbolicName");
+        assert "1.0.0".equals(map.get("Bundle-Version")) : "Version should have been '1.0.0',
was " + map.get("Bundle-Version");
+        assert "My Cool Bundle-1.0.0".equals(map.get(ArtifactObject.KEY_ARTIFACT_NAME)) :
"Artifact name should have been 'My Cool Bundle-1.0.0', was " + map.get(ArtifactObject.KEY_ARTIFACT_NAME);
     }
-    
+
     @Test(groups = { UNIT })
     public void testLocalizedManifestExtraction() {
-    	ArtifactResource artifact = new ArtifactResource() {
-			
-			@Override
-			public InputStream openStream() throws IOException {
-				ByteArrayOutputStream baos = new ByteArrayOutputStream();
-				Manifest manifest = new Manifest();
-				Attributes attrs = manifest.getMainAttributes();
-		        attrs.putValue("Manifest-Version", "1");
-				attrs.putValue("Bundle-SymbolicName", "mybundle");
-				attrs.putValue("Bundle-Version", "1.0.0");
-				attrs.putValue("Bundle-Name", "%bundleName");
-				attrs.putValue("Bundle-Localization", "locale");
-				JarOutputStream jos = new JarOutputStream(baos, manifest);
-				jos.putNextEntry(new ZipEntry("locale.properties"));
-				String content = "bundleName=The Coolest Bundle";
-				jos.write(content.getBytes(), 0, content.getBytes().length);
-                jos.closeEntry();
-				jos.close();
-
-//				// if you want to validate that the bundle is okay
-//				FileOutputStream fos = new FileOutputStream(new File("/Users/marceloffermans/unittest.jar"));
-//				fos.write(baos.toByteArray(), 0, baos.size());
-//				fos.close();
-				
-				return new ByteArrayInputStream(baos.toByteArray());
-			}
-			
-			@Override
-			public URL getURL() {
-				return null;
-			}
-		};
-    	Map<String, String> map = m_helper.extractMetaData(artifact);
-    	assert "mybundle".equals(map.get("Bundle-SymbolicName")) : "Symbolic name should have
been 'mybundle', was " + map.get("Bundle-SymbolicName");
-    	assert "1.0.0".equals(map.get("Bundle-Version")) : "Version should have been '1.0.0',
was " + map.get("Bundle-Version");
-    	assert "The Coolest Bundle-1.0.0".equals(map.get(ArtifactObject.KEY_ARTIFACT_NAME))
: "Artifact name should have been 'The Coolest Bundle-1.0.0', was " + map.get(ArtifactObject.KEY_ARTIFACT_NAME);
+        ArtifactResource artifact = new ArtifactResource() {
+
+            @Override
+            public InputStream openStream() throws IOException {
+                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                Manifest manifest = new Manifest();
+                Attributes attrs = manifest.getMainAttributes();
+                attrs.putValue("Manifest-Version", "1");
+                attrs.putValue("Bundle-SymbolicName", "mybundle");
+                attrs.putValue("Bundle-Version", "1.0.0");
+                attrs.putValue("Bundle-Name", "%bundleName");
+                attrs.putValue("Bundle-Localization", "locale");
+                JarOutputStream jos = new JarOutputStream(baos, manifest);
+                jos.putNextEntry(new ZipEntry("locale.properties"));
+                String content = "bundleName=The Coolest Bundle";
+                jos.write(content.getBytes(), 0, content.getBytes().length);
+                jos.closeEntry();
+                jos.close();
+
+                // if you want to validate that the bundle is okay
+                // FileOutputStream fos = new FileOutputStream(new File("/Users/marceloffermans/unittest.jar"));
+                // fos.write(baos.toByteArray(), 0, baos.size());
+                // fos.close();
+
+                return new ByteArrayInputStream(baos.toByteArray());
+            }
+
+            @Override
+            public URL getURL() {
+                return null;
+            }
+        };
+        Map<String, String> map = m_helper.extractMetaData(artifact);
+        assert "mybundle".equals(map.get("Bundle-SymbolicName")) : "Symbolic name should
have been 'mybundle', was " + map.get("Bundle-SymbolicName");
+        assert "1.0.0".equals(map.get("Bundle-Version")) : "Version should have been '1.0.0',
was " + map.get("Bundle-Version");
+        assert "The Coolest Bundle-1.0.0".equals(map.get(ArtifactObject.KEY_ARTIFACT_NAME))
: "Artifact name should have been 'The Coolest Bundle-1.0.0', was " + map.get(ArtifactObject.KEY_ARTIFACT_NAME);
+    }
+
+    @Test(groups = { UNIT })
+    public void testLocalizedManifestExtractionWithDefaultBase() {
+
+        // note that we do not set the Bundle-Localization header
+
+        ArtifactResource artifact = new ArtifactResource() {
+
+            @Override
+            public InputStream openStream() throws IOException {
+                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                Manifest manifest = new Manifest();
+                Attributes attrs = manifest.getMainAttributes();
+                attrs.putValue("Manifest-Version", "1");
+                attrs.putValue("Bundle-SymbolicName", "mybundle");
+                attrs.putValue("Bundle-Version", "1.0.0");
+                attrs.putValue("Bundle-Name", "%bundleName");
+                JarOutputStream jos = new JarOutputStream(baos, manifest);
+                jos.putNextEntry(new ZipEntry(Constants.BUNDLE_LOCALIZATION_DEFAULT_BASENAME
+ ".properties"));
+                String content = "bundleName=The Coolest Bundle";
+                jos.write(content.getBytes(), 0, content.getBytes().length);
+                jos.closeEntry();
+                jos.close();
+                return new ByteArrayInputStream(baos.toByteArray());
+            }
+
+            @Override
+            public URL getURL() {
+                return null;
+            }
+        };
+        Map<String, String> map = m_helper.extractMetaData(artifact);
+        assert "mybundle".equals(map.get("Bundle-SymbolicName")) : "Symbolic name should
have been 'mybundle', was " + map.get("Bundle-SymbolicName");
+        assert "1.0.0".equals(map.get("Bundle-Version")) : "Version should have been '1.0.0',
was " + map.get("Bundle-Version");
+        assert "The Coolest Bundle-1.0.0".equals(map.get(ArtifactObject.KEY_ARTIFACT_NAME))
: "Artifact name should have been 'The Coolest Bundle-1.0.0', was " + map.get(ArtifactObject.KEY_ARTIFACT_NAME);
+    }
+
+    @Test(groups = { UNIT })
+    public void testLocalizedManifestExtractionWithLocale() {
+        ArtifactResource artifact = new ArtifactResource() {
+
+            @Override
+            public InputStream openStream() throws IOException {
+                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                Manifest manifest = new Manifest();
+                Attributes attrs = manifest.getMainAttributes();
+                attrs.putValue("Manifest-Version", "1");
+                attrs.putValue("Bundle-SymbolicName", "mybundle");
+                attrs.putValue("Bundle-Version", "1.0.0");
+                attrs.putValue("Bundle-Name", "%bundleName");
+                attrs.putValue("Bundle-Localization", "locale");
+                JarOutputStream jos = new JarOutputStream(baos, manifest);
+                jos.putNextEntry(new ZipEntry("locale_nl.properties"));
+                String content = "bundleName=De koelste Bundle";
+                jos.write(content.getBytes(), 0, content.getBytes().length);
+                jos.closeEntry();
+                jos.close();
+                return new ByteArrayInputStream(baos.toByteArray());
+            }
+
+            @Override
+            public URL getURL() {
+                return null;
+            }
+        };
+        Map<String, String> map = m_helper.extractMetaData(artifact);
+        assert "mybundle".equals(map.get("Bundle-SymbolicName")) : "Symbolic name should
have been 'mybundle', was " + map.get("Bundle-SymbolicName");
+        assert "1.0.0".equals(map.get("Bundle-Version")) : "Version should have been '1.0.0',
was " + map.get("Bundle-Version");
+        assert "De koelste Bundle-1.0.0".equals(map.get(ArtifactObject.KEY_ARTIFACT_NAME))
: "Artifact name should have been 'The Coolest Bundle-1.0.0', was " + map.get(ArtifactObject.KEY_ARTIFACT_NAME);
+    }
+
+    @Test(groups = { UNIT })
+    public void testLocalizedManifestExtractionWithLocaleOverrule() {
+        ArtifactResource artifact = new ArtifactResource() {
+
+            @Override
+            public InputStream openStream() throws IOException {
+                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                Manifest manifest = new Manifest();
+                Attributes attrs = manifest.getMainAttributes();
+                attrs.putValue("Manifest-Version", "1");
+                attrs.putValue("Bundle-SymbolicName", "mybundle");
+                attrs.putValue("Bundle-Version", "1.0.0");
+                attrs.putValue("Bundle-Name", "%bundleName");
+                attrs.putValue("Bundle-Localization", "locale");
+                JarOutputStream jos = new JarOutputStream(baos, manifest);
+                jos.putNextEntry(new ZipEntry("locale.properties"));
+                String content = "bundleName=De koelste Bundle";
+                jos.write(content.getBytes(), 0, content.getBytes().length);
+                jos.closeEntry();
+                jos.putNextEntry(new ZipEntry("locale_" + Locale.ENGLISH + ".properties"));
+                String contentEN = "bundleName=A damn fine Bundle";
+                jos.write(contentEN.getBytes(), 0, contentEN.getBytes().length);
+                jos.closeEntry();
+                jos.putNextEntry(new ZipEntry("locale_" + Locale.US + ".properties"));
+                String contentUS = "bundleName=The Coolest Bundle";
+                jos.write(contentUS.getBytes(), 0, contentUS.getBytes().length);
+                jos.closeEntry();
+
+                jos.close();
+                return new ByteArrayInputStream(baos.toByteArray());
+            }
+
+            @Override
+            public URL getURL() {
+                return null;
+            }
+        };
+        Map<String, String> map = m_helper.extractMetaData(artifact);
+        assert "mybundle".equals(map.get("Bundle-SymbolicName")) : "Symbolic name should
have been 'mybundle', was " + map.get("Bundle-SymbolicName");
+        assert "1.0.0".equals(map.get("Bundle-Version")) : "Version should have been '1.0.0',
was " + map.get("Bundle-Version");
+        assert "The Coolest Bundle-1.0.0".equals(map.get(ArtifactObject.KEY_ARTIFACT_NAME))
: "Artifact name should have been 'The Coolest Bundle-1.0.0', was " + map.get(ArtifactObject.KEY_ARTIFACT_NAME);
     }
 }



Mime
View raw message