@After
  public void after() {

    // clean up parent directory
    File f = new File(Const.getKettleUserRepositoriesFile());
    f.delete();
    f = new File(Const.getKettleDirectory());
    f.delete();
    BasePropertyHandler.getInstance()
        .notify((PropertyHandler) LAFFactory.getHandler(PropertyHandler.class));
  }
@SuppressWarnings("nls")
/**
 * This test case writes to the current user's home .kettleTest folder and then cleans it up
 * afterwards
 */
public class RepositorySyncWebServiceTest implements Serializable {

  private static final long serialVersionUID =
      -5736355727016778872L; /* EESOURCE: UPDATE SERIALVERUID */

  final PropertyHandler handler = (PropertyHandler) LAFFactory.getHandler(PropertyHandler.class);

  public class TestAppContext implements IApplicationContext {
    public String getBaseUrl() {
      return "";
    }

    public File createTempFile(
        IPentahoSession session, String prefix, String extension, File parentDir, boolean trackFile)
        throws IOException {
      return null;
    }

    public File createTempFile(
        IPentahoSession session, String prefix, String extension, boolean trackFile)
        throws IOException {
      return null;
    }

    public String getApplicationPath(String path) {
      return null;
    }

    public Object getContext() {
      return null;
    }

    public String getFileOutputPath(String path) {
      return null;
    }

    public String getPentahoServerName() {
      return null;
    }

    public String getProperty(String key) {
      return null;
    }

    public String getProperty(String key, String defaultValue) {
      return null;
    }

    public String getSolutionPath(String path) {
      return "";
    }

    public String getSolutionRootPath() {
      return null;
    }

    public void invokeEntryPoints() {}

    public void invokeExitPoints() {}

    public void removeEntryPointHandler(IPentahoSystemEntryPoint entryPoint) {}

    public void removeExitPointHandler(IPentahoSystemExitPoint exitPoint) {}

    public void setContext(Object context) {}

    public void setSolutionRootPath(String path) {}

    public void addEntryPointHandler(IPentahoSystemEntryPoint entryPoint) {}

    public void addExitPointHandler(IPentahoSystemExitPoint exitPoint) {}

    public String getFullyQualifiedServerURL() {
      return fullyQualifiedServerUrl;
    }

    public void setFullyQualifiedServerURL(String url) {
      fullyQualifiedServerUrl = url;
    }

    public void setBaseUrl(String url) {}
  }

  public class TestPropertyHandler implements PropertyHandler {
    public boolean exists(String arg0) {
      throw new RuntimeException("Not Expecting call to exists(" + arg0 + ")");
    }

    public String getProperty(String arg0) {
      return handler.getProperty(arg0);
      // throw new RuntimeException("Not Expecting call to getProperty("+arg0+")");
    }

    public String getProperty(String arg0, String arg1) {
      if (arg0.equals("userBaseDir")) {
        return ".kettleTest";
      }
      throw new RuntimeException("Not Expecting request for " + arg0);
    }

    public boolean loadProps(String arg0) {
      throw new RuntimeException("Not Expecting call to loadProps");
    }
  }

  @RepositoryPlugin(
      id = "PentahoEnterpriseRepository",
      name = "PentahoEnterpriseRepository",
      metaClass = "com.pentaho.pdi.ws.RepositorySyncWebServiceTest$TestRepositoryMeta")
  public static class TestRepositoryMeta extends BaseRepositoryMeta implements RepositoryMeta {
    public TestRepositoryMeta() {
      super("PentahoEnterpriseRepository");
    }

    public RepositoryCapabilities getRepositoryCapabilities() {
      return null;
    }

    public RepositoryMeta clone() {
      return null;
    }

    String url;

    public String getXML() {
      StringBuffer retval = new StringBuffer(100);

      retval.append("  ").append(XMLHandler.openTag(XML_TAG));
      retval.append(super.getXML());
      retval.append("    ").append(XMLHandler.addTagValue("repository_location_url", url));
      retval.append("  ").append(XMLHandler.closeTag(XML_TAG));

      return retval.toString();
    }

    public void loadXML(Node repnode, List<DatabaseMeta> databases) throws KettleException {
      super.loadXML(repnode, databases);
      try {
        url = XMLHandler.getTagValue(repnode, "repository_location_url");
      } catch (Exception e) {
        throw new KettleException("Unable to load Kettle database repository meta object", e);
      }
    }
  }

  public IRepositorySyncWebService getRepositorySyncWebService() {
    return new RepositorySyncWebService();
  }

  String fullyQualifiedServerUrl = "http://localhost:8080/pentaho-di";

  @After
  public void after() {

    // clean up parent directory
    File f = new File(Const.getKettleUserRepositoriesFile());
    f.delete();
    f = new File(Const.getKettleDirectory());
    f.delete();
    BasePropertyHandler.getInstance()
        .notify((PropertyHandler) LAFFactory.getHandler(PropertyHandler.class));
  }

  @Test
  public void testSyncWebService() throws Exception {

    // first init kettle

    KettleEnvironment.init(false);
    BasePropertyHandler.getInstance().notify(new TestPropertyHandler());
    File f = new File(Const.getKettleDirectory());
    f.mkdirs();

    // second init platform
    PentahoSystem.registerObjectFactory(new SimpleObjectFactory());
    PentahoSystem.init(new TestAppContext(), null);
    PentahoSystem.setSystemSettingsService(
        new ISystemSettings() {
          public String getSystemCfgSourceName() {
            return null;
          }

          public String getSystemSetting(String arg0, String arg1) {
            if ("singleDiServerInstance".equals(arg0)) {
              return "false";
            }
            return arg1;
          }

          public String getSystemSetting(String arg0, String arg1, String arg2) {
            return null;
          }

          public List getSystemSettings(String arg0) {
            return null;
          }

          public List getSystemSettings(String arg0, String arg1) {
            return null;
          }

          public Document getSystemSettingsDocument(String arg0) {
            return null;
          }

          public Properties getSystemSettingsProperties(String arg0) {
            return null;
          }

          public void resetSettingsCache() {}
        });

    // now test the webservice
    IRepositorySyncWebService webservice = getRepositorySyncWebService();

    // first without the plugin available
    try {
      webservice.sync("test id", "http://localhost:8080/pentaho-di");
      Assert.fail();
    } catch (RepositorySyncException e) {
      Assert.assertTrue(
          e.getMessage().indexOf("unable to load the PentahoEnterpriseRepository plugin") >= 0);
    }

    // second with plugin but not registered
    RepositoryPluginType.getInstance()
        .registerCustom(
            TestRepositoryMeta.class,
            "PentahoEnterpriseRepository",
            "PentahoEnterpriseRepository",
            "PentahoEnterpriseRepository",
            "PentahoEnterpriseRepository",
            "");
    PluginRegistry.getInstance()
        .getPlugin(RepositoryPluginType.class, "PentahoEnterpriseRepository")
        .getClassMap()
        .put(
            RepositoryMeta.class,
            "com.pentaho.pdi.ws.RepositorySyncWebServiceTest$TestRepositoryMeta");

    RepositorySyncStatus status = webservice.sync("test id", "http://localhost:8080/pentaho-di");

    Assert.assertEquals(RepositorySyncStatus.REGISTERED, status);

    // third after already registered
    status = webservice.sync("test id", "http://localhost:8080/pentaho-di");

    Assert.assertEquals(RepositorySyncStatus.ALREADY_REGISTERED, status);

    // forth test with different url
    try {
      webservice.sync("test id", "http://localhost:9090/pentaho-di");
      Assert.fail();
    } catch (RepositorySyncException e) {
      Assert.assertTrue(e.getMessage().indexOf("with the URL:") >= 0);
    }

    // fifth test different base-url
    fullyQualifiedServerUrl = "http://localhost:9090/pentaho-di";
    try {
      webservice.sync("test id", "http://localhost:8080/pentaho-di");
      Assert.fail();
    } catch (RepositorySyncException e) {
      Assert.assertTrue(e.getMessage().indexOf("fully qualified server url") >= 0);
    }
  }
}