Checkstyle fixes

This commit is contained in:
Andreas Dangel
2014-11-17 21:45:37 +01:00
parent fbe6742666
commit e86341bf8e
11 changed files with 203 additions and 122 deletions

View File

@ -4,36 +4,36 @@
package net.sourceforge.pmd.lang;
/**
* Represents a set of configuration options for a {@link Parser}. For each
* Represents a set of configuration options for a {@link Parser}. For each
* unique combination of ParserOptions a Parser will be used to create an AST.
* Therefore, implementations must implement {@link Object#equals(Object)}
* and {@link Object#hashCode()}.
* Therefore, implementations must implement {@link Object#equals(Object)} and
* {@link Object#hashCode()}.
*/
public class ParserOptions {
protected String suppressMarker;
public String getSuppressMarker() {
return suppressMarker;
return suppressMarker;
}
public void setSuppressMarker(String suppressMarker) {
this.suppressMarker = suppressMarker;
this.suppressMarker = suppressMarker;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final ParserOptions that = (ParserOptions) obj;
return this.suppressMarker.equals(that.suppressMarker);
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final ParserOptions that = (ParserOptions) obj;
return this.suppressMarker.equals(that.suppressMarker);
}
@Override
public int hashCode() {
return suppressMarker != null ? suppressMarker.hashCode() : 0;
return suppressMarker != null ? suppressMarker.hashCode() : 0;
}
}

View File

@ -3,8 +3,15 @@
*/
package net.sourceforge.pmd.cpd;
/**
* Language implementation for PHP
*/
public class PHPLanguage extends AbstractLanguage {
public PHPLanguage() {
super("PHP", "php", new PHPTokenizer(), ".php", ".class");
}
/**
* Creates a new PHP Language instance.
*/
public PHPLanguage() {
super("PHP", "php", new PHPTokenizer(), ".php", ".class");
}
}

View File

@ -5,19 +5,23 @@ package net.sourceforge.pmd.cpd;
import java.util.List;
/**
* Simple tokenizer for PHP.
*/
public class PHPTokenizer implements Tokenizer {
@Override
public void tokenize(SourceCode tokens, Tokens tokenEntries) {
List<String> code = tokens.getCode();
for (int i = 0; i < code.size(); i++) {
String currentLine = code.get(i);
for (int j = 0; j < currentLine.length(); j++) {
char tok = currentLine.charAt(j);
if (!Character.isWhitespace(tok) && tok != '{' && tok != '}' && tok != ';') {
tokenEntries.add(new TokenEntry(String.valueOf(tok), tokens.getFileName(), i + 1));
}
}
}
tokenEntries.add(TokenEntry.getEOF());
List<String> code = tokens.getCode();
for (int i = 0; i < code.size(); i++) {
String currentLine = code.get(i);
for (int j = 0; j < currentLine.length(); j++) {
char tok = currentLine.charAt(j);
if (!Character.isWhitespace(tok) && tok != '{' && tok != '}' && tok != ';') {
tokenEntries.add(new TokenEntry(String.valueOf(tok), tokens.getFileName(), i + 1));
}
}
}
tokenEntries.add(TokenEntry.getEOF());
}
}

View File

@ -1,15 +1,23 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.php;
import net.sourceforge.pmd.lang.BaseLanguageModule;
/**
* Created by christoferdutz on 20.09.14.
* Language Module for PHP.
*/
public class PhpLanguageModule extends BaseLanguageModule {
/** The name. */
public static final String NAME = "PHP: Hypertext Preprocessor";
/** The terse name. */
public static final String TERSE_NAME = "php";
/**
* Create a new instance of the PHP Language Module.
*/
public PhpLanguageModule() {
super(NAME, "PHP", TERSE_NAME, null, "php", "class");
addVersion("", null, true);

View File

@ -1,3 +1,6 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd;
import java.util.Arrays;
@ -16,7 +19,7 @@ public class LanguageVersionTest extends AbstractLanguageVersionTest {
}
@Parameters
public static Collection data() {
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ PhpLanguageModule.NAME, PhpLanguageModule.TERSE_NAME, "", LanguageRegistry.getLanguage(PhpLanguageModule.NAME).getDefaultVersion() }
});

View File

@ -4,11 +4,16 @@
package net.sourceforge.pmd.cpd;
/**
*
* Language implemention for Ruby.
*
* @author Zev Blut zb@ubit.com
*/
public class RubyLanguage extends AbstractLanguage {
public RubyLanguage() {
super("Ruby", "ruby", new RubyTokenizer(), ".rb", ".cgi", ".class");
}
/**
* Creates a new Ruby Language instance.
*/
public RubyLanguage() {
super("Ruby", "ruby", new RubyTokenizer(), ".rb", ".cgi", ".class");
}
}

View File

@ -6,30 +6,33 @@ package net.sourceforge.pmd.cpd;
import java.util.ArrayList;
/**
*
* Tokenizer for Ruby.
*
* @author Zev Blut zb@ubit.com
*/
public class RubyTokenizer extends AbstractTokenizer
{
public RubyTokenizer()
{
// setting markers for "string" in ruby
this.stringToken = new ArrayList<String>();
this.stringToken.add("\'");
this.stringToken.add("\"");
// setting markers for 'ignorable character' in Ruby
this.ignorableCharacter = new ArrayList<String>();
this.ignorableCharacter.add("{");
this.ignorableCharacter.add("}");
this.ignorableCharacter.add("(");
this.ignorableCharacter.add(")");
this.ignorableCharacter.add(";");
this.ignorableCharacter.add(",");
public class RubyTokenizer extends AbstractTokenizer {
// setting markers for 'ignorable string' in Ruby
this.ignorableStmt = new ArrayList<String>();
this.ignorableStmt.add("while");
this.ignorableStmt.add("do");
this.ignorableStmt.add("end");
/**
* Creates a new Ruby tokenizer.
*/
public RubyTokenizer() {
// setting markers for "string" in ruby
this.stringToken = new ArrayList<String>();
this.stringToken.add("\'");
this.stringToken.add("\"");
// setting markers for 'ignorable character' in Ruby
this.ignorableCharacter = new ArrayList<String>();
this.ignorableCharacter.add("{");
this.ignorableCharacter.add("}");
this.ignorableCharacter.add("(");
this.ignorableCharacter.add(")");
this.ignorableCharacter.add(";");
this.ignorableCharacter.add(",");
// setting markers for 'ignorable string' in Ruby
this.ignorableStmt = new ArrayList<String>();
this.ignorableStmt.add("while");
this.ignorableStmt.add("do");
this.ignorableStmt.add("end");
}
}

View File

@ -1,18 +1,25 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ruby;
import net.sourceforge.pmd.lang.BaseLanguageModule;
/**
* Created by christoferdutz on 20.09.14.
* Language module for Ruby.
*/
public class RubyLanguageModule extends BaseLanguageModule {
/** The name. */
public static final String NAME = "Ruby";
/** The terse name. */
public static final String TERSE_NAME = "ruby";
/**
* Creates a new Ruby Language Module instance.
*/
public RubyLanguageModule() {
super(NAME, null, TERSE_NAME, null, "rb", "cgi", "class");
addVersion("", null, true);
}
}

View File

@ -1,6 +1,8 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd;
import java.util.Arrays;
import java.util.Collection;
@ -17,9 +19,8 @@ public class LanguageVersionTest extends AbstractLanguageVersionTest {
}
@Parameters
public static Collection data() {
return Arrays.asList(new Object[][] {
{ RubyLanguageModule.NAME, RubyLanguageModule.TERSE_NAME, "", LanguageRegistry.getLanguage(RubyLanguageModule.NAME).getDefaultVersion() }
});
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { { RubyLanguageModule.NAME, RubyLanguageModule.TERSE_NAME, "",
LanguageRegistry.getLanguage(RubyLanguageModule.NAME).getDefaultVersion() } });
}
}

View File

@ -1,7 +1,9 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd;
import static org.junit.Assert.assertEquals;
import junit.framework.JUnit4TestAdapter;
import net.sourceforge.pmd.ant.SourceLanguage;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageRegistry;
@ -11,6 +13,25 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
/**
* Base test class for {@link LanguageVersion} implementations.
* <br>Each language implementation should subclass this and provide a data method.
* <pre>
* @Parameters
* public static Collection<Object[]> data() {
* return Arrays.asList(new Object[][] {
* { MyLanguageModule.NAME, MyLanguageModule.TERSE_NAME, "1.1",
* LanguageRegistry.getLanguage(MyLanguageModule.NAME).getVersion("1.1") },
* { MyLanguageModule.NAME, MyLanguageModule.TERSE_NAME, "1.2",
* LanguageRegistry.getLanguage(MyLanguageModule.NAME).getVersion("1.2") },
*
* // doesn't exist
* { MyLanguageModule.NAME, MyLanguageModule.TERSE_NAME, "1.3",
* null }
* });
* </pre>
* For the parameters, see the constructor {@link #AbstractLanguageVersionTest(String, String, String, LanguageVersion)}.
*/
@RunWith(Parameterized.class)
public class AbstractLanguageVersionTest {
@ -19,6 +40,13 @@ public class AbstractLanguageVersionTest {
private String terseName;
private LanguageVersion expected;
/**
* Creates a new {@link AbstractLanguageVersionTest}
* @param name the name under which the language module is registered
* @param terseName the terse name under which the language module is registered
* @param version the specific version of the language version
* @param expected the expected {@link LanguageVersion} instance
*/
public AbstractLanguageVersionTest(String name, String terseName, String version, LanguageVersion expected) {
this.name = name;
this.version = version;
@ -29,11 +57,18 @@ public class AbstractLanguageVersionTest {
this.expected = expected;
}
/**
* Checks that the expected {@link LanguageVersion} can be found by the combination of
* {@link #terseName} and {@link #version}.
*/
@Test
public void testGetLanguageVersionForTerseName() {
assertEquals(expected, LanguageRegistry.findLanguageVersionByTerseName(terseName));
}
/**
* Checks that the expected {@link LanguageVersion} can be found via {@link #name} and {@link #version}.
*/
@Test
public void testFindVersionsForLanguageNameAndVersion() {
SourceLanguage sourceLanguage = new SourceLanguage();
@ -48,8 +83,4 @@ public class AbstractLanguageVersionTest {
assertEquals(expected, languageVersion);
}
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(AbstractLanguageVersionTest.class);
}
}

View File

@ -7,76 +7,88 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotEquals;
import org.junit.Test;
/**
* Unit tests for {@link ParserOptions}.
*/
public class ParserOptionsTest {
/**
* SuppressMarker should be initially null and changeable.
*/
@Test
public void testSuppressMarker() throws Exception {
ParserOptions parserOptions = new ParserOptions();
assertNull(parserOptions.getSuppressMarker());
parserOptions.setSuppressMarker("foo");
assertEquals("foo", parserOptions.getSuppressMarker());
public void testSuppressMarker() {
ParserOptions parserOptions = new ParserOptions();
assertNull(parserOptions.getSuppressMarker());
parserOptions.setSuppressMarker("foo");
assertEquals("foo", parserOptions.getSuppressMarker());
}
/**
* Verify that the equals and hashCode methods work as expected.
*/
@Test
public void testEqualsHashcode() throws Exception {
ParserOptions options1 = new ParserOptions();
options1.setSuppressMarker("foo");
ParserOptions options2 = new ParserOptions();
options2.setSuppressMarker("bar");
ParserOptions options3 = new ParserOptions();
options3.setSuppressMarker("foo");
ParserOptions options4 = new ParserOptions();
options4.setSuppressMarker("bar");
verifyOptionsEqualsHashcode(options1, options2, options3, options4);
public void testEqualsHashcode() {
ParserOptions options1 = new ParserOptions();
options1.setSuppressMarker("foo");
ParserOptions options2 = new ParserOptions();
options2.setSuppressMarker("bar");
ParserOptions options3 = new ParserOptions();
options3.setSuppressMarker("foo");
ParserOptions options4 = new ParserOptions();
options4.setSuppressMarker("bar");
verifyOptionsEqualsHashcode(options1, options2, options3, options4);
}
// 1 and 3 are equals, as are 2 and 4.
@SuppressWarnings("PMD.UseAssertSameInsteadOfAssertTrue")
/**
* Verify equals and hashCode for 4 {@link ParserOptions} instances.
* The given options should be as follows: 1 and 3 are equals, as are 2 and 4.
*
* @param options1 first option instance - equals third
* @param options2 second option instance - equals fourth
* @param options3 third option instance - equals first
* @param options4 fourth option instance - equals second
*/
public static void verifyOptionsEqualsHashcode(ParserOptions options1, ParserOptions options2,
ParserOptions options3, ParserOptions options4) {
// Objects should be different
assertNotSame(options1, options2);
assertNotSame(options1, options2);
assertNotSame(options1, options3);
assertNotSame(options2, options3);
assertNotSame(options2, options4);
assertNotSame(options3, options4);
ParserOptions options3, ParserOptions options4) {
// Objects should be different
assertNotSame(options1, options2);
assertNotSame(options1, options2);
assertNotSame(options1, options3);
assertNotSame(options2, options3);
assertNotSame(options2, options4);
assertNotSame(options3, options4);
// Check all 16 equality combinations
assertEquals(options1, options1);
assertFalse(options1.equals(options2));
assertEquals(options1, options3);
assertFalse(options1.equals(options4));
// Check all 16 equality combinations
assertEquals(options1, options1);
assertFalse(options1.equals(options2));
assertEquals(options1, options3);
assertFalse(options1.equals(options4));
assertFalse(options2.equals(options1));
assertEquals(options2, options2);
assertFalse(options2.equals(options3));
assertEquals(options2, options4);
assertFalse(options2.equals(options1));
assertEquals(options2, options2);
assertFalse(options2.equals(options3));
assertEquals(options2, options4);
assertEquals(options3, options1);
assertFalse(options3.equals(options2));
assertEquals(options3, options3);
assertFalse(options3.equals(options4));
assertEquals(options3, options1);
assertFalse(options3.equals(options2));
assertEquals(options3, options3);
assertFalse(options3.equals(options4));
assertFalse(options4.equals(options1));
assertEquals(options4, options2);
assertFalse(options4.equals(options3));
assertEquals(options4, options4);
assertFalse(options4.equals(options1));
assertEquals(options4, options2);
assertFalse(options4.equals(options3));
assertEquals(options4, options4);
// Hashcodes should match up
assertFalse(options1.hashCode() == options2.hashCode());
assertTrue(options1.hashCode() == options3.hashCode());
assertFalse(options1.hashCode() == options4.hashCode());
assertFalse(options2.hashCode() == options3.hashCode());
assertTrue(options2.hashCode() == options4.hashCode());
assertFalse(options3.hashCode() == options4.hashCode());
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(ParserOptionsTest.class);
// Hashcodes should match up
assertNotEquals(options1.hashCode(), options2.hashCode());
assertEquals(options1.hashCode(), options3.hashCode());
assertNotEquals(options1.hashCode(), options4.hashCode());
assertNotEquals(options2.hashCode(), options3.hashCode());
assertEquals(options2.hashCode(), options4.hashCode());
assertNotEquals(options3.hashCode(), options4.hashCode());
}
}