@Override public DaemonOperationOptions parseOptions(String[] operationArgs) throws Exception { DaemonOperationOptions operationOptions = new DaemonOperationOptions(); OptionParser parser = new OptionParser(); OptionSet options = parser.parse(operationArgs); // Files List<?> nonOptionArgs = options.nonOptionArguments(); if (nonOptionArgs.size() == 0) { throw new Exception("Invalid syntax, no action given (start, stop, reload, restart, status, force-stop, list, add, remove)."); } // <action> String actionStr = nonOptionArgs.get(0).toString(); action = parseDaemonAction(actionStr); operationOptions.setAction(action); // add|remove (<folder-path> ...) if (action == DaemonAction.ADD || action == DaemonAction.REMOVE) { if (nonOptionArgs.size() < 2) { throw new Exception("Invalid syntax, please specify a folder path."); } // <folder-path> ... List<String> watchRoots = new ArrayList<>(); for (int i = 1; i < nonOptionArgs.size(); i++) { watchRoots.add(nonOptionArgs.get(i).toString()); } operationOptions.setWatchRoots(watchRoots); } return operationOptions; }
@Override public DaemonOperationResult execute() throws Exception { logger.log(Level.INFO, "Starting daemon operation with action " + options.getAction() + " ..."); switch (options.getAction()) { case LIST: return executeList(); case ADD: return executeAdd(); case REMOVE: return executeRemove(); case RUN: return executeRun(); default: throw new Exception("Unknown action: " + options.getAction()); } }
private int countWatchedMatchingFolders() { int watchedMatchingFoldersCount = 0; for (FolderTO folderTO : daemonConfig.getFolders()) { if (options.getWatchRoots().contains(folderTO.getPath())) { watchedMatchingFoldersCount++; } } return watchedMatchingFoldersCount; }
@Test public void testSetWatchRoots() { options = new DaemonOperationOptions(); List<String> watchRoots = new ArrayList<String>(); watchRoots.add("root1"); watchRoots.add("root2"); options.setWatchRoots(watchRoots); List<String> res = options.getWatchRoots(); assertNotNull(res); assertFalse(res.isEmpty()); assertTrue(res.contains("root1")); assertTrue(res.contains("root2")); } }
@Test public void testGetWatchRoots() { options = new DaemonOperationOptions(); List<String> res = options.getWatchRoots(); assertNotNull(res); assertTrue(res.isEmpty()); }
@Test public void testExecuteRemove() throws Exception { when(options.getAction()).thenReturn(DaemonAction.REMOVE); List<String> watchRoots = new ArrayList<String>(); watchRoots.add(tempWatchRootAppFolder.getParent()); when(options.getWatchRoots()).thenReturn(watchRoots); DaemonOperationResult res = deamonOp.execute(); assertNotNull(res); assertEquals(DaemonResultCode.OK, res.getResultCode()); assertNotNull(res.getWatchList()); assertTrue(res.getWatchList().isEmpty()); }
public DaemonOperation() { this(new DaemonOperationOptions(DaemonAction.RUN)); }
@Test public void testExecuteAdd() throws Exception { when(options.getAction()).thenReturn(DaemonAction.ADD); List<String> watchRoots = new ArrayList<String>(); watchRoots.add(tempWatchRootAppFolder.getParent()); when(options.getWatchRoots()).thenReturn(watchRoots); DaemonOperationResult res = deamonOp.execute(); assertNotNull(res); assertEquals(DaemonResultCode.OK, res.getResultCode()); assertNotNull(res.getWatchList()); assertEquals(1, res.getWatchList().size()); assertEquals(tempWatchRootAppFolder.getParentFile().getAbsolutePath(), res.getWatchList().get(0).getPath()); }
@Before public void setUp() { options = new DaemonOperationOptions(DaemonAction.ADD); }
@Test public void testGetAction() { DaemonAction res = options.getAction(); assertNotNull(res); assertEquals(DaemonAction.ADD, res); }
private DaemonOperationResult executeAdd() throws Exception { // Check all folders for (String watchRoot : options.getWatchRoots()) { File watchRootFolder = new File(watchRoot); File watchRootAppFolder = new File(watchRootFolder, Config.DIR_APPLICATION); if (!watchRootFolder.isDirectory() || !watchRootAppFolder.isDirectory()) { throw new Exception("Given argument is not an existing folder, or a valid Syncany folder: " + watchRoot); } } // Add them for (String watchRoot : options.getWatchRoots()) { DaemonConfigHelper.addFolder(new File(watchRoot)); } // Determine return code loadOrCreateConfig(); int watchedMatchingFoldersCount = countWatchedMatchingFolders(); if (watchedMatchingFoldersCount == options.getWatchRoots().size()) { return new DaemonOperationResult(DaemonResultCode.OK, daemonConfig.getFolders()); } else if (watchedMatchingFoldersCount > 0) { return new DaemonOperationResult(DaemonResultCode.OK_PARTIAL, daemonConfig.getFolders()); } else { return new DaemonOperationResult(DaemonResultCode.NOK, daemonConfig.getFolders()); } }
@Test public void testExecuteList() throws Exception { when(options.getAction()).thenReturn(DaemonAction.LIST); DaemonOperationResult res = deamonOp.execute(); assertNotNull(res); assertEquals(DaemonResultCode.OK, res.getResultCode()); assertNotNull(res.getWatchList()); }
private DaemonOperationResult executeRemove() throws ConfigException { // Sort Collections.sort(options.getWatchRoots(), Ordering.natural().reverse()); // Remove all folders for (String watchRoot : options.getWatchRoots()) { logger.log(Level.INFO, "- Removing folder from daemon config: " + watchRoot + " ..."); DaemonConfigHelper.removeFolder(watchRoot); } // Check if folders were removed loadOrCreateConfig(); int watchedMatchingFoldersCount = countWatchedMatchingFolders(); if (watchedMatchingFoldersCount == options.getWatchRoots().size()) { return new DaemonOperationResult(DaemonResultCode.NOK, daemonConfig.getFolders()); } else if (watchedMatchingFoldersCount > 0) { return new DaemonOperationResult(DaemonResultCode.NOK_PARTIAL, daemonConfig.getFolders()); } else { return new DaemonOperationResult(DaemonResultCode.OK, daemonConfig.getFolders()); } }