Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dbfab516024f8bab596c8ab4bc5dc08a2d5e693b (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
 * Copyright (c) 2010-2012 Eike Stepper (Berlin, Germany) 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 Fluegge - initial API and implementation
 */
package org.eclipse.emf.cdo.tests;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.impl.EReferenceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.Test;
import junit.framework.TestSuite;

/**
 * Note! This class should never be attached to our the suites until this notification is removed. Otherwise the test
 * run will fail because this test cases is not yet integrated into our test framework! Only run this test as local
 * JUnit test case.
 * 
 * @author Martin Fluegge
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class MapDynamicTest extends MapTest
{
  private EPackage dynamicPackage;

  private Map objects;

  private EStructuralFeature feature;

  private final EObject container;

  public MapDynamicTest(EObject mapContainer, EReference mapFeature, Map objects2, EPackage dynamicMapEPackge)
  {
    container = mapContainer;
    feature = mapFeature;
    objects = objects2;
    dynamicPackage = dynamicMapEPackge;
    super.setName(feature.getName() + "Test");
  }

  @Override
  protected void runTest() throws Throwable
  {
    testMap(container, (EReference)feature, objects, dynamicPackage);
  }

  public static Test suite() throws Exception
  {
    TestSuite suite = new TestSuite(MapDynamicTest.class.getName());

    EPackage dynamicMapEPackge = createPackage();
    EFactory dynamicMapEFactoryInstance = dynamicMapEPackge.getEFactoryInstance();

    for (EStructuralFeature mapFeature : mapContainerEClass.getEAllStructuralFeatures())
    {
      EObject mapContainer = dynamicMapEFactoryInstance.create(mapContainerEClass);
      System.out.println(mapFeature);
      if (mapFeature.getName().endsWith("Map"))
      {

        Map objects = new HashMap();
        List key = dummyData.get(getEDataType(mapFeature, "key"));
        List value = dummyData.get(getEDataType(mapFeature, "value"));
        System.out.println("Testing " + mapFeature.getName() + "key: " + key + " / value: " + value);
        for (int i = 0; i < 3; i++)
        {
          objects.put(key.get(i), value.get(i));
        }

        // do the actual test
        suite.addTest(new MapDynamicTest(mapContainer, (EReference)mapFeature, objects, dynamicMapEPackge));
      }
    }

    return suite;
  }

  private static EDataType getEDataType(EStructuralFeature feature, String type) throws Exception
  {
    EClass eType = (EClass)feature.getEType();
    for (EStructuralFeature f : eType.getEAllStructuralFeatures())
    {
      if (f.getName().equals(type))
      {
        return (EDataType)f.getEType();
      }
    }

    throw new Exception("Could not find " + type + " for " + feature);
  }

  private void testMap(EObject container, EReference feature, Map objects, EPackage epackage) throws Exception
  {
    boolean keyIsReference = false;
    boolean valueIsReference = false;
    boolean keyNotContained = false;
    boolean valueNotContained = false;

    String resourceName = "/test1" + count++;

    EClass eType = (EClass)feature.getEType();
    for (EStructuralFeature f : eType.getEAllStructuralFeatures())
    {
      if (f.getName().equals("key"))
      {
        if (f instanceof EReference)
        {
          keyIsReference = true;
          if (!((EReferenceImpl)f).isContainment())
          {
            keyNotContained = true;
          }
        }
      }
      else if (f.getName().equals("value"))
      {
        if (f instanceof EReference)
        {
          valueIsReference = true;
          if (!((EReferenceImpl)f).isContainment())
          {
            valueNotContained = true;
          }
        }
      }
    }

    {
      CDOSession session = openSession();
      if (epackage != null)
      {
        session.getPackageRegistry().putEPackage(epackage);
      }

      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.createResource(resourceName);

      EMap map = (EMap)container.eGet(feature);

      for (Object key : objects.keySet())
      {
        map.put(key, objects.get(key));
      }

      compareSimple(objects, map);

      resource.getContents().add(container);
      if (keyNotContained && keyIsReference) // avoid dangling references if needed
      {
        for (Object key : map.keySet())
        {
          if (key instanceof EObject)
          {
            resource.getContents().add((EObject)key);
          }
        }
      }

      if (valueNotContained && valueIsReference) // avoid dangling references if needed
      {
        for (Object value : map.values())
        {
          if (value instanceof EObject)
          {
            resource.getContents().add((EObject)value);
          }
        }
      }

      compareSimple(objects, map);

      transaction.commit();
      session.close();
    }

    {
      CDOSession session = openSession();
      if (epackage != null)
      {
        session.getPackageRegistry().putEPackage(epackage);
      }

      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.getResource(getResourcePath(resourceName));

      EObject mapHolder = resource.getContents().get(0);

      for (EStructuralFeature e : mapHolder.eClass().getEAllStructuralFeatures())
      {
        System.out.println(e);
      }

      EMap map = (EMap)mapHolder.eGet(feature);

      compare(objects, keyIsReference, valueIsReference, map);

      assertEquals(map.size(), objects.size());

      map.remove(0);

      assertEquals(map.size(), objects.size() - 1);

      transaction.commit();
      session.close();
    }

    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      if (epackage != null)
      {
        session.getPackageRegistry().putEPackage(epackage);
      }

      CDOResource resource = transaction.getResource(getResourcePath(resourceName));

      EObject mapHolder = resource.getContents().get(0);

      EMap map = (EMap)mapHolder.eGet(feature);

      compare(objects, keyIsReference, valueIsReference, map);

      assertEquals(map.size(), objects.size() - 1);

      map.clear();

      transaction.commit();
      session.close();
    }

    {
      CDOSession session = openSession();
      if (epackage != null)
      {
        session.getPackageRegistry().putEPackage(epackage);
      }

      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.getResource(getResourcePath(resourceName));

      EObject mapHolder = resource.getContents().get(0);

      EMap map = (EMap)mapHolder.eGet(feature);
      assertEquals(map.size(), 0);

      transaction.close();
      session.close();
    }
  }

  private void compareSimple(Map objects, EMap map)
  {
    for (Object key : objects.keySet())
    {
      assertEquals(true, map.keySet().contains(key));
      assertEquals(true, map.values().contains(objects.get(key)));
    }
  }

  /**
   * This one looks a bit complicated but we must make sure the reference objects a compared by CDO IDs. Because the
   * test case is designed to be as generic as possible to allow easy enhancement.
   */
  private void compare(Map objects, boolean keyIsReference, boolean valueIsReference, EMap map)
  {
    for (Object key : map.keySet())
    {
      if (!keyIsReference)
      {
        assertEquals(true, objects.keySet().contains(key));
      }
      else
      {
        boolean foundObjectWithSameID = false;
        for (Object keyMap : objects.keySet())
        {
          CDOID valueID = CDOUtil.getCDOObject((EObject)key).cdoID();
          CDOID valueMapID = CDOUtil.getCDOObject((EObject)keyMap).cdoID();

          if (valueID.equals(valueMapID))
          {
            foundObjectWithSameID = true;
          }
        }

        if (!foundObjectWithSameID)
        {
          fail("key reference with CDOID could not be found");
        }
      }

      if (!valueIsReference)
      {
        assertEquals(true, objects.keySet().contains(key));
      }
      else
      {
        boolean foundObjectWithSameID = false;
        for (Object valueMap : objects.values())
        {
          CDOID valueID = CDOUtil.getCDOObject((EObject)map.get(key)).cdoID();
          CDOID valueMapID = CDOUtil.getCDOObject((EObject)valueMap).cdoID();

          if (valueID.equals(valueMapID))
          {
            foundObjectWithSameID = true;
          }
        }
        if (!foundObjectWithSameID)
        {
          fail("value reference with CDOID could not be found");
        }
      }
    }
  }

  // private static EPackage createPackage()
  // {
  //
  // EcoreFactory theCoreFactory = EcoreFactory.eINSTANCE;
  // EcorePackage theCorePackage = EcorePackage.eINSTANCE;
  //
  // mapContainerEClass = theCoreFactory.createEClass();
  // mapContainerEClass.setName("MapContainer");
  //
  // EPackage dynamicMapEPackage = theCoreFactory.createEPackage();
  // dynamicMapEPackage.setName("DynamicMapPackage");
  // dynamicMapEPackage.setNsPrefix("dynamicmap");
  // dynamicMapEPackage.setNsURI("http:///org.mftech.examples.emf.dynamic.map");
  //
  // dynamicMapEPackage.getEClassifiers().add(mapContainerEClass);
  //
  // // ++++++++++++++ create dynamic
  // // TODO provide Reference mapping
  // Map<EDataType, Boolean> dataTypes = new HashMap<EDataType, Boolean>();
  // dataTypes.put(theCorePackage.getEBigDecimal(), true);
  // dataTypes.put(theCorePackage.getEBigInteger(), true);
  // dataTypes.put(theCorePackage.getEBoolean(), true);
  // dataTypes.put(theCorePackage.getEBooleanObject(), true);
  // dataTypes.put(theCorePackage.getEByte(), true);
  // // //dataTypes.put(theCorePackage.getEByteArray(), true);
  // dataTypes.put(theCorePackage.getEByteObject(), true);
  // dataTypes.put(theCorePackage.getEChar(), true);
  // // dataTypes.put(theCorePackage.getECharacterObject(), false);
  // dataTypes.put(theCorePackage.getEDate(), true);
  // // // dataTypes.put(theCorePackage.getEDiagnosticChain(), true);
  // dataTypes.put(theCorePackage.getEDouble(), true);
  // // dataTypes.put(theCorePackage.getEDoubleObject(), false);
  // dataTypes.put(theCorePackage.getEFloat(), true);
  // // dataTypes.put(theCorePackage.getEFloatObject(), false);
  // dataTypes.put(theCorePackage.getEInt(), true);
  // // dataTypes.put(theCorePackage.getEIntegerObject(), false);
  // dataTypes.put(theCorePackage.getEJavaObject(), true);
  // dataTypes.put(theCorePackage.getEJavaClass(), true);
  // dataTypes.put(theCorePackage.getELong(), true);
  // // dataTypes.put(theCorePackage.getELongObject(), false);
  // // dataTypes.put(theCorePackage.getEMap(), false);
  // dataTypes.put(theCorePackage.getEShort(), true);
  // dataTypes.put(theCorePackage.getEShortObject(), true);
  // dataTypes.put(theCorePackage.getEString(), true);
  //
  // System.out.println("Start");
  //
  // int i = 0;
  // for (EDataType keyDataType : dataTypes.keySet())
  // {
  // String keyName = keyDataType.getName();
  // for (EDataType valueDataType : dataTypes.keySet())
  // {
  //
  // String valueName = valueDataType.getName();
  // String mapName = keyName + "To" + valueName + "Map";
  //
  // EClass mapEClass = theCoreFactory.createEClass();
  // mapEClass.setName(mapName);
  // mapEClass.setInstanceTypeName("java.util.Map$Entry");
  //
  // EStructuralFeature key;
  // if (dataTypes.get(keyDataType))
  // {
  // key = theCoreFactory.createEAttribute();
  // }
  // else
  // {
  // key = theCoreFactory.createEReference();
  // }
  //
  // key.setName("key");
  // key.setEType(keyDataType);
  //
  // // EStructuralFeature value = theCoreFactory.createEAttribute();
  // EStructuralFeature value;
  // if (dataTypes.get(valueDataType))
  // {
  // value = theCoreFactory.createEAttribute();
  // }
  // else
  // {
  // value = theCoreFactory.createEReference();
  // }
  // value.setName("value");
  // value.setEType(valueDataType);
  //
  // mapEClass.getEStructuralFeatures().add(key);
  // mapEClass.getEStructuralFeatures().add(value);
  //
  // EReference containerMapFeature = theCoreFactory.createEReference();
  //
  // containerMapFeature.setName(mapName);
  // containerMapFeature.setEType(mapEClass);
  // containerMapFeature.setResolveProxies(false);
  // containerMapFeature.setUpperBound(EStructuralFeature.UNBOUNDED_MULTIPLICITY);
  // containerMapFeature.setContainment(true);
  //
  // dynamicMapEPackage.getEClassifiers().add(mapEClass);
  // mapContainerEClass.getEStructuralFeatures().add(containerMapFeature);
  //
  // System.out.println("Created " + mapName);
  // i++;
  // }
  // }
  //
  // System.out.println("Created " + i + " maps.");
  //
  // initDummyData(theCorePackage);
  // CDOUtil.prepareDynamicEPackage(dynamicMapEPackage);
  // return dynamicMapEPackage;
  //
  // }

  // private static void initDummyData(EcorePackage theCorePackage)
  // {
  // List data = new ArrayList();
  // data.add("A");
  // data.add("B");
  // data.add("C");
  // dummyData.put(theCorePackage.getEString(), data);
  //
  // data = new ArrayList();
  // data.add(new BigInteger("11111111111111"));
  // data.add(new BigInteger("22222222222222"));
  // data.add(new BigInteger("333333333333333"));
  // dummyData.put(theCorePackage.getEBigInteger(), data);
  //
  // data = new ArrayList();
  // data.add(new BigDecimal("44444444444444"));
  // data.add(new BigDecimal("55555555555555"));
  // data.add(new BigDecimal("66666666666666"));
  // dummyData.put(theCorePackage.getEBigDecimal(), data);
  //
  // data = new ArrayList();
  // data.add(true);
  // data.add(false);
  // data.add(true);
  // dummyData.put(theCorePackage.getEBoolean(), data);
  //
  // data = new ArrayList();
  // data.add(new Boolean(true));
  // data.add(new Boolean("false"));
  // data.add(new Boolean(true));
  // dummyData.put(theCorePackage.getEBooleanObject(), data);
  //
  // data = new ArrayList();
  // data.add((byte)1);
  // data.add((byte)2);
  // data.add((byte)3);
  // dummyData.put(theCorePackage.getEByte(), data);
  //
  // // TODO create additional dummy Data
  // // dummyData.put(theCorePackage.getEByteArray(), data);
  //
  // data = new ArrayList();
  // data.add(new Byte((byte)1));
  // data.add(new Byte("2"));
  // data.add(new Byte((byte)3));
  // dummyData.put(theCorePackage.getEByteObject(), data);
  //
  // data = new ArrayList();
  // data.add('a');
  // data.add((char)99);
  // data.add('c');
  // dummyData.put(theCorePackage.getEChar(), data);
  //
  // data = new ArrayList();
  // data.add(new Character('a'));
  // data.add(new Character((char)99));
  // data.add(new Character('c'));
  // dummyData.put(theCorePackage.getECharacterObject(), data);
  //
  // data = new ArrayList();
  // data.add(new Date());
  // data.add(new Date());
  // data.add(new Date());
  // dummyData.put(theCorePackage.getEDate(), data);
  //
  // // TODO create additional dummy Data
  // // dummyData.put(theCorePackage.getEDiagnosticChain(), data);
  //
  // data = new ArrayList();
  // data.add(111.111D);
  // data.add(222.222D);
  // data.add(333.333D);
  // dummyData.put(theCorePackage.getEDouble(), data);
  //
  // data = new ArrayList();
  // data.add(new Double(444.444));
  // data.add(new Double("555.555"));
  // data.add(new Double(666.666));
  // dummyData.put(theCorePackage.getEDoubleObject(), data);
  //
  // data = new ArrayList();
  // data.add(111.0001f);
  // data.add(222.0002f);
  // data.add(333.0003f);
  // dummyData.put(theCorePackage.getEFloat(), data);
  //
  // data = new ArrayList();
  // data.add(new Float(444.0004f));
  // data.add(new Float(555.0005d));
  // data.add(new Float("666.0006"));
  // dummyData.put(theCorePackage.getEFloatObject(), data);
  //
  // data = new ArrayList();
  // data.add(11);
  // data.add(22);
  // data.add(33);
  // dummyData.put(theCorePackage.getEInt(), data);
  //
  // data = new ArrayList();
  // data.add(new Integer(44));
  // data.add(new Integer("55"));
  // data.add(new Integer(66));
  // dummyData.put(theCorePackage.getEIntegerObject(), data);
  //
  // data = new ArrayList();
  // data.add(new Long(1));
  // data.add(new Date());
  // data.add(new Integer(2));
  // dummyData.put(theCorePackage.getEJavaObject(), data);
  //
  // data = new ArrayList();
  // data.add(Object.class);
  // data.add(Date.class);
  // data.add(Long.class);
  // dummyData.put(theCorePackage.getEJavaClass(), data);
  //
  // data = new ArrayList();
  // data.add(10101010101L);
  // data.add(20202020202L);
  // data.add(30303030303L);
  // dummyData.put(theCorePackage.getELong(), data);
  //
  // data = new ArrayList();
  // data.add(new Long(40404040404L));
  // data.add(new Long("5050505050505"));
  // data.add(new Long(6060606060606L));
  // dummyData.put(theCorePackage.getELongObject(), data);
  //
  // data = new ArrayList();
  //
  // Map map = new HashMap();
  // map.put(1, 2);
  // map.put(3, 4);
  // data.add(map);
  // map = new HashMap();
  // map.put("A", "B");
  // map.put("C", "D");
  // data.add(map);
  // map = new HashMap();
  // map.put('a', 'b');
  // map.put('c', 'd');
  // data.add(map);
  // dummyData.put(theCorePackage.getEMap(), data);
  //
  // data = new ArrayList();
  // data.add((short)111);
  // data.add((short)222);
  // data.add((short)333);
  // dummyData.put(theCorePackage.getEShort(), data);
  //
  // data = new ArrayList();
  // data.add(new Short((short)111));
  // data.add(new Short((short)222));
  // data.add(new Short((short)333));
  // dummyData.put(theCorePackage.getEShortObject(), data);
  //
  // }
}

Back to the top