~dhis2-academy/dhis2-academy/scheduling

« back to all changes in this revision

Viewing changes to dhis-2/dhis-support/dhis-support-scheduling/src/test/java/org/hisp/dist/scheduling/DefaultSchedulingTest.java

  • Committer: Christian Tryti
  • Date: 2010-11-18 12:56:54 UTC
  • Revision ID: cotryti@ifi.uio.no-20101118125654-wreek0jbd7xm7mc0
added done, scheduled and running task-lists. Updated vm-files to show these 

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
import org.springframework.test.context.ContextConfiguration;
15
15
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
16
16
 
17
 
class MyRunnable implements Runnable {
18
 
        String name;
19
 
        
20
 
        MyRunnable() {
21
 
                this.name = "MyRunnable";
22
 
        }
23
 
        
24
 
        public void run() {
25
 
                System.out.println("Process was run");
26
 
                try {
27
 
                        Thread.currentThread().sleep((int)(1000*Math.random()));
28
 
                }
29
 
                catch (Exception e) {
30
 
                        
31
 
                }
32
 
        }
 
17
class MyRunnable
 
18
    implements Runnable
 
19
{
 
20
    String name;
 
21
 
 
22
    MyRunnable()
 
23
    {
 
24
        this.name = "MyRunnable";
 
25
    }
 
26
 
 
27
    public void run()
 
28
    {
 
29
        System.out.println( "Process was run" );
 
30
        try
 
31
        {
 
32
            Thread.currentThread().sleep( (int) (1000 * Math.random()) );
 
33
        }
 
34
        catch ( Exception e )
 
35
        {
 
36
 
 
37
        }
 
38
    }
33
39
}
34
40
 
35
 
@RunWith(SpringJUnit4ClassRunner.class)
36
 
@ContextConfiguration(locations = {"classpath*:/META-INF/dhis/beans.xml"})
37
 
public class DefaultSchedulingTest {
38
 
        DefaultSchedulingImpl test;
39
 
        
40
 
        @Before
41
 
        public void init() {
42
 
                test = new DefaultSchedulingImpl();
43
 
        }
44
 
        
45
 
        @Test
46
 
        public void addTaskForSchedulingTest() {
47
 
                System.out.println("START TEST 1");
48
 
                int id = test.addTaskForScheduling(new MyRunnable(), new Date(System.currentTimeMillis() + 2000), true, false);
49
 
                assertNotNull(test.getScheduledTask(id));
50
 
                try {
51
 
                        Thread.currentThread().sleep(30000);
52
 
                }
53
 
                catch (Exception e) {
54
 
                        
55
 
                }
56
 
                System.out.println("END TEST 1");
57
 
        }
58
 
        
59
 
        @Test
60
 
        public void delTaskFromScheduling() {
61
 
                // TODO Auto-generated method stub
62
 
                int id = test.addTaskForScheduling(new MyRunnable(), new Date(System.currentTimeMillis() + 1000), true, false);
63
 
                test.delTaskFromScheduling(id);
64
 
                assertNull(test.getScheduledTask(id));
65
 
        }
66
 
 
67
 
        @Test
68
 
        public void getAllScheduledTasks() {
69
 
                try {
70
 
                test.addTaskForScheduling(new MyRunnable(), new Date(System.currentTimeMillis() + 1000), false, false);
71
 
                Thread.currentThread().sleep(1000);
72
 
                test.addTaskForScheduling(new MyRunnable(), new Date(System.currentTimeMillis() + 1000), false, false);
73
 
                Thread.currentThread().sleep(1000);
74
 
                test.addTaskForScheduling(new MyRunnable(), new Date(System.currentTimeMillis() + 1000), false, false);
75
 
                Thread.currentThread().sleep(1000);
76
 
                test.addTaskForScheduling(new MyRunnable(), new Date(System.currentTimeMillis() + 1000), false, false);
77
 
                Thread.currentThread().sleep(1000);
78
 
                test.addTaskForScheduling(new MyRunnable(), new Date(System.currentTimeMillis() + 1000), false, false);
79
 
 
80
 
                
81
 
                assertEquals(5, test.getAllScheduledTasks().size());
82
 
                
83
 
                        Thread.currentThread().sleep(5000);
84
 
                }
85
 
                catch (Exception e) {
86
 
                        
87
 
                }
88
 
                // TODO Auto-generated method stub
89
 
        }
90
 
        
91
 
        @Test
92
 
        public void getScheduledTask() {
93
 
                // TODO Auto-generated method stub
94
 
                int id = test.addTaskForScheduling(new MyRunnable(), new Date(System.currentTimeMillis() + 10000), true, false);
95
 
                assertNotNull(test.getScheduledTask(id));
96
 
                
97
 
        }
98
 
        
99
 
        public void getLoggingForSpecificTask() {
100
 
                // TODO Auto-generated method stub
101
 
        }
102
 
        
103
 
        @Test
104
 
        public void startLogging() {
105
 
                // TODO Auto-generated method stub
106
 
                test.startLogging();
107
 
                assertTrue(test.shouldLog);
108
 
        }
109
 
 
110
 
        @Test
111
 
        public void stopLogging() {
112
 
                // TODO Auto-generated method stub
113
 
                test.stopLogging();
114
 
                assertFalse(test.shouldLog);
115
 
        }
116
 
        
117
 
        public void getAllLogging() {
118
 
                // TODO Auto-generated method stub
119
 
        }
120
 
        
121
 
        @Test
122
 
        public void updateScheduledTask() {
123
 
                // TODO Auto-generated method stub
124
 
                int id = test.addTaskForScheduling(new MyRunnable(), new Date(System.currentTimeMillis() + 100000), false, false);
125
 
                
126
 
                Date before = test.getScheduledTask(id).getDate();
127
 
                
128
 
                test.updateScheduledTask(id, new Date(System.currentTimeMillis() + 5000), false, false);
129
 
                Date after = test.getScheduledTask(id).getDate();
130
 
                assertTrue(before.compareTo(after) > 0);
131
 
                
132
 
                try {
133
 
                        Thread.currentThread().sleep(10000);
134
 
                }
135
 
                catch (Exception e) {
136
 
                        
137
 
                }
138
 
        }
 
41
@RunWith( SpringJUnit4ClassRunner.class )
 
42
@ContextConfiguration( locations = { "classpath*:/META-INF/dhis/beans.xml" } )
 
43
public class DefaultSchedulingTest
 
44
{
 
45
    DefaultSchedulingImpl test;
 
46
 
 
47
    @Before
 
48
    public void init()
 
49
    {
 
50
        test = new DefaultSchedulingImpl();
 
51
    }
 
52
 
 
53
    @Test
 
54
    public void addTaskForSchedulingTest()
 
55
    {
 
56
        System.out.println( "START TEST 1" );
 
57
        int id = test.addTaskForScheduling( new MyRunnable(), new Date( System.currentTimeMillis() + 2000 ), true,
 
58
            false, "test" );
 
59
        assertNotNull( test.getScheduledTask( id ) );
 
60
        try
 
61
        {
 
62
            Thread.currentThread().sleep( 30000 );
 
63
        }
 
64
        catch ( Exception e )
 
65
        {
 
66
 
 
67
        }
 
68
        System.out.println( "END TEST 1" );
 
69
    }
 
70
 
 
71
    @Test
 
72
    public void delTaskFromScheduling()
 
73
    {
 
74
        // TODO Auto-generated method stub
 
75
        int id = test.addTaskForScheduling( new MyRunnable(), new Date( System.currentTimeMillis() + 1000 ), true,
 
76
            false , "test");
 
77
        test.delTaskFromScheduling( id );
 
78
        assertNull( test.getScheduledTask( id ) );
 
79
    }
 
80
 
 
81
    @Test
 
82
    public void getAllScheduledTasks()
 
83
    {
 
84
        try
 
85
        {
 
86
            test.addTaskForScheduling( new MyRunnable(), new Date( System.currentTimeMillis() + 1000 ), false, false, "test" );
 
87
            Thread.currentThread().sleep( 1000 );
 
88
            test.addTaskForScheduling( new MyRunnable(), new Date( System.currentTimeMillis() + 1000 ), false, false, "test" );
 
89
            Thread.currentThread().sleep( 1000 );
 
90
            test.addTaskForScheduling( new MyRunnable(), new Date( System.currentTimeMillis() + 1000 ), false, false, "test" );
 
91
            Thread.currentThread().sleep( 1000 );
 
92
            test.addTaskForScheduling( new MyRunnable(), new Date( System.currentTimeMillis() + 1000 ), false, false, "test" );
 
93
            Thread.currentThread().sleep( 1000 );
 
94
            test.addTaskForScheduling( new MyRunnable(), new Date( System.currentTimeMillis() + 1000 ), false, false, "test" );
 
95
 
 
96
            assertEquals( 5, test.getAllScheduledTasks().size() );
 
97
 
 
98
            Thread.currentThread().sleep( 5000 );
 
99
        }
 
100
        catch ( Exception e )
 
101
        {
 
102
 
 
103
        }
 
104
        // TODO Auto-generated method stub
 
105
    }
 
106
 
 
107
    @Test
 
108
    public void getScheduledTask()
 
109
    {
 
110
        // TODO Auto-generated method stub
 
111
        int id = test.addTaskForScheduling( new MyRunnable(), new Date( System.currentTimeMillis() + 10000 ), true,
 
112
            false, "test" );
 
113
        assertNotNull( test.getScheduledTask( id ) );
 
114
 
 
115
    }
 
116
 
 
117
    public void getLoggingForSpecificTask()
 
118
    {
 
119
        // TODO Auto-generated method stub
 
120
    }
 
121
 
 
122
    @Test
 
123
    public void startLogging()
 
124
    {
 
125
        // TODO Auto-generated method stub
 
126
        test.startLogging();
 
127
        assertTrue( test.shouldLog );
 
128
    }
 
129
 
 
130
    @Test
 
131
    public void stopLogging()
 
132
    {
 
133
        // TODO Auto-generated method stub
 
134
        test.stopLogging();
 
135
        assertFalse( test.shouldLog );
 
136
    }
 
137
 
 
138
    public void getAllLogging()
 
139
    {
 
140
        // TODO Auto-generated method stub
 
141
    }
 
142
 
 
143
    @Test
 
144
    public void updateScheduledTask()
 
145
    {
 
146
        // TODO Auto-generated method stub
 
147
        int id = test.addTaskForScheduling( new MyRunnable(), new Date( System.currentTimeMillis() + 100000 ), false,
 
148
            false, "test" );
 
149
 
 
150
        Date before = test.getScheduledTask( id ).getDate();
 
151
 
 
152
        test.updateScheduledTask( id, new Date( System.currentTimeMillis() + 5000 ), false, false );
 
153
        Date after = test.getScheduledTask( id ).getDate();
 
154
        assertTrue( before.compareTo( after ) > 0 );
 
155
 
 
156
        try
 
157
        {
 
158
            Thread.currentThread().sleep( 10000 );
 
159
        }
 
160
        catch ( Exception e )
 
161
        {
 
162
 
 
163
        }
 
164
    }
139
165
 
140
166
}