View Javadoc

1   package org.ogf.saga.job.run;
2   
3   import java.text.DateFormat;
4   import java.text.ParseException;
5   import java.text.SimpleDateFormat;
6   import java.util.Date;
7   import java.util.Locale;
8   
9   import org.apache.log4j.Logger;
10  import org.junit.Test;
11  import org.ogf.saga.job.Job;
12  import org.ogf.saga.job.JobDescription;
13  import org.ogf.saga.job.base.JobBaseTest;
14  
15  /* ***************************************************
16  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
17  * ***             http://cc.in2p3.fr/             ***
18  * ***************************************************
19  * File:   JobRunInfoTest
20  * Author: Lionel Schwarz (lionel.schwarz@in2p3.fr)
21  * Date:   20 jan 2011
22  * ***************************************************
23  * Description: 
24  * This test suite is made to be sure that the plug-in 
25  * properly implements the  JobInfoAdaptor interface  */
26  /**
27   *
28   */
29  public abstract class InfoTest extends JobBaseTest {
30      private Logger logger = Logger.getLogger(this.getClass());
31      private static final String FORMAT_DATETOSTRING = "EEE MMM dd HH:mm:ss z yyyy";
32     
33      protected InfoTest(String jobprotocol) throws Exception {
34          super(jobprotocol);
35      }
36  
37      /**
38       * Runs simple job and expects 0 as return code
39       */
40      @Test
41      public void test_exitcode() throws Exception {
42          
43      	// prepare
44      	JobDescription desc = createSimpleJob();
45      	
46          // submit
47          Job job = runJob(desc);
48          logger.info(job.getAttribute(Job.JOBID));   // for detecting hang in run()
49  
50          // wait for the END
51          job.waitFor();
52          logger.info("Job finished.");               // for detecting hang in waitFor()
53  
54          // check exit code
55          assertEquals(
56                  String.valueOf(0),
57                  job.getAttribute(Job.EXITCODE));
58      }
59  
60      /**
61       * Runs simple job and get Creation date
62       */
63      @Test
64      public void test_created() throws Exception {
65          
66      	Date now = new Date(new Date().getTime()/1000*1000); // ignore milliseconds as they are lost by getAttribute
67      	
68      	// prepare
69      	JobDescription desc = createSimpleJob();
70      	
71          // submit
72          Job job = runJob(desc);
73          logger.info(job.getAttribute(Job.JOBID));   // for detecting hang in run()
74  
75          // wait for the END
76          job.waitFor();
77          logger.info("Job finished.");               // for detecting hang in waitFor()
78  
79          // check creation date
80  		try {
81  	        Date creationTime = parse(job.getAttribute(Job.CREATED));
82  	        assertFalse(creationTime.before(now));
83  		} catch (ParseException e) {
84  			fail(e.getMessage());
85  		}
86  
87      }
88      
89      /**
90       * Runs simple job and get start and end date
91       */
92      @Test
93      public void test_dates() throws Exception {
94          
95      	Date now = new Date(new Date().getTime()/1000*1000); // ignore milliseconds as they are lost by getAttribute
96      	
97      	// prepare
98      	JobDescription desc = createSimpleJob();
99      	
100         // submit
101         Job job = runJob(desc);
102         logger.info(job.getAttribute(Job.JOBID));   // for detecting hang in run()
103 
104         // wait for the END
105         job.waitFor();
106         logger.info("Job finished.");               // for detecting hang in waitFor()
107 
108         // check creation date
109 		try {
110 	        Date startTime = parse(job.getAttribute(Job.STARTED));
111 	        Date endTime = parse(job.getAttribute(Job.FINISHED));
112 	        // Compare both dates
113 	        assertFalse(startTime.after(endTime));
114 		} catch (ParseException e) {
115 			fail(e.getMessage());
116 		}
117 
118     }
119     
120     /**
121      * Runs simple job and get start and end date
122      */
123     @Test
124     public void test_execution_hosts() throws Exception {
125         
126     	// prepare
127     	JobDescription desc = createSimpleJob();
128     	
129         // submit
130         Job job = runJob(desc);
131         logger.info(job.getAttribute(Job.JOBID));   // for detecting hang in run()
132         
133         // wait for the END
134         job.waitFor();
135         logger.info("Job finished.");               // for detecting hang in waitFor()
136 
137         // check execution hosts
138         String executionHosts[] = job.getVectorAttribute(Job.EXECUTIONHOSTS);
139         assertFalse(executionHosts == null);
140         assertTrue(executionHosts.length > 0);
141         for (int i=0; i<executionHosts.length; i++) {
142         	assertNotNull(executionHosts[i]);
143         	assertFalse(executionHosts[i].equals(""));
144         }
145     }
146     
147     
148     private Date parse(String date) throws ParseException {
149 		// Parse the CREATED attribute
150 		// built with Date.toString => "EEE MMM dd HH:mm:ss z yyyy" e.g. "Thu Jan 20 16:26:10 CET 2011"
151 		// This String has to be parsed in Locale.US
152 		DateFormat df = new SimpleDateFormat(FORMAT_DATETOSTRING, Locale.US);
153         return df.parse(date);
154     	
155     }
156 }