Skip to content

Commit

Permalink
[liferay-labs#7] Fix ImportPackageManagerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
cgoncas committed Oct 24, 2016
1 parent 7975354 commit e6b7987
Showing 1 changed file with 145 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,39 @@

package org.arquillian.container.osgi.remote.deploy.processor.test;

import aQute.bnd.header.Attrs;
import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Descriptors;
import aQute.bnd.osgi.Packages;

import com.liferay.arquillian.container.osgi.remote.processor.service.ImportPackageManager;
import com.liferay.arquillian.container.osgi.remote.processor.service.ImportPackageManagerImpl;
import com.liferay.arquillian.container.osgi.remote.processor.service.ManifestManager;
import com.liferay.arquillian.container.osgi.remote.processor.service.ManifestManagerImpl;

import java.io.File;
import java.io.IOException;

import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;

import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import org.arquillian.container.osgi.remote.deploy.processor.test.mock.DummyInstanceProducerImpl;
import org.arquillian.container.osgi.remote.deploy.processor.test.util.ManifestUtil;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import org.osgi.framework.Constants;

/**
* @author Cristina González
*/
Expand All @@ -51,61 +59,161 @@ public void setUp() throws IllegalAccessException, NoSuchFieldException {

@Test
public void testCleanRepeatedImportsWithoutRepeatedImports()
throws IOException {
throws Exception {

//given:

JavaArchive javaArchive = createJavaArchive();

List<String> imports = new ArrayList<>();
imports.add("dummy.package");

ManifestUtil.createManifest(javaArchive, imports);

Manifest manifest = _manifestManager.getManifest(javaArchive);
ManifestUtil.createManifest(javaArchive);

List<Archive<?>> archives = new ArrayList<>();
archives.add(javaArchive);

//when:
Manifest actualManifest = _importPackageManager.cleanRepeatedImports(
manifest, archives);
Analyzer analyzer = new Analyzer();

try {
List<File> files = new ArrayList<>();

File archiveFile = getFileFromArchive(javaArchive);

files.add(archiveFile);

analyzer.setJar(archiveFile);

Properties properties = new Properties();

properties.setProperty(Constants.IMPORT_PACKAGE, "*,dummy.package");

analyzer.setProperties(properties);

for (Archive<?> classPathArchive : archives) {
File classPathFile = getFileFromArchive(classPathArchive);

analyzer.addClasspath(classPathFile);

files.add(archiveFile);
}

Attributes mainAttributes = actualManifest.getMainAttributes();
analyzer.analyze();

//then:
Assert.assertEquals(
"dummy.package", mainAttributes.get(
new Attributes.Name("Import-Package")));
//when:
_importPackageManager.cleanImports(
analyzer.getImports(), analyzer.getClasspathExports());

//then:
Packages importsPackages = analyzer.getImports();

int cont = 0;

for (
Map.Entry<Descriptors.PackageRef, Attrs> packageRefAttrsEntry :
importsPackages.entrySet()) {

Descriptors.PackageRef packageRef =
packageRefAttrsEntry.getKey();

String path = packageRef.getPath();

if (path.equals("dummy/package")) {
cont++;
}
}

Assert.assertEquals(1, cont);

for (File file : files) {
Files.deleteIfExists(Paths.get(file.toURI()));
}
}
finally {
analyzer.close();
}
}

@Test
public void testCleanRepeatedImportsWithRepeatedImports()
throws IOException {

public void testCleanRepeatedImportsWithRepeatedImports() throws Exception {
//given:

JavaArchive javaArchive = createJavaArchive();

List<String> imports = new ArrayList<>();
imports.add(ImportPackageManagerTest.class.getPackage().getName());

ManifestUtil.createManifest(javaArchive, imports);

Manifest manifest = _manifestManager.getManifest(javaArchive);
ManifestUtil.createManifest(javaArchive);

List<Archive<?>> archives = new ArrayList<>();
archives.add(javaArchive);

//when:
Manifest actualManifest = _importPackageManager.cleanRepeatedImports(
manifest, archives);
Analyzer analyzer = new Analyzer();

try {
List<File> files = new ArrayList<>();

File archiveFile = getFileFromArchive(javaArchive);

files.add(archiveFile);

analyzer.setJar(archiveFile);

Properties properties = new Properties();

properties.setProperty(
Constants.IMPORT_PACKAGE,
"*,dummy.package,dummy.package");

analyzer.setProperties(properties);

for (Archive<?> classPathArchive : archives) {
File classPathFile = getFileFromArchive(classPathArchive);

analyzer.addClasspath(classPathFile);

files.add(archiveFile);
}

analyzer.analyze();

//when:
_importPackageManager.cleanImports(
analyzer.getImports(), analyzer.getClasspathExports());

//then:
Packages importsPackages = analyzer.getImports();

int cont = 0;

for (
Map.Entry<Descriptors.PackageRef, Attrs> packageRefAttrsEntry :
importsPackages.entrySet()) {

Attributes mainAttributes = actualManifest.getMainAttributes();
Descriptors.PackageRef packageRef =
packageRefAttrsEntry.getKey();

//then:
Assert.assertEquals(
"", mainAttributes.get(new Attributes.Name("Import-Package")));
String path = packageRef.getPath();

if (path.equals("dummy/package")) {
cont++;
}
}

Assert.assertEquals(1, cont);

for (File file : files) {
Files.deleteIfExists(Paths.get(file.toURI()));
}
}
finally {
analyzer.close();
}
}

protected static File getFileFromArchive(Archive<?> archive)
throws Exception {

File archiveFile = File.createTempFile(
archive.getName() + UUID.randomUUID(), ".jar");

archive.as(ZipExporter.class).exportTo(archiveFile, true);

return archiveFile;
}

private JavaArchive createJavaArchive() {
Expand All @@ -126,24 +234,9 @@ private JavaArchive createJavaArchive() {

private void initImportPackageManager()
throws IllegalAccessException, NoSuchFieldException {

Field manifestManagerInstance =
ImportPackageManagerImpl.class.getDeclaredField(
"_manifestManagerInstance");
manifestManagerInstance.setAccessible(true);

DummyInstanceProducerImpl manifestManagerDummyInstance =
new DummyInstanceProducerImpl();

manifestManagerDummyInstance.set(new ManifestManagerImpl());

manifestManagerInstance.set(
_importPackageManager, manifestManagerDummyInstance);
}

private static final ImportPackageManager _importPackageManager =
new ImportPackageManagerImpl();
private static final ManifestManager _manifestManager =
new ManifestManagerImpl();

}

0 comments on commit e6b7987

Please sign in to comment.