From 3e675de3beabdd57832d249ff93cbbdc503a69f8 Mon Sep 17 00:00:00 2001 From: kmoore Date: Thu, 16 Dec 2010 15:02:49 +0000 Subject: added XmlElementDecl annotation context model support --- .../core/context/JaxbElementFactoryMethod.java | 49 ++- .../java/GenericJavaElementFactoryMethod.java | 111 +++++- .../java/GenericJavaElementFactoryMethodTests.java | 404 +++++++++++++++++++++ .../java/JaxbCoreJavaContextModelTests.java | 1 + 4 files changed, 553 insertions(+), 12 deletions(-) create mode 100644 jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/GenericJavaElementFactoryMethodTests.java diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbElementFactoryMethod.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbElementFactoryMethod.java index 22c51e914a..3aa677ea2c 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbElementFactoryMethod.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbElementFactoryMethod.java @@ -38,17 +38,44 @@ public interface JaxbElementFactoryMethod void setElementName(String elementName); String ELEMENT_NAME_PROPERTY = "elementName"; //$NON-NLS-1$ + /** + * Corresponds to the XmlElementDecl annotation 'defaultValue' element + */ + String getDefaultValue(); + void setDefaultValue(String defaultValue); + String DEFAULT_VALUE_PROPERTY = "defaultValue"; //$NON-NLS-1$ + String DEFAULT_DEFAULT_VALUE = "\u0000"; //$NON-NLS-1$ - //String defaultValue - //String namespace - //Class scope - //String substitutionHeadName - //String substitutionHeadNamespace - -// String getValue(); -// String getDefaultValue(); -// String getSpecifiedValue(); -// void setSpecifiedValue(String value); -// String SPECIFIED_VALUE_PROPERTY = "specifiedValue"; //$NON-NLS-1$ + /** + * Corresponds to the XmlElementDecl annotation 'namespace' element + */ + String getNamespace(); + void setNamespace(String namespace); + String NAMESPACE_PROPERTY = "namespace"; //$NON-NLS-1$ + String DEFAULT_NAMESPACE = "##default"; //$NON-NLS-1$ + + /** + * Corresponds to the XmlElementDecl annotation 'substitutionHeadName' element + */ + String getSubstitutionHeadName(); + void setSubstitutionHeadName(String substitutionHeadName); + String SUBSTIUTION_HEAD_NAME_PROPERTY = "substitutionHeadName"; //$NON-NLS-1$ + String DEFAULT_SUBSTIUTION_HEAD_NAME = ""; //$NON-NLS-1$ + /** + * Corresponds to the XmlElementDecl annotation 'substitutionHeadNamespace' element + */ + String getSubstitutionHeadNamespace(); + void setSubstitutionHeadNamespace(String substitutionHeadNamespace); + String SUBSTIUTION_HEAD_NAMESPACE_PROPERTY = "substitutionHeadNamespace"; //$NON-NLS-1$ + String DEFAULT_SUBSTIUTION_HEAD_NAMESPACE = "##default"; //$NON-NLS-1$ + + + /** + * Corresponds to the XmlElementDecl annotation 'scope' element + */ + String getScope(); + void setScope(String scope); + String SCOPE_PROPERTY = "scope"; //$NON-NLS-1$ + String DEFAULT_SCOPE_CLASS_NAME = "javax.xml.bind.annotation.XmlElementDecl.GLOBAL"; //$NON-NLS-1$ } diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaElementFactoryMethod.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaElementFactoryMethod.java index 0e2839eecb..06fab230ad 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaElementFactoryMethod.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaElementFactoryMethod.java @@ -22,11 +22,21 @@ public class GenericJavaElementFactoryMethod protected final JavaResourceMethod resourceMethod; protected String elementName; + protected String defaultValue; + protected String namespace; + protected String scope; + protected String substitutionHeadName; + protected String substitutionHeadNamespace; public GenericJavaElementFactoryMethod(JaxbRegistry parent, JavaResourceMethod resourceMethod) { super(parent); this.resourceMethod = resourceMethod; this.elementName = this.getResourceElementName(); + this.defaultValue = this.getResourceDefaultValue(); + this.namespace = this.getResourceNamespace(); + this.scope = this.getResourceScope(); + this.substitutionHeadName = this.getResourceSubstitutionHeadName(); + this.substitutionHeadNamespace = this.getResourceSubstitutionHeadNamespace(); } public JavaResourceMethod getResourceMethod() { @@ -37,6 +47,11 @@ public class GenericJavaElementFactoryMethod public void synchronizeWithResourceModel() { this.setElementName_(this.getResourceElementName()); + this.setDefaultValue_(this.getResourceDefaultValue()); + this.setNamespace_(this.getResourceNamespace()); + this.setScope_(this.getResourceScope()); + this.setSubstitutionHeadName_(this.getResourceSubstitutionHeadName()); + this.setSubstitutionHeadNamespace_(this.getResourceSubstitutionHeadNamespace()); } public void update() { @@ -62,7 +77,6 @@ public class GenericJavaElementFactoryMethod return this.elementName; } - public void setElementName(String elementName) { this.getXmlElementDeclAnnotation().setName(elementName); this.setElementName_(elementName); @@ -77,4 +91,99 @@ public class GenericJavaElementFactoryMethod protected String getResourceElementName() { return this.getXmlElementDeclAnnotation().getName(); } + + public String getDefaultValue() { + return this.defaultValue; + } + + public void setDefaultValue(String defaultValue) { + this.getXmlElementDeclAnnotation().setDefaultValue(defaultValue); + this.setDefaultValue_(defaultValue); + } + + protected void setDefaultValue_(String defaultValue) { + String old = this.defaultValue; + this.defaultValue = defaultValue; + this.firePropertyChanged(DEFAULT_VALUE_PROPERTY, old, defaultValue); + } + + protected String getResourceDefaultValue() { + return this.getXmlElementDeclAnnotation().getDefaultValue(); + } + + public String getNamespace() { + return this.namespace; + } + + public void setNamespace(String namespace) { + this.getXmlElementDeclAnnotation().setNamespace(namespace); + this.setNamespace_(namespace); + } + + protected void setNamespace_(String namespace) { + String old = this.namespace; + this.namespace = namespace; + this.firePropertyChanged(NAMESPACE_PROPERTY, old, namespace); + } + + protected String getResourceNamespace() { + return this.getXmlElementDeclAnnotation().getNamespace(); + } + + public String getScope() { + return this.scope; + } + + public void setScope(String scope) { + this.getXmlElementDeclAnnotation().setScope(scope); + this.setScope_(scope); + } + + protected void setScope_(String scope) { + String old = this.scope; + this.scope = scope; + this.firePropertyChanged(SCOPE_PROPERTY, old, scope); + } + + protected String getResourceScope() { + return this.getXmlElementDeclAnnotation().getScope(); + } + + public String getSubstitutionHeadName() { + return this.substitutionHeadName; + } + + public void setSubstitutionHeadName(String substitutionHeadName) { + this.getXmlElementDeclAnnotation().setSubstitutionHeadName(substitutionHeadName); + this.setSubstitutionHeadName_(substitutionHeadName); + } + + protected void setSubstitutionHeadName_(String substitutionHeadName) { + String old = this.substitutionHeadName; + this.substitutionHeadName = substitutionHeadName; + this.firePropertyChanged(SUBSTIUTION_HEAD_NAME_PROPERTY, old, substitutionHeadName); + } + + protected String getResourceSubstitutionHeadName() { + return this.getXmlElementDeclAnnotation().getSubstitutionHeadName(); + } + + public String getSubstitutionHeadNamespace() { + return this.substitutionHeadNamespace; + } + + public void setSubstitutionHeadNamespace(String substitutionHeadNamespace) { + this.getXmlElementDeclAnnotation().setSubstitutionHeadNamespace(substitutionHeadNamespace); + this.setSubstitutionHeadNamespace_(substitutionHeadNamespace); + } + + protected void setSubstitutionHeadNamespace_(String substitutionHeadNamespace) { + String old = this.substitutionHeadNamespace; + this.substitutionHeadNamespace = substitutionHeadNamespace; + this.firePropertyChanged(SUBSTIUTION_HEAD_NAMESPACE_PROPERTY, old, substitutionHeadNamespace); + } + + protected String getResourceSubstitutionHeadNamespace() { + return this.getXmlElementDeclAnnotation().getSubstitutionHeadNamespace(); + } } diff --git a/jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/GenericJavaElementFactoryMethodTests.java b/jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/GenericJavaElementFactoryMethodTests.java new file mode 100644 index 0000000000..1689498a5f --- /dev/null +++ b/jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/GenericJavaElementFactoryMethodTests.java @@ -0,0 +1,404 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.jaxb.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.dom.Annotation; +import org.eclipse.jdt.core.dom.MarkerAnnotation; +import org.eclipse.jpt.core.utility.jdt.AnnotatedElement; +import org.eclipse.jpt.core.utility.jdt.Member; +import org.eclipse.jpt.core.utility.jdt.ModifiedDeclaration; +import org.eclipse.jpt.jaxb.core.context.JaxbElementFactoryMethod; +import org.eclipse.jpt.jaxb.core.context.JaxbRegistry; +import org.eclipse.jpt.jaxb.core.resource.java.JAXB; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceMethod; +import org.eclipse.jpt.jaxb.core.resource.java.XmlElementDeclAnnotation; +import org.eclipse.jpt.jaxb.core.tests.internal.context.JaxbContextModelTestCase; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + + +@SuppressWarnings("nls") +public class GenericJavaElementFactoryMethodTests extends JaxbContextModelTestCase +{ + + public GenericJavaElementFactoryMethodTests(String name) { + super(name); + } + + private ICompilationUnit createTypeWithXmlRegistry() throws Exception { + return this.createTestType(PACKAGE_NAME, "ObjectFactory.java", "ObjectFactory", new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JAXB.XML_REGISTRY, JAXB.XML_ELEMENT_DECL, "javax.xml.bind.JAXBElement"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@XmlRegistry"); + } + + @Override + public void appendGetNameMethodAnnotationTo(StringBuilder sb) { + sb.append("@XmlElementDecl").append(CR); + sb.append(" JAXBElement createFoo(AnnotationTestType value) {return null}").append(CR); + sb.append(CR); + sb.append(" @XmlElementDecl(name=\"bar\")").append(CR); + sb.append(" JAXBElement createBar(Object value) {return null}").append(CR); + sb.append(CR); + } + }); + } + + private ICompilationUnit createTypeWithXmlType() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JAXB.XML_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@XmlType").append(CR); + } + }); + } + + public void testModifyElementName() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getElementName()); + + elementFactoryMethod.setElementName("bar"); + XmlElementDeclAnnotation elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertEquals("bar", elementDeclAnnotation.getName()); + assertEquals("bar", elementFactoryMethod.getElementName()); + + //verify the xml element decl annotation is not removed when the element name is set to null + elementFactoryMethod.setElementName(null); + elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertNull(elementDeclAnnotation.getName()); + assertNull(elementFactoryMethod.getElementName()); + } + + public void testUpdateElementName() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + assertEquals(2, contextRegistry.getElementFactoryMethodsSize()); + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getElementName()); + + //add a name member value pair + AnnotatedElement annotatedElement = this.annotatedElement(resourceMethod); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.addXmlElementDeclMemberValuePair(declaration, JAXB.XML_ELEMENT_DECL__NAME, "foo"); + } + }); + assertEquals("foo", elementFactoryMethod.getElementName()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.removeXmlElementDeclAnnotation(declaration); + } + }); + assertEquals(1, contextRegistry.getElementFactoryMethodsSize()); + } + + public void testModifyDefaultValue() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getDefaultValue()); + + elementFactoryMethod.setDefaultValue("bar"); + XmlElementDeclAnnotation elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertEquals("bar", elementDeclAnnotation.getDefaultValue()); + assertEquals("bar", elementFactoryMethod.getDefaultValue()); + + //verify the xml element decl annotation is not removed when the element name is set to null + elementFactoryMethod.setDefaultValue(null); + elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertNull(elementDeclAnnotation.getDefaultValue()); + assertNull(elementFactoryMethod.getDefaultValue()); + } + + public void testUpdateDefaultValue() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + assertEquals(2, contextRegistry.getElementFactoryMethodsSize()); + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getDefaultValue()); + + //add a name member value pair + AnnotatedElement annotatedElement = this.annotatedElement(resourceMethod); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.addXmlElementDeclMemberValuePair(declaration, JAXB.XML_ELEMENT_DECL__DEFAULT_VALUE, "foo"); + } + }); + assertEquals("foo", elementFactoryMethod.getDefaultValue()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.removeXmlElementDeclAnnotation(declaration); + } + }); + assertEquals(1, contextRegistry.getElementFactoryMethodsSize()); + } + + public void testModifyScope() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getScope()); + + elementFactoryMethod.setScope("Bar"); + XmlElementDeclAnnotation elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertEquals("Bar", elementDeclAnnotation.getScope()); + assertEquals("Bar", elementFactoryMethod.getScope()); + + //verify the xml element decl annotation is not removed when the element name is set to null + elementFactoryMethod.setScope(null); + elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertNull(elementDeclAnnotation.getScope()); + assertNull(elementFactoryMethod.getScope()); + } + + public void testUpdateScope() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + assertEquals(2, contextRegistry.getElementFactoryMethodsSize()); + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getScope()); + + //add a name member value pair + AnnotatedElement annotatedElement = this.annotatedElement(resourceMethod); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.addXmlElementDeclTypeMemberValuePair(declaration, JAXB.XML_ELEMENT_DECL__SCOPE, "Foo"); + } + }); + assertEquals("Foo", elementFactoryMethod.getScope()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.removeXmlElementDeclAnnotation(declaration); + } + }); + assertEquals(1, contextRegistry.getElementFactoryMethodsSize()); + } + + public void testModifySubstitutionHeadName() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getSubstitutionHeadName()); + + elementFactoryMethod.setSubstitutionHeadName("bar"); + XmlElementDeclAnnotation elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertEquals("bar", elementDeclAnnotation.getSubstitutionHeadName()); + assertEquals("bar", elementFactoryMethod.getSubstitutionHeadName()); + + //verify the xml element decl annotation is not removed when the element name is set to null + elementFactoryMethod.setSubstitutionHeadName(null); + elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertNull(elementDeclAnnotation.getSubstitutionHeadName()); + assertNull(elementFactoryMethod.getSubstitutionHeadName()); + } + + public void testUpdateSubstitutionHeadName() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + assertEquals(2, contextRegistry.getElementFactoryMethodsSize()); + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getSubstitutionHeadName()); + + //add a name member value pair + AnnotatedElement annotatedElement = this.annotatedElement(resourceMethod); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.addXmlElementDeclMemberValuePair(declaration, JAXB.XML_ELEMENT_DECL__SUBSTITUTION_HEAD_NAME, "foo"); + } + }); + assertEquals("foo", elementFactoryMethod.getSubstitutionHeadName()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.removeXmlElementDeclAnnotation(declaration); + } + }); + assertEquals(1, contextRegistry.getElementFactoryMethodsSize()); + } + + public void testModifySubstitutionHeadNamespace() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getSubstitutionHeadNamespace()); + + elementFactoryMethod.setSubstitutionHeadNamespace("bar"); + XmlElementDeclAnnotation elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertEquals("bar", elementDeclAnnotation.getSubstitutionHeadNamespace()); + assertEquals("bar", elementFactoryMethod.getSubstitutionHeadNamespace()); + + //verify the xml element decl annotation is not removed when the element name is set to null + elementFactoryMethod.setSubstitutionHeadNamespace(null); + elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertNull(elementDeclAnnotation.getSubstitutionHeadNamespace()); + assertNull(elementFactoryMethod.getSubstitutionHeadNamespace()); + } + + public void testUpdateSubstitutionHeadNamespace() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + assertEquals(2, contextRegistry.getElementFactoryMethodsSize()); + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getSubstitutionHeadNamespace()); + + //add a name member value pair + AnnotatedElement annotatedElement = this.annotatedElement(resourceMethod); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.addXmlElementDeclMemberValuePair(declaration, JAXB.XML_ELEMENT_DECL__SUBSTITUTION_HEAD_NAMESPACE, "foo"); + } + }); + assertEquals("foo", elementFactoryMethod.getSubstitutionHeadNamespace()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.removeXmlElementDeclAnnotation(declaration); + } + }); + assertEquals(1, contextRegistry.getElementFactoryMethodsSize()); + } + + public void testModifyNamespace() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getNamespace()); + + elementFactoryMethod.setNamespace("bar"); + XmlElementDeclAnnotation elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertEquals("bar", elementDeclAnnotation.getNamespace()); + assertEquals("bar", elementFactoryMethod.getNamespace()); + + //verify the xml element decl annotation is not removed when the element name is set to null + elementFactoryMethod.setNamespace(null); + elementDeclAnnotation = (XmlElementDeclAnnotation) resourceMethod.getAnnotation(XmlElementDeclAnnotation.ANNOTATION_NAME); + assertNull(elementDeclAnnotation.getNamespace()); + assertNull(elementFactoryMethod.getNamespace()); + } + + public void testUpdateNamespace() throws Exception { + createTypeWithXmlType(); + createTypeWithXmlRegistry(); + + JaxbRegistry contextRegistry = CollectionTools.get(getContextRoot().getRegistries(), 0); + + assertEquals(2, contextRegistry.getElementFactoryMethodsSize()); + Iterator elementFactoryMethods = contextRegistry.getElementFactoryMethods().iterator(); + JaxbElementFactoryMethod elementFactoryMethod = elementFactoryMethods.next(); + JavaResourceMethod resourceMethod = elementFactoryMethod.getResourceMethod(); + assertNull(elementFactoryMethod.getNamespace()); + + //add a name member value pair + AnnotatedElement annotatedElement = this.annotatedElement(resourceMethod); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.addXmlElementDeclMemberValuePair(declaration, JAXB.XML_ELEMENT_DECL__NAMESPACE, "foo"); + } + }); + assertEquals("foo", elementFactoryMethod.getNamespace()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaElementFactoryMethodTests.this.removeXmlElementDeclAnnotation(declaration); + } + }); + assertEquals(1, contextRegistry.getElementFactoryMethodsSize()); + } + + protected void addXmlElementDeclMemberValuePair(ModifiedDeclaration declaration, String name, String value) { + this.addMemberValuePair((MarkerAnnotation) this.getXmlElementDeclAnnotation(declaration), name, value); + } + + protected Annotation getXmlElementDeclAnnotation(ModifiedDeclaration declaration) { + return declaration.getAnnotationNamed(XmlElementDeclAnnotation.ANNOTATION_NAME); + } + + protected void removeXmlElementDeclAnnotation(ModifiedDeclaration declaration) { + this.removeAnnotation(declaration, XmlElementDeclAnnotation.ANNOTATION_NAME); + } + + protected void addXmlElementDeclTypeMemberValuePair(ModifiedDeclaration declaration, String name, String typeName) { + this.addMemberValuePair( + (MarkerAnnotation) this.getXmlElementDeclAnnotation(declaration), + name, + this.newTypeLiteral(declaration.getAst(), typeName)); + } + +} \ No newline at end of file diff --git a/jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/JaxbCoreJavaContextModelTests.java b/jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/JaxbCoreJavaContextModelTests.java index eb74a4705d..64e2830472 100644 --- a/jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/JaxbCoreJavaContextModelTests.java +++ b/jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/JaxbCoreJavaContextModelTests.java @@ -17,6 +17,7 @@ public class JaxbCoreJavaContextModelTests extends TestCase { public static Test suite() { TestSuite suite = new TestSuite(JaxbCoreJavaContextModelTests.class.getName()); + suite.addTestSuite(GenericJavaElementFactoryMethodTests.class); suite.addTestSuite(GenericJavaEnumConstantTests.class); suite.addTestSuite(GenericJavaPackageInfoTests.class); suite.addTestSuite(GenericJavaPersistentClassTests.class); -- cgit v1.2.3