MessageFormatCheck implements ReviewParticipant { @Override public Review review(PropertyFileDescriptor descriptor, Property master, Property slave) { if(master==null || slave==null || master.getValue()==null || slave.getValue()==null) return null; Matcher masterMatcher = PATTERN.matcher(master.getValue()); Set<String> masterPatterns = new HashSet<String>(); while(masterMatcher.find()) { masterPatterns.add("{"+masterMatcher.group(1)+"}"); } Set<String> mustHavePatterns = new HashSet<String>(masterPatterns); Matcher slaveMatcher = PATTERN.matcher(slave.getValue()); while(slaveMatcher.find()) { String patternNumber = slaveMatcher.group(1); String pattern = "{"+patternNumber+"}"; if(!masterPatterns.contains(pattern)) { Review review = PropertiesFactory.eINSTANCE.createReview(); review.setCreated(System.currentTimeMillis()); String message = "Translation contains message format ''{0}'' which is not present in the template language"; review.setMessage(MessageFormat.format(message, pattern)); review.setUser("Jabylon"); review.setReviewType("Message Format"); review.setSeverity(Severity.ERROR); return review; } mustHavePatterns.remove(pattern); } if(!mustHavePatterns.isEmpty()) { Review review = PropertiesFactory.eINSTANCE.createReview(); String message = "The template language contains message format ''{0}'' which is not referenced in the translation"; review.setMessage(MessageFormat.format(message, mustHavePatterns.iterator().next())); review.setUser("Jabylon"); review.setSeverity(Severity.ERROR); review.setReviewType(getReviewType()); return review; } return null; } MessageFormatCheck(); @Override Review review(PropertyFileDescriptor descriptor, Property master, Property slave); @Override String getReviewType(); @Override String getID(); @Override String getDescription(); @Override String getName();  }
@Test public void testReview() { Property master = createProperty("key", "value {0}"); Property slave = createProperty("value", "{0} wert"); assertNull(getFixture().review(null, master, slave)); }
@Test public void testReviewNoHit() { Property master = createProperty("key", "value"); Property slave = createProperty("value", "wert"); assertNull(getFixture().review(null, master, slave)); }
@Test public void testReviewWrongNumber() { Property master = createProperty("key", "value {0}"); Property slave = createProperty("value", "{1} wert"); assertNotNull(getFixture().review(null, master, slave)); }
@Test public void testReviewMissingFormat() { Property master = createProperty("key", "value {0}"); Property slave = createProperty("value", " wert"); assertNotNull(getFixture().review(null, master, slave)); }
@Test public void testReviewExtraFormat() { Property master = createProperty("key", "value {0}"); Property slave = createProperty("value", "{0} wert {2}"); assertNotNull(getFixture().review(null, master, slave)); }
@Test public void testReviewDoubleUsageMaster() { Property master = createProperty("key", "{0} value {0}"); Property slave = createProperty("value", "{0} wert"); assertNull(getFixture().review(null, master, slave)); }
@Test public void testReviewDoubleUsageSlave() { Property master = createProperty("key", "value {0}"); Property slave = createProperty("value", "{0} wert {0}"); assertNull(getFixture().review(null, master, slave)); }
@Test public void testReviewChoiceFormat() { Property master = createProperty("key", "value {0,choice,0#days|1#day|1<days}"); Property slave = createProperty("value", "wert {0,choice,0#Tage|1#Tag|1<Tage}"); assertNull("this is ok, because the 0 argument is present in both cases",getFixture().review(null, master, slave)); }