Skip to main content
summaryrefslogtreecommitdiffstats
blob: 66899500daf771b5569f9017d0905816c60afbec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007 Springsite BV (The Netherlands) 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:
 *   Martin Taal
 * </copyright>
 *
 * $Id: JoinColumnsAction.java,v 1.4 2007/02/01 12:35:36 mtaal Exp $
 */

package org.eclipse.emf.teneo.test.emf.annotations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.annotations.pannotation.InheritanceType;
import org.eclipse.emf.teneo.samples.emf.annotations.joincolumns.Child;
import org.eclipse.emf.teneo.samples.emf.annotations.joincolumns.JoincolumnsFactory;
import org.eclipse.emf.teneo.samples.emf.annotations.joincolumns.JoincolumnsPackage;
import org.eclipse.emf.teneo.samples.emf.annotations.joincolumns.Parent;
import org.eclipse.emf.teneo.test.AbstractTestAction;
import org.eclipse.emf.teneo.test.StoreTestException;
import org.eclipse.emf.teneo.test.stores.TestStore;

/**
 * Testcase
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.4 $
 */
public class JoinColumnsAction extends AbstractTestAction {
	/**
	 * Constructor
	 */
	public JoinColumnsAction() {
		super(JoincolumnsPackage.eINSTANCE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.test.AbstractTestAction#getExtraConfigurationProperties()
	 */
	public Properties getExtraConfigurationProperties() {
		final Properties props = new Properties();
		props.setProperty(PersistenceOptions.VERSION_COLUMN_NAME, "myversion");
		return props;
	}

	/** Creates an item, an address and links them to a po. */
	public void doAction(TestStore store) {
		final JoincolumnsFactory factory = JoincolumnsFactory.eINSTANCE;
		{
			store.beginTransaction();
			Parent parent = factory.createParent();
			parent.setFirstName("John");
			parent.setLastName("Smith");
			Child child1 = factory.createChild();
			child1.setFirstName("Johnny");
			child1.setLastName("Smith");
			parent.getChildren().add(child1);
			Child child2 = factory.createChild();
			child2.setFirstName("Jane");
			child2.setLastName("Smith");
			parent.getChildren().add(child2);
			store.store(parent);
			store.commitTransaction();
		}

		// read again
		{
			store.beginTransaction();
			Parent parent = (Parent) store.getObject(Parent.class);
			assertEquals(2, parent.getChildren().size());
			assertEquals("Johnny", ((Child) parent.getChildren().get(0)).getFirstName());
			assertEquals("Jane", ((Child) parent.getChildren().get(1)).getFirstName());
			store.commitTransaction();
		}

		// check version column and foreign keys
		Connection conn = null;
		Statement stmt = null;
		try {
			try {
				conn = store.getConnection();
				stmt = conn.createStatement();

				checkVersion(store);

				// depending on the inheritance strategy the foreign key is stored in different table
				if (store.getInheritanceType().getValue() == InheritanceType.SINGLE_TABLE) {
					ResultSet rs = stmt.executeQuery("select myParentFirstName from person");
					assertTrue(rs.next());
				} else {
					ResultSet rs = stmt.executeQuery("select myParentFirstName from child");
					assertTrue(rs.next());
				}
			} finally {
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
		} catch (SQLException e) {
			throw new StoreTestException("Sql exception when checking db schema", e);
		}
	}

	/** Checks the version column */
	protected void checkVersion(TestStore store) {
	}
}

Back to the top