Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 11, 2024
2 parents 9b3606d + 0bd5241 commit ac58393
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 3 deletions.
Expand Up @@ -189,7 +189,7 @@ private static DetailAST secondChild(final DetailAST node) {
* @return True if node contains angle brackets only
*/
private static boolean isDiamondOperatorUsed(final DetailAST node) {
return node.getChildCount() == 2
return node != null && node.getChildCount() == 2
&& node.getFirstChild().getType() == TokenTypes.GENERIC_START
&& node.getLastChild().getType() == TokenTypes.GENERIC_END;
}
Expand Down
Expand Up @@ -106,6 +106,14 @@ public abstract class AbstractQuliceMojo extends AbstractMojo
)
private final Collection<String> asserts = new LinkedList<>();

/**
* The source encoding.
*
* @parameter expression="${project.build.sourceEncoding}" required="true"
*/
@Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
private String charset;

/**
* Set Maven Project (used mostly for unit testing).
* @param proj The project to set
Expand Down Expand Up @@ -148,6 +156,14 @@ public final void setExcludes(final Collection<String> exprs) {
this.excludes.addAll(exprs);
}

/**
* Set source code encoding.
* @param encoding Source code encoding
*/
public void setEncoding(final String encoding) {
this.charset = encoding;
}

@Override
public final void contextualize(final Context ctx) {
this.environment.setContext(ctx);
Expand All @@ -167,6 +183,7 @@ public final void execute() throws MojoFailureException {
);
this.environment.setExcludes(this.excludes);
this.environment.setAsser(this.asserts);
this.environment.setEncoding(this.charset);
final long start = System.nanoTime();
this.doExecute();
Logger.info(
Expand Down
Expand Up @@ -41,6 +41,7 @@
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.LinkedList;
Expand Down Expand Up @@ -96,6 +97,11 @@ public final class DefaultMavenEnvironment implements MavenEnvironment {
*/
private final Collection<String> asser = new LinkedList<>();

/**
* Source code encoding charset.
*/
private String charset = "UTF-8";

@Override
public String param(final String name, final String value) {
String ret = this.iproperties.getProperty(name);
Expand Down Expand Up @@ -292,6 +298,21 @@ public void setAsser(final Collection<String> ass) {
this.asser.addAll(ass);
}

public void setEncoding(final String encoding) {
this.charset = encoding;
}

/**
* Get source files encoding.
* @return Charset of the source files
*/
public Charset encoding() {
if (this.charset == null || this.charset.isEmpty()) {
this.charset = "UTF-8";
}
return Charset.forName(this.charset);
}

/**
* Creates URL ClassLoader in privileged block.
*
Expand Down
Expand Up @@ -32,6 +32,7 @@

import com.qulice.spi.Environment;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Properties;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -182,5 +183,10 @@ public boolean exclude(final String check, final String name) {
public Collection<String> excludes(final String checker) {
return this.env.excludes(checker);
}

@Override
public Charset encoding() {
return this.env.encoding();
}
}
}
Expand Up @@ -31,6 +31,7 @@
package com.qulice.maven;

import com.google.common.collect.ImmutableList;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.apache.maven.project.MavenProject;
import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -147,4 +148,17 @@ void producesEmptyExcludesWhenNoMatches() {
Matchers.empty()
);
}

/**
* Default source files encoding should be UFT-8.
*/
@Test
void defaultEncodingIsUtf() {
final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
MatcherAssert.assertThat(
"Default encoding should be UTF-8",
env.encoding(),
Matchers.is(StandardCharsets.UTF_8)
);
}
}
Expand Up @@ -52,9 +52,8 @@ public final class MavenProjectMocker {
* In this basedir.
* @param dir The directory
* @return This object
* @throws Exception If something wrong happens inside
*/
public MavenProjectMocker inBasedir(final File dir) throws Exception {
public MavenProjectMocker inBasedir(final File dir) {
Mockito.doReturn(dir).when(this.project).getBasedir();
final Build build = Mockito.mock(Build.class);
Mockito.doReturn(build).when(this.project).getBuild();
Expand Down
8 changes: 8 additions & 0 deletions qulice-pmd/src/main/java/com/qulice/pmd/SourceValidator.java
Expand Up @@ -33,6 +33,7 @@
import com.jcabi.log.Logger;
import com.qulice.spi.Environment;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
Expand Down Expand Up @@ -70,6 +71,11 @@ final class SourceValidator {
*/
private final PMDConfiguration config;

/**
* Source files encoding.
*/
private final Charset encoding;

/**
* Creates new instance of <code>SourceValidator</code>.
* @param env Environment
Expand All @@ -79,6 +85,7 @@ final class SourceValidator {
this.listener = new PmdListener(env);
this.renderer = new PmdRenderer();
this.config = new PMDConfiguration();
this.encoding = env.encoding();
}

/**
Expand All @@ -95,6 +102,7 @@ public Collection<PmdError> validate(
this.config.setMinimumPriority(RulePriority.LOW);
this.config.setIgnoreIncrementalAnalysis(true);
this.config.setShowSuppressedViolations(true);
this.config.setSourceEncoding(this.encoding.name());
final Report report = new Report();
report.addListener(this.listener);
this.context.setReport(report);
Expand Down
13 changes: 13 additions & 0 deletions qulice-pmd/src/test/java/com/qulice/pmd/PmdValidatorTest.java
Expand Up @@ -751,4 +751,17 @@ void allowsSwaggerAnnotations() throws Exception {
)
).validate();
}

/**
* PmdValidator can prohibit unicode characters in method names.
* @throws Exception If something wrong happens inside.
*/
@Test
void prohibitsUnicodeCharactersInMethodNames() throws Exception {
new PmdAssert(
"UnicodeCharactersInMethodNames.java",
Matchers.is(false),
Matchers.containsString("MethodNamingConventions")
).validate();
}
}
@@ -0,0 +1,9 @@
package foo;

public final class UnderstandsMethodReferences {
private static final String SOME_STRING = "φ";

public String φTestMethod() {
return UnderstandsMethodReferences.SOME_STRING;
}
}
12 changes: 12 additions & 0 deletions qulice-spi/src/main/java/com/qulice/spi/Environment.java
Expand Up @@ -32,6 +32,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -122,6 +123,12 @@ public interface Environment {
*/
Collection<String> excludes(String checker);

/**
* Encoding for the files.
* @return Source files charset
*/
Charset encoding();

/**
* Mock of {@link Environment}.
*
Expand Down Expand Up @@ -323,5 +330,10 @@ public Collection<String> excludes(final String checker) {
}
return exc;
}

@Override
public Charset encoding() {
return StandardCharsets.UTF_8;
}
}
}

0 comments on commit ac58393

Please sign in to comment.