Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 11ebdeb97138d415a3e330a6e84b2b3c11ded614 (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
/**
 * <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: DetachFeatureMapAction.java,v 1.6 2008/02/28 07:08:16 mtaal Exp $
 */

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.edit.command.CopyCommand;
import org.eclipse.emf.edit.command.CopyCommand.Helper;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;
import org.eclipse.emf.teneo.samples.emf.detach.detachfeaturemap.Contacts;
import org.eclipse.emf.teneo.samples.emf.detach.detachfeaturemap.DetachfeaturemapFactory;
import org.eclipse.emf.teneo.samples.emf.detach.detachfeaturemap.DetachfeaturemapPackage;
import org.eclipse.emf.teneo.samples.emf.detach.detachfeaturemap.Person;
import org.eclipse.emf.teneo.samples.emf.detach.detachfeaturemap.SpecialPerson;
import org.eclipse.emf.teneo.samples.emf.detach.detachfeaturemap.provider.DetachfeaturemapItemProviderAdapterFactory;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.resource.StoreResource;
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 for featuremap with different kinds of methods
 * on the featuremap (move, set, etc).
 *  
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.6 $ 
 */
public class DetachFeatureMapAction extends AbstractTestAction {

	/** Constructor */
	public DetachFeatureMapAction() {
		super(DetachfeaturemapPackage.eINSTANCE);
	}

	/**
	 * Says to use the mapping file, actually only relevant for hibernate
	 */
	@Override
	public Properties getExtraConfigurationProperties() {
		final Properties props = new Properties();
		props.put(PersistenceOptions.USE_MAPPING_FILE, "true");
		return props;
	}

	/** Stores a TopModel Object */
	@Override
	@SuppressWarnings("unchecked")
	public void doAction(TestStore store) {
		try {
			DetachfeaturemapFactory factory = DetachfeaturemapFactory.eINSTANCE;

			{
				final Resource resource = store.getResource();

				resource.load(Collections.EMPTY_MAP);
				assertTrue(resource.getContents().size() == 0);

				// add test data
				Contacts contacts = factory.createContacts();
				Person person = factory.createPerson();
				person.setName("");
				person.getMobile().add("001");
				person.getMobile().add("002");

				contacts.getPersons().add(person);

				SpecialPerson specialPerson = factory.createSpecialPerson();
				specialPerson.setName("");
				specialPerson.getMobile().add("001");
				specialPerson.getMobile().add("002");
				contacts.getPersons().add(specialPerson);

				resource.getContents().add(contacts);

				resource.save(Collections.EMPTY_MAP);

				contacts = (Contacts) resource.getContents().get(0);
				person = (Person) contacts.getPersons().get(0);

				assertEquals(2, person.getMobile().size());

				EList<Object> mobiles = person.getMobile();

				Object m1 = mobiles.get(0);
				Object m2 = mobiles.get(1);

				for (Object o : new ArrayList<Object>(mobiles)) {
					mobiles.remove(o);
				}
				// mobiles.clear();

				mobiles.add(m1);
				mobiles.add(m2);

				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}

			{
				Resource resource = store.getResource();
				resource.load(Collections.EMPTY_MAP);
				Contacts contacts = (Contacts) resource.getContents().get(0);
				Person person = (Person) contacts.getPersons().get(0);

				assertEquals(2, person.getMobile().size());
				resource.unload();
			}

			// now move everything around
			{
				Resource resource = store.getResource();
				resource.load(Collections.EMPTY_MAP);
				Contacts contacts = (Contacts) resource.getContents().get(0);
				Person person = (Person) contacts.getPersons().get(0);

				Object p0 = person.getPhones().get(0);
				Object p1 = person.getPhones().get(1);
				person.getPhones().clear();
				person.getPhones().add((FeatureMap.Entry) p1);
				person.getPhones().add((FeatureMap.Entry) p0);

				assertEquals(2, person.getMobile().size());
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}

			{
				Resource resource = store.getResource();
				resource.load(Collections.EMPTY_MAP);
				Contacts contacts = (Contacts) resource.getContents().get(0);
				Person person = (Person) contacts.getPersons().get(0);
				assertTrue(person.getMobile().get(0).toString().compareTo("002") == 0);
				assertTrue(person.getMobile().get(1).toString().compareTo("001") == 0);
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}

			// add two and delete 1
			{
				Resource resource = store.getResource();
				resource.load(Collections.EMPTY_MAP);
				Contacts contacts = (Contacts) resource.getContents().get(0);
				Person person = (Person) contacts.getPersons().get(0);
				person.getMobile().add("003");
				person.getMobile().add("004");
				person.getMobile().remove(1); // removes 001
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}

			// check if this actually worked
			{
				Resource resource = store.getResource();
				resource.load(Collections.EMPTY_MAP);
				Contacts contacts = (Contacts) resource.getContents().get(0);
				Person person = (Person) contacts.getPersons().get(0);
				assertTrue(person.getMobile().get(0).toString().compareTo("002") == 0);
				assertTrue(person.getMobile().get(1).toString().compareTo("003") == 0);
				assertTrue(person.getMobile().get(2).toString().compareTo("004") == 0);
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}

			// do a set and move
			{
				Resource resource = store.getResource();
				resource.load(Collections.EMPTY_MAP);
				Contacts contacts = (Contacts) resource.getContents().get(0);
				Person person = (Person) contacts.getPersons().get(0);
				person.getPhones().move(0, 2); // order is now 004, 002, 003
				person.getMobile().set(2, "005"); // order is now 004, 002, 005
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}

			// the order should be 004, 002, 005
			{
				Resource resource = store.getResource();
				resource.load(Collections.EMPTY_MAP);
				Contacts contacts = (Contacts) resource.getContents().get(0);
				Person person = (Person) contacts.getPersons().get(0);
				assertTrue(person.getMobile().get(0).toString().compareTo("004") == 0);
				assertTrue(person.getMobile().get(1).toString().compareTo("002") == 0);
				assertTrue(person.getMobile().get(2).toString().compareTo("005") == 0);
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}

			// test a specific feature related to copy action and then changing the changed object
			{
				List factories = new ArrayList();
				factories.add(new ResourceItemProviderAdapterFactory());
				factories.add(new DetachfeaturemapItemProviderAdapterFactory());
				factories.add(new ReflectiveItemProviderAdapterFactory());

				// create an editing domain
				ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(factories);
				BasicCommandStack commandStack = new BasicCommandStack();
				EditingDomain editDomain = new AdapterFactoryEditingDomain(adapterFactory, commandStack, new HashMap());
				;

				// get and load the resource
				final String uriStr = getResourceUri(store);
				final URI uri = URI.createURI(uriStr);
				Resource resource = editDomain.getResourceSet().createResource(uri);
				resource.load(Collections.EMPTY_MAP);

				// get contacts and person
				Contacts contacts = (Contacts) resource.getContents().get(0);
				Person person = (Person) contacts.getPersons().get(0);

				// copy
				CopyCommand cpcmd = new CopyCommand(editDomain, person, new Helper()); // (CopyCommand)CopyCommand.create(editDomain,
				// person);
				cpcmd.execute();

				// get the copied person, change it and add it to the resource
				Person cpPerson = (Person) cpcmd.getResult().iterator().next();
				cpPerson.setName("copy");
				contacts.getPersons().add(cpPerson);

				// save it
				resource.save(Collections.EMPTY_MAP);

				// change the copy
				cpPerson.setName("copy2");

				// before the next save gave an error but now it works fine.
				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}

			// the order should be 004, 002, 005
			{
				Resource resource = store.getResource();
				resource.load(Collections.EMPTY_MAP);
				Contacts contacts = (Contacts) resource.getContents().get(0);
				Person person = (Person) contacts.getPersons().get(0);
				assertTrue(person.getName().compareTo("copy2") != 0);
				assertTrue(person.getPhones().size() > 0);

				person.getMobile().clear();

				resource.save(Collections.EMPTY_MAP);
				resource.unload();
			}
		} catch (Exception e) {
			throw new StoreTestException("Need to catch the resource ioexception", e);
		}
	}

	/** Returns the resource uri, jdo is default (for now) */
	public String getResourceUri(TestStore store) {
		return "jpoxdao://?" + StoreResource.DS_NAME_PARAM + "=" + store.getDatabaseAdapter().getDbName();
	}
}

Back to the top