Skip to content

Commit

Permalink
reficio#241: errors in embedded bnd result in an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
sparsick committed Mar 11, 2021
1 parent bd5a6c6 commit f570b14
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 7 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@
<scope>provided</scope>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -257,10 +256,16 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.18.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/reficio/p2/bundler/AquteAnalyzerException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2012 Reficio (TM) - Reestablish your software! All Rights Reserved.
*
* 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.reficio.p2.bundler;

public class AquteAnalyzerException extends RuntimeException {
public AquteAnalyzerException(String message) {
super(message);
}
}
15 changes: 10 additions & 5 deletions src/main/java/org/reficio/p2/bundler/impl/AquteBundler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Jar;
import org.apache.commons.io.FileUtils;
import org.reficio.p2.bundler.AquteAnalyzerException;
import org.reficio.p2.bundler.ArtifactBundler;
import org.reficio.p2.bundler.ArtifactBundlerInstructions;
import org.reficio.p2.bundler.ArtifactBundlerRequest;
Expand Down Expand Up @@ -54,6 +55,11 @@ public AquteBundler(boolean pedantic) {
this.pedantic = pedantic;
}

AquteBundler(boolean pedantic, BundleUtils bundleUtils) {
this.bundleUtils = bundleUtils;
this.pedantic = pedantic;
}

public void execute(ArtifactBundlerRequest request, ArtifactBundlerInstructions instructions) {
log().info("Executing Bundler:");
try {
Expand Down Expand Up @@ -89,13 +95,12 @@ private void prepareOutputFile(File file) {
}

private void handleVanillaJarWrap(ArtifactBundlerRequest request, ArtifactBundlerInstructions instructions) throws Exception {
Analyzer analyzer = AquteHelper.buildAnalyzer(request, instructions, pedantic);
try {
try (Analyzer analyzer = AquteHelper.buildAnalyzer(request, instructions, pedantic)) {
populateJar(analyzer, request.getBinaryOutputFile());
bundleUtils.reportErrors(analyzer);
if (bundleUtils.reportErrors(analyzer)) {
throw new AquteAnalyzerException("Aqute Jar Analyser reports error.");
}
removeSignature(request.getBinaryOutputFile());
} finally {
analyzer.close();
}
}

Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/reficio/p2/bundler/impl/AnalyzerStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2012 Reficio (TM) - Reestablish your software! All Rights Reserved.
*
* 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.reficio.p2.bundler.impl;

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

import java.util.jar.Manifest;

class AnalyzerStub extends Analyzer {
@Override
public Manifest calcManifest() throws Exception {
return new Manifest();
}

@Override
public Jar getJar() {
return new Jar("fake");
}
}
59 changes: 59 additions & 0 deletions src/test/java/org/reficio/p2/bundler/impl/AquteBundlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2012 Reficio (TM) - Reestablish your software! All Rights Reserved.
*
* 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.reficio.p2.bundler.impl;


import org.apache.maven.plugin.logging.SystemStreamLog;
import org.assertj.core.api.ThrowableAssert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.reficio.p2.bundler.AquteAnalyzerException;
import org.reficio.p2.bundler.ArtifactBundlerInstructions;
import org.reficio.p2.bundler.ArtifactBundlerRequest;
import org.reficio.p2.logger.Logger;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mockStatic;

public class AquteBundlerTest {


@BeforeClass
public static void setUpClass() {
Logger.initialize(new SystemStreamLog());
}

@Test
public void bndAnalyzerProduceErrorThenExceptionIsExpected() {
try (MockedStatic<AquteHelper> theMock = mockStatic(AquteHelper.class)) {
theMock.when(() -> AquteHelper.buildAnalyzer(any(ArtifactBundlerRequest.class), any(ArtifactBundlerInstructions.class), anyBoolean()))
.thenReturn(new AnalyzerStub());

AquteBundler bundlerUnderTest = new AquteBundler(true, new BundleUtilsStub());
ThrowableAssert.ThrowingCallable methodUnderTest = () -> bundlerUnderTest.execute(new ArtifactBundlerRequest(new File(""), new File("target/tmp.jar"), null, null, true), ArtifactBundlerInstructions.builder().build());

assertThatThrownBy(methodUnderTest).isInstanceOf(RuntimeException.class).hasRootCauseInstanceOf(AquteAnalyzerException.class);
}
}
}
30 changes: 30 additions & 0 deletions src/test/java/org/reficio/p2/bundler/impl/BundleUtilsStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2012 Reficio (TM) - Reestablish your software! All Rights Reserved.
*
* 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.reficio.p2.bundler.impl;

import aQute.bnd.osgi.Analyzer;
import org.reficio.p2.utils.BundleUtils;

class BundleUtilsStub extends BundleUtils {

@Override
public boolean reportErrors(Analyzer analyzer) {
return true;
}
}

0 comments on commit f570b14

Please sign in to comment.