AliasManager { public String parseAlias(String buffer) { if(buffer.trim().equals(ALIAS)) return printAllAliases(); if (aliasHelpPattern.matcher(buffer).matches()) return aliasUsage(); Matcher aliasMatcher = aliasPattern.matcher(buffer); boolean aliasMatched = false; if(aliasMatcher.matches()) { aliasMatched = true; String name = aliasMatcher.group(2); String value = aliasMatcher.group(3); if(value.startsWith("'")) { if(value.endsWith("'")) value = value.substring(1,value.length()-1); else return aliasUsage(); } else if(value.startsWith("\"")) { if(value.endsWith("\"")) value = value.substring(1,value.length()-1); else return aliasUsage(); } if(name.contains(" ")) return aliasUsage(); try { if(verifyNoNewAliasConflict(name)) { addAlias(name, value); return null; } else return "Alias "+name+" is in conflict with an existing command"; } catch(AliasConflictException ace) { return ace.getMessage(); } } Matcher listMatcher = listAliasPattern.matcher(buffer); if(listMatcher.matches()) { StringBuilder sb = new StringBuilder(); for(String s : listMatcher.group(2).trim().split(" ")) { if(s != null) { Optional<Alias> a = getAlias(s.trim()); if(a.isPresent()) sb.append(ALIAS_SPACE).append(a.get().getName()).append("='") .append(a.get().getValue()).append("'").append(Config.getLineSeparator()); else sb.append("alias: ").append(s) .append(" : not found").append(Config.getLineSeparator()); } } return sb.toString(); } if (!aliasMatched) { StringBuilder sb = new StringBuilder(); sb.append(buffer).append(" is not valid command, make sure alias name is among of [a-zA-Z0-9_]").append(Config.getLineSeparator()); return sb.toString(); } return null; } AliasManager(File aliasFile, boolean persistAlias); boolean verifyNoNewAliasConflict(String aliasName); void persist(); @SuppressWarnings("unchecked") String printAllAliases(); Optional<Alias> getAlias(String name); Optional<String> getAliasName(String input); List<String> findAllMatchingNames(String name); List<String> getAllNames(); String removeAlias(String buffer); String parseAlias(String buffer); String aliasUsage(); String unaliasUsage();  }
@Test public void testParseAlias() { assertNull(manager.parseAlias("alias foo2='bar -s -h'")); assertNull(manager.parseAlias("alias foo=bar")); assertNull(manager.parseAlias("alias foo3=bar --help")); String out = manager.parseAlias("alias foo"); Assert.assertEquals("alias foo='bar'"+ Config.getLineSeparator(), out); out = manager.parseAlias("alias foo2"); assertEquals("alias foo2='bar -s -h'"+ Config.getLineSeparator(), out); out = manager.parseAlias("alias foo3"); assertEquals("alias foo3='bar --help'"+ Config.getLineSeparator(), out); out = manager.parseAlias("alias"); StringBuilder sb = new StringBuilder(); sb.append("alias foo='bar'").append(Config.getLineSeparator()) .append("alias foo2='bar -s -h'").append(Config.getLineSeparator()) .append("alias foo3='bar --help'").append(Config.getLineSeparator()); assertEquals(sb.toString(), out); out = manager.parseAlias("alias --help"); assertEquals("alias: usage: alias [name[=value] ... ]" + Config.getLineSeparator(), out); out = manager.parseAlias("alias --help"); assertEquals("alias: usage: alias [name[=value] ... ]" + Config.getLineSeparator(), out); }