VirtualMachineHandler { public ProcessDescription findProcess(Pattern pPattern) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { List<ProcessDescription> ret = new ArrayList<ProcessDescription>(); String ownId = getOwnProcessId(); for (ProcessDescription desc : listProcesses()) { Matcher matcher = pPattern.matcher(desc.getDisplay()); if (!desc.getId().equals(ownId) && matcher.find()) { ret.add(desc); } } if (ret.size() == 1) { return ret.get(0); } else if (ret.size() == 0) { throw new IllegalArgumentException("No attachable process found matching \"" + pPattern.pattern() + "\""); } else { StringBuilder buf = new StringBuilder(); for (ProcessDescription desc : ret) { buf.append(desc.getId()).append(" (").append(desc.getDisplay()).append("),"); } throw new IllegalArgumentException("More than one attachable process found matching \"" + pPattern.pattern() + "\": " + buf.substring(0,buf.length()-1)); } } VirtualMachineHandler(OptionsAndArgs pOptions); Object attachVirtualMachine(); void detachAgent(Object pVm); List<ProcessDescription> listProcesses(); ProcessDescription findProcess(Pattern pPattern);  }
@Test public void findProcess() throws Exception, NoSuchMethodException, IllegalAccessException { List<ProcessDescription> procs = filterOwnProcess(vmHandler.listProcesses()); for (ProcessDescription desc : procs) { try { if (desc.getDisplay() != null && desc.getDisplay().length() > 0) { Pattern singleHitPattern = Pattern.compile("^" + Pattern.quote(desc.getDisplay()) + "$"); assertTrue(tryAttach(singleHitPattern.pattern())); break; } } catch (Exception exp) { System.out.println("Cannot attach to " + desc.getDisplay() + " (" + desc.getId() + "): " + exp); exp.printStackTrace(); } } assertFalse(tryAttach("RobertMakClaudioPizarro",".*No.*process.*")); if (procs.size() >= 2) { assertFalse(tryAttach(".",procs.get(0).getId())); } }