View Javadoc
1   package org.melati.admin.test;
2   
3   
4   import junit.framework.TestCase;
5   
6   import org.melati.LogicalDatabase;
7   import org.melati.admin.AnticipatedException;
8   import org.melati.admin.Copy;
9   import org.melati.poem.PoemTask;
10  import org.melati.poem.test.Dynamic;
11  import org.melati.poem.test.EverythingDatabase;
12  import org.melati.poem.test.StringField;
13  
14  /**
15   * Test Copy.
16   */
17  public class CopyTest extends TestCase {
18  
19    public CopyTest(String name) {
20      super(name);
21    }
22  
23    public void testCopyDissimilarDbs() { 
24      try { 
25        Copy.copy("everything", "melatitest");
26        fail("Should have bombed");
27      } catch (AnticipatedException e) { 
28        e = null;
29      }
30    }
31    
32    public void testCopyOntoItself() { 
33      try { 
34        Copy.copy("everything", "everything");
35        fail("Should have bombed");
36      } catch (AnticipatedException e) { 
37        e = null;
38      }
39    }
40    
41    public void testRecordsActuallyCopied() {
42      final EverythingDatabase edb = (EverythingDatabase)LogicalDatabase.getDatabase("everything");
43      final EverythingDatabase edb2 = (EverythingDatabase)LogicalDatabase.getDatabase("everything2");
44      edb.inSessionAsRoot( 
45          new PoemTask() {
46            public void run() {
47            StringField p = (StringField)(edb).getStringFieldTable().newPersistent();
48              p.setStringfield("1");
49              p.makePersistent();
50              assertEquals(1, edb.getStringFieldTable().count());
51            }
52          });
53      Copy.copy(edb, edb2);
54      edb2.inSessionAsRoot( 
55          new PoemTask() {
56            public void run() {
57              assertEquals(1, edb2.getStringFieldTable().count());
58            }
59          });
60    }
61      
62    public void  testRecordsNotCopiedIfAnyPresent() {
63      final EverythingDatabase edb = (EverythingDatabase)LogicalDatabase.getDatabase("everything");
64      final EverythingDatabase edb2 = (EverythingDatabase)LogicalDatabase.getDatabase("everything2");
65      edb.inSessionAsRoot( 
66          new PoemTask() {
67            public void run() {
68              Dynamic p = (Dynamic)(edb).getDynamicTable().newPersistent();
69              p.setName("1");
70              p.makePersistent();
71              assertEquals(3, edb.getDynamicTable().count());
72            }
73          });
74      Copy.copy(edb, edb2);
75      edb2.inSessionAsRoot( 
76          new PoemTask() {
77            public void run() {
78              assertEquals(2, edb2.getDynamicTable().count());
79            }
80          });
81    }
82      
83  }