Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d6b04f50bbb06e4e387a85d01fc29f3722d52509 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
 * <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.HbDataStore;
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;
import org.hibernate.Query;

/**
 * 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);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.test.AbstractTestAction#getExtraConfigurationProperties ()
	 */
	@Override
	public Properties getExtraConfigurationProperties() {
		final Properties props = new Properties();
		// props.put(PersistenceOptions.AUDITING_DB_SCHEMA, "dbschema");
		props.put(PersistenceOptions.AUDITING_PRUNE_OLD_ENTRIES_DAYS, "1");
		props.put(PersistenceOptions.AUDITING_PRUNE_COMMIT_INTERVAL, "10");
		props.setProperty(PersistenceOptions.ENABLE_AUDITING, "true");
		return props;
	}

	/** Creates an item, an address and links them to a po. */
	@Override
	public void doAction(TestStore store) {
		System.err.println(store.getMappingXML());

		testSimpleChange(store);
		testContainer(store);
		testQuery(store);
		testEntryPruning(store);
	}

	private void testSimpleChange(TestStore store) {
		int name = 0;
		{
			store.beginTransaction();
			final Writer writer = LibraryFactory.eINSTANCE.createWriter();
			writer.setName(name++ + "");
			store.store(writer);
			store.commitTransaction();
		}
		{
			store.beginTransaction();
			final Writer writer = store.getObject(Writer.class);
			writer.setName(name++ + "");
			store.store(writer);
			store.commitTransaction();
		}
		{
			store.beginTransaction();
			final Writer writer = store.getObject(Writer.class);
			writer.setName(name++ + "");
			store.store(writer);
			store.commitTransaction();
		}
		{
			store.beginTransaction();
			final Writer writer = store.getObject(Writer.class);
			writer.setName(name++ + "");
			store.store(writer);
			store.commitTransaction();
		}
		{
			store.beginTransaction();
			final Writer writer = store.getObject(Writer.class);
			writer.setName(name++ + "");
			store.store(writer);
			store.commitTransaction();
		}
		{
			store.beginTransaction();
			final Writer writer = store.getObject(Writer.class);
			writer.setName(name++ + "");
			final Book bk1 = LibraryFactory.eINSTANCE.createBook();
			bk1.setTitle("1-simple");
			bk1.setPages(0);
			final Book bk2 = LibraryFactory.eINSTANCE.createBook();
			bk2.setTitle("2-simple");
			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("" + name++);
				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);
			writer.setName(name++ + "");
			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() == 12);
			for (int i = 0; i < 12; i++) {
				if (i == 0) {
					assertTrue(revisions.get(i).getTeneo_audit_kind() == TeneoAuditKind.ADD);
				} else if (i == 11) {
					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 < 12; i++) {
				final TeneoAuditEntry audit = revisions.get(i);
				final Writer testW = (Writer) vp.getRevision(LibraryPackage.eINSTANCE.getWriter(), id,
						audit.getTeneo_start());

				System.err.println(testW.getName());
				if (audit.getTeneo_audit_kind() != TeneoAuditKind.DELETE) {
					assertEquals("" + i, testW.getName());
				}
				if (testW.getBooks().size() > 0) {
					// check the books that they are in the correct revision to
					assertFalse(testW.getBooks().get(0).eIsProxy());
					assertFalse(testW.getBooks().get(1).eIsProxy());
					assertEquals(i - 5, testW.getBooks().get(0).getPages());
					assertEquals(i - 5, testW.getBooks().get(1).getPages());
				}
			}

			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();
		}
	}

	private void testQuery(TestStore store) {

		Writer w = null;
		{
			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("0");
			bk1.setPages(0);
			writer.getBooks().add(bk1);
			w = writer;
			library.getWriters().add(writer);
			library.getBooks().add(bk1);
			store.store(library);
			store.store(bk1);
			store.commitTransaction();
		}
		{
			store.beginTransaction();
			final Book bk = store.getObject(Book.class);
			bk.setPages(1);
			store.commitTransaction();
		}
		{
			final HibernateTestStore testStore = (HibernateTestStore) store;
			{
				store.beginTransaction();
				final AuditVersionProvider vp = testStore.getEmfDataStore().getAuditVersionProvider();
				final Query qry = testStore.getSession().createQuery(
						"select e from " + getAuditEntityName("Book") + " e where author=:author");
				final String idString = vp.getIdString(w);
				qry.setParameter("author", idString);
				final List<?> list = qry.list();
				assertTrue(list.size() == 2);
				Book bk1 = (Book) vp.getRevision((TeneoAuditEntry) list.get(0));
				assertTrue(bk1.getPages() == 0);
				Book bk2 = (Book) vp.getRevision((TeneoAuditEntry) list.get(1));
				assertTrue(bk2.getPages() == 1);
				store.commitTransaction();
			}
			{
				store.beginTransaction();
				final AuditVersionProvider vp = testStore.getEmfDataStore().getAuditVersionProvider();
				final Query qry = testStore.getSession().createQuery(
						"select e from " + getAuditEntityName("Book") + " e where pages=1 and author=:author");
				qry.setParameter("author", vp.getIdString(w));
				final List<?> list = qry.list();
				assertTrue(list.size() == 1);
				Book bk1 = (Book) vp.getRevision((TeneoAuditEntry) list.get(0));
				assertTrue(bk1.getPages() == 1);
				store.commitTransaction();
			}
		}
	}

	private void testEntryPruning(TestStore store) {
		{
			store.beginTransaction();
			final Library library = store.getObject(Library.class);
			store.deleteObject(library);
			store.commitTransaction();
		}

		final HibernateTestStore testStore = (HibernateTestStore) store;
		final HbDataStore dataStore = testStore.getEmfDataStore();
		final long currentPruneTime = dataStore.getAuditProcessHandler().getPruneTime();
		assertTrue(currentPruneTime == (24 * 1000 * 3600));
		dataStore.getAuditProcessHandler().setPruneTime(500);
		Object id = null;
		{
			store.beginTransaction();
			final Library library = LibraryFactory.eINSTANCE.createLibrary();
			library.setName("abc");
			store.store(library);
			store.commitTransaction();
			id = IdentifierCacheHandler.getInstance().getID(library);
		}
		{
			for (int i = 0; i < 8; i++) {
				store.beginTransaction();
				final Library library = store.getObject(Library.class);
				library.setName("def" + i + System.currentTimeMillis());
				store.store(library);
				store.commitTransaction();
			}
		}

		int size1 = 0;
		{
			store.beginTransaction();
			final AuditVersionProvider vp = testStore.getEmfDataStore().getAuditVersionProvider();
			final List<TeneoAuditEntry> entries = vp.getAllAuditEntries(
					LibraryPackage.eINSTANCE.getLibrary(), id);
			size1 = entries.size();
			store.commitTransaction();
		}
		try {
			Thread.sleep(500);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		{
			for (int i = 0; i < 5; i++) {
				store.beginTransaction();
				final Library library = store.getObject(Library.class);
				library.setName("def" + i + System.currentTimeMillis());
				store.store(library);
				store.commitTransaction();
			}
		}
		{
			store.beginTransaction();
			final AuditVersionProvider vp = testStore.getEmfDataStore().getAuditVersionProvider();
			final List<TeneoAuditEntry> entries = vp.getAllAuditEntries(
					LibraryPackage.eINSTANCE.getLibrary(), id);
			assertTrue(size1 > entries.size());
			store.commitTransaction();
		}
	}

	protected String getAuditEntityName(String eClassName) {
		return eClassName + "Auditing";
	}
}

Back to the top