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
|
/*******************************************************************************
* Copyright (c) 2008 Oracle and Geensys.
* 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 and Geensys - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.teneo.eclipselink.examples.library.orm.tests;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
import org.eclipse.emf.teneo.eclipselink.examples.library.Book;
import org.eclipse.emf.teneo.eclipselink.examples.library.Library;
import org.eclipse.emf.teneo.eclipselink.examples.library.Writer;
public class LibraryXMIPersistenceTest extends LibraryJPATest {
public LibraryXMIPersistenceTest(String name) {
super(name);
}
public void testInsertWriterAndBooksAndLibraryWithCache() throws Exception {
boolean checkCache = true;
verifyInsertWriterBookLibrary(checkCache);
}
public void testInsertWriterAndBooksAndLibraryNoCache() throws Exception {
boolean checkCache = false;
verifyInsertWriterBookLibrary(checkCache);
}
/**
* The test case creates a library object model holding a library, writer and book object. It generates a XMI file
* using the build in persistency API of EMF. The first XMI file is generated directly after creation of the library
* object model. A second XMI file is created based on the library object model which is read back from the data
* base. Both files shall be identical.
*
* @param checkCache
*/
private void verifyInsertWriterBookLibrary(boolean checkCache) throws Exception {
beginTransaction();
Writer writer = createAnonymousWriterWithOneBook(em);
String writerName = writer.getName();
Book book = writer.getBooks().get(0);
String bookTitle = book.getTitle();
Library library = createAnonymousLibrary(em);
String libraryName = library.getName();
library.getBooks().put(book.getTitle(), book);
library.getWriters().add(writer);
File tempFile = File.createTempFile("temp", "library.xmi");
saveToXMIFile(library, tempFile);
commitTransaction();
// verify
if (!checkCache) {
reinitializeCachesAndEntityManager();
}
Writer actualWriter = findWriterWithName(em, writerName);
assertNotNull("writer", actualWriter);
assertEquals("writer name", writerName, actualWriter.getName());
Book actualBook = findBookWithTitle(em, bookTitle);
assertNotNull("book", actualBook);
assertEquals("number of writer books", 1, actualWriter.getBooks().size());
Book actualWriterBook = actualWriter.getBooks().get(0);
assertEquals("book/writer book", actualBook, actualWriterBook);
assertEquals("writer book", actualWriter.getBooks().get(0), actualBook);
Library actualLibrary = findLibraryWithName(em, libraryName);
Book actualLibraryBook = actualLibrary.getBooks().get(bookTitle);
assertEquals("book/library book", actualBook, actualLibraryBook);
Writer actualLibraryWriter = actualLibrary.getWriters().get(0);
assertEquals("writer/library writer", actualWriter, actualLibraryWriter);
// Start the writing to XMI
saveToXMIFile(actualLibrary, tempFile);
}
/**
* The private method saveToXMIFile allocates a resource set and stores the contents of the library object model to
* a XMI file.
*
* @param library
* The root object of the library object model
* @param fileURI
* The name of the file "file://C:/<file name>"
* @throws IOException
*/
private void saveToXMIFile(Library library, File aFile) throws IOException {
new ResourceSetImpl();
URI resURI = URI.createFileURI(aFile.getAbsolutePath());
XMIResource xmiResource = new XMIResourceImpl(resURI);
xmiResource.getContents().add(library);
FileWriter fw = new FileWriter(aFile);
xmiResource.save(fw, Collections.EMPTY_MAP);
fw.close();
}
}
|