Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rule to limit excessive use of sequential loggers #56

Merged
merged 17 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.avioconsulting.mule.linter.model

class Version {

enum Operator {

EQUAL,
GREATER_THAN

}

private String version

Version(String version) {
this.version = version
}

void setVersion(String version) {
this.version = version
}

Boolean isEqual(String version) {
return this.version == version
}

Boolean isGreater(String version) {
return this.version < version
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.avioconsulting.mule.linter.model.pom

import com.avioconsulting.mule.linter.parser.MuleXmlParser
import groovy.xml.slurpersupport.GPathResult

class ArtifactDescriptor {

String groupId
String artifactId
PomElement version
Integer lineNo
PomFile pomFile
GPathResult pluginXml

ArtifactDescriptor(GPathResult pluginXml, PomFile pomFile) {
this.pluginXml = pluginXml
this.groupId = pluginXml.groupId as String
this.artifactId = pluginXml.artifactId as String
this.lineNo = MuleXmlParser.getNodeLineNumber(pluginXml)
this.pomFile = pomFile
this.version = getAttribute('version')
}

PomElement getAttribute(String attributeName) {
PomElement pElement = null
GPathResult element = pluginXml.depthFirst().find {
it.name() == attributeName
}

if (element != null ) {
if ( isExpression(element.text()) ) {
pElement = pomFile.getPomProperty(variableName(element.text()))
} else {
pElement = new PomElement()
pElement.name = element.name()
pElement.value = element.text()
pElement.lineNo = MuleXmlParser.getNodeLineNumber(element)
}
}

return pElement
}

PomElement getConfigProperty(String propertyName) {
PomElement pElement = null
pluginXml.configuration.depthFirst().each {
if (it.name() == propertyName) {
pElement = new PomElement()
pElement.name = propertyName
pElement.value = it.text()
pElement.lineNo = MuleXmlParser.getNodeLineNumber(it)
}
}
return pElement
}

private Boolean isExpression(String expression) {
expression.startsWith('${')
}

private String variableName(String expression) {
expression.takeAfter('${').takeBefore('}')
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.avioconsulting.mule.linter.model.pom

import groovy.xml.slurpersupport.GPathResult

class PomDependency extends ArtifactDescriptor {

PomDependency(GPathResult pluginXml, PomFile pomFile) {
super(pluginXml, pomFile)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ class PomFile extends ProjectFile {
return plugin
}

PomDependency getDependency(String groupId, String artifactId) {
PomDependency dependency
GPathResult dependencyPath = pomXml.dependencies.dependency.find {
it.groupId == groupId && it.artifactId == artifactId
} as GPathResult

if (dependencyPath != null && dependencyPath.size() > 0) {
dependency = new PomDependency(dependencyPath, this)
}
return dependency
}

MunitMavenPlugin getMunitPlugin() {
PomPlugin pp = getPlugin(MunitMavenPlugin.GROUP_ID, MunitMavenPlugin.ARTIFACT_ID)
return pp == null ? null : new MunitMavenPlugin(pp.pluginXml, this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,11 @@
package com.avioconsulting.mule.linter.model.pom

import com.avioconsulting.mule.linter.parser.MuleXmlParser
import groovy.xml.slurpersupport.GPathResult

class PomPlugin {

String groupId
String artifactId
PomElement version
Integer lineNo
PomFile pomFile
GPathResult pluginXml
class PomPlugin extends ArtifactDescriptor {

PomPlugin(GPathResult pluginXml, PomFile pomFile) {
this.pluginXml = pluginXml
this.groupId = pluginXml.groupId as String
this.artifactId = pluginXml.artifactId as String
this.lineNo = MuleXmlParser.getNodeLineNumber(pluginXml)
this.pomFile = pomFile
this.version = getAttribute('version')
}

PomElement getAttribute(String attributeName) {
PomElement pElement = null
GPathResult element = pluginXml.depthFirst().find {
it.name() == attributeName
}

if (element != null ) {
if ( isExpression(element.text()) ) {
pElement = pomFile.getPomProperty(variableName(element.text()))
} else {
pElement = new PomElement()
pElement.name = element.name()
pElement.value = element.text()
pElement.lineNo = MuleXmlParser.getNodeLineNumber(element)
}
}

return pElement
}

PomElement getConfigProperty(String propertyName) {
PomElement pElement = null
pluginXml.configuration.depthFirst().each {
if (it.name() == propertyName) {
pElement = new PomElement()
pElement.name = propertyName
pElement.value = it.text()
pElement.lineNo = MuleXmlParser.getNodeLineNumber(it)
}
}
return pElement
super(pluginXml, pomFile)
}

private Boolean isExpression(String expression) {
expression.startsWith('${')
}

private String variableName(String expression) {
expression.takeAfter('${').takeBefore('}')
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.avioconsulting.mule.linter.rule.pom

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.Version
import com.avioconsulting.mule.linter.model.pom.PomDependency
import com.avioconsulting.mule.linter.model.pom.PomElement
import com.avioconsulting.mule.linter.model.rule.Rule
import com.avioconsulting.mule.linter.model.rule.RuleViolation

class PomDependencyVersionRule extends Rule {

static final String RULE_ID = 'POM_DEPENDENCY_VERSION_CHECK'
static final String RULE_NAME = 'Maven dependency matches exists in pom.xml and matches to specified version criteria'
static final String MISSING_DEPENDENCY = 'Dependency does not exist: '
static final String RULE_VIOLATION_MESSAGE = 'Dependency exist but invalid version: '
Version version= new Version()

private final String groupId
private final String artifactId
private final String artifactVersion
private final Version.Operator versionOperator

PomDependencyVersionRule(String groupId, String artifactId, String artifactVersion) {
this(groupId, artifactId, artifactVersion, Version.Operator.EQUAL)
}

PomDependencyVersionRule(String groupId, String artifactId, String artifactVersion,
Version.Operator versionOperator) {
this.ruleId = RULE_ID
this.ruleName = RULE_NAME
this.groupId = groupId
this.artifactId = artifactId
this.artifactVersion = artifactVersion
this.versionOperator = versionOperator
version.setVersion(artifactVersion)
}

@Override
List<RuleViolation> execute(Application app) {
List<RuleViolation> violations = []

PomDependency dependency = app.pomFile.getDependency(groupId, artifactId)

if ( dependency == null ) {
violations.add(new RuleViolation(this, app.pomFile.path, 0, MISSING_DEPENDENCY + "$groupId , $artifactId"))
} else {
Boolean isViolated = false;
PomElement attribute = dependency.getAttribute('version')
String dependencyVersion = attribute.value
switch (versionOperator) {
case Version.Operator.EQUAL:
isViolated = (!version.isEqual(dependencyVersion)) ? true : false
break;
case Version.Operator.GREATER_THAN:
isViolated = (!version.isGreater(dependencyVersion)) ? true : false
}
if (isViolated) {
violations.add(new RuleViolation(this, app.pomFile.path, attribute.lineNo,
RULE_VIOLATION_MESSAGE + "$groupId , $artifactId, $attribute.value"))
}
}
return violations
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.avioconsulting.mule.linter.model.rule.Rule
import com.avioconsulting.mule.linter.model.rule.RuleViolation
import spock.lang.Specification

class PomPluginAttributeRuleTest extends Specification {
class PomArtifactAttributeRuleTest extends Specification {

private final TestApplication testApp = new TestApplication()
private Application app
Expand Down
Loading