1 package com.atlassian.activeobjects.backup.types;
2
3 import net.java.ao.Entity;
4 import net.java.ao.EntityManager;
5 import net.java.ao.RawEntity;
6 import net.java.ao.schema.AutoIncrement;
7 import net.java.ao.schema.PrimaryKey;
8 import net.java.ao.test.jdbc.NonTransactional;
9 import org.junit.Test;
10
11 import java.util.concurrent.atomic.AtomicInteger;
12
13 import static org.junit.Assert.assertEquals;
14
15 public final class TestLongBackup extends AbstractTestTypeBackup {
16 @Test
17 public void testAutoIncrementId() throws Exception {
18 testBackupType(new BackupType<Long>() {
19 @Override
20 public Class<? extends RawEntity<Long>> getEntityClass() {
21 return AutoIncrementId.class;
22 }
23
24 @Override
25 public void createData(EntityManager em) throws Exception {
26 em.create(AutoIncrementId.class);
27 }
28
29 @Override
30 public void checkData(EntityManager em) throws Exception {
31 assertEquals(1, em.find(AutoIncrementId.class).length);
32 }
33 });
34 }
35
36 @Test
37 @NonTransactional
38 public void testSimpleEntityWithValue() throws Exception {
39 testBackupWithValue(123l);
40 }
41
42 @Test
43 @NonTransactional
44 public void testSimpleEntityWithNull() throws Exception {
45 testBackupWithValue(null);
46 }
47
48 private void testBackupWithValue(final Long value) throws Exception {
49 final AtomicInteger eId = new AtomicInteger(-1);
50 testBackupType(new BackupType<Integer>() {
51 @Override
52 public Class<? extends RawEntity<Integer>> getEntityClass() {
53 return SimpleEntity.class;
54 }
55
56 @Override
57 public void createData(EntityManager em) throws Exception {
58 SimpleEntity e = em.create(SimpleEntity.class);
59 e.setValue(value);
60 e.save();
61 eId.set(e.getID());
62 }
63
64 @Override
65 public void checkData(EntityManager em) throws Exception {
66 assertEquals(value, em.get(SimpleEntity.class, eId.get()).getValue());
67 }
68 });
69 }
70
71 public static interface AutoIncrementId extends RawEntity<Long> {
72 @PrimaryKey
73 @AutoIncrement
74 public Long getId();
75 }
76
77 public static interface SimpleEntity extends Entity {
78 public Long getValue();
79
80 public void setValue(Long value);
81 }
82 }