Project

General

Profile

Download (2.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.cache;
10

    
11

    
12
import org.joda.time.DateTime;
13
import org.junit.Assert;
14
import org.junit.Test;
15

    
16
import eu.etaxonomy.cdm.model.occurrence.Collection;
17

    
18
/**
19
 * @author a.kohlbecker
20
 * @since 09.11.2017
21
 *
22
 */
23
public class EntityCacheTest {
24

    
25
    @Test
26
    public void entityKeyTest() {
27

    
28
        Collection collection1 = Collection.NewInstance();
29
        Collection collection2 = Collection.NewInstance();
30
        collection1.setId(81);
31
        collection2.setId(81);
32

    
33

    
34
        CdmEntityCache cache = new CdmEntityCache(collection1);
35

    
36
        CdmEntityCache.EntityKey key1 = cache.new EntityKey(collection1);
37

    
38
        CdmEntityCache.EntityKey key2 = cache.new EntityKey(collection2);
39

    
40
        Assert.assertEquals(key1, key2);
41

    
42
        Assert.assertEquals(key1.hashCode(), key2.hashCode());
43

    
44
        Assert.assertNotNull(cache.find(collection1));
45
    }
46

    
47
    @Test
48
    public void testFindOrUpdate() {
49

    
50
        Collection collection = Collection.NewInstance();
51
        collection.setId(10);
52
        collection.setCode("A");
53

    
54
        CdmEntityCache cache = new CdmEntityCache(collection);
55

    
56
        Assert.assertEquals(collection, cache.find(Collection.class, 10));
57
        Assert.assertEquals("A", cache.find(Collection.class, 10).getCode());
58

    
59
        Collection copyCollection = collection.clone();
60
        copyCollection.setId(collection.getId()); // clone does not copy the id!
61
        copyCollection.setUuid(collection.getUuid());
62
        copyCollection.setCode("B");
63
        copyCollection.setUpdated(new DateTime(2017, 12, 1, 0, 0));
64
        Assert.assertEquals("B", cache.findAndUpdate(copyCollection).getCode());
65

    
66
        copyCollection = collection.clone();
67
        copyCollection.setId(10); // clone does not copy the id!
68
        copyCollection.setUuid(collection.getUuid());
69
        copyCollection.setCode("C");
70
        copyCollection.setUpdated(new DateTime(2018, 1, 1, 0, 0));
71
        Assert.assertEquals("C", cache.findAndUpdate(copyCollection).getCode());
72

    
73
    }
74

    
75
}
    (1-1/1)