View Javadoc

1   package org.ogf.saga;
2   
3   //import junit.framework.TestCase;
4   import org.apache.log4j.Logger;
5   import org.apache.log4j.Priority;
6   import org.junit.After;
7   import org.junit.Assert;
8   import org.junit.BeforeClass;
9   import org.junit.Rule;
10  import org.junit.rules.TestRule;
11  import org.junit.rules.TestWatcher;
12  import org.junit.runner.Description;
13  import org.ogf.saga.error.*;
14  import org.ogf.saga.url.URL;
15  import org.ogf.saga.url.URLFactory;
16  
17  import java.io.*;
18  import java.util.*;
19  
20  /* ***************************************************
21  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
22  * ***             http://cc.in2p3.fr/             ***
23  * ***************************************************
24  * File:   BaseTest
25  * Author: Lionel Schwarz (lionel.schwarz@in2p3.fr)
26  * Date:   4 NOV 2013
27  * ***************************************************
28  * Description:                                      */
29  /**
30   *
31   */
32  public abstract class JSAGABaseTest extends Assert {
33      /** jobservice.url (required): for testing job service */
34      protected static final String CONFIG_JOBSERVICE_URL = "jobservice.url";
35      /** base.url (required): for testing any protocol */
36      protected static final String CONFIG_BASE_URL = "base.url";
37      /** base2.url (optional): for testing protocols supporting third-party transfer */
38      protected static final String CONFIG_BASE2_URL = "base2.url";
39      /** physical.protocol (optional): for testing logical protocols */
40      protected static final String CONFIG_PHYSICAL_PROTOCOL = "physical.protocol";
41  
42      private Properties m_properties;
43      private long startTime;
44      private Logger logger = Logger.getLogger(this.getClass());
45  
46      public JSAGABaseTest() throws Exception {
47          // set configuration files to use
48          if (System.getProperty("jsaga.default.contexts") == null) {
49              java.net.URL defaultContexts = this.getResource("etc/jsaga-default-contexts.xml");
50              if (defaultContexts != null) {
51                  System.setProperty("jsaga.default.contexts", defaultContexts.toString());
52              }
53          }
54          if (System.getProperty("jsaga.timeout") == null) {
55              java.net.URL timeout = this.getResource("etc/jsaga-timeout.properties");
56              if (timeout != null) {
57                  System.setProperty("jsaga.timeout", timeout.toString());
58              }
59          }
60          if (System.getProperty("log4j.configuration") == null) {
61              java.net.URL log4j = this.getResource("etc/log4j.properties");
62              if (log4j != null) {
63                  System.setProperty("log4j.configuration", log4j.toString());
64              }
65          }
66  
67          // load test config
68          java.net.URL test = this.getResource("saga-test.properties");
69          if (test == null) {
70              throw new Exception("Resource not found: saga-test.properties");
71          }
72          m_properties = new Properties();
73          m_properties.load(test.openStream());
74          // may override test config
75          File developerTestProps = new File(new File(System.getProperty("user.home"), ".jsaga"), "saga-test.properties");
76          if (developerTestProps.exists()) {
77              Properties developerProps = new Properties();
78              developerProps.load(new FileInputStream(developerTestProps));
79              m_properties.putAll(developerProps);
80          }
81          // forward test config to System properties
82          System.getProperties().putAll(m_properties);
83      }
84  
85      @Rule
86      public TestRule watcher = new TestWatcher() {
87         protected void starting(Description description) {
88         	if(logger.isInfoEnabled())
89      		logger.info(description.getMethodName()+" running...");
90      	startTime = new Date().getTime();
91         }
92         protected void finished(Description description) {
93           if(logger.isDebugEnabled())
94          	 logger.debug(description.getMethodName()+" - Duration: "+ (new Date().getTime() - startTime)+" ms");
95         }
96  
97      };
98      
99      /** Implicitly invoked before executing each test method */
100 //    protected void setUp() throws Exception {
101 //    	if(logger.isInfoEnabled())
102 //    		logger.info(this.getName()+" running...");
103 //    	startTime = new Date().getTime();
104 //    	super.setUp();
105 //    }
106         
107     /** Implicitly invoked after executing each test method */
108 //    protected void tearDown() throws Exception {
109 //        if(logger.isDebugEnabled())
110 //        	logger.debug(this.getName()+" - Duration: "+ (new Date().getTime() - startTime)+" ms");
111 //    }
112 
113 //    protected void ignore(String message) {
114 //        if(logger.isEnabledFor(Priority.WARN))
115 //            logger.warn(this.getName()+" ignored"+ (message!=null ? " ("+message+")" : ""));
116 //    }
117     
118     protected String getOptionalProperty(String protocol, String name) {
119         return m_properties.getProperty(protocol+"."+name);
120     }
121 
122     protected String getOptionalProperty(String protocol, String name, String defaultValue) {
123         return m_properties.getProperty(protocol+"."+name, defaultValue);
124     }
125 
126     protected String getRequiredProperty(String protocol, String name) throws Exception {
127         String value = m_properties.getProperty(protocol+"."+name);
128         if (value != null) {
129             return value;
130         } else {
131             throw new Exception("Test properties file is missing required property: "+protocol+"."+name);
132         }
133     }
134 
135     protected static URL createURL(URL base, String name) throws NotImplementedException, NoSuccessException, BadParameterException {
136         String basePath = base.getPath();
137         String path = (basePath.endsWith("/") ? basePath+name : basePath+"/"+name);
138         URL url = URLFactory.createURL(base.toString());
139         url.setPath(path);
140         return url;
141     }
142 
143     private java.net.URL getResource(String path) throws IOException {
144         ClassLoader loader = this.getClass().getClassLoader();
145         // get class JAR
146         String classPath = this.getClass().getName().replaceAll("\\.", "/") + ".class";
147         java.net.URL classResource = loader.getResource(classPath);
148         String classJar = getJar(classResource, classPath);
149         // find resource matching class JAR
150         for (Enumeration<java.net.URL> e=loader.getResources(path); e.hasMoreElements(); ) {
151             // get resource JAR
152             java.net.URL resource = e.nextElement();
153             String jar = getJar(resource, path);
154             // compare
155             if (classJar.equals(jar)) {
156                 return resource;
157             }
158         }
159         return null;
160     }
161     private static String getJar(java.net.URL resource, String path) {
162         int index = resource.toString().indexOf(path);
163         if (index > -1) {
164             return resource.toString().substring(0, index);
165         } else {
166             return null;
167         }
168     }
169 }