AlarmMessageFormatter { public String format(MetaInAlarm meta) { StringBuilder message = new StringBuilder(); for (int i = 0; i < formatSegments.size(); i++) { message.append(formatSegments.get(i)); if (i != formatSegments.size() - 1) { switch (valueFroms.get(i)) { case ID: message.append(meta.getId0()); break; case NAME: message.append(meta.getName()); } } } return message.toString(); } AlarmMessageFormatter(String format); String format(MetaInAlarm meta);  }
@Test public void testStringFormatWithNoArg() { AlarmMessageFormatter formatter = new AlarmMessageFormatter("abc words {sdf"); String message = formatter.format(new MetaInAlarm() { @Override public String getScope() { return "SERVICE"; } @Override public int getScopeId() { return -1; } @Override public String getName() { return null; } @Override public String getMetricsName() { return null; } @Override public String getId0() { return ""; } @Override public String getId1() { return ""; } }); Assert.assertEquals("abc words {sdf", message); }
@Test public void testStringFormatWithArg() { AlarmMessageFormatter formatter = new AlarmMessageFormatter("abc} words {name} - {id} .. {"); String message = formatter.format(new MetaInAlarm() { @Override public String getScope() { return "SERVICE"; } @Override public int getScopeId() { return -1; } @Override public String getName() { return "service"; } @Override public String getMetricsName() { return null; } @Override public String getId0() { return "1290"; } @Override public String getId1() { return ""; } }); Assert.assertEquals("abc} words service - 1290 .. {", message); }