    1   package javax.jdo;
    2
    3   import java.net.URL;
    4   import java.util.Enumeration;
    5   import java.util.Iterator;
    6   import java.util.Map;
    7   import java.util.Random;
    8
    9   import javax.jdo.util.AbstractTest;
    10
    11  public abstract class AbstractJDOConfigTest extends AbstractTest {
    12
    13      /**
    14       * A class path prefix used in the various tests where the class path
    15       * needs to be set.
    16       */
    17      protected static String JDOCONFIG_CLASSPATH_PREFIX
    18              = initJDOConfigClasspathPrefix();
    19
    20      /**
    21       * Returns the JDO configuration class path prefix's default value, which is
    22       * the project base directory suffixed by the path to the configuration
    23       * directory (<tt>test/schema/jdoconfig</tt>).
    24       *
    25       * @return the default class path prefix used by this test suite.
    26       *
    27       */
    28      protected static String initJDOConfigClasspathPrefix() {
    29          return initBasedir() + "test/schema/jdoconfig";
    30      }
    31
    32      /**
    33       * The class path used to specify the location of test class files.
    34       * @return the class path where test class files can be found.
    35       */
    36      protected static String TEST_CLASSPATH =
    37              initTestClasspath();
    38
    39      /**
    40       * Returns the default class path for JDO test class files
    41       * (<tt>target/test-classes/</tt>).
    42       * @return the default class path for JDO test class files.
    43       */
    44      protected static String initTestClasspath() {
    45          return initBasedir() + "target/test-classes/";
    46      }
    47
    48      /**
    49       * The class path used to locate the JDO API class files.
    50       */
    51      protected static String API_CLASSPATH =
    52              initAPIClasspath();
    53
    54      /**
    55       * Returns the default class path for JDO API class files
    56       * (<tt>target/classes/</tt>).
    57       * @return the default class path for JDO API class files.
    58       */
    59      protected static String initAPIClasspath() {
    60          return initBasedir() + "target/classes/";
    61      }
    62
    63      /**
    64       * Returns the base directory for this project.  This base directory
    65       * is used to build up the other class paths defined in this test suite.
    66       * The value returned is the value returned by
    67       * <code>System.getProperty("basedir")</code>.
    68       * A trailing slash is appended to the path if it doesn't exist.
    69       *
    70       * @return the default base directory of the project.
    71       */
    72      protected static String initBasedir() {
    73          String basedir = System.getProperty("basedir");
    74          if (basedir != null) {
    75              if (!basedir.endsWith("/")) {
    76                  basedir += "/";
    77              }
    78          } else {
    79              basedir = "";
    80          }
    81          return basedir;
    82      }
    83
    84      /**
    85       * A randomizer seeded with the system clock's current time.
    86       */
    87      protected static Random RANDOM = new Random(System.currentTimeMillis());
    88
    89      /**
    90       * Fails the test if the number of properties in the two specified
    91       * {@link java.util.Map Map} objects are not identical or their values
    92       * do not match.
    93       * @param expected the first {@link java.util.Map Map} object to test.
    94       * @param actual the second {@link java.util.Map Map} object to test.
    95       */
    96      static void assertEqualProperties(Map expected, Map actual) {
    97          Iterator i = expected.entrySet().iterator();
    98          while (i.hasNext()) {
    99              Map.Entry entry = (Map.Entry) i.next();
    100             String key = (String) entry.getKey();
    101             String expectedValue = (String) entry.getValue();
    102             String actualValue = (String) actual.get(key);
    103
    104             assertEquals(
    105                     "Actual property at key [" + key + "] with value [" +
    106                             actualValue + "] not equal to expected value [" +
    107                             expectedValue + "]",
    108                     expectedValue,
    109                     actualValue);
    110         }
    111     }
    112
    113     protected String getPMFClassNameViaServiceLookup(ClassLoader loader) {
    114         try {
    115             Enumeration urls = JDOHelper.getResources(loader,
    116                 SERVICE_LOOKUP_PMF_RESOURCE_NAME);
    117             while (urls.hasMoreElements()) {
    118                 // return the first one found
    119                 return JDOHelper.getClassNameFromURL((URL)urls.nextElement());
    120             }
    121         } catch (Exception ex) {
    122             // ignore exceptions from i/o errors
    123         }
    124         return null;
    125     }
    126 }