public void testGetFileURL() throws Exception {
    // Empty CM
    ConfigurationManager cm = new ConfigurationManagerImpl();
    URL url = cm.getURL(null);
    assertNull(url);
    url = cm.getURL("file:F:\\somepath\\path\\configuration.xml");
    assertEquals("file:/F:/somepath/path/configuration.xml", url.toString());

    // make context configuration starting fith "file:D:..."
    try {
      cm.addConfiguration("file:D:\\somepath\\config.xml");
    } catch (Exception e) {
      // thats is ok, because such config does not exists,
      // but ConfigurationManagerInmp.contextPath going to be initialized
      // thats all we need to reproduce bug.
    }

    // now lets check relative url
    url = cm.getURL("configuration.xml");
    assertEquals("file:/D:/somepath/configuration.xml", url.toString());
  }
  public void testGetURL() throws Exception {
    // Empty CM
    ConfigurationManager cm = new ConfigurationManagerImpl();
    URL url = cm.getURL(null);
    assertNull(url);
    url = cm.getURL("jar:/org/exoplatform/container/configuration/empty-config.xml");
    checkURL(url);
    url = cm.getURL("jar:/org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);
    url = cm.getURL("classpath:/org/exoplatform/container/configuration/empty-config.xml");
    checkURL(url);
    url = cm.getURL("classpath:/org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);
    try {
      url = cm.getURL("war:/org/exoplatform/container/configuration/empty-config.xml");
      fail("An error should be thrown");
    } catch (Exception e) {
      // ok;
    }
    try {
      url = cm.getURL("war:/org/exoplatform/container/configuration/empty-config-fake.xml");
      fail("An error should be thrown");
    } catch (Exception e) {
      // ok;
    }
    String sURL = getClass().getResource("empty-config.xml").toString();
    assertNotNull(sURL);
    assertTrue("the expected path should starts with file:", sURL.startsWith("file:"));
    sURL = sURL.substring(0, sURL.lastIndexOf('/'));
    sURL = sURL.substring(0, sURL.lastIndexOf('/'));
    url = cm.getURL(sURL + "/configuration/empty-config.xml");
    checkURL(url);
    url = cm.getURL(sURL + "/configuration/empty-config-fake.xml");
    checkURL(url, true);
    url = cm.getURL(sURL + "\\configuration\\empty-config.xml");
    checkURL(url);
    url = cm.getURL(sURL + "\\configuration\\empty-config-fake.xml");
    checkURL(url, true);
    String incompleteURL = "file:/" + getClass().getResource("empty-config.xml").getPath();
    incompleteURL = incompleteURL.substring(0, incompleteURL.lastIndexOf('/'));
    url = cm.getURL(incompleteURL + "/empty-config.xml");
    checkURL(url);
    url = cm.getURL(incompleteURL + "/empty-config-fake.xml");
    checkURL(url, true);
    incompleteURL = "file:" + getClass().getResource("empty-config.xml").getPath();
    incompleteURL = incompleteURL.substring(0, incompleteURL.lastIndexOf('/'));
    url = cm.getURL(incompleteURL + "/empty-config.xml");
    checkURL(url);
    url = cm.getURL(incompleteURL + "/empty-config-fake.xml");
    checkURL(url, true);
    url = cm.getURL("org/exoplatform/container/configuration/empty-config.xml");
    assertNull(url);
    url = cm.getURL("org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);

    // CM with ClassLoader
    ConfigurationManager cm1 =
        new ConfigurationManagerImpl(Thread.currentThread().getContextClassLoader(), null);
    url = cm1.getURL(null);
    assertNull(url);
    url = cm1.getURL("jar:/org/exoplatform/container/configuration/empty-config.xml");
    checkURL(url);
    url = cm1.getURL("jar:/org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);
    url = cm1.getURL("classpath:/org/exoplatform/container/configuration/empty-config.xml");
    checkURL(url);
    url = cm1.getURL("classpath:/org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);
    url = cm1.getURL("war:/org/exoplatform/container/configuration/empty-config.xml");
    checkURL(url);
    url = cm1.getURL("war:/org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);
    url = cm1.getURL(sURL + "/configuration/empty-config.xml");
    checkURL(url);
    url = cm1.getURL(sURL + "/configuration/empty-config-fake.xml");
    checkURL(url, true);
    url = cm1.getURL("org/exoplatform/container/configuration/empty-config.xml");
    assertNull(url);
    url = cm1.getURL("org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);

    // CM with ServletContext
    ConfigurationManager cm2 = new ConfigurationManagerImpl(new MockServletContext(), null);
    url = cm2.getURL(null);
    assertNull(url);
    url = cm2.getURL("jar:/org/exoplatform/container/configuration/empty-config.xml");
    checkURL(url);
    url = cm2.getURL("jar:/org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);
    url = cm2.getURL("classpath:/org/exoplatform/container/configuration/empty-config.xml");
    checkURL(url);
    url = cm2.getURL("classpath:/org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);
    url = cm2.getURL("war:/org/exoplatform/container/configuration/empty-config.xml");
    checkURL(url);
    url = cm2.getURL("war:/org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);
    url = cm2.getURL(sURL + "/configuration/empty-config.xml");
    checkURL(url);
    url = cm2.getURL(sURL + "/configuration/empty-config-fake.xml");
    checkURL(url, true);
    url = cm2.getURL("org/exoplatform/container/configuration/empty-config.xml");
    assertNull(url);
    url = cm2.getURL("org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);

    // CM with Context path
    ConfigurationManager cm3 = new ConfigurationManagerImpl();
    String path = getClass().getResource("empty-config.xml").getPath();
    assertNotNull(path);
    path = path.substring(0, path.lastIndexOf('/'));
    cm3.addConfiguration((new File(path)).toURI().toURL());
    url = cm3.getURL(null);
    assertNull(url);
    url = cm3.getURL("jar:/org/exoplatform/container/configuration/empty-config.xml");
    checkURL(url);
    url = cm3.getURL("jar:/org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);
    url = cm3.getURL("classpath:/org/exoplatform/container/configuration/empty-config.xml");
    checkURL(url);
    url = cm3.getURL("classpath:/org/exoplatform/container/configuration/empty-config-fake.xml");
    assertNull(url);
    try {
      url = cm3.getURL("war:/org/exoplatform/container/configuration/empty-config.xml");
      fail("An error should be thrown");
    } catch (Exception e) {
      // ok;
    }
    try {
      url = cm3.getURL("war:/org/exoplatform/container/configuration/empty-config-fake.xml");
      fail("An error should be thrown");
    } catch (Exception e) {
      // ok;
    }
    url = cm3.getURL(sURL + "/configuration/empty-config.xml");
    checkURL(url);
    url = cm3.getURL(sURL + "/configuration/empty-config-fake.xml");
    checkURL(url, true);
    url = cm3.getURL("configuration/empty-config.xml");
    checkURL(url);
    url = cm3.getURL("configuration/empty-config-fake.xml");
    checkURL(url, true);
    url = cm3.getURL("configuration\\empty-config.xml");
    checkURL(url);
    url = cm3.getURL("configuration\\empty-config-fake.xml");
    checkURL(url, true);
  }