@Test public void whenReplaceKeyInLineWithProperKeyMapThenResultLineWithReplacedWord() { final String lineToReplace = "How ${are} you ${are}? ${g}"; final String expectedResult = "How do you do? good"; try { String result = generator.generate(lineToReplace, keyMap); assertThat(result, is(expectedResult)); } catch (NoKeyException | WrongKeyException nke) { nke.printStackTrace(); } }
@Override public String generate(String line, Map<String, String> keyMap) throws NoKeyException, WrongKeyException { StringBuilder sb = new StringBuilder(); sb.append(line); Matcher m = PATTERN.matcher(sb); HashSet<String> keySetFromLine = new HashSet<>(); while (m.find()) { keySetFromLine.add(m.group(0)); if (keyMap.containsKey(m.group(0))) { sb.replace(m.start(), m.end(), keyMap.get(m.group(0))); } else { throw new NoKeyException("Error: no Key in Map"); } m.reset(sb); } if (keySetFromLine.size() != keyMap.size()) { throw new WrongKeyException("Error: Map has unnecessary key"); } return sb.toString(); }
SimpleGenerator implements Template { @Override public String generate(String line, Map<String, String> keyMap) throws NoKeyException, WrongKeyException { StringBuilder sb = new StringBuilder(); sb.append(line); Matcher m = PATTERN.matcher(sb); HashSet<String> keySetFromLine = new HashSet<>(); while (m.find()) { keySetFromLine.add(m.group(0)); if (keyMap.containsKey(m.group(0))) { sb.replace(m.start(), m.end(), keyMap.get(m.group(0))); } else { throw new NoKeyException("Error: no Key in Map"); } m.reset(sb); } if (keySetFromLine.size() != keyMap.size()) { throw new WrongKeyException("Error: Map has unnecessary key"); } return sb.toString(); } }
SimpleGenerator implements Template { @Override public String generate(String line, Map<String, String> keyMap) throws NoKeyException, WrongKeyException { StringBuilder sb = new StringBuilder(); sb.append(line); Matcher m = PATTERN.matcher(sb); HashSet<String> keySetFromLine = new HashSet<>(); while (m.find()) { keySetFromLine.add(m.group(0)); if (keyMap.containsKey(m.group(0))) { sb.replace(m.start(), m.end(), keyMap.get(m.group(0))); } else { throw new NoKeyException("Error: no Key in Map"); } m.reset(sb); } if (keySetFromLine.size() != keyMap.size()) { throw new WrongKeyException("Error: Map has unnecessary key"); } return sb.toString(); }  }
SimpleGenerator implements Template { @Override public String generate(String line, Map<String, String> keyMap) throws NoKeyException, WrongKeyException { StringBuilder sb = new StringBuilder(); sb.append(line); Matcher m = PATTERN.matcher(sb); HashSet<String> keySetFromLine = new HashSet<>(); while (m.find()) { keySetFromLine.add(m.group(0)); if (keyMap.containsKey(m.group(0))) { sb.replace(m.start(), m.end(), keyMap.get(m.group(0))); } else { throw new NoKeyException("Error: no Key in Map"); } m.reset(sb); } if (keySetFromLine.size() != keyMap.size()) { throw new WrongKeyException("Error: Map has unnecessary key"); } return sb.toString(); }  @Override String generate(String line, Map<String, String> keyMap); }
SimpleGenerator implements Template { @Override public String generate(String line, Map<String, String> keyMap) throws NoKeyException, WrongKeyException { StringBuilder sb = new StringBuilder(); sb.append(line); Matcher m = PATTERN.matcher(sb); HashSet<String> keySetFromLine = new HashSet<>(); while (m.find()) { keySetFromLine.add(m.group(0)); if (keyMap.containsKey(m.group(0))) { sb.replace(m.start(), m.end(), keyMap.get(m.group(0))); } else { throw new NoKeyException("Error: no Key in Map"); } m.reset(sb); } if (keySetFromLine.size() != keyMap.size()) { throw new WrongKeyException("Error: Map has unnecessary key"); } return sb.toString(); }  @Override String generate(String line, Map<String, String> keyMap);  }