Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 960c15f69dc4b6609d79948b4b122a1236450068 (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
/*******************************************************************************
 * 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.util.Map.Entry;

import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.ecore.EObject;
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 EMapTest extends LibraryJPATest {

	public EMapTest(String name) {
		super(name);
	}

	public void testInsertLibraryAndBooksWithCache() throws Exception {
		boolean checkCache = true;
		verifyInsertLibraryAndBooks(checkCache);
	}

	public void testInsertLibaryAndBooksNoCache() throws Exception {
		boolean checkCache = false;
		verifyInsertLibraryAndBooks(checkCache);
	}

	private void verifyInsertLibraryAndBooks(boolean checkCache) {
		beginTransaction();
		Library library = createAnonymousLibrary(em);
		String libraryName = library.getName();
		Book book = createAnonymousBook(em);
		String bookTitle = book.getTitle();
		library.getBooks().put(bookTitle, book);
		commitTransaction();
		// verify
		if (!checkCache) {
			reinitializeCachesAndEntityManager();
		}
		Library actualLibrary = findLibraryWithName(em, libraryName);
		assertNotNull("library", actualLibrary);
		Book actualBook = findBookWithTitle(em, bookTitle);
		assertNotNull("book", actualBook);
		EMap<String, Book> actualBooks = actualLibrary.getBooks();
		int numberActualBooks = actualBooks.size();
		assertEquals("number of book", 1, numberActualBooks);
		EObject actualLibraryBook = actualBooks.get(bookTitle);
		assertEquals("library book", actualBook, actualLibraryBook);

		EObject bookContainer = actualBook.eContainer();
		EObject libraryBookContainer = actualLibraryBook.eContainer();
		assertEquals("book container", bookContainer, libraryBookContainer);

		EObject bookEntryContainer = bookContainer.eContainer();
		EObject libraryBookEntryContainer = libraryBookContainer.eContainer();
		assertEquals("entry container", bookEntryContainer, libraryBookEntryContainer);
		assertEquals("entry container", actualLibrary, bookEntryContainer);
		assertEquals("entry container", actualLibrary, libraryBookEntryContainer);
	}

	public void testAddBookToLibraryWithCache() throws Exception {
		boolean checkCache = true;
		verifyAddBookToLibrary(checkCache);
	}

	public void testAddBookToLibraryNoCache() throws Exception {
		boolean checkCache = false;
		verifyAddBookToLibrary(checkCache);
	}

	private void verifyAddBookToLibrary(boolean checkCache) {
		beginTransaction();
		// setup fixture
		Library library = createAnonymousLibrary(em);
		Writer writer = createAnonymousWriter(em);
		Book bookOne = createAnonymousBookForWriter(em, writer);
		String bookOneTitle = bookOne.getTitle();
		library.getBooks().put(bookOneTitle, bookOne);
		library.getWriters().add(writer);
		String libraryName = library.getName();
		String writerName = writer.getName();
		commitTransaction();
		// exercise SUT
		if (!checkCache) {
			reinitializeCachesAndEntityManager();
		}

		beginTransaction();
		library = findLibraryWithName(em, libraryName);
		writer = findWriterWithName(em, writerName);
		Book bookTwo = createAnonymousBookForWriter(em, writer);
		String bookTwoTitle = bookTwo.getTitle();
		library.getBooks().put(bookTwoTitle, bookTwo);
		commitTransaction();

		// verify - cache
		if (!checkCache) {
			reinitializeCachesAndEntityManager();
		}

		Library actualLibrary = findLibraryWithName(em, libraryName);
		Book actualBookOne = findBookWithTitle(em, bookOneTitle);
		Book actualBookTwo = findBookWithTitle(em, bookTwoTitle);
		EMap<String, Book> libraryBooks = actualLibrary.getBooks();
		assertEquals("number of books", 2, libraryBooks.size());
		assertEquals("first library book", actualBookOne, libraryBooks.get(bookOneTitle));
		assertEquals("second library book", actualBookTwo, libraryBooks.get(bookTwoTitle));
	}

	public void testRemoveBookFromLibraryWithCache() throws Exception {
		boolean checkCache = true;
		verifyRemoveBookFromLibrary(checkCache);
	}

	public void testRemoveBookFromLibraryNoCache() throws Exception {
		boolean checkCache = false;
		verifyRemoveBookFromLibrary(checkCache);
	}

	private void verifyRemoveBookFromLibrary(boolean checkCache) {
		beginTransaction();
		// fixture
		Writer writer = createAnonymousWriter(em);
		Book bookOne = createAnonymousBookForWriter(em, writer);
		Book bookTwo = createAnonymousBookForWriter(em, writer);
		Library library = createAnonymousLibrary(em);
		library.getBooks().put(bookOne.getTitle(), bookOne);
		library.getBooks().put(bookTwo.getTitle(), bookTwo);
		String libraryName = library.getName();
		String bookOneTitle = bookOne.getTitle();
		String bookTwoTitle = bookTwo.getTitle();
		commitTransaction();
		// exercise SUT
		if (!checkCache) {
			reinitializeCachesAndEntityManager();
		}
		beginTransaction();
		library = findLibraryWithName(em, libraryName);
		bookTwo = findBookWithTitle(em, bookTwoTitle);
		Book bookTwoFromLibrary = library.getBooks().removeKey(bookTwoTitle);
		assertEquals(bookTwo, bookTwoFromLibrary);
		// TODO: ResourceImpl will get callback to delete MapEntry
		EObject bookTwoEContainer = bookTwo.eContainer();
		if ((bookTwoEContainer != null) && (bookTwoEContainer instanceof Entry)) {
			em.remove(bookTwoEContainer);
		}
		em.remove(bookTwo);
		commitTransaction();
		// verify
		if (!checkCache) {
			reinitializeCachesAndEntityManager();
		}

		Library actualLibrary = findLibraryWithName(em, libraryName);
		Book actualBookOne = findBookWithTitle(em, bookOneTitle);
		EMap<String, Book> actualBooks = actualLibrary.getBooks();
		assertEquals("number of books", 1, actualBooks.size());
		Book actualLibraryBook = actualBooks.get(bookOneTitle);
		assertEquals("book in library", actualBookOne, actualLibraryBook);
	}

}

Back to the top