Skip to main content
summaryrefslogtreecommitdiffstats
blob: d1d48e6bf3cf5d1edfec8c64872a6112ca73e2a2 (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
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006 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: DetachEListAction.java,v 1.2 2006/09/21 00:57:18 mtaal Exp $
 */

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

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.teneo.samples.emf.detach.detachelist.Contacts;
import org.eclipse.emf.teneo.samples.emf.detach.detachelist.DetachelistFactory;
import org.eclipse.emf.teneo.samples.emf.detach.detachelist.DetachelistPackage;
import org.eclipse.emf.teneo.samples.emf.detach.detachelist.Person;
import org.eclipse.emf.teneo.test.AbstractTestAction;
import org.eclipse.emf.teneo.test.StoreTestException;
import org.eclipse.emf.teneo.test.stores.TestStore;

/**
 * Is a test case to test detach functionality in the dao resource using an elist, checks insert, move, etc.
 *  
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.2 $
*/
public class DetachEListAction extends AbstractTestAction 
{

	/** Constructor */
	public DetachEListAction()
	{
		super(DetachelistPackage.eINSTANCE);
	}
	
	/** Stores a TopModel Object */
	public void doAction(TestStore store)
	{
		try
		{
			DetachelistFactory factory = DetachelistFactory.eINSTANCE;

			{
	    		final Resource resource = store.getResource();
	    		
			    resource.load (Collections.EMPTY_MAP);
			    assertTrue(resource.getContents().size() == 0);
	
			    // add test data
			    Contacts contacts = factory.createContacts();
			    Person person1 = factory.createPerson();
				person1.setName("001");
			    Person person2 = factory.createPerson();
				person2.setName("002");
				
				contacts.getPersons().add(person1);    	 
				contacts.getPersons().add(person2);    	 
				resource.getContents().add(contacts);
				resource.getContents().add(person1);
				resource.getContents().add(person2);
				
				// this prevents a null constraint exc in hibernate
				//assertEquals(0, contacts.getContainedPersons().size());
				resource.save(Collections.EMPTY_MAP);

				contacts = (Contacts)resource.getContents().get(0);
				person1 = (Person) contacts.getPersons().get(0);
				person2 = (Person) contacts.getPersons().get(1);

				contacts.getPersons().clear();
				contacts.getPersons().add(person1);
				contacts.getPersons().add(person2);
				resource.save(Collections.EMPTY_MAP);       
				resource.unload();
			}

			assertFalse(store.transactionActive());
			
			{
	    		Resource resource = store.getResource();
			    resource.load (Collections.EMPTY_MAP);
				Contacts contacts = getContacts(resource);
				Person person = (Person) contacts.getPersons().get(0);
				assertEquals("001", person.getName());
				person = (Person) contacts.getPersons().get(1);
				assertEquals("002", person.getName());
				resource.unload();
			}

			assertFalse(store.transactionActive());
			
			// now move everything around
			{
	    		Resource resource = store.getResource();
			    resource.load (Collections.EMPTY_MAP);
				Contacts contacts = getContacts(resource);
				Person person1 = (Person) contacts.getPersons().get(0);
				Person person2 = (Person) contacts.getPersons().get(1);
				contacts.getPersons().clear();
				contacts.getPersons().add(person2);
				contacts.getPersons().add(person1);
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}
			assertFalse(store.transactionActive());

			{
	    		Resource resource = store.getResource();
			    resource.load (Collections.EMPTY_MAP);
				Contacts contacts = getContacts(resource);
				Person person1 = (Person) contacts.getPersons().get(0);
				Person person2 = (Person) contacts.getPersons().get(1);
				assertTrue(person1.getName().compareTo("002") == 0);
				assertTrue(person2.getName().compareTo("001") == 0);
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}
			assertFalse(store.transactionActive());
			
			// add two and delete 1
			{
	    		Resource resource = store.getResource();
			    resource.load (Collections.EMPTY_MAP);
				Contacts contacts = getContacts(resource);
			    Person person3 = factory.createPerson();
				person3.setName("003");
			    Person person4 = factory.createPerson();
				person4.setName("004");
				
				contacts.getPersons().add(person3);
				contacts.getPersons().add(person4);
				contacts.getPersons().remove(1); // remove person 001
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}
			assertFalse(store.transactionActive());

			// check if this actually worked
			{
	    		Resource resource = store.getResource();
			    resource.load (Collections.EMPTY_MAP);
				Contacts contacts = getContacts(resource);
				assertEquals("002", ((Person) contacts.getPersons().get(0)).getName());
				assertEquals("003", ((Person) contacts.getPersons().get(1)).getName());
				assertEquals("004", ((Person) contacts.getPersons().get(2)).getName());
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}
			assertFalse(store.transactionActive());
			
			// do a set and move 
			{
	    		Resource resource = store.getResource();
			    resource.load (Collections.EMPTY_MAP);
				Contacts contacts = getContacts(resource);
				contacts.getPersons().move(0, 2); // order is now 004, 002, 003
				Person person5 = factory.createPerson();
				person5.setName("005");
				contacts.getPersons().set(2, person5); // order is now 004, 002, 005
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}
			assertFalse(store.transactionActive());

			// the order should be 004, 002, 005
			{
	    		Resource resource = store.getResource();
			    resource.load (Collections.EMPTY_MAP);
				Contacts contacts = getContacts(resource);
				assertEquals("004", ((Person) contacts.getPersons().get(0)).getName());
				assertEquals("002", ((Person) contacts.getPersons().get(1)).getName());
				assertEquals("005", ((Person) contacts.getPersons().get(2)).getName());
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}
			assertFalse(store.transactionActive());
			
			{
				Resource resource = store.getResource();
				resource.load(Collections.EMPTY_MAP);
				Contacts contacts = getContacts(resource);
			    Person person = factory.createPerson();
				person.setName("c1");
				contacts.getContainedPersons().add(person);
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}
			assertFalse(store.transactionActive());

			{
				Resource resource = store.getResource();
				resource.load(Collections.EMPTY_MAP);
				Contacts contacts = getContacts(resource);
				Person c1 = (Person) contacts.getContainedPersons().get(0);
			    Person person004_1 = factory.createPerson();
				person004_1.setName("004.1");
				c1.getChildren().add(person004_1);
				resource.save(Collections.EMPTY_MAP);
				contacts.getContainedPersons().remove(0);
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}
			{
				// check that the previous step actually deleted the persons
				store.beginTransaction();
				final List list = store.getObjects(Person.class);
				final Iterator it = list.iterator();
				while (it.hasNext()) {
					final Person person = (Person)it.next();
					assertTrue(person.getName().compareTo("c1") != 0 && person.getName().compareTo("004.1") != 0);
				}
				store.commitTransaction();
			}
			assertFalse(store.transactionActive());
		}
		catch (Exception e)
		{
			throw new StoreTestException("Exception during detach elist test", e);
		}
    }
	
	/** Get the contacts object */
	private Contacts getContacts(Resource resource)
	{
		final Iterator it = resource.getContents().iterator();
		while (it.hasNext())
		{
			final Object obj = it.next();
			if (obj instanceof Contacts) return (Contacts)obj;
		}
		return null;
	}
}

Back to the top