From eee03d1a683aaa6acaa308ca8cca12a3e59786e5 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Mon, 4 Mar 2013 10:17:46 +0100 Subject: [PATCH] BVTCK-42 Adding support for adding an empty beans.xml file to test archives --- .../tck/util/shrinkwrap/ArchiveBuilder.java | 41 +++++++++++------ .../util/shrinkwrap/WebArchiveBuilder.java | 45 ++++++++++++++++++- 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/util/shrinkwrap/ArchiveBuilder.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/util/shrinkwrap/ArchiveBuilder.java index 279205a7..4784eaba 100644 --- a/tests/src/main/java/org/hibernate/beanvalidation/tck/util/shrinkwrap/ArchiveBuilder.java +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/util/shrinkwrap/ArchiveBuilder.java @@ -4,6 +4,7 @@ import java.util.List; import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.asset.Asset; import org.jboss.shrinkwrap.api.container.ClassContainer; import org.jboss.shrinkwrap.api.container.ResourceContainer; import org.jboss.shrinkwrap.impl.base.URLPackageScanner; @@ -118,13 +119,12 @@ public T withResource(String source, boolean useTestPackageToLocateSource) { } public T withResource(String source, String target, boolean useTestPackageToLocateSource) { - if ( this.resources == null ) { this.resources = new ArrayList(); } - this.resources - .add( new ResourceDescriptor( source, target, useTestPackageToLocateSource ) ); + this.resources.add( new ResourceDescriptor( source, target, useTestPackageToLocateSource ) ); + return self(); } @@ -132,6 +132,8 @@ public T withValidationXml(String source) { return withResource( source, "META-INF/validation.xml", true ); } + public abstract T withEmptyBeansXml(); + /** * @return self to enable generic builder */ @@ -207,11 +209,16 @@ protected void processResources(ResourceContainer archive) { } for ( ResourceDescriptor resource : resources ) { - if ( resource.getTarget() == null ) { - archive.addAsResource( resource.getSource() ); + if ( resource.getSource() != null ) { + if ( resource.getTarget() == null ) { + archive.addAsResource( resource.getSource() ); + } + else { + archive.addAsResource( resource.getSource(), resource.getTarget() ); + } } - else { - archive.addAsResource( resource.getSource(), resource.getTarget() ); + else if ( resource.getAsset() != null ) { + archive.addAsResource( resource.getAsset(), resource.getTarget() ); } } } @@ -248,17 +255,29 @@ public Class[] getServiceImplementations() { */ protected class ResourceDescriptor { + private final Asset asset; private final String source; private final String target; - private boolean useTestPackageToLocateSource = true; + private final boolean useTestPackageToLocateSource; public ResourceDescriptor(String source, String target, boolean useTestPackageToLocateSource) { - super(); + this.asset = null; this.source = source; this.target = target; this.useTestPackageToLocateSource = useTestPackageToLocateSource; } + public ResourceDescriptor(Asset asset, String target) { + this.asset = asset; + this.source = null; + this.target = target; + this.useTestPackageToLocateSource = false; + } + + public Asset getAsset() { + return asset; + } + public String getSource() { return useTestPackageToLocateSource ? getTestPackagePath() + source : source; } @@ -270,7 +289,6 @@ public String getPlainSource() { public String getTarget() { return target; } - } private String getTestPackagePath() { @@ -284,6 +302,3 @@ public String getName() { return name; } } - - - diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/util/shrinkwrap/WebArchiveBuilder.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/util/shrinkwrap/WebArchiveBuilder.java index 21e650ac..4bce8fe5 100644 --- a/tests/src/main/java/org/hibernate/beanvalidation/tck/util/shrinkwrap/WebArchiveBuilder.java +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/util/shrinkwrap/WebArchiveBuilder.java @@ -1,6 +1,11 @@ package org.hibernate.beanvalidation.tck.util.shrinkwrap; +import java.util.ArrayList; +import java.util.List; + import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.Asset; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.jboss.shrinkwrap.descriptor.api.Descriptors; @@ -11,8 +16,13 @@ * covering common TCK needs. Use shrinkwrap API to adapt archive to advanced scenarios. * * @author Martin Kouba + * @author Gunnar Morling + * */ public class WebArchiveBuilder extends ArchiveBuilder { + + private List webInfResources = null; + @Override public WebArchiveBuilder self() { return this; @@ -32,11 +42,44 @@ protected WebArchive buildInternal() { processPackages( webArchive ); processClasses( webArchive ); processResources( webArchive ); + processWebInfResources( webArchive ); webArchive.setWebXML( new StringAsset( Descriptors.create( WebAppDescriptor.class ).exportAsString() ) ); return webArchive; } -} + @Override + public WebArchiveBuilder withEmptyBeansXml() { + return withWebInfResource( EmptyAsset.INSTANCE, "beans.xml" ); + } + + private WebArchiveBuilder withWebInfResource(Asset asset, String target) { + if ( this.webInfResources == null ) { + this.webInfResources = new ArrayList(); + } + + this.webInfResources.add( new ResourceDescriptor( asset, target ) ); + + return self(); + } + private void processWebInfResources(WebArchive archive) { + if ( webInfResources == null ) { + return; + } + for ( ResourceDescriptor resource : webInfResources ) { + if ( resource.getSource() != null ) { + if ( resource.getTarget() == null ) { + archive.addAsWebInfResource( resource.getSource() ); + } + else { + archive.addAsWebInfResource( resource.getSource(), resource.getTarget() ); + } + } + else if ( resource.getAsset() != null ) { + archive.addAsWebInfResource( resource.getAsset(), resource.getTarget() ); + } + } + } +}