| /******************************************************************************* |
| * Copyright (c) 2007, 2008 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.eclipselink.core.tests.internal.context.java; |
| |
| import java.util.Iterator; |
| import org.eclipse.jdt.core.ICompilationUnit; |
| import org.eclipse.jpt.core.context.BasicMapping; |
| import org.eclipse.jpt.core.context.PersistentAttribute; |
| import org.eclipse.jpt.core.resource.java.JPA; |
| import org.eclipse.jpt.core.resource.java.JavaResourcePersistentAttribute; |
| import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType; |
| import org.eclipse.jpt.eclipselink.core.context.Convert; |
| import org.eclipse.jpt.eclipselink.core.context.StructConverter; |
| import org.eclipse.jpt.eclipselink.core.resource.java.EclipseLinkJPA; |
| import org.eclipse.jpt.eclipselink.core.resource.java.StructConverterAnnotation; |
| import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; |
| |
| public class EclipseLinkJavaStructConverterTests extends EclipseLinkJavaContextModelTestCase |
| { |
| |
| private void createConvertAnnotation() throws Exception{ |
| this.createAnnotationAndMembers(EclipseLinkJPA.PACKAGE, "Convert", "String value() default \"none\";"); |
| } |
| |
| private void createStructConverterAnnotation() throws Exception { |
| this.createAnnotationAndMembers(EclipseLinkJPA.PACKAGE, "StructConverter", "String name(); String converter()"); |
| } |
| |
| |
| private ICompilationUnit createTestEntityWithConvertAndStructConverter() throws Exception { |
| createConvertAnnotation(); |
| createStructConverterAnnotation(); |
| return this.createTestType(new DefaultAnnotationWriter() { |
| @Override |
| public Iterator<String> imports() { |
| return new ArrayIterator<String>(JPA.ENTITY, EclipseLinkJPA.CONVERT, EclipseLinkJPA.STRUCT_CONVERTER); |
| } |
| @Override |
| public void appendTypeAnnotationTo(StringBuilder sb) { |
| sb.append("@Entity"); |
| } |
| |
| @Override |
| public void appendIdFieldAnnotationTo(StringBuilder sb) { |
| sb.append("@Convert(\"foo\")").append(CR); |
| sb.append(" @StructConverter(name=\"foo\""); |
| } |
| }); |
| } |
| |
| private ICompilationUnit createTestEntityWithConvertAndStructConverterClass() throws Exception { |
| createConvertAnnotation(); |
| createStructConverterAnnotation(); |
| return this.createTestType(new DefaultAnnotationWriter() { |
| @Override |
| public Iterator<String> imports() { |
| return new ArrayIterator<String>(JPA.ENTITY, EclipseLinkJPA.CONVERT, EclipseLinkJPA.STRUCT_CONVERTER); |
| } |
| @Override |
| public void appendTypeAnnotationTo(StringBuilder sb) { |
| sb.append("@Entity"); |
| } |
| |
| @Override |
| public void appendIdFieldAnnotationTo(StringBuilder sb) { |
| sb.append("@Convert(\"foo\")").append(CR); |
| sb.append(" @StructConverter(converter=\"Foo\""); |
| } |
| }); |
| } |
| |
| public EclipseLinkJavaStructConverterTests(String name) { |
| super(name); |
| } |
| |
| |
| public void testGetName() throws Exception { |
| createTestEntityWithConvertAndStructConverter(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); |
| Convert eclipseLinkConvert = (Convert) basicMapping.getConverter(); |
| StructConverter converter = (StructConverter) eclipseLinkConvert.getConverter(); |
| |
| assertEquals("foo", converter.getName()); |
| } |
| |
| public void testSetName() throws Exception { |
| createTestEntityWithConvertAndStructConverter(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); |
| Convert eclipseLinkConvert = (Convert) basicMapping.getConverter(); |
| StructConverter converter = (StructConverter) eclipseLinkConvert.getConverter(); |
| assertEquals("foo", converter.getName()); |
| |
| converter.setName("bar"); |
| assertEquals("bar", converter.getName()); |
| |
| JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); |
| StructConverterAnnotation converterAnnotation = (StructConverterAnnotation) attributeResource.getSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| assertEquals("bar", converterAnnotation.getName()); |
| |
| |
| converter.setName(null); |
| assertEquals(null, converter.getName()); |
| converterAnnotation = (StructConverterAnnotation) attributeResource.getSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| assertEquals(null, converterAnnotation.getName()); |
| |
| |
| converter.setName("bar"); |
| assertEquals("bar", converter.getName()); |
| converterAnnotation = (StructConverterAnnotation) attributeResource.getSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| assertEquals("bar", converterAnnotation.getName()); |
| } |
| |
| public void testGetNameUpdatesFromResourceModelChange() throws Exception { |
| createTestEntityWithConvertAndStructConverter(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); |
| Convert eclipseLinkConvert = (Convert) basicMapping.getConverter(); |
| StructConverter converter = (StructConverter) eclipseLinkConvert.getConverter(); |
| |
| assertEquals("foo", converter.getName()); |
| |
| JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); |
| StructConverterAnnotation converterAnnotation = (StructConverterAnnotation) attributeResource.getSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| converterAnnotation.setName("bar"); |
| assertEquals("bar", converter.getName()); |
| |
| attributeResource.removeSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| assertEquals(null, eclipseLinkConvert.getConverter()); |
| |
| converterAnnotation = (StructConverterAnnotation) attributeResource.addSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| assertNotNull(eclipseLinkConvert.getConverter()); |
| |
| converterAnnotation.setName("FOO"); |
| assertEquals("FOO", eclipseLinkConvert.getConverter().getName()); |
| } |
| |
| |
| public void testGetConverterClass() throws Exception { |
| createTestEntityWithConvertAndStructConverterClass(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); |
| Convert eclipseLinkConvert = (Convert) basicMapping.getConverter(); |
| StructConverter converter = (StructConverter) eclipseLinkConvert.getConverter(); |
| |
| assertEquals("Foo", converter.getConverterClass()); |
| } |
| |
| public void testSetConverterClass() throws Exception { |
| createTestEntityWithConvertAndStructConverterClass(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); |
| Convert eclipseLinkConvert = (Convert) basicMapping.getConverter(); |
| StructConverter converter = (StructConverter) eclipseLinkConvert.getConverter(); |
| assertEquals("Foo", converter.getConverterClass()); |
| |
| converter.setConverterClass("Bar"); |
| assertEquals("Bar", converter.getConverterClass()); |
| |
| JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); |
| StructConverterAnnotation converterAnnotation = (StructConverterAnnotation) attributeResource.getSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| assertEquals("Bar", converterAnnotation.getConverter()); |
| |
| |
| converter.setConverterClass(null); |
| assertEquals(null, converter.getConverterClass()); |
| converterAnnotation = (StructConverterAnnotation) attributeResource.getSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| assertEquals(null, converterAnnotation.getConverter()); |
| |
| |
| converter.setConverterClass("Bar"); |
| assertEquals("Bar", converter.getConverterClass()); |
| converterAnnotation = (StructConverterAnnotation) attributeResource.getSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| assertEquals("Bar", converterAnnotation.getConverter()); |
| } |
| |
| public void testGetConverterClassUpdatesFromResourceModelChange() throws Exception { |
| createTestEntityWithConvertAndStructConverterClass(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); |
| Convert eclipseLinkConvert = (Convert) basicMapping.getConverter(); |
| StructConverter converter = (StructConverter) eclipseLinkConvert.getConverter(); |
| |
| assertEquals("Foo", converter.getConverterClass()); |
| |
| JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); |
| StructConverterAnnotation converterAnnotation = (StructConverterAnnotation) attributeResource.getSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| converterAnnotation.setConverter("Bar"); |
| assertEquals("Bar", converter.getConverterClass()); |
| |
| attributeResource.removeSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| assertEquals(null, eclipseLinkConvert.getConverter()); |
| |
| converterAnnotation = (StructConverterAnnotation) attributeResource.addSupportingAnnotation(StructConverterAnnotation.ANNOTATION_NAME); |
| assertNotNull(eclipseLinkConvert.getConverter()); |
| |
| converterAnnotation.setConverter("FooBar"); |
| assertEquals("FooBar", ((StructConverter) eclipseLinkConvert.getConverter()).getConverterClass()); |
| } |
| } |