diff options
Diffstat (limited to 'plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/CustomTypeMappingTest.java')
-rw-r--r-- | plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/CustomTypeMappingTest.java | 296 |
1 files changed, 148 insertions, 148 deletions
diff --git a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/CustomTypeMappingTest.java b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/CustomTypeMappingTest.java index 18a07b38e6..c1095474cc 100644 --- a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/CustomTypeMappingTest.java +++ b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/CustomTypeMappingTest.java @@ -1,148 +1,148 @@ -/**
- * Copyright (c) 2004 - 2010 Eike Stepper (Berlin, Germany) and others.
- * 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:
- * Stefan Winkler - initial API and implementation
- * Stefan Winkler - Bug 285426: [DB] Implement user-defined typeMapping support
- */
-package org.eclipse.emf.cdo.tests.db;
-
-import org.eclipse.emf.cdo.common.model.EMFUtil;
-import org.eclipse.emf.cdo.eresource.CDOResource;
-import org.eclipse.emf.cdo.server.db.mapping.AbstractTypeMapping;
-import org.eclipse.emf.cdo.server.db.mapping.AbstractTypeMappingFactory;
-import org.eclipse.emf.cdo.server.db.mapping.ITypeMapping;
-import org.eclipse.emf.cdo.server.internal.db.mapping.TypeMappingUtil;
-import org.eclipse.emf.cdo.session.CDOSession;
-import org.eclipse.emf.cdo.tests.AbstractCDOTest;
-import org.eclipse.emf.cdo.tests.db.verifier.DBStoreVerifier;
-import org.eclipse.emf.cdo.transaction.CDOTransaction;
-import org.eclipse.emf.cdo.util.CDOUtil;
-import org.eclipse.emf.cdo.util.CommitException;
-
-import org.eclipse.net4j.db.DBType;
-import org.eclipse.net4j.db.DBUtil;
-import org.eclipse.net4j.util.container.IPluginContainer;
-
-import org.eclipse.emf.ecore.EAnnotation;
-import org.eclipse.emf.ecore.EAttribute;
-import org.eclipse.emf.ecore.EClass;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.EPackage;
-import org.eclipse.emf.ecore.EcoreFactory;
-import org.eclipse.emf.ecore.EcorePackage;
-import org.eclipse.emf.ecore.util.EcoreUtil;
-
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-
-/**
- * @author Stefan Winkler
- */
-public class CustomTypeMappingTest extends AbstractCDOTest
-{
-
- public void testCustomTypeMapping() throws CommitException
- {
- // manually register type mapping
- IPluginContainer.INSTANCE.registerFactory(new MyIntToVarcharTypeMapping.Factory());
-
- EPackage pkg = EMFUtil.createEPackage("underscoreTest", "uct", "http://cdo.eclipse.org/tests/underscoreTest.ecore");
- EClass cls = EMFUtil.createEClass(pkg, "foo", false, false);
- EAttribute att = EMFUtil.createEAttribute(cls, "bar", EcorePackage.eINSTANCE.getEInt());
-
- // annotate type mapping and column type
- EAnnotation annotation = EcoreFactory.eINSTANCE.createEAnnotation();
- annotation.setSource("http://www.eclipse.org/CDO/DBStore");
- annotation.getDetails().put("typeMapping", "org.eclipse.emf.cdo.tests.db.EIntToVarchar");
- annotation.getDetails().put("columnType", DBType.VARCHAR.getKeyword());
- att.getEAnnotations().add(annotation);
-
- if (!isConfig(LEGACY))
- {
- CDOUtil.prepareDynamicEPackage(pkg);
- }
-
- CDOSession session = openSession();
- CDOTransaction transaction = session.openTransaction();
- CDOResource resource = transaction.createResource("/test");
-
- EObject obj = EcoreUtil.create(cls);
- obj.eSet(att, 42);
-
- resource.getContents().add(obj);
- transaction.commit();
- transaction.close();
- session.close();
-
- msg("Check if type was mapped to string...");
- new DBStoreVerifier(getRepository())
- {
- @Override
- protected void doVerify() throws Exception
- {
- Statement stmt = null;
- ResultSet rset = null;
- try
- {
- stmt = getStatement();
- rset = stmt.executeQuery("SELECT bar FROM foo");
- assertEquals("java.lang.String", rset.getMetaData().getColumnClassName(1));
-
- rset.next();
- assertEquals("2a", rset.getString(1));
- }
- finally
- {
- DBUtil.close(rset);
- DBUtil.close(stmt);
- }
- }
- }.verify();
- }
-
- public static class MyIntToVarcharTypeMapping extends AbstractTypeMapping
- {
- public static final ITypeMapping.Descriptor DESCRIPTOR = TypeMappingUtil.createDescriptor(
- "org.eclipse.emf.cdo.tests.db.EIntToVarchar", EcorePackage.eINSTANCE.getEInt(), DBType.VARCHAR);
-
- public static class Factory extends AbstractTypeMappingFactory
- {
- public Factory()
- {
- super(DESCRIPTOR);
- }
-
- @Override
- public ITypeMapping create(String description)
- {
- return new MyIntToVarcharTypeMapping();
- }
- }
-
- public MyIntToVarcharTypeMapping()
- {
- super();
- }
-
- @Override
- protected void doSetValue(PreparedStatement stmt, int index, Object value) throws SQLException
- {
- Integer val = (Integer)value;
- stmt.setString(index, Integer.toHexString(val));
- }
-
- @Override
- protected Object getResultSetValue(ResultSet resultSet) throws SQLException
- {
- String stringVal = resultSet.getString(getField().getName());
- return Integer.parseInt(stringVal, 16);
- }
- }
-}
+/** + * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others. + * 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: + * Stefan Winkler - initial API and implementation + * Stefan Winkler - Bug 285426: [DB] Implement user-defined typeMapping support + */ +package org.eclipse.emf.cdo.tests.db; + +import org.eclipse.emf.cdo.common.model.EMFUtil; +import org.eclipse.emf.cdo.eresource.CDOResource; +import org.eclipse.emf.cdo.server.db.mapping.AbstractTypeMapping; +import org.eclipse.emf.cdo.server.db.mapping.AbstractTypeMappingFactory; +import org.eclipse.emf.cdo.server.db.mapping.ITypeMapping; +import org.eclipse.emf.cdo.server.internal.db.mapping.TypeMappingUtil; +import org.eclipse.emf.cdo.session.CDOSession; +import org.eclipse.emf.cdo.tests.AbstractCDOTest; +import org.eclipse.emf.cdo.tests.db.verifier.DBStoreVerifier; +import org.eclipse.emf.cdo.transaction.CDOTransaction; +import org.eclipse.emf.cdo.util.CDOUtil; +import org.eclipse.emf.cdo.util.CommitException; + +import org.eclipse.net4j.db.DBType; +import org.eclipse.net4j.db.DBUtil; +import org.eclipse.net4j.util.container.IPluginContainer; + +import org.eclipse.emf.ecore.EAnnotation; +import org.eclipse.emf.ecore.EAttribute; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.EcoreFactory; +import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.ecore.util.EcoreUtil; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +/** + * @author Stefan Winkler + */ +public class CustomTypeMappingTest extends AbstractCDOTest +{ + + public void testCustomTypeMapping() throws CommitException + { + // manually register type mapping + IPluginContainer.INSTANCE.registerFactory(new MyIntToVarcharTypeMapping.Factory()); + + EPackage pkg = EMFUtil.createEPackage("underscoreTest", "uct", "http://cdo.eclipse.org/tests/underscoreTest.ecore"); + EClass cls = EMFUtil.createEClass(pkg, "foo", false, false); + EAttribute att = EMFUtil.createEAttribute(cls, "bar", EcorePackage.eINSTANCE.getEInt()); + + // annotate type mapping and column type + EAnnotation annotation = EcoreFactory.eINSTANCE.createEAnnotation(); + annotation.setSource("http://www.eclipse.org/CDO/DBStore"); + annotation.getDetails().put("typeMapping", "org.eclipse.emf.cdo.tests.db.EIntToVarchar"); + annotation.getDetails().put("columnType", DBType.VARCHAR.getKeyword()); + att.getEAnnotations().add(annotation); + + if (!isConfig(LEGACY)) + { + CDOUtil.prepareDynamicEPackage(pkg); + } + + CDOSession session = openSession(); + CDOTransaction transaction = session.openTransaction(); + CDOResource resource = transaction.createResource("/test"); + + EObject obj = EcoreUtil.create(cls); + obj.eSet(att, 42); + + resource.getContents().add(obj); + transaction.commit(); + transaction.close(); + session.close(); + + msg("Check if type was mapped to string..."); + new DBStoreVerifier(getRepository()) + { + @Override + protected void doVerify() throws Exception + { + Statement stmt = null; + ResultSet rset = null; + try + { + stmt = getStatement(); + rset = stmt.executeQuery("SELECT bar FROM foo"); + assertEquals("java.lang.String", rset.getMetaData().getColumnClassName(1)); + + rset.next(); + assertEquals("2a", rset.getString(1)); + } + finally + { + DBUtil.close(rset); + DBUtil.close(stmt); + } + } + }.verify(); + } + + public static class MyIntToVarcharTypeMapping extends AbstractTypeMapping + { + public static final ITypeMapping.Descriptor DESCRIPTOR = TypeMappingUtil.createDescriptor( + "org.eclipse.emf.cdo.tests.db.EIntToVarchar", EcorePackage.eINSTANCE.getEInt(), DBType.VARCHAR); + + public static class Factory extends AbstractTypeMappingFactory + { + public Factory() + { + super(DESCRIPTOR); + } + + @Override + public ITypeMapping create(String description) + { + return new MyIntToVarcharTypeMapping(); + } + } + + public MyIntToVarcharTypeMapping() + { + super(); + } + + @Override + protected void doSetValue(PreparedStatement stmt, int index, Object value) throws SQLException + { + Integer val = (Integer)value; + stmt.setString(index, Integer.toHexString(val)); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet) throws SQLException + { + String stringVal = resultSet.getString(getField().getName()); + return Integer.parseInt(stringVal, 16); + } + } +} |