-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from DXHeroes/practice/java_uses_logger
feat: java logger used practice
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { JavaLoggerUsedPractice } from './JavaLoggerUsedPractice'; | ||
import { PracticeEvaluationResult, ProgrammingLanguage } from '../../model'; | ||
import { TestContainerContext, createTestContainer } from '../../inversify.config'; | ||
import { IPackageInspector } from '../../inspectors/IPackageInspector'; | ||
|
||
describe('JavaLoggerUsedPractice', () => { | ||
let practice: JavaLoggerUsedPractice; | ||
let containerCtx: TestContainerContext; | ||
let packageInspector: IPackageInspector; | ||
|
||
beforeAll(() => { | ||
containerCtx = createTestContainer(); | ||
containerCtx.container.bind('JavaLoggerUsedPractice').to(JavaLoggerUsedPractice); | ||
practice = containerCtx.container.get('JavaLoggerUsedPractice'); | ||
packageInspector = <IPackageInspector>containerCtx.practiceContext.packageInspector; | ||
}); | ||
|
||
it('Detects a Java logger dependency', async () => { | ||
packageInspector.hasOneOfPackages = () => true; | ||
|
||
const result = await practice.evaluate(containerCtx.practiceContext); | ||
expect(result).toEqual(PracticeEvaluationResult.practicing); | ||
}); | ||
|
||
it('Did not detect a Java logger dependency', async () => { | ||
packageInspector.hasOneOfPackages = () => false; | ||
|
||
const result = await practice.evaluate(containerCtx.practiceContext); | ||
expect(result).toEqual(PracticeEvaluationResult.notPracticing); | ||
}); | ||
|
||
it('Did not recognize packageInspector', async () => { | ||
const result = await practice.evaluate({ ...containerCtx.practiceContext, packageInspector: undefined }); | ||
expect(result).toEqual(PracticeEvaluationResult.unknown); | ||
}); | ||
|
||
it('Is applicable if it is Java', async () => { | ||
containerCtx.practiceContext.projectComponent.language = ProgrammingLanguage.Java; | ||
|
||
const result = await practice.isApplicable(containerCtx.practiceContext); | ||
expect(result).toEqual(true); | ||
}); | ||
|
||
it('Is applicable if it is not Java', async () => { | ||
containerCtx.practiceContext.projectComponent.language = ProgrammingLanguage.Haskell; | ||
|
||
const result = await practice.isApplicable(containerCtx.practiceContext); | ||
expect(result).toEqual(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { IPractice } from '../IPractice'; | ||
import { PracticeEvaluationResult, PracticeImpact, ProgrammingLanguage } from '../../model'; | ||
import { DxPractice } from '../DxPracticeDecorator'; | ||
import { PracticeContext } from '../../contexts/practice/PracticeContext'; | ||
|
||
@DxPractice({ | ||
id: 'Java.LoggerUsedPractice', | ||
name: 'Use a Java Logging Dependency', | ||
impact: PracticeImpact.small, | ||
suggestion: | ||
'Use a logging library to avoid errors and even cyber attacks. The most widely used logging library in the Java community is Log4j 2.', | ||
reportOnlyOnce: true, | ||
url: 'https://logging.apache.org/log4j/2.x/', | ||
}) | ||
export class JavaLoggerUsedPractice implements IPractice { | ||
async isApplicable(ctx: PracticeContext): Promise<boolean> { | ||
return ctx.projectComponent.language === ProgrammingLanguage.Java; | ||
} | ||
|
||
async evaluate(ctx: PracticeContext): Promise<PracticeEvaluationResult> { | ||
if (!ctx.packageInspector) { | ||
return PracticeEvaluationResult.unknown; | ||
} | ||
|
||
if ( | ||
ctx.packageInspector.hasOneOfPackages([ | ||
'org.apache.logging.log4j:log4j', | ||
'org.apache.logging.log4j:log4j-api', | ||
'org.apache.logging.log4j:log4j-core', | ||
'ch.qos.logback:logback-classic', | ||
'org.slf4j:slf4j-api', | ||
]) | ||
) { | ||
return PracticeEvaluationResult.practicing; | ||
} | ||
return PracticeEvaluationResult.notPracticing; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters