/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.arquillian.processor;

import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Jar;

import java.io.ByteArrayOutputStream;

import java.util.jar.Manifest;

import org.jboss.arquillian.container.test.spi.client.deployment.ApplicationArchiveProcessor;
import org.jboss.arquillian.test.spi.TestClass;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;

/**
 * This processor analyzes the jar after adding the test to fix the imports
 * when needed.
 *
 * @author Carlos Sierra Andrés
 */
public class BndApplicationArchiveProcessor implements ApplicationArchiveProcessor {

	public static final String MANIFEST_PATH = "META-INF/MANIFEST.MF";

	@Override
	public void process(Archive<?> applicationArchive, TestClass testClass) {

		JavaArchive javaArchive = applicationArchive.as(JavaArchive.class);

		javaArchive.addClass(testClass.getJavaClass());

		Analyzer analyzer = new Analyzer();

		try {
			Manifest manifest = new Manifest(applicationArchive.get(MANIFEST_PATH).getAsset().openStream());

			String exportPackage = manifest.getMainAttributes().getValue("Export-Package");

			if (exportPackage == null || exportPackage.isEmpty()) {
				exportPackage = testClass.getJavaClass().getPackage().getName();
			}
			else {
				exportPackage += "," + testClass.getJavaClass().getPackage().getName();
			}

			manifest.getMainAttributes().putValue("Export-Package", exportPackage);

			analyzer.mergeManifest(manifest);

			ZipExporter zipExporter = applicationArchive.as(ZipExporter.class);

			Jar jar = new Jar(applicationArchive.getName(), zipExporter.exportAsInputStream());

			analyzer.setJar(jar);

			manifest = analyzer.calcManifest();

			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			manifest.write(baos);

			ByteArrayAsset byteArrayAsset = new ByteArrayAsset(
				baos.toByteArray());

			replaceManifest(applicationArchive, byteArrayAsset);
		}
		catch (Exception e) {
			throw new IllegalStateException(e);
		}
		finally {
			analyzer.close();
		}
	}

	private void replaceManifest(
		Archive<?> archive, ByteArrayAsset byteArrayAsset) {

		archive.delete(MANIFEST_PATH);

		archive.add(byteArrayAsset, MANIFEST_PATH);
	}

}
@@ -943,16 +943,54 @@
 rchive;%0A
+import org.jboss.shrinkwrap.api.Node;%0A
 import o
@@ -1693,41 +1693,20 @@
 %0A%09%09%09
-Manifest manifest = new Manifest(
+Node node = 
 appl
@@ -1742,32 +1742,343 @@
 ATH)
-.getAsset().openStream()
+;%0A%0A%09%09%09if (node != null) %7B%0A%09%09%09%09analyzer.mergeManifest(new Manifest(node.getAsset().openStream()));%0A%09%09%09%7D%0A%0A%09%09%09ZipExporter zipExporter = applicationArchive.as(ZipExporter.class);%0A%0A%09%09%09Jar jar = new Jar(applicationArchive.getName(), zipExporter.exportAsInputStream());%0A%0A%09%09%09analyzer.setJar(jar);%0A%0A%09%09%09Manifest manifest = analyzer.calcManifest(
 );%0A%0A
@@ -2466,272 +2466,8 @@
 );%0A%0A
-%09%09%09analyzer.mergeManifest(manifest);%0A%0A%09%09%09ZipExporter zipExporter = applicationArchive.as(ZipExporter.class);%0A%0A%09%09%09Jar jar = new Jar(applicationArchive.getName(), zipExporter.exportAsInputStream());%0A%0A%09%09%09analyzer.setJar(jar);%0A%0A%09%09%09manifest = analyzer.calcManifest();%0A%0A
 %09%09%09B

Reorganize code so we can handle a case in which no Manifest is provided.