88
99public class HelperUtils {
1010
11- private static Properties testProperties ;
12- static {
11+ private static final Properties testProperties = new Properties ();
1312
14- File propertiesFile = new File ((String ) System .getProperties ().get ("user.home" ), "test-gitlab4j.properties" );
15- if (!propertiesFile .exists ()) {
13+ static {
1614
17- // Get the maven basedir, we use it to locate the properties for the unit tests
18- String basedir = (String ) System .getProperties ().get ("basedir" );
15+ boolean propertiesLoaded = false ;
1916
20- // If we are performing a release in target/checkout, trim off the target/checkout directory from basedir
21- if (basedir != null && (basedir .endsWith ("target/checkout" ) || basedir .endsWith ("target\\ checkout" ))) {
22- basedir = basedir .substring (0 , basedir .length () - 15 );
23- }
17+ // Get the maven basedir, we use it to locate the default properties for the unit tests
18+ String basedir = (String ) System .getProperties ().get ("basedir" );
2419
25- propertiesFile = new File (basedir , "src/test/gitlab/test-gitlab4j.properties" );
20+ // If we are performing a release in target/checkout, trim off the target/checkout directory from basedir
21+ if (basedir != null && (basedir .endsWith ("target/checkout" ) || basedir .endsWith ("target\\ checkout" ))) {
22+ basedir = basedir .substring (0 , basedir .length () - 15 );
2623 }
2724
25+ // Load the base test properties from "src/test/resources/test-gitlab4j.properties"
26+ File propertiesFile = new File (basedir , "src/test/resources/test-gitlab4j.properties" );
2827 if (propertiesFile .exists ()) {
28+ try (InputStream input = new FileInputStream (propertiesFile )) {
29+ testProperties .load (input );
30+ System .out .println ("Loaded base test properties from: " + propertiesFile .getAbsolutePath ());
31+ propertiesLoaded = true ;
32+ } catch (IOException ioe ) {
33+ System .err .println ("Error loading base test properties, error=" + ioe .getMessage ());
34+ }
35+ }
2936
30- System . out . println ( " test-gitlab4j. properties location: " + propertiesFile . getAbsolutePath ());
31-
32- testProperties = new Properties ();
37+ // Now load the overriding test properties if found in the user's home directory
38+ propertiesFile = new File (( String ) System . getProperties (). get ( "user.home" ), "test-gitlab4j.properties" );
39+ if ( propertiesFile . exists ()) {
3340 try (InputStream input = new FileInputStream (propertiesFile )) {
3441 testProperties .load (input );
42+ System .out .println ("Loaded overriding test properties from: " + propertiesFile .getAbsolutePath ());
43+ propertiesLoaded = true ;
3544 } catch (IOException ioe ) {
45+ System .err .println ("Error loading overriding test properties, error=" + ioe .getMessage ());
3646 }
47+ }
3748
38- } else {
39- System .out .println ("No test-gitlab4j. properties file found " );
49+ if (! propertiesLoaded ) {
50+ System .out .println ("No test properties have been loaded! " );
4051 }
4152 }
4253
@@ -50,6 +61,28 @@ public static final String getProperty(String key) {
5061 return (testProperties .getProperty (key ));
5162 }
5263
64+ /**
65+ * Get a named property from the test-gitlab4j.properties file,
66+ * will return the defaultValue if null or empty.
67+ *
68+ * @param key the key of the property to get
69+ * @param defaultValue the value to return if property is null or empty
70+ * @return the named property from the test-gitlab4j.properties file
71+ */
72+ public static final String getProperty (String key , String defaultValue ) {
73+
74+ String value = getProperty (key );
75+ if (value != null && value .trim ().length () > 0 ) {
76+ return (value );
77+ }
78+
79+ if (defaultValue != null ) {
80+ testProperties .setProperty (key , defaultValue );
81+ }
82+
83+ return (defaultValue );
84+ }
85+
5386 /**
5487 * Set a named property, this will amend and overwrite properties read from the test-gitlab4j.properties file.
5588 *
0 commit comments