View Javadoc
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.test.jdbc.NonTransactional;
7   import org.junit.Test;
8   
9   import java.text.SimpleDateFormat;
10  import java.util.Date;
11  import java.util.concurrent.atomic.AtomicInteger;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  public final class TestDateBackup extends AbstractTestTypeBackup {
16      private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
17      private static final String DATE = "2011-11-27 15:47:31";
18  
19      @Test
20      @NonTransactional
21      public void testSimpleEntityWithLegalDate() throws Exception {
22          testBackupWithValue(new SimpleDateFormat(DATE_FORMAT).parse(DATE));
23      }
24  
25      @Test
26      @NonTransactional
27      public void testSimpleEntityWithNull() throws Exception {
28          testBackupWithValue(null);
29      }
30  
31      private void testBackupWithValue(final Date value) throws Exception {
32          final AtomicInteger eId = new AtomicInteger(-1);
33          testBackupType(new BackupType<Integer>() {
34              @Override
35              public Class<? extends RawEntity<Integer>> getEntityClass() {
36                  return SimpleEntity.class;
37              }
38  
39              @Override
40              public void createData(EntityManager em) throws Exception {
41                  SimpleEntity e = em.create(SimpleEntity.class);
42                  e.setValue(value);
43                  e.save();
44                  eId.set(e.getID());
45              }
46  
47              @Override
48              public void checkData(EntityManager em) throws Exception {
49                  final Date actual = em.get(SimpleEntity.class, eId.get()).getValue();
50                  assertEquals(formatDate(value), formatDate(actual));
51              }
52  
53              private String formatDate(Date date) {
54                  return date == null ? null : new SimpleDateFormat(DATE_FORMAT).format(date);
55              }
56          });
57      }
58  
59      public static interface SimpleEntity extends Entity {
60          public Date getValue();
61  
62          public void setValue(Date value);
63      }
64  }