MessageFormatIntegrityChecker extends AbstractTextUnitIntegrityChecker { @Override public void check(String sourceContent, String targetContent) throws MessageFormatIntegrityCheckerException { MessageFormat targetMessageFormat = null; MessageFormat sourceMessageFormat = null; logger.debug("Check if the target pattern is valid"); try { targetMessageFormat = new MessageFormat(targetContent); } catch (IllegalArgumentException iae) { throw new MessageFormatIntegrityCheckerException("Invalid pattern", iae); } logger.debug("Check if the source pattern is valid to compare the number of format/placeholder"); try { sourceMessageFormat = new MessageFormat(sourceContent); } catch (IllegalArgumentException iae) { throw new MessageFormatIntegrityCheckerException("Invalid source pattern", iae); } logger.debug("Check number of format/placeholder in the source and target message formats is the same"); int numberSourceFormats = sourceMessageFormat.getFormats().length; int numberTargetFormats = targetMessageFormat.getFormats().length; if (numberSourceFormats != numberTargetFormats) { throw new MessageFormatIntegrityCheckerException("Number of placeholders in source (" + numberSourceFormats + ") and target (" + numberTargetFormats + ") is different"); } Set<String> sourceArgumentNames = sourceMessageFormat.getArgumentNames(); Set<String> targetArgumentNames = targetMessageFormat.getArgumentNames(); if (!sourceArgumentNames.equals(targetArgumentNames)) { throw new MessageFormatIntegrityCheckerException("Different placeholder name in source and target"); } }  @Override void check(String sourceContent, String targetContent); @Override LocalizableString extractNonLocalizableParts(String string);  }
@Test public void testCompilationCheckWorks() throws IntegrityCheckException { MessageFormatIntegrityChecker checker = new MessageFormatIntegrityChecker(); String source = "{numFiles, plural, one{# There is one file} other{There are # files}}"; String target = "{numFiles, plural, one{Il y a un fichier} other{Il y a # fichiers}}"; checker.check(source, target); }
@Test public void testCompilationCheckWorksWithMoreForms() throws IntegrityCheckException { MessageFormatIntegrityChecker checker = new MessageFormatIntegrityChecker(); String source = "{numFiles, plural, one{# There is one file} other{There are # files}}"; String target = "{numFiles, plural, zero{Il n'y a pas de fichier} one{Il y a un fichier} other{Il y a # fichiers}}"; checker.check(source, target); }
@Test public void testCompilationCheckFailsIfMissingBracket() throws IntegrityCheckException { MessageFormatIntegrityChecker checker = new MessageFormatIntegrityChecker(); String source = "{numFiles, plural, one{# There is one file} other{There are # files}}"; String target = "{numFiles, plural, one{Il y a un fichier} other{Il y a # fichiers}"; try { checker.check(source, target); fail("MessageFormatIntegrityCheckerException must be thrown"); } catch (MessageFormatIntegrityCheckerException e) { assertEquals("Invalid pattern", e.getMessage()); } }
@Test public void testCompilationCheckFailsIfPluralElementGetsTranslated() throws IntegrityCheckException { MessageFormatIntegrityChecker checker = new MessageFormatIntegrityChecker(); String source = "{numFiles, plural, one{# There is one file} other{There are # files}}"; String target = "{numFiles, plural, un{Il y a un fichier} autre{Il y a # fichiers}}"; try { checker.check(source, target); fail("Exception must be thrown"); } catch (MessageFormatIntegrityCheckerException e) { assertEquals("Invalid pattern", e.getMessage()); } }
@Test public void testNumberOfPlaceholder() throws MessageFormatIntegrityCheckerException { MessageFormatIntegrityChecker checker = new MessageFormatIntegrityChecker(); String source = "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}."; String target = "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}."; checker.check(source, target); }
@Test public void testWrongNumberOfPlaceholder() throws MessageFormatIntegrityCheckerException { MessageFormatIntegrityChecker checker = new MessageFormatIntegrityChecker(); String source = "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}."; String target = "At on {1,date}, there was {2} on planet {0,number,integer}."; try { checker.check(source, target); fail("Exception must be thrown"); } catch (MessageFormatIntegrityCheckerException e) { assertEquals("Number of placeholders in source (4) and target (3) is different", e.getMessage()); } }
@Test public void testNamedParametersChanged() throws MessageFormatIntegrityCheckerException { MessageFormatIntegrityChecker checker = new MessageFormatIntegrityChecker(); String source = "{username} likes skydiving"; String target = "{utilisateur} aime le saut en parachute"; try { checker.check(source, target); fail("Exception must be thrown"); } catch (MessageFormatIntegrityCheckerException e) { assertEquals("Different placeholder name in source and target", e.getMessage()); } }
@Test public void testNamedParametersChangedButWithDuplicates() throws MessageFormatIntegrityCheckerException { MessageFormatIntegrityChecker checker = new MessageFormatIntegrityChecker(); String source = "{1} {1} {2}"; String target = "{1} {2} {2}"; try { checker.check(source, target); } catch (MessageFormatIntegrityCheckerException e) { } }