Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 62faaf6990f4fbf19ca03c4464c397e44f5d34e7 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
 * <copyright> Copyright (c) 2005, 2006, 2007, 2008 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:
 * DynamicAction.java,v 1.4 2007/03/20 23:33:38 mtaal Exp $
 */

package org.eclipse.emf.teneo.hibernate.test.emf.auditing;

import java.util.List;
import java.util.Properties;

import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.hibernate.auditing.AuditVersionProvider;
import org.eclipse.emf.teneo.hibernate.auditing.model.teneoauditing.TeneoAuditEntry;
import org.eclipse.emf.teneo.hibernate.auditing.model.teneoauditing.TeneoAuditKind;
import org.eclipse.emf.teneo.hibernate.mapping.identifier.IdentifierCacheHandler;
import org.eclipse.emf.teneo.hibernate.test.stores.HibernateTestStore;
import org.eclipse.emf.teneo.samples.emf.sample.library.Book;
import org.eclipse.emf.teneo.samples.emf.sample.library.Library;
import org.eclipse.emf.teneo.samples.emf.sample.library.LibraryFactory;
import org.eclipse.emf.teneo.samples.emf.sample.library.LibraryPackage;
import org.eclipse.emf.teneo.samples.emf.sample.library.Writer;
import org.eclipse.emf.teneo.test.AbstractTestAction;
import org.eclipse.emf.teneo.test.stores.TestStore;

/**
 * Simple testcase to test auditing.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.17 $
 */
public class SimpleLibraryAuditingAction extends AbstractTestAction {
	/**
	 * Constructor.
	 * 
	 * @param arg0
	 */
	public SimpleLibraryAuditingAction() {
		super(LibraryPackage.eINSTANCE);
	}

	@Override
	public Properties getExtraConfigurationProperties() {
		final Properties props = new Properties();
		props.put(PersistenceOptions.ENABLE_AUDITING, "true");
		return props;
	}

	/** Creates an item, an address and links them to a po. */
	@Override
	public void doAction(TestStore store) {
		final LibraryFactory factory = LibraryFactory.eINSTANCE;

		testSimpleChange(store);
		testContainer(store);
	}

	private void testSimpleChange(TestStore store) {
		{
			store.beginTransaction();
			final Writer writer = LibraryFactory.eINSTANCE.createWriter();
			writer.setName("0");
			final Book bk1 = LibraryFactory.eINSTANCE.createBook();
			bk1.setTitle("1");
			bk1.setPages(0);
			final Book bk2 = LibraryFactory.eINSTANCE.createBook();
			bk2.setTitle("2");
			bk2.setPages(0);
			writer.getBooks().add(bk1);
			writer.getBooks().add(bk2);
			store.store(writer);
			store.store(bk1);
			store.store(bk2);
			store.commitTransaction();
			final Object id = IdentifierCacheHandler.getInstance().getID(writer);
			for (int i = 0; i < 5; i++) {
				store.beginTransaction();
				final Writer w = store.getObject(Writer.class);
				w.setName("" + (i + 1));
				w.getBooks().get(0).setPages(i + 1);
				w.getBooks().get(1).setPages(i + 1);
				store.commitTransaction();
			}

			// and delete
			store.beginTransaction();
			final Writer w = store.getObject(Writer.class);
			store.deleteObject(w.getBooks().get(1));
			store.deleteObject(w.getBooks().get(0));
			store.deleteObject(w);
			store.commitTransaction();

			store.beginTransaction();
			final HibernateTestStore testStore = (HibernateTestStore) store;
			final AuditVersionProvider auditVersionProvider = testStore.getEmfDataStore()
					.getAuditVersionProvider();
			final List<TeneoAuditEntry> revisions = auditVersionProvider.getAllAuditEntries(
					LibraryPackage.eINSTANCE.getWriter(), id);
			assertTrue(revisions.size() == 7);
			for (int i = 0; i < 7; i++) {
				if (i == 0) {
					assertTrue(revisions.get(i).getTeneo_audit_kind() == TeneoAuditKind.ADD);
				} else if (i == 6) {
					assertTrue(revisions.get(i).getTeneo_audit_kind() == TeneoAuditKind.DELETE);
				} else {
					assertTrue(revisions.get(i).getTeneo_audit_kind() == TeneoAuditKind.UPDATE);
				}
			}

			// now get the revisions one by one, not the last one
			final AuditVersionProvider vp = testStore.getEmfDataStore().getAuditVersionProvider();
			for (int i = 0; i < 6; i++) {
				final TeneoAuditEntry audit = revisions.get(i);
				final Writer testW = (Writer) vp.getRevision(LibraryPackage.eINSTANCE.getWriter(), id,
						audit.getTeneo_start());

				assertTrue(testW.getName().equals("" + i));
				// check the books that they are in the correct revision to
				assertFalse(testW.getBooks().get(0).eIsProxy());
				assertFalse(testW.getBooks().get(1).eIsProxy());
				assertTrue(testW.getBooks().get(0).getPages() == i);
				assertTrue(testW.getBooks().get(1).getPages() == i);
			}

			store.commitTransaction();
		}
	}

	private void testContainer(TestStore store) {

		{
			store.beginTransaction();
			final Library library = LibraryFactory.eINSTANCE.createLibrary();
			library.setName("0");
			final Writer writer = LibraryFactory.eINSTANCE.createWriter();
			writer.setName("0");
			final Book bk1 = LibraryFactory.eINSTANCE.createBook();
			bk1.setTitle("1");
			bk1.setPages(0);
			writer.getBooks().add(bk1);
			library.getWriters().add(writer);
			library.getBooks().add(bk1);
			store.store(library);
			store.store(bk1);
			store.commitTransaction();
			final Object id = IdentifierCacheHandler.getInstance().getID(writer);
			for (int i = 0; i < 5; i++) {
				store.beginTransaction();
				final Writer w = store.getObject(Writer.class);
				w.setName("" + (i + 1));
				w.getBooks().get(0).setPages(i + 1);
				store.commitTransaction();
			}

			{
				// and delete
				store.beginTransaction();
				final Library l = store.getObject(Library.class);
				store.deleteObject(l);
				store.commitTransaction();
			}

			store.beginTransaction();
			final HibernateTestStore testStore = (HibernateTestStore) store;
			final AuditVersionProvider auditVersionProvider = testStore.getEmfDataStore()
					.getAuditVersionProvider();
			final List<TeneoAuditEntry> auditEntries = auditVersionProvider.getAllAuditEntries(
					LibraryPackage.eINSTANCE.getWriter(), id);

			// now get the revisions one by one, not the last one
			final AuditVersionProvider vp = testStore.getEmfDataStore().getAuditVersionProvider();
			for (int i = 0; i < 6; i++) {
				final TeneoAuditEntry audit = auditEntries.get(i);
				final Writer testW = (Writer) vp.getRevision(LibraryPackage.eINSTANCE.getWriter(), id,
						audit.getTeneo_start());
				assertTrue(testW.eContainer() instanceof Library);
			}

			store.commitTransaction();
		}
	}

}

Back to the top