Skip to content

Commit

Permalink
[SHRINKDESC-20] Allow empty contents for Descriptors on import.
Browse files Browse the repository at this point in the history
Originally contributed by Andy Gibson in: https://github.com/andygibson/descriptors/commit/2a4bbd403dff44d91c5fa288549bdf0f1779ab2a

Refactored by ALR to match current backing design
  • Loading branch information
Andrew Lee Rubinger committed Mar 17, 2011
1 parent 8c1e732 commit e4ba790
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.shrinkwrap.descriptor.api;

/**
* Indirection exposing some package-private methods
* in the Descriptors API to the implementation classes.
*
* TO BE MOVED TO THE impl-base PACKAGE ONCE IT EXISTS; the user API
* should NOT see this on the compilation ClassPath: SHRINKDESC-42
*
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
*/
// Mark as @Deprecated just to show that this will move
@Deprecated
public final class ApiExposition
{
//-------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||

/**
* No instances permitted
*/
private ApiExposition()
{
throw new UnsupportedOperationException("No instances permitted");
}

//-------------------------------------------------------------------------------------||
// Functional Methods -----------------------------------------------------------------||
//-------------------------------------------------------------------------------------||

/**
* Creates a {@link Descriptor} instance from the specified implementation
* class name, also using the specified name
* @param implClass
* @param descriptorName
* @return
* @throws IllegalArgumentException If either argument is not specified
*/
public static Descriptor createFromImplModelType(final Class<? extends Descriptor> implClass, String descriptorName)
throws IllegalArgumentException
{
return DescriptorInstantiator.createFromImplModelType(implClass, descriptorName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,63 @@ private DescriptorInstantiator()
* @return
* @throws IllegalArgumentException If the user view class was not specified
*/
static <T extends Descriptor> T createFromUserView(final Class<T> userViewClass, String descriptorName) throws IllegalArgumentException
static <T extends Descriptor> T createFromUserView(final Class<T> userViewClass, String descriptorName)
throws IllegalArgumentException
{
// Get the construction information
final DescriptorConstructionInfo info = getDescriptorConstructionInfoForUserView(userViewClass);


final Class<? extends Descriptor> implClass = info.implClass;
String name = info.defaultName;
if (descriptorName != null)
{
name = descriptorName;
}

// Make model class
final Descriptor descriptor = createFromImplModelType(implClass, name);

// Return
return userViewClass.cast(descriptor);
}

/**
* Creates a {@link Descriptor} instance from the specified implementation
* class name, also using the specified name
* @param implClass
* @param descriptorName
* @return
* @throws IllegalArgumentException If either argument is not specified
*/
static Descriptor createFromImplModelType(final Class<? extends Descriptor> implClass, String descriptorName)
throws IllegalArgumentException
{
// Precondition checks
if (implClass == null)
{
throw new IllegalArgumentException("implClass must be specified");
}
if (descriptorName == null || descriptorName.length() == 0)
{
throw new IllegalArgumentException("descriptorName must be specified");
}

// Get the constructor to use in making the new instance
final Constructor<? extends Descriptor> ctor;
try
{
ctor = info.implClass.getConstructor(String.class);
ctor = implClass.getConstructor(String.class);
}
catch (final NoSuchMethodException nsme)
{
throw new RuntimeException(info.implClass + " must contain a constructor with a single String argument");
}

String name = info.defaultName;
if(descriptorName != null)
{
name = descriptorName;
throw new RuntimeException(implClass + " must contain a constructor with a single String argument");
}

// Create a new descriptor instance using the backing model
final Descriptor descriptor;
try
{
descriptor = ctor.newInstance(name);
descriptor = ctor.newInstance(descriptorName);
}
// Handle all construction errors equally
catch (final Exception e)
Expand All @@ -121,8 +151,7 @@ static <T extends Descriptor> T createFromUserView(final Class<T> userViewClass,
}

// Return
return userViewClass.cast(descriptor);

return descriptor;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.InputStream;
import java.lang.reflect.Constructor;

import org.jboss.shrinkwrap.descriptor.api.ApiExposition;
import org.jboss.shrinkwrap.descriptor.api.Descriptor;
import org.jboss.shrinkwrap.descriptor.api.DescriptorImportException;
import org.jboss.shrinkwrap.descriptor.api.DescriptorImporter;
Expand All @@ -46,7 +47,7 @@ public abstract class DescriptorImporterBase<T extends Descriptor> implements De
*/
private final Class<T> endUserViewImplType;

private final String descriptorName;
private final String descriptorName;

//-------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
Expand All @@ -60,7 +61,7 @@ public abstract class DescriptorImporterBase<T extends Descriptor> implements De
* @throws IllegalArgumentException If the model type is not specified
* @throws IllegalArgumentException If the descriptorName not specified
*/
public DescriptorImporterBase(final Class<T> endUserViewImplType, String descriptorName)
public DescriptorImporterBase(final Class<T> endUserViewImplType, final String descriptorName)
throws IllegalArgumentException
{
// Precondition checks
Expand All @@ -74,7 +75,6 @@ public DescriptorImporterBase(final Class<T> endUserViewImplType, String descrip
}

// Set

this.endUserViewImplType = endUserViewImplType;
this.descriptorName = descriptorName;
}
Expand Down Expand Up @@ -108,11 +108,17 @@ public T from(final File file) throws IllegalArgumentException, DescriptorImport
public T from(final String string) throws IllegalArgumentException, DescriptorImportException
{
// Precondition check
if (string == null || string.length() == 0)
if (string == null)
{
throw new IllegalArgumentException("Input must be specified");
}

// Check if empty String
if (string.trim().length() == 0)
{
return endUserViewImplType.cast(ApiExposition.createFromImplModelType(endUserViewImplType, descriptorName));
}

// Return
return this.from(new ByteArrayInputStream(string.getBytes()));
}
Expand Down Expand Up @@ -165,4 +171,5 @@ public T from(final InputStream in) throws IllegalArgumentException, DescriptorI
* @return The Root node extracted.
*/
public abstract Node importRootNode(InputStream stream) throws DescriptorImportException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ public Node importRootNode(InputStream stream) throws DescriptorImportException
{
try
{
// Empty contents? If so, no root Node
if (stream.available() == 0)
{
return null;
}

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(stream);

Document doc=builder.parse(stream);
Node root = new Node(doc.getDocumentElement().getNodeName());
readRecursive(root, doc.getDocumentElement());
return root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.jboss.shrinkwrap.descriptor.api.spec.cdi.beans.BeansDescriptor;
import org.jboss.shrinkwrap.descriptor.api.spec.servlet.web.WebAppDescriptor;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -78,4 +79,43 @@ public void shouldThrowExceptionOnMissingInputStream() throws Exception
{
Descriptors.importAs(WebAppDescriptor.class).from((InputStream) null);
}

/**
* SHRINKDESC-20
*/
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionOnMissingString() throws Exception
{
Descriptors.importAs(WebAppDescriptor.class).from((String) null);
}

/**
* SHRINKDESC-20
*/
@Test
public void shouldBeAbleToImportWhiteSpaceString()
{
BeansDescriptor descriptor = Descriptors.importAs(BeansDescriptor.class).from(" \n \n ");
Assert.assertNotNull("Verify the descriptor was created from an empty string",descriptor);
}

/**
* SHRINKDESC-20
*/
@Test
public void shouldBeAbleToImportEmptyString()
{
BeansDescriptor descriptor = Descriptors.importAs(BeansDescriptor.class).from("");
Assert.assertNotNull("Verify the descriptor was created from an empty string",descriptor);
}

/**
* SHRINKDESC-20
*/
@Test
public void shouldBeAbleToImportEmptyFile()
{
BeansDescriptor descriptor = Descriptors.importAs(BeansDescriptor.class).from(getClass().getResourceAsStream("/empty.xml"));
Assert.assertNotNull("Verify the descriptor was created from and empty file",descriptor);
}
}
Empty file.

0 comments on commit e4ba790

Please sign in to comment.