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
16
17
18
19
20 public class TestIntegerBackup extends AbstractTestTypeBackup {
21 @Test
22 public void testAutoIncrementId() throws Exception {
23 testBackupType(new BackupType<Integer>() {
24 @Override
25 public Class<? extends RawEntity<Integer>> getEntityClass() {
26 return AutoIncrementId.class;
27 }
28
29 @Override
30 public void createData(EntityManager em) throws Exception {
31 em.create(AutoIncrementId.class);
32 }
33
34 @Override
35 public void checkData(EntityManager em) throws Exception {
36 assertEquals(1, em.find(AutoIncrementId.class).length);
37 }
38 });
39 }
40
41 @Test
42 @NonTransactional
43 public void testSimpleEntityWithValue() throws Exception {
44 testBackupWithValue(123);
45 }
46
47 @Test
48 @NonTransactional
49 public void testSimpleEntityWithNull() throws Exception {
50 testBackupWithValue(null);
51 }
52
53 private void testBackupWithValue(final Integer value) throws Exception {
54 final AtomicInteger e1Id = new AtomicInteger(-1);
55 testBackupType(new BackupType<Integer>() {
56 @Override
57 public Class<? extends RawEntity<Integer>> getEntityClass() {
58 return SimpleEntity.class;
59 }
60
61 @Override
62 public void createData(EntityManager em) throws Exception {
63 SimpleEntity e1 = em.create(SimpleEntity.class);
64 e1.setValue(value);
65 e1.save();
66 e1Id.set(e1.getID());
67 }
68
69 @Override
70 public void checkData(EntityManager em) throws Exception {
71 assertEquals(value, em.get(SimpleEntity.class, e1Id.get()).getValue());
72 }
73 });
74 }
75
76 public static interface AutoIncrementId extends RawEntity<Integer> {
77 @PrimaryKey
78 @AutoIncrement
79 public Integer getId();
80 }
81
82 public static interface SimpleEntity extends Entity {
83 public Integer getValue();
84
85 public void setValue(Integer value);
86 }
87 }