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

WIP RAT-336: Start to migrate tests in first module #264

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.rat.analysis;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void standardTypeAnalyser() throws Exception {
reporter.report(document);
String result = out.toString();
for (String exp : expected) {
assertTrue(result.contains(exp), () -> exp);
assertThat(result).contains(exp);
}
}

Expand All @@ -91,7 +92,7 @@ public void noteTypeAnalyser() throws Exception {
Resources.getResourceFile("/elements/LICENSE"));
analyser.analyse(document);
reporter.report(document);
assertEquals("<resource name='src/test/resources/elements/LICENSE' type='NOTICE'/>", out.toString());
assertThat(out.toString()).isEqualTo("<resource name='src/test/resources/elements/LICENSE' type='NOTICE'/>");
}

@Test
Expand All @@ -100,7 +101,7 @@ public void binaryTypeAnalyser() throws Exception {
Resources.getResourceFile("/elements/Image.png"));
analyser.analyse(document);
reporter.report(document);
assertEquals("<resource name='src/test/resources/elements/Image.png' type='BINARY'/>", out.toString());
assertThat(out.toString()).isEqualTo("<resource name='src/test/resources/elements/Image.png' type='BINARY'/>");
}

@Test
Expand All @@ -114,7 +115,7 @@ public void archiveTypeAnalyserTest() throws Exception {
analyser = DefaultAnalyserFactory.createDefaultAnalyser(config);
analyser.analyse(document);
reporter.report(document);
assertEquals("<resource name='src/test/resources/elements/dummy.jar' type='ARCHIVE'/>", out.toString());
assertThat(out.toString()).isEqualTo("<resource name='src/test/resources/elements/dummy.jar' type='ARCHIVE'/>");
}

@Test
Expand All @@ -130,9 +131,9 @@ public void archivesAbsenceTest() throws Exception {
analyser.analyse(document);
reporter.report(document);
String result = out.toString();
TextUtils.assertContains("<resource name='src/test/resources/elements/dummy.jar' type='ARCHIVE'>", out.toString());
TextUtils.assertContains("<license id='?????' name='Unknown license' approval='false' family='?????'/>", out.toString());
TextUtils.assertContains("<license id='ASL' name='Applied Apache License Version 2.0' approval='false' family='AL '/>", out.toString());
TextUtils.assertContains("<resource name='src/test/resources/elements/dummy.jar' type='ARCHIVE'>", result);
TextUtils.assertContains("<license id='?????' name='Unknown license' approval='false' family='?????'/>", result);
TextUtils.assertContains("<license id='ASL' name='Applied Apache License Version 2.0' approval='false' family='AL '/>", result);
}

@Test
Expand All @@ -148,9 +149,9 @@ public void archivesPresenceTest() throws Exception {
analyser.analyse(document);
reporter.report(document);
String result = out.toString();
TextUtils.assertContains("<resource name='src/test/resources/elements/dummy.jar' type='ARCHIVE'>", out.toString());
TextUtils.assertNotContains("<license id='?????' name='Unknown license' approval='false' family='?????'/>", out.toString());
TextUtils.assertContains("<license id='ASL' name='Applied Apache License Version 2.0' approval='false' family='AL '/>", out.toString());
TextUtils.assertContains("<resource name='src/test/resources/elements/dummy.jar' type='ARCHIVE'>", result);
TextUtils.assertNotContains("<license id='?????' name='Unknown license' approval='false' family='?????'/>", result);
TextUtils.assertContains("<license id='ASL' name='Applied Apache License Version 2.0' approval='false' family='AL '/>", result);
}

@Test
Expand All @@ -159,7 +160,7 @@ public void archiveTypeAnalyser() throws Exception {
Resources.getResourceFile("/elements/dummy.jar"));
analyser.analyse(document);
reporter.report(document);
assertEquals("<resource name='src/test/resources/elements/dummy.jar' type='ARCHIVE'/>", out.toString());
assertThat(out.toString()).isEqualTo("<resource name='src/test/resources/elements/dummy.jar' type='ARCHIVE'/>");
}

@Test
Expand All @@ -168,7 +169,7 @@ public void RAT211_bmp_Test() throws Exception {
Resources.getResourceFile("/jira/RAT211/side_left.bmp"));
analyser.analyse(document);
reporter.report(document);
assertEquals("<resource name='src/test/resources/jira/RAT211/side_left.bmp' type='BINARY'/>", out.toString());
assertThat(out.toString()).isEqualTo("<resource name='src/test/resources/jira/RAT211/side_left.bmp' type='BINARY'/>");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
*/
package org.apache.rat.analysis.license;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.BufferedReader;
Expand All @@ -34,15 +33,13 @@
import org.apache.rat.license.ILicense;
import org.apache.rat.license.LicenseSetFactory;
import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
import org.apache.rat.testhelpers.TestingLicense;
import org.apache.rat.utils.DefaultLog;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Test to see if short form license information will be recognized correctly.
*
*/
abstract public class AbstractLicenseTest {
private static int NAME = 0;
Expand Down Expand Up @@ -77,8 +74,8 @@ public void testMatchProcessing(String id, String familyPattern, String name, St
for (String[] target : targets) {
if (processText(license, target[TEXT])) {
data.reportOnLicense(license);
assertEquals(1, data.licenses().count());
assertEquals(license, data.licenses().findFirst().get());
assertThat(data.licenses()).hasSize(1);
assertThat(data.licenses().findFirst()).isPresent().hasValue(license);
} else {
fail(license + " was not matched by " + target[NAME]);
}
Expand Down Expand Up @@ -110,8 +107,7 @@ public void testEmbeddedStrings(String id, String family, String name, String no
for (String fmt : formats) {
boolean found = processText(license, String.format(fmt, target[TEXT]));
license.reset();
assertTrue(found, () -> String.format("%s %s did not match pattern '%s' for target string %s", id,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Claudenw it seems that assertJ has no equivalent to this supplier construct.
Do you have an idea how to transform these kind of tests into assertJ or should we leave the other assertTrue() - WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could generate the message string and just use it. It will have some performance impact. Or you could just leave the junit 5 format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never worked with that construct before and wondered what it is good for. Thx.

name, fmt, target[NAME]));
assertThat(found).isTrue();
}
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

/**
* Apache Software License detection tests.
*
*/
public class ApacheSoftwareLicenseTest extends AbstractLicenseTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class DirectoryScanner {
*
* @param directory the directory containing the files
* @param matcher the license matcher
* @param expected the expected result of the each scan
* @param expected the expected result of each scan
* @throws Exception
*/
@Test
public static void testFilesInDir(String directory, ILicense license, boolean expected) throws Exception {
public static void runTestsOnFilesInDir(String directory, ILicense license, boolean expected) throws Exception {
final File[] resourceFiles = Resources.getResourceFiles(directory);
if (resourceFiles.length == 0) {
fail("No files found under " + directory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class GeneratedLicenseTest extends AbstractLicenseTest {
private static String id = "GEN";
private static String name = "Generated Files";
private static String notes = "Files that are autmoatically generated.";
private static String notes = "Files that are automatically generated.";
private static String[][] targets = { { "Cayenne", "generated by Cayenne" }, { "JJTree", "Generated By:JJTree" },
{ "JavaCC", "Generated By:JavaCC" }, { "AUTOMATIC", "THIS FILE IS AUTOMATICALLY GENERATED" },
{ "XBeans", "NOTE: this file is autogenerated by XBeans" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.util.stream.Stream;

import org.apache.rat.license.ILicense;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.provider.Arguments;

public class OASISLicenseTest extends AbstractLicenseTest {
Expand All @@ -36,15 +38,17 @@ public class OASISLicenseTest extends AbstractLicenseTest {
public static Stream<Arguments> parameterProvider() {
return Stream.of(Arguments.of(id, id, name, notes, targets));
}
/*
@Test(timeout = 2000) // may need to be adjusted if many more files are added

/* TODO
private static ILicense license = ILicense.builder().setId(id).build();
@Test // may need to be adjusted if many more files are added
public void goodFiles() throws Exception {
DirectoryScanner.testFilesInDir("oasis/good", license, true);
DirectoryScanner.runTestsOnFilesInDir("oasis/good", license, true);
}

@Test(timeout = 2000) // may need to be adjusted if many more files are added
public void baddFiles() throws Exception {
DirectoryScanner.testFilesInDir("oasis/bad", license, false);
@Test // may need to be adjusted if many more files are added
public void badFiles() throws Exception {
DirectoryScanner.runTestsOnFilesInDir("oasis/bad", license, false);
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.rat.analysis.matchers;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Locale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
*/
package org.apache.rat.analysis.matchers;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -35,8 +34,8 @@ public void setup() {

@Test
public void testMatch() {
assertFalse(target.matches(AbstractMatcherTest.makeHeaders(null, "what in the world")));
assertTrue(target.matches(AbstractMatcherTest.makeHeaders(null, "hello world")));
assertTrue(target.matches(AbstractMatcherTest.makeHeaders(null, "HELLO world")));
assertThat(target.matches(AbstractMatcherTest.makeHeaders(null, "what in the world"))).isFalse();
assertThat(target.matches(AbstractMatcherTest.makeHeaders(null, "hello world"))).isTrue();
assertThat(target.matches(AbstractMatcherTest.makeHeaders(null, "HELLO world"))).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
*/
package org.apache.rat.analysis.matchers;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

import org.apache.rat.analysis.IHeaderMatcher;
import org.apache.rat.analysis.IHeaders;
Expand All @@ -45,9 +44,9 @@ public void testMatch() {

IHeaders headers = AbstractMatcherTest.makeHeaders(sb.toString(),null);

assertTrue(target1.matches(headers));
assertTrue(target2.matches(headers));
assertFalse(target3.matches(headers));
assertThat(target1.matches(headers)).isTrue();
assertThat(target2.matches(headers)).isTrue();
assertThat(target3.matches(headers)).isFalse();
target1.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
*/
package org.apache.rat.analysis.matchers;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

Expand All @@ -29,8 +28,8 @@ public class SimpleCopyrightTests {

@Test
public void testTrueIsAlwaysTrue() {
assertTrue(target.matches(AbstractMatcherTest.makeHeaders("hello Copyright 1999", null)));
assertFalse(target.matches(AbstractMatcherTest.makeHeaders("A non matching line", null)));
assertThat(target.matches(AbstractMatcherTest.makeHeaders("hello Copyright 1999", null))).isTrue();
assertThat(target.matches(AbstractMatcherTest.makeHeaders("A non matching line", null))).isFalse();
target.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.rat.analysis.matchers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.regex.Pattern;

Expand All @@ -36,8 +36,8 @@ public void setup() {

@Test
public void test() {
assertEquals(false, target.matches(AbstractMatcherTest.makeHeaders("what in the world", null)));
assertEquals(true, target.matches(AbstractMatcherTest.makeHeaders("hello world", null)));
assertThat(target.matches(AbstractMatcherTest.makeHeaders("what in the world", null))).isFalse();
assertThat(target.matches(AbstractMatcherTest.makeHeaders("hello world", null))).isTrue();
target.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
*/
package org.apache.rat.analysis.matchers;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -35,8 +34,8 @@ public void setup() {

@Test
public void testMatch() {
assertFalse(target.matches(AbstractMatcherTest.makeHeaders("what in the world", null)));
assertTrue(target.matches(AbstractMatcherTest.makeHeaders("hello world", null)));
assertThat(target.matches(AbstractMatcherTest.makeHeaders("what in the world", null))).isFalse();
assertThat(target.matches(AbstractMatcherTest.makeHeaders("hello world", null))).isTrue();
target.reset();
}

Expand Down