@Test public void testOriginalTypeChecking() throws Exception { Map<Project, Map<String, PropertyMapper>> propertyMap = new HashMap<>(); CommonState state = new CommonState( new Properties() ); Project project = getProject(); try { assertFalse( PropertiesUtils.cacheProperty( project, state, propertyMap, "${foobar}", null, null, false ) ); fail("Should have thrown an exception"); } catch (ManipulationException e) { assertTrue( e.getMessage().contains( "Unknown type for null" ) ); } }
public static boolean cacheProperty( Project project, CommonState state, Map<Project, Map<String, PropertyMapper>> versionPropertyUpdateMap, String oldVersion, String newVersion, Object originalType, boolean force ) throws ManipulationException { final Map<String, PropertyMapper> projectProps = versionPropertyUpdateMap.computeIfAbsent( project, k -> new HashMap<>() ); boolean result = false; if ( oldVersion != null && oldVersion.contains( "${" ) ) { final int endIndex = oldVersion.indexOf( '}' ); final String oldProperty = oldVersion.substring( 2, endIndex ); if ( oldVersion.contains( "${" ) && !( oldVersion.startsWith( "${" ) && oldVersion.endsWith( "}" ) ) || ( StringUtils.countMatches( oldVersion, "${" ) > 1 ) ) { logger.debug( "For {} ; original version contains hardcoded value or multiple embedded properties. Not caching value ( {} -> {} )", originalType, oldVersion, newVersion ); } else if ( "project.version".equals( oldProperty ) ) { logger.debug( "For {} ; original version was a property mapping. Not caching value as property is built-in ( {} -> {} )", originalType, oldProperty, newVersion ); } else { logger.debug( "For {} ; original version was a property mapping; caching new value for update {} -> {} for project {} ", originalType, oldProperty, newVersion, project ); final String oldVersionProp = oldVersion.substring( 2, oldVersion.length() - 1 ); final PropertyMapper container = projectProps.computeIfAbsent( oldVersionProp, k -> new PropertyMapper() ); final String existingPropertyMapping = container.getNewVersion(); final ProjectRef originalReference; if ( originalType instanceof ProjectVersionRef ) { originalReference = ( ( ProjectVersionRef ) originalType ).asProjectRef(); } else if ( originalType instanceof Plugin ) { originalReference = new SimpleProjectRef( ( (Plugin) originalType ).getGroupId(), ( (Plugin) originalType ).getArtifactId() ); } else { throw new ManipulationException( "Unknown type for {}", ( originalType == null ? null : originalType.getClass() ) ); } if ( existingPropertyMapping != null && !existingPropertyMapping.equals( newVersion ) ) { if ( force ) { logger.debug( "Override property replacement of {} with force version override {}", existingPropertyMapping, newVersion ); } else { if ( state.isPropertyClashFails() ) { logger.error( "Replacing property '{}' with a new version but the existing version does not match. " + "Old value is {} and new is {}. Context was replacing {} and clashed with {}", oldVersionProp, existingPropertyMapping, newVersion, originalType, container.getDependencies() ); throw new ManipulationException( "Property replacement clash - updating property '{}' to both {} and {} ", oldVersionProp, existingPropertyMapping, newVersion ); } else { logger.warn( "Replacing property '{}' with a new version would clash with existing version which does not match. " + "Old value is {} and new is {}. Purging update of existing property. Context was replacing {} and clashed with {}", oldVersionProp, existingPropertyMapping, newVersion, originalType, container.getDependencies() ); projectProps.remove( oldVersionProp ); return false; } } } container.getDependencies().add( originalReference ); container.setOriginalVersion( findProperty( project, oldVersionProp ) ); container.setNewVersion( newVersion ); logger.debug( "Container is {} ", container ); result = true; } } return result; }
PropertiesUtils { public static boolean cacheProperty( Project project, CommonState state, Map<Project, Map<String, PropertyMapper>> versionPropertyUpdateMap, String oldVersion, String newVersion, Object originalType, boolean force ) throws ManipulationException { final Map<String, PropertyMapper> projectProps = versionPropertyUpdateMap.computeIfAbsent( project, k -> new HashMap<>() ); boolean result = false; if ( oldVersion != null && oldVersion.contains( "${" ) ) { final int endIndex = oldVersion.indexOf( '}' ); final String oldProperty = oldVersion.substring( 2, endIndex ); if ( oldVersion.contains( "${" ) && !( oldVersion.startsWith( "${" ) && oldVersion.endsWith( "}" ) ) || ( StringUtils.countMatches( oldVersion, "${" ) > 1 ) ) { logger.debug( "For {} ; original version contains hardcoded value or multiple embedded properties. Not caching value ( {} -> {} )", originalType, oldVersion, newVersion ); } else if ( "project.version".equals( oldProperty ) ) { logger.debug( "For {} ; original version was a property mapping. Not caching value as property is built-in ( {} -> {} )", originalType, oldProperty, newVersion ); } else { logger.debug( "For {} ; original version was a property mapping; caching new value for update {} -> {} for project {} ", originalType, oldProperty, newVersion, project ); final String oldVersionProp = oldVersion.substring( 2, oldVersion.length() - 1 ); final PropertyMapper container = projectProps.computeIfAbsent( oldVersionProp, k -> new PropertyMapper() ); final String existingPropertyMapping = container.getNewVersion(); final ProjectRef originalReference; if ( originalType instanceof ProjectVersionRef ) { originalReference = ( ( ProjectVersionRef ) originalType ).asProjectRef(); } else if ( originalType instanceof Plugin ) { originalReference = new SimpleProjectRef( ( (Plugin) originalType ).getGroupId(), ( (Plugin) originalType ).getArtifactId() ); } else { throw new ManipulationException( "Unknown type for {}", ( originalType == null ? null : originalType.getClass() ) ); } if ( existingPropertyMapping != null && !existingPropertyMapping.equals( newVersion ) ) { if ( force ) { logger.debug( "Override property replacement of {} with force version override {}", existingPropertyMapping, newVersion ); } else { if ( state.isPropertyClashFails() ) { logger.error( "Replacing property '{}' with a new version but the existing version does not match. " + "Old value is {} and new is {}. Context was replacing {} and clashed with {}", oldVersionProp, existingPropertyMapping, newVersion, originalType, container.getDependencies() ); throw new ManipulationException( "Property replacement clash - updating property '{}' to both {} and {} ", oldVersionProp, existingPropertyMapping, newVersion ); } else { logger.warn( "Replacing property '{}' with a new version would clash with existing version which does not match. " + "Old value is {} and new is {}. Purging update of existing property. Context was replacing {} and clashed with {}", oldVersionProp, existingPropertyMapping, newVersion, originalType, container.getDependencies() ); projectProps.remove( oldVersionProp ); return false; } } } container.getDependencies().add( originalReference ); container.setOriginalVersion( findProperty( project, oldVersionProp ) ); container.setNewVersion( newVersion ); logger.debug( "Container is {} ", container ); result = true; } } return result; } }
PropertiesUtils { public static boolean cacheProperty( Project project, CommonState state, Map<Project, Map<String, PropertyMapper>> versionPropertyUpdateMap, String oldVersion, String newVersion, Object originalType, boolean force ) throws ManipulationException { final Map<String, PropertyMapper> projectProps = versionPropertyUpdateMap.computeIfAbsent( project, k -> new HashMap<>() ); boolean result = false; if ( oldVersion != null && oldVersion.contains( "${" ) ) { final int endIndex = oldVersion.indexOf( '}' ); final String oldProperty = oldVersion.substring( 2, endIndex ); if ( oldVersion.contains( "${" ) && !( oldVersion.startsWith( "${" ) && oldVersion.endsWith( "}" ) ) || ( StringUtils.countMatches( oldVersion, "${" ) > 1 ) ) { logger.debug( "For {} ; original version contains hardcoded value or multiple embedded properties. Not caching value ( {} -> {} )", originalType, oldVersion, newVersion ); } else if ( "project.version".equals( oldProperty ) ) { logger.debug( "For {} ; original version was a property mapping. Not caching value as property is built-in ( {} -> {} )", originalType, oldProperty, newVersion ); } else { logger.debug( "For {} ; original version was a property mapping; caching new value for update {} -> {} for project {} ", originalType, oldProperty, newVersion, project ); final String oldVersionProp = oldVersion.substring( 2, oldVersion.length() - 1 ); final PropertyMapper container = projectProps.computeIfAbsent( oldVersionProp, k -> new PropertyMapper() ); final String existingPropertyMapping = container.getNewVersion(); final ProjectRef originalReference; if ( originalType instanceof ProjectVersionRef ) { originalReference = ( ( ProjectVersionRef ) originalType ).asProjectRef(); } else if ( originalType instanceof Plugin ) { originalReference = new SimpleProjectRef( ( (Plugin) originalType ).getGroupId(), ( (Plugin) originalType ).getArtifactId() ); } else { throw new ManipulationException( "Unknown type for {}", ( originalType == null ? null : originalType.getClass() ) ); } if ( existingPropertyMapping != null && !existingPropertyMapping.equals( newVersion ) ) { if ( force ) { logger.debug( "Override property replacement of {} with force version override {}", existingPropertyMapping, newVersion ); } else { if ( state.isPropertyClashFails() ) { logger.error( "Replacing property '{}' with a new version but the existing version does not match. " + "Old value is {} and new is {}. Context was replacing {} and clashed with {}", oldVersionProp, existingPropertyMapping, newVersion, originalType, container.getDependencies() ); throw new ManipulationException( "Property replacement clash - updating property '{}' to both {} and {} ", oldVersionProp, existingPropertyMapping, newVersion ); } else { logger.warn( "Replacing property '{}' with a new version would clash with existing version which does not match. " + "Old value is {} and new is {}. Purging update of existing property. Context was replacing {} and clashed with {}", oldVersionProp, existingPropertyMapping, newVersion, originalType, container.getDependencies() ); projectProps.remove( oldVersionProp ); return false; } } } container.getDependencies().add( originalReference ); container.setOriginalVersion( findProperty( project, oldVersionProp ) ); container.setNewVersion( newVersion ); logger.debug( "Container is {} ", container ); result = true; } } return result; } private  PropertiesUtils(); }
PropertiesUtils { public static boolean cacheProperty( Project project, CommonState state, Map<Project, Map<String, PropertyMapper>> versionPropertyUpdateMap, String oldVersion, String newVersion, Object originalType, boolean force ) throws ManipulationException { final Map<String, PropertyMapper> projectProps = versionPropertyUpdateMap.computeIfAbsent( project, k -> new HashMap<>() ); boolean result = false; if ( oldVersion != null && oldVersion.contains( "${" ) ) { final int endIndex = oldVersion.indexOf( '}' ); final String oldProperty = oldVersion.substring( 2, endIndex ); if ( oldVersion.contains( "${" ) && !( oldVersion.startsWith( "${" ) && oldVersion.endsWith( "}" ) ) || ( StringUtils.countMatches( oldVersion, "${" ) > 1 ) ) { logger.debug( "For {} ; original version contains hardcoded value or multiple embedded properties. Not caching value ( {} -> {} )", originalType, oldVersion, newVersion ); } else if ( "project.version".equals( oldProperty ) ) { logger.debug( "For {} ; original version was a property mapping. Not caching value as property is built-in ( {} -> {} )", originalType, oldProperty, newVersion ); } else { logger.debug( "For {} ; original version was a property mapping; caching new value for update {} -> {} for project {} ", originalType, oldProperty, newVersion, project ); final String oldVersionProp = oldVersion.substring( 2, oldVersion.length() - 1 ); final PropertyMapper container = projectProps.computeIfAbsent( oldVersionProp, k -> new PropertyMapper() ); final String existingPropertyMapping = container.getNewVersion(); final ProjectRef originalReference; if ( originalType instanceof ProjectVersionRef ) { originalReference = ( ( ProjectVersionRef ) originalType ).asProjectRef(); } else if ( originalType instanceof Plugin ) { originalReference = new SimpleProjectRef( ( (Plugin) originalType ).getGroupId(), ( (Plugin) originalType ).getArtifactId() ); } else { throw new ManipulationException( "Unknown type for {}", ( originalType == null ? null : originalType.getClass() ) ); } if ( existingPropertyMapping != null && !existingPropertyMapping.equals( newVersion ) ) { if ( force ) { logger.debug( "Override property replacement of {} with force version override {}", existingPropertyMapping, newVersion ); } else { if ( state.isPropertyClashFails() ) { logger.error( "Replacing property '{}' with a new version but the existing version does not match. " + "Old value is {} and new is {}. Context was replacing {} and clashed with {}", oldVersionProp, existingPropertyMapping, newVersion, originalType, container.getDependencies() ); throw new ManipulationException( "Property replacement clash - updating property '{}' to both {} and {} ", oldVersionProp, existingPropertyMapping, newVersion ); } else { logger.warn( "Replacing property '{}' with a new version would clash with existing version which does not match. " + "Old value is {} and new is {}. Purging update of existing property. Context was replacing {} and clashed with {}", oldVersionProp, existingPropertyMapping, newVersion, originalType, container.getDependencies() ); projectProps.remove( oldVersionProp ); return false; } } } container.getDependencies().add( originalReference ); container.setOriginalVersion( findProperty( project, oldVersionProp ) ); container.setNewVersion( newVersion ); logger.debug( "Container is {} ", container ); result = true; } } return result; } private  PropertiesUtils(); static Map<String, String> getPropertiesByPrefix( final Properties properties, final String prefix ); static PropertyUpdate updateProperties( ManipulationSession session, Project project, boolean ignoreStrict,
                                                   String key, String newValue ); static String getSuffix( ManipulationSession session ); static boolean checkStrictValue( ManipulationSession session, String oldValue, String newValue ); static boolean cacheProperty( Project project, CommonState state, Map<Project, Map<String, PropertyMapper>> versionPropertyUpdateMap,
                                         String oldVersion, String newVersion, Object originalType, boolean force ); static String extractPropertyName( String version ); static void verifyPropertyMapping( CommonState cState, Project project, Map<Project, Map<String, PropertyMapper>> versionPropertyMap,
                                              ProjectVersionRef pvr, String version ); static void handleConfigPrecedence( Properties userProperties, Properties config ); }
PropertiesUtils { public static boolean cacheProperty( Project project, CommonState state, Map<Project, Map<String, PropertyMapper>> versionPropertyUpdateMap, String oldVersion, String newVersion, Object originalType, boolean force ) throws ManipulationException { final Map<String, PropertyMapper> projectProps = versionPropertyUpdateMap.computeIfAbsent( project, k -> new HashMap<>() ); boolean result = false; if ( oldVersion != null && oldVersion.contains( "${" ) ) { final int endIndex = oldVersion.indexOf( '}' ); final String oldProperty = oldVersion.substring( 2, endIndex ); if ( oldVersion.contains( "${" ) && !( oldVersion.startsWith( "${" ) && oldVersion.endsWith( "}" ) ) || ( StringUtils.countMatches( oldVersion, "${" ) > 1 ) ) { logger.debug( "For {} ; original version contains hardcoded value or multiple embedded properties. Not caching value ( {} -> {} )", originalType, oldVersion, newVersion ); } else if ( "project.version".equals( oldProperty ) ) { logger.debug( "For {} ; original version was a property mapping. Not caching value as property is built-in ( {} -> {} )", originalType, oldProperty, newVersion ); } else { logger.debug( "For {} ; original version was a property mapping; caching new value for update {} -> {} for project {} ", originalType, oldProperty, newVersion, project ); final String oldVersionProp = oldVersion.substring( 2, oldVersion.length() - 1 ); final PropertyMapper container = projectProps.computeIfAbsent( oldVersionProp, k -> new PropertyMapper() ); final String existingPropertyMapping = container.getNewVersion(); final ProjectRef originalReference; if ( originalType instanceof ProjectVersionRef ) { originalReference = ( ( ProjectVersionRef ) originalType ).asProjectRef(); } else if ( originalType instanceof Plugin ) { originalReference = new SimpleProjectRef( ( (Plugin) originalType ).getGroupId(), ( (Plugin) originalType ).getArtifactId() ); } else { throw new ManipulationException( "Unknown type for {}", ( originalType == null ? null : originalType.getClass() ) ); } if ( existingPropertyMapping != null && !existingPropertyMapping.equals( newVersion ) ) { if ( force ) { logger.debug( "Override property replacement of {} with force version override {}", existingPropertyMapping, newVersion ); } else { if ( state.isPropertyClashFails() ) { logger.error( "Replacing property '{}' with a new version but the existing version does not match. " + "Old value is {} and new is {}. Context was replacing {} and clashed with {}", oldVersionProp, existingPropertyMapping, newVersion, originalType, container.getDependencies() ); throw new ManipulationException( "Property replacement clash - updating property '{}' to both {} and {} ", oldVersionProp, existingPropertyMapping, newVersion ); } else { logger.warn( "Replacing property '{}' with a new version would clash with existing version which does not match. " + "Old value is {} and new is {}. Purging update of existing property. Context was replacing {} and clashed with {}", oldVersionProp, existingPropertyMapping, newVersion, originalType, container.getDependencies() ); projectProps.remove( oldVersionProp ); return false; } } } container.getDependencies().add( originalReference ); container.setOriginalVersion( findProperty( project, oldVersionProp ) ); container.setNewVersion( newVersion ); logger.debug( "Container is {} ", container ); result = true; } } return result; } private  PropertiesUtils(); static Map<String, String> getPropertiesByPrefix( final Properties properties, final String prefix ); static PropertyUpdate updateProperties( ManipulationSession session, Project project, boolean ignoreStrict,
                                                   String key, String newValue ); static String getSuffix( ManipulationSession session ); static boolean checkStrictValue( ManipulationSession session, String oldValue, String newValue ); static boolean cacheProperty( Project project, CommonState state, Map<Project, Map<String, PropertyMapper>> versionPropertyUpdateMap,
                                         String oldVersion, String newVersion, Object originalType, boolean force ); static String extractPropertyName( String version ); static void verifyPropertyMapping( CommonState cState, Project project, Map<Project, Map<String, PropertyMapper>> versionPropertyMap,
                                              ProjectVersionRef pvr, String version ); static void handleConfigPrecedence( Properties userProperties, Properties config );  }