Invoke private constructors to increase coverage, issue #840

This commit is contained in:
Michal Kordas 2015-03-22 22:52:07 +01:00 committed by Roman Ivanov
parent 17a8f553a2
commit 5b4a81a4e1
5 changed files with 68 additions and 3 deletions

View File

@ -589,7 +589,7 @@
<regex><pattern>.*.PropertiesExpander</pattern><branchRate>50</branchRate><lineRate>83</lineRate></regex>
<regex><pattern>.*.PropertyCacheFile</pattern><branchRate>22</branchRate><lineRate>18</lineRate></regex>
<regex><pattern>.*.TreeWalker</pattern><branchRate>90</branchRate><lineRate>83</lineRate></regex>
<regex><pattern>com.puppycrawl.tools.checkstyle.Utils</pattern><branchRate>69</branchRate><lineRate>69</lineRate></regex>
<regex><pattern>com.puppycrawl.tools.checkstyle.Utils</pattern><branchRate>78</branchRate><lineRate>71</lineRate></regex>
<regex><pattern>.*.XMLLogger</pattern><branchRate>86</branchRate><lineRate>97</lineRate></regex>
@ -616,7 +616,7 @@
<regex><pattern>.*.api.LocalizedMessage\$.*</pattern><branchRate>41</branchRate><lineRate>66</lineRate></regex>
<regex><pattern>.*.api.ScopeUtils</pattern><branchRate>90</branchRate><lineRate>94</lineRate></regex>
<regex><pattern>.*.api.SeverityLevelCounter</pattern><branchRate>50</branchRate><lineRate>76</lineRate></regex>
<regex><pattern>.*.api.TokenTypes</pattern><branchRate>62</branchRate><lineRate>72</lineRate></regex>
<regex><pattern>.*.api.TokenTypes</pattern><branchRate>62</branchRate><lineRate>80</lineRate></regex>
<regex><pattern>.*.checks.AbstractOptionCheck</pattern><branchRate>100</branchRate><lineRate>80</lineRate></regex>
@ -764,7 +764,7 @@
<regex><pattern>.*.checks.javadoc.JavadocTag</pattern><branchRate>92</branchRate><lineRate>85</lineRate></regex>
<regex><pattern>.*.checks.javadoc.JavadocTagContinuationIndentationCheck</pattern><branchRate>81</branchRate><lineRate>86</lineRate></regex>
<regex><pattern>.*.checks.javadoc.JavadocTypeCheck</pattern><branchRate>95</branchRate><lineRate>91</lineRate></regex>
<regex><pattern>.*.checks.javadoc.JavadocUtils</pattern><branchRate>83</branchRate><lineRate>89</lineRate></regex>
<regex><pattern>.*.checks.javadoc.JavadocUtils</pattern><branchRate>83</branchRate><lineRate>91</lineRate></regex>
<regex><pattern>.*.checks.javadoc.JavadocVariableCheck</pattern><branchRate>93</branchRate><lineRate>90</lineRate></regex>
<regex><pattern>.*.checks.javadoc.SummaryJavadocCheck</pattern><branchRate>93</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.javadoc.TagParser</pattern><branchRate>92</branchRate><lineRate>98</lineRate></regex>

View File

@ -0,0 +1,43 @@
////////////////////////////////////////////////////////////////////////////////
// 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;
import org.junit.Assert;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
public class TestUtils {
private TestUtils() {
}
/**
* Verifies that utils class has private constructor and invokes it to satisfy code coverage.
*/
public static void assertUtilsClassHasPrivateConstructor(final Class<?> utilClass)
throws ReflectiveOperationException {
final Constructor<?> constructor = utilClass.getDeclaredConstructor();
if (!Modifier.isPrivate(constructor.getModifiers())) {
Assert.fail("Constructor is not private");
}
constructor.setAccessible(true);
constructor.newInstance();
}
}

View File

@ -18,6 +18,7 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
import static com.puppycrawl.tools.checkstyle.TestUtils.assertUtilsClassHasPrivateConstructor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@ -77,4 +78,11 @@ public class UtilsTest
file = new File("file.java");
assertTrue(fileExtensionMatches(file, fileExtensions));
}
@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException
{
assertUtilsClassHasPrivateConstructor(Utils.class);
}
}

View File

@ -18,6 +18,7 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.api;
import static com.puppycrawl.tools.checkstyle.TestUtils.assertUtilsClassHasPrivateConstructor;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
@ -31,4 +32,10 @@ public class TokenTypesTest
"The <code>==</code> (equal) operator.", TokenTypes
.getShortDescription("EQUAL"));
}
@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException
{
assertUtilsClassHasPrivateConstructor(TokenTypes.class);
}
}

View File

@ -18,6 +18,7 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.javadoc;
import static com.puppycrawl.tools.checkstyle.TestUtils.assertUtilsClassHasPrivateConstructor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@ -188,4 +189,10 @@ public class JavadocUtilsTest
assertTrue(JavadocUtils.isJavadocComment(commentBegin));
}
@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException
{
assertUtilsClassHasPrivateConstructor(JavadocUtils.class);
}
}