2525import java .util .HashMap ;
2626import java .util .List ;
2727import java .util .Map ;
28+ import java .util .ServiceLoader ;
29+ import java .util .ServiceLoader .Provider ;
2830import java .util .Set ;
2931import java .util .function .BiPredicate ;
3032import java .util .function .Function ;
3335import java .util .jar .JarFile ;
3436import java .util .stream .Collectors ;
3537
36- import org .eclipse .core .runtime .CoreException ;
38+ import org .eclipse .core .runtime .IStatus ;
39+ import org .eclipse .core .runtime .MultiStatus ;
40+ import org .eclipse .core .runtime .Status ;
3741import org .eclipse .core .runtime .URIUtil ;
3842import org .eclipse .equinox .frameworkadmin .BundleInfo ;
39- import org .eclipse .m2e .core .embedder .ArtifactKey ;
40- import org .eclipse .m2e .pde .target .MavenTargetLocationFactory ;
41- import org .eclipse .pde .core .target .ITargetDefinition ;
43+ import org .eclipse .m2e .pde .target .tests .spi .TargetLocationLoader ;
4244import org .eclipse .pde .core .target .ITargetLocation ;
43- import org .eclipse .pde .core .target .ITargetPlatformService ;
4445import org .eclipse .pde .core .target .NameVersionDescriptor ;
4546import org .eclipse .pde .core .target .TargetBundle ;
4647import org .eclipse .pde .core .target .TargetFeature ;
48+ import org .junit .Rule ;
49+ import org .junit .rules .TemporaryFolder ;
4750
4851public abstract class AbstractMavenTargetTest {
4952 static final String SOURCE_BUNDLE_SUFFIX = ".source" ;
5053 static final TargetBundle [] EMPTY = {};
5154
52- static ITargetDefinition resolveMavenTarget (String targetXML ) throws CoreException {
53- @ SuppressWarnings ("restriction" )
54- ITargetPlatformService s = org .eclipse .pde .internal .core .PDECore .getDefault ()
55- .acquireService (ITargetPlatformService .class );
56- ITargetDefinition target = s .newTarget ();
55+ @ Rule
56+ public TemporaryFolder temporaryFolder = new TemporaryFolder ();
5757
58- setMavenTargetLocationAndResolver ( target , targetXML );
59- return target ;
60- }
58+ private static ServiceLoader < TargetLocationLoader > LOCATION_LOADER = ServiceLoader . load ( TargetLocationLoader . class ,
59+ AbstractMavenTargetTest . class . getClassLoader ()) ;
60+ private static TargetLocationLoader loader ;
6161
62- static void setMavenTargetLocationAndResolver (ITargetDefinition target , String targetXML ) throws CoreException {
63- ITargetLocation targetLocation = new MavenTargetLocationFactory ().getTargetLocation ("Maven" , targetXML );
64- target .setTargetLocations (new ITargetLocation [] { targetLocation });
65- target .resolve (null );
62+ ITargetLocation resolveMavenTarget (String targetXML ) throws Exception {
63+ return getLoader ().resolveMavenTarget (targetXML , temporaryFolder .newFolder ());
6664 }
6765
68- // --- common assertion utilities ---
69-
70- interface ExpectedUnit {
71-
72- String id ();
73-
74- boolean isSourceBundle ();
75-
76- boolean isOriginal ();
66+ private static TargetLocationLoader getLoader () {
67+ if (loader == null ) {
68+ Provider <TargetLocationLoader > provider = LOCATION_LOADER .stream ().sorted ().findFirst ().orElseThrow (
69+ () -> new IllegalStateException ("No TargetLocationLoader found on classpath of test!" ));
70+ loader = provider .get ();
71+ }
72+ return loader ;
73+ }
7774
78- ArtifactKey key ();
75+ protected static void assertStatusOk (IStatus status ) {
76+ if (!status .isOK ()) {
77+ throw new AssertionError (status .toString (), status .getException ());
78+ }
7979 }
8080
81+ // --- common assertion utilities ---
82+
8183 private static <U extends ExpectedUnit , T > Map <U , T > assertTargetContent (List <U > expectedUnits , T [] allUnit ,
8284 BiPredicate <U , T > matcher , Function <T , URI > getLocation , Predicate <T > isSourceUnit ,
8385 Function <T , String > getSourceTarget , Function <T , String > toString ) {
@@ -130,20 +132,6 @@ private static <T> boolean isOriginalArtifact(ExpectedUnit expectedUnit, T unit,
130132
131133 // --- assertion utilities for Bundles in target ---
132134
133- static record ExpectedBundle (String bsn , String version , boolean isSourceBundle , boolean isOriginal ,
134- ArtifactKey key ) implements ExpectedUnit {
135-
136- @ Override
137- public String id () {
138- return bsn ();
139- }
140-
141- @ Override
142- public String toString () {
143- return bsn + ":" + version ;
144- }
145- }
146-
147135 static ExpectedBundle originalOSGiBundle (String bsn , String version , String groupArtifact ) {
148136 return originalOSGiBundle (bsn , version , groupArtifact , version );
149137 }
@@ -161,8 +149,8 @@ static ExpectedBundle generatedBundle(String bsn, String version, String groupAr
161149 static List <ExpectedBundle > withSourceBundles (List <ExpectedBundle > mainBundles ) {
162150 return mainBundles .stream ().<ExpectedBundle >mapMulti ((unit , downStream ) -> {
163151 downStream .accept (unit );
164- String sourceId = unit .bsn + SOURCE_BUNDLE_SUFFIX ;
165- ExpectedBundle sourceUnit = new ExpectedBundle (sourceId , unit .version , true , false , unit .key );
152+ String sourceId = unit .bsn () + SOURCE_BUNDLE_SUFFIX ;
153+ ExpectedBundle sourceUnit = new ExpectedBundle (sourceId , unit .version () , true , false , unit .key () );
166154 downStream .accept (sourceUnit );
167155 }).toList ();
168156 }
@@ -175,12 +163,12 @@ static Attributes getManifestMainAttributes(TargetBundle targetBundle) throws IO
175163 }
176164 }
177165
178- static void assertTargetBundles (ITargetDefinition target , List <ExpectedBundle > expectedUnits ) {
179- assertTargetContent (expectedUnits , target .getAllBundles (), //
166+ static void assertTargetBundles (ITargetLocation target , List <ExpectedBundle > expectedUnits ) {
167+ assertTargetContent (expectedUnits , target .getBundles (), //
180168 (expectedBundle , bundle ) -> {
181169 BundleInfo info = bundle .getBundleInfo ();
182170 return expectedBundle .bsn ().equals (info .getSymbolicName ())
183- && expectedBundle .version .equals (info .getVersion ());
171+ && expectedBundle .version () .equals (info .getVersion ());
184172 }, //
185173 tb -> tb .getBundleInfo ().getLocation (), //
186174 tb -> tb .isSourceBundle (),
@@ -190,15 +178,6 @@ static void assertTargetBundles(ITargetDefinition target, List<ExpectedBundle> e
190178
191179 // --- assertion utilities for Features in a target ---
192180
193- static record ExpectedFeature (String id , String version , boolean isSourceBundle , boolean isOriginal ,
194- ArtifactKey key , List <NameVersionDescriptor > containedPlugins ) implements ExpectedUnit {
195-
196- @ Override
197- public String toString () {
198- return id + ":" + version ;
199- }
200- }
201-
202181 static ExpectedFeature originalFeature (String id , String version , String groupArtifact ,
203182 List <NameVersionDescriptor > containedPlugins ) {
204183 ArtifactKey key = ArtifactKey .fromPortableString (groupArtifact + ":" + version + "::" );
@@ -217,19 +196,19 @@ static List<ExpectedFeature> withSourceFeatures(List<ExpectedFeature> mainFeatur
217196 return mainFeatures .stream ().<ExpectedFeature >mapMulti ((feature , downStream ) -> {
218197 downStream .accept (feature );
219198 String sourceId = feature .id () + SOURCE_BUNDLE_SUFFIX ;
220- List <NameVersionDescriptor > sourcePlugins = feature .containedPlugins .stream ()
199+ List <NameVersionDescriptor > sourcePlugins = feature .containedPlugins () .stream ()
221200 .map (d -> featurePlugin (d .getId () + SOURCE_BUNDLE_SUFFIX , d .getVersion ())).toList ();
222- ExpectedFeature sourceUnit = new ExpectedFeature (sourceId , feature .version , true , false , feature .key ,
201+ ExpectedFeature sourceUnit = new ExpectedFeature (sourceId , feature .version () , true , false , feature .key () ,
223202 sourcePlugins );
224203 downStream .accept (sourceUnit );
225204 }).toList ();
226205 }
227206
228- static Map <ExpectedFeature , TargetFeature > assertTargetFeatures (ITargetDefinition target ,
207+ static Map <ExpectedFeature , TargetFeature > assertTargetFeatures (ITargetLocation target ,
229208 List <ExpectedFeature > expectedFeatures ) {
230- var encounteredFeatures = assertTargetContent (expectedFeatures , target .getAllFeatures (), //
209+ var encounteredFeatures = assertTargetContent (expectedFeatures , target .getFeatures (), //
231210 (expectedFeature , feature ) -> expectedFeature .id ().equals (feature .getId ())
232- && expectedFeature .version .equals (feature .getVersion ()), //
211+ && expectedFeature .version () .equals (feature .getVersion ()), //
233212 f -> Path .of (f .getLocation ()).toUri (), //
234213 f -> isSourceFeature (f ), //
235214 f -> isSourceFeature (f ) ? f .getId ().substring (0 , f .getId ().length () - SOURCE_BUNDLE_SUFFIX .length ())
@@ -241,8 +220,27 @@ static Map<ExpectedFeature, TargetFeature> assertTargetFeatures(ITargetDefinitio
241220 return encounteredFeatures ;
242221 }
243222
244- private static boolean isSourceFeature (TargetFeature f ) {
223+ static boolean isSourceFeature (TargetFeature f ) {
245224 return f .getId ().endsWith (SOURCE_BUNDLE_SUFFIX )
246225 && Arrays .stream (f .getPlugins ()).allMatch (d -> d .getId ().endsWith (SOURCE_BUNDLE_SUFFIX ));
247226 }
227+
228+ static IStatus getTargetStatus (ITargetLocation target ) {
229+ IStatus status = target .getStatus ();
230+ if (status != null && !status .isOK ()) {
231+ return status ;
232+ }
233+ MultiStatus result = new MultiStatus ("org.eclipse.pde.core" , 0 ,
234+ "Problems occurred getting the plug-ins in this container" , null );
235+ for (TargetBundle targetBundle : target .getBundles ()) {
236+ IStatus bundleStatus = targetBundle .getStatus ();
237+ if (!bundleStatus .isOK ()) {
238+ result .add (bundleStatus );
239+ }
240+ }
241+ if (result .isOK ()) {
242+ return Status .OK_STATUS ;
243+ }
244+ return result ;
245+ }
248246}
0 commit comments