Skip to content

Commit

Permalink
Merge pull request #219 from DXHeroes/practice/java_uses_logger
Browse files Browse the repository at this point in the history
feat: java logger used practice
  • Loading branch information
prokopsimek authored Jan 27, 2020
2 parents ddde292 + 185bf81 commit f197ee9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/practices/Java/JavaLoggerUsedPractice.spec.ts
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);
});
});
38 changes: 38 additions & 0 deletions src/practices/Java/JavaLoggerUsedPractice.ts
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;
}
}
2 changes: 2 additions & 0 deletions src/practices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ThinPullRequestsPractice } from './LanguageIndependent/ThinPullRequests
import { SecurityVulnerabilitiesPractice } from './JavaScript/SecurityVulnerabilitiesPractice';
import { FirstTestPractice, SecondTestPractice, InvalidTestPractice } from '../scanner';
import { JavaPackageManagementUsedPractice } from './Java/JavaPackageManagementUsed';
import { JavaLoggerUsedPractice } from './Java/JavaLoggerUsedPractice';
import { JavaTestingFrameworkUsedPractice } from './Java/JavaTestingFrameworkUsedPractice';
import { JavaMockingFrameworkUsedPractice } from './Java/JavaMockingFrameworkUsed';

Expand Down Expand Up @@ -68,6 +69,7 @@ export const practices = [
ThinPullRequestsPractice,
SecurityVulnerabilitiesPractice,
JavaPackageManagementUsedPractice,
JavaLoggerUsedPractice,
JavaTestingFrameworkUsedPractice,
JavaMockingFrameworkUsedPractice,
];

0 comments on commit f197ee9

Please sign in to comment.