added validation for header in setHeader in RegexpHeaderCheck.java to provide better feedback when an invalid Pattern is specified (was InvocationTargetException only before). Changes are made base on @krichter722 commit. Issue #897
This commit is contained in:
parent
ab2f93f9bf
commit
589c5a261a
|
|
@ -26,9 +26,11 @@ import java.util.List;
|
|||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.Utils;
|
||||
import org.apache.commons.beanutils.ConversionException;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* Checks the header of the source against a header file that contains a
|
||||
|
|
@ -145,4 +147,22 @@ public class RegexpHeaderCheck extends AbstractHeaderCheck
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the {@code header} by compiling it with
|
||||
* {@link Pattern#compile(java.lang.String) } and throws
|
||||
* {@link PatternSyntaxException} if {@code header} isn't a valid pattern.
|
||||
* @param header the header value to validate and set (in that order)
|
||||
*/
|
||||
@Override
|
||||
public void setHeader(String header)
|
||||
{
|
||||
if (StringUtils.isBlank(header)) {
|
||||
return;
|
||||
}
|
||||
if (!Utils.isPatternValid(header)) {
|
||||
throw new ConversionException("Unable to parse format: " + header);
|
||||
}
|
||||
super.setHeader(header);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,94 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// checkstyle: Checks Java source code for adherence to a set of rules.
|
||||
// Copyright (C) 2001-2015 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
package com.puppycrawl.tools.checkstyle.checks.header;
|
||||
|
||||
import org.apache.commons.beanutils.ConversionException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author richter
|
||||
*/
|
||||
public class RegexpHeaderCheckTest
|
||||
{
|
||||
|
||||
public RegexpHeaderCheckTest()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of setHeader method, of class RegexpHeaderCheck.
|
||||
*/
|
||||
@Test
|
||||
public void testSetHeaderNull()
|
||||
{
|
||||
// check null passes
|
||||
RegexpHeaderCheck instance = new RegexpHeaderCheck();
|
||||
// recreate for each test because multiple invocations fail
|
||||
String header = null;
|
||||
instance.setHeader(header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of setHeader method, of class RegexpHeaderCheck.
|
||||
*/
|
||||
@Test
|
||||
public void testSetHeaderEmpty()
|
||||
{
|
||||
// check null passes
|
||||
RegexpHeaderCheck instance = new RegexpHeaderCheck();
|
||||
// check empty string passes
|
||||
instance = new RegexpHeaderCheck();
|
||||
String header = "";
|
||||
instance.setHeader(header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of setHeader method, of class RegexpHeaderCheck.
|
||||
*/
|
||||
@Test
|
||||
public void testSetHeaderSimple()
|
||||
{
|
||||
RegexpHeaderCheck instance = new RegexpHeaderCheck();
|
||||
// check valid header passes
|
||||
instance = new RegexpHeaderCheck();
|
||||
String header = "abc.*";
|
||||
instance.setHeader(header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of setHeader method, of class RegexpHeaderCheck.
|
||||
*/
|
||||
@Test
|
||||
public void testSetHeader()
|
||||
{
|
||||
// check invalid header passes
|
||||
RegexpHeaderCheck instance = new RegexpHeaderCheck();
|
||||
String header = "^/**\\n * Licensed to the Apache Software Foundation (ASF)";
|
||||
try {
|
||||
instance.setHeader(header);
|
||||
Assert.fail(String.format("%s should have been thrown", ConversionException.class));
|
||||
}
|
||||
catch (ConversionException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue