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

feat: java logger used practice #219

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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,
];