Skip to content

Commit

Permalink
Refactor AbstractRequireRoles.getRolesFromString
Browse files Browse the repository at this point in the history
- Add UT for the new method
- Extract method
- Rename parameters and local variable
  • Loading branch information
pzygielo authored and slachiewicz committed Jul 31, 2021
1 parent e5db0cf commit c97a9d2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
Expand Up @@ -141,16 +141,21 @@ final Set<String> getRolesFromProject( MavenProject mavenProject )
/**
* Returns the set of required roles from the property.
*
* @param toSet
* @param csRoles comma-separated roles to be split
* @return
*/
Set<String> getRolesFromString( final String toSet )
Set<String> getRolesFromString( final String csRoles )
{
final String[] asList = StringUtils.split( toSet, "," );
return splitCsvToSet( csRoles );
}

static Set<String> splitCsvToSet( final String csv )
{
final String [] splitValues = StringUtils.split( csv, "," );
final Set<String> result = new HashSet<>();
for ( String role : asList )
for ( String value : splitValues )
{
result.add( role.trim() );
result.add( value.trim() );
}
return result;
}
Expand Down
@@ -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<String> values = AbstractRequireRoles.splitCsvToSet( CSV_TO_SPLIT );

assert values.size() == 3;
}

@Test
public void testCsvSplitExpectedElements()
{
Set<String> values = AbstractRequireRoles.splitCsvToSet( CSV_TO_SPLIT );

assert values.contains( "a" );
assert values.contains( "b" );
assert values.contains( "c" );
}

@Test
public void testCsvSplitTrimsValues()
{
Set<String> values = AbstractRequireRoles.splitCsvToSet( CSV_WITH_SPACES_TO_SPLIT );

assert values.contains( "a" );
assert values.contains( "b" );
assert values.contains( "c" );
}
}

0 comments on commit c97a9d2

Please sign in to comment.