RequireOsgiCompatibleVersionRule implements EnforcerRule { public void ensureValidOsgiVersion(String version) throws EnforcerRuleException { if (version.endsWith("SNAPSHOT")) { return; } String parts[] = version.split("\\."); if (parts.length > 4) { throw new EnforcerRuleException("The version \"" + version + "\" does not conform to the OSGi version requirements. " + "It can't have more than 4 parts (#.#.#.*)" + " - The '.' character has a special meaning"); } if (parts.length < 3) { throw new EnforcerRuleException("The version \"" + version + "\" does not conform to the OSGi version requirements. " + "It must have 3 (or 4) parts (#.#.#.*)"); } for (int i = 0; i < 3; i++) { if (!StringUtils.isNumeric(parts[i])) { throw new EnforcerRuleException("The version \"" + version + "\" does not conform to the OSGi version requirements. " + "Part #" + (i + 1) + " \"" + parts[i] + "\" of an OSGi version must be a non-negative number (#.#.#.*)."); } if (Integer.parseInt(parts[i]) < 0) { throw new EnforcerRuleException("The version \"" + version + "\" does not conform to the OSGi version requirements. " + "Part #" + (i + 1) + " \"" + parts[i] + "\" of an OSGi version must be a non-negative number (#.#.#.*)."); } } if (parts.length == 4) { for (char c : parts[3].toCharArray()) { if ((c >= '0') && (c <= '9')) { continue; } if ((c >= 'A') && (c <= 'Z')) { continue; } if ((c >= 'a') && (c <= 'z')) { continue; } if ((c == '-') || (c == '_')) { continue; } throw new EnforcerRuleException("The OSGi qualifier of \"" + version + "\" does not conform to the OSGi version requirements. " + "Only Letters (upper and lower), numbers, dash '-', and underscore '_' are allowed."); } } }  void execute(EnforcerRuleHelper helper); void ensureValidOsgiVersion(String version); String getCacheId(); boolean isCacheable(); boolean isResultValid(EnforcerRule rule);  }
@Test public void testValidOsgiVersion() throws EnforcerRuleException { RequireOsgiCompatibleVersionRule rule = new RequireOsgiCompatibleVersionRule(); rule.ensureValidOsgiVersion( "1.0.0" ); rule.ensureValidOsgiVersion( "1.2.3.4" ); rule.ensureValidOsgiVersion( "12.0.0.v20100511-114423" ); rule.ensureValidOsgiVersion( "4.0.2.SNAPSHOT" ); rule.ensureValidOsgiVersion( "3.4.2.r342_v20081028-0800" ); rule.ensureValidOsgiVersion( "3.4.3.R34x_v20081215-1030" ); rule.ensureValidOsgiVersion( "3.0.5.v20090218-1800-e3x" ); rule.ensureValidOsgiVersion( "1.1.1.M20080827-0800b" ); rule.ensureValidOsgiVersion( "0.1.37.v200803061811" ); }
@Test public void testTooLongOsgiVersion() throws EnforcerRuleException { Assertions.assertThrows( EnforcerRuleException.class, () -> { RequireOsgiCompatibleVersionRule rule = new RequireOsgiCompatibleVersionRule(); rule.ensureValidOsgiVersion( "1.2.3.4.5" ); } ); }
@Test public void testTooShortOsgiVersion() throws EnforcerRuleException { Assertions.assertThrows( EnforcerRuleException.class, () -> { RequireOsgiCompatibleVersionRule rule = new RequireOsgiCompatibleVersionRule(); rule.ensureValidOsgiVersion( "1.2" ); } ); }
@Test public void testIgnoredSnapshotOsgiVersion() throws EnforcerRuleException { RequireOsgiCompatibleVersionRule rule = new RequireOsgiCompatibleVersionRule(); rule.ensureValidOsgiVersion( "7.0.2-SNAPSHOT" ); }
@Test public void testLettersInPartOneOsgiVersion() throws EnforcerRuleException { Assertions.assertThrows( EnforcerRuleException.class, () -> { RequireOsgiCompatibleVersionRule rule = new RequireOsgiCompatibleVersionRule(); rule.ensureValidOsgiVersion( "a.0.2" ); } ); }
@Test public void testLettersInPartOneAOsgiVersion() throws EnforcerRuleException { Assertions.assertThrows( EnforcerRuleException.class, () -> { RequireOsgiCompatibleVersionRule rule = new RequireOsgiCompatibleVersionRule(); rule.ensureValidOsgiVersion( "1a.0.2" ); } ); }
@Test public void testLettersInPartTwoOsgiVersion() throws EnforcerRuleException { Assertions.assertThrows( EnforcerRuleException.class, () -> { RequireOsgiCompatibleVersionRule rule = new RequireOsgiCompatibleVersionRule(); rule.ensureValidOsgiVersion( "1.1a.2" ); } ); }
@Test public void testBetaOsgiVersion() throws EnforcerRuleException { Assertions.assertThrows( EnforcerRuleException.class, () -> { RequireOsgiCompatibleVersionRule rule = new RequireOsgiCompatibleVersionRule(); rule.ensureValidOsgiVersion( "1.beta.2" ); } ); }
@Test public void testAlphaOsgiVersion() throws EnforcerRuleException { Assertions.assertThrows( EnforcerRuleException.class, () -> { RequireOsgiCompatibleVersionRule rule = new RequireOsgiCompatibleVersionRule(); rule.ensureValidOsgiVersion( "1.0.alpha-2" ); } ); }
@Test public void testInvalidQualifierOsgiVersion() throws EnforcerRuleException { Assertions.assertThrows( EnforcerRuleException.class, () -> { RequireOsgiCompatibleVersionRule rule = new RequireOsgiCompatibleVersionRule(); rule.ensureValidOsgiVersion( "1.0.2.2009:05:12" ); } ); }
@Test public void testGoodSnapshotOsgiVersion() throws EnforcerRuleException { RequireOsgiCompatibleVersionRule rule = new RequireOsgiCompatibleVersionRule(); rule.ensureValidOsgiVersion( "7.0.2.SNAPSHOT" ); }