Skip to content

Commit

Permalink
BVTCK-42 Adding support for adding an empty beans.xml file to test ar…
Browse files Browse the repository at this point in the history
…chives
  • Loading branch information
gunnarmorling committed Mar 4, 2013
1 parent 954ea76 commit eee03d1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -118,20 +119,21 @@ public T withResource(String source, boolean useTestPackageToLocateSource) {
}

public T withResource(String source, String target, boolean useTestPackageToLocateSource) {

if ( this.resources == null ) {
this.resources = new ArrayList<ResourceDescriptor>();
}

this.resources
.add( new ResourceDescriptor( source, target, useTestPackageToLocateSource ) );
this.resources.add( new ResourceDescriptor( source, target, useTestPackageToLocateSource ) );

return self();
}

public T withValidationXml(String source) {
return withResource( source, "META-INF/validation.xml", true );
}

public abstract T withEmptyBeansXml();

/**
* @return self to enable generic builder
*/
Expand Down Expand Up @@ -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() );
}
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -270,7 +289,6 @@ public String getPlainSource() {
public String getTarget() {
return target;
}

}

private String getTestPackagePath() {
Expand All @@ -284,6 +302,3 @@ public String getName() {
return name;
}
}



@@ -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;
Expand All @@ -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<WebArchiveBuilder, WebArchive> {

private List<ResourceDescriptor> webInfResources = null;

@Override
public WebArchiveBuilder self() {
return this;
Expand All @@ -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<ResourceDescriptor>();
}

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() );
}
}
}
}

0 comments on commit eee03d1

Please sign in to comment.