Skip to content

Commit

Permalink
jaxb2-plugins: tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ketoth Xupack <ketoth.xupack@gmail.com>
  • Loading branch information
KetothXupack committed Oct 11, 2013
1 parent a68c69c commit 1d5d614
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<javax-inject.version>1</javax-inject.version>
<jaxb-xjc.version>2.2.7</jaxb-xjc.version>
<jaxb2-basics.version>0.6.4</jaxb2-basics.version>
<jaxb2-testing.version>0.8.3</jaxb2-testing.version>
<joda-time.version>2.3</joda-time.version>
<jongo.version>0.2</jongo.version>
<logback.version>1.0.13</logback.version>
Expand Down
17 changes: 17 additions & 0 deletions projects/jaxb2-plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<relativePath>../../pom.xml</relativePath>
</parent>

<properties>
<skip.enforcer>true</skip.enforcer>
</properties>

<!-- enforce @Nonnull checks -->
<build>
<plugins>
Expand All @@ -36,6 +40,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
<version>${jaxb2-basics.version}</version>
</dependency>

<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-tools</artifactId>
Expand All @@ -61,6 +71,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin-testing</artifactId>
<version>${jaxb2-testing.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.nohope.jaxb2.plugin.metadata;

import org.junit.BeforeClass;
import org.junit.Test;
import org.jvnet.jaxb2.maven2.AbstractXJC2Mojo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Class.forName;
import static org.junit.Assert.*;
import static org.nohope.reflection.IntrospectionUtils.instanceOf;
import static org.nohope.reflection.IntrospectionUtils.invoke;
import static org.nohope.reflection.ModifierMatcher.*;

/**
* @author <a href="mailto:ketoth.xupack@gmail.com">Ketoth Xupack</a>
* @since 2013-10-11 16:31
*/
public class RunMetadataPluginTest {
//static {
// System.setProperty("javax.xml.accessExternalStylesheet", "all");
// System.setProperty("javax.xml.accessExternalSchema", "all");
// System.setProperty("javax.xml.accessExternalDTD", "all");
//}

private static class RunMetadataPlugin extends org.jvnet.jaxb2.maven2.test.RunXJC2Mojo {

@Override
public File getSchemaDirectory() {
return new File(getBaseDir(), "src/test/resources/metadata");
}

@Override
protected void configureMojo(final AbstractXJC2Mojo mojo) {
super.configureMojo(mojo);
mojo.setForceRegenerate(true);
}

@Override
public List<String> getArgs() {
final List<String> args = new ArrayList<>(super.getArgs());
args.add("-Xmetadata");
return args;
}
}

@BeforeClass
public static void generateCode() throws Exception {
new RunMetadataPlugin().testExecute();
}

@Test
public void checkGeneratedObjectsValidity() throws Exception {
final Class<?> cobj1 = forName("org.nohope.metadata.ComplexObjectType");
final Object descriptor = invoke(cobj1, ALL, "descriptor");

final CallChain callChain =
(CallChain) invoke(invoke(invoke(descriptor,
not(ABSTRACT), "getObjectField"),
not(ABSTRACT), "getFloatField"),
not(ABSTRACT), "getCallChain");

final List<NamedDescriptor<?>> chain = callChain.getChain();
assertEquals(2, chain.size());
assertEquals("object_field", chain.get(0).getProperty());
assertTrue(instanceOf(chain.get(0).getParentDescriptor(),
forName("org.nohope.metadata.ComplexObjectType$IDescriptor")));

assertEquals("float_field", chain.get(1).getProperty());
assertTrue(instanceOf(chain.get(1).getParentDescriptor(),
forName("org.nohope.metadata.ComplexObjectType2$IDescriptor")));
assertEquals(forName("org.nohope.metadata.ComplexObjectType2"),
chain.get(1).getParentDescriptor().getFieldType().getTypeClass());
}

@Test
public void properties() {
final MetadataPlugin plugin = new MetadataPlugin();
assertNotNull(plugin.getUsage());
}
}
45 changes: 45 additions & 0 deletions projects/jaxb2-plugins/src/test/resources/metadata/schema.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:tns="http://no-hope.org/"
targetNamespace="http://no-hope.org/"
jaxb:version="1.0">

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings choiceContentProperty="true" generateIsSetMethod="true" />
<jaxb:schemaBindings>
<jaxb:package name="org.nohope.metadata"/>
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>

<xs:element name="complexObject" type="tns:complexObjectType"/>
<xs:complexType name="complexObjectType">
<xs:sequence>
<xs:element name="object_field" type="tns:complexObjectType2"/>

<xs:element name="datetime_field" type="xs:dateTime"/>
<xs:element name="time_field" type="xs:time"/>
<xs:element name="date_field" type="xs:date"/>
<xs:element name="float_field" type="xs:float"/>
<xs:element name="string_field" type="xs:string"/>
<xs:element name="boolean_field" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>

<xs:element name="complexObject2" type="tns:complexObjectType2"/>
<xs:complexType name="complexObjectType2">
<xs:sequence>
<xs:element name="datetime_field" type="xs:dateTime"/>
<xs:element name="time_field" type="xs:time"/>
<xs:element name="date_field" type="xs:date"/>
<xs:element name="float_field" type="xs:float"/>
<xs:element name="string_field" type="xs:string"/>
<xs:element name="boolean_field" type="xs:boolean"/>

<!-- this field should not have name attribute in @XmlElement -->
<xs:element name="simple" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

0 comments on commit 1d5d614

Please sign in to comment.