diff --git a/src/main/java/org/apache/maven/plugins/enforcer/AbstractRequireRoles.java b/src/main/java/org/apache/maven/plugins/enforcer/AbstractRequireRoles.java index 2443cd73..ea272930 100644 --- a/src/main/java/org/apache/maven/plugins/enforcer/AbstractRequireRoles.java +++ b/src/main/java/org/apache/maven/plugins/enforcer/AbstractRequireRoles.java @@ -141,16 +141,21 @@ final Set getRolesFromProject( MavenProject mavenProject ) /** * Returns the set of required roles from the property. * - * @param toSet + * @param csRoles comma-separated roles to be split * @return */ - Set getRolesFromString( final String toSet ) + Set getRolesFromString( final String csRoles ) { - final String[] asList = StringUtils.split( toSet, "," ); + return splitCsvToSet( csRoles ); + } + + static Set splitCsvToSet( final String csv ) + { + final String [] splitValues = StringUtils.split( csv, "," ); final Set result = new HashSet<>(); - for ( String role : asList ) + for ( String value : splitValues ) { - result.add( role.trim() ); + result.add( value.trim() ); } return result; } diff --git a/src/test/java/org/apache/maven/plugins/enforcer/AbstractRequireRolesTest.java b/src/test/java/org/apache/maven/plugins/enforcer/AbstractRequireRolesTest.java new file mode 100644 index 00000000..c447b296 --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/enforcer/AbstractRequireRolesTest.java @@ -0,0 +1,57 @@ +package org.apache.maven.plugins.enforcer; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import java.util.Set; + +import org.junit.Test; + +public class AbstractRequireRolesTest +{ + private final String CSV_TO_SPLIT = "a,b,c"; + private final String CSV_WITH_SPACES_TO_SPLIT = " a, b ,c "; + + @Test + public void testCsvSplitSize() + { + Set values = AbstractRequireRoles.splitCsvToSet( CSV_TO_SPLIT ); + + assert values.size() == 3; + } + + @Test + public void testCsvSplitExpectedElements() + { + Set values = AbstractRequireRoles.splitCsvToSet( CSV_TO_SPLIT ); + + assert values.contains( "a" ); + assert values.contains( "b" ); + assert values.contains( "c" ); + } + + @Test + public void testCsvSplitTrimsValues() + { + Set values = AbstractRequireRoles.splitCsvToSet( CSV_WITH_SPACES_TO_SPLIT ); + + assert values.contains( "a" ); + assert values.contains( "b" ); + assert values.contains( "c" ); + } +}