Skip to content

Commit

Permalink
Added javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
nimakarimipour committed Jul 19, 2022
1 parent 2977561 commit bb44dac
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void checkExceptionIsThrownIfConfigPathNotSet() {
.setOutputFileNameAndHeader("Unknown", "Unknown")
.addSourceFile("SampleClassForTest.java")
.setFactory(factory);
tester.doTest(
tester.doTestWithExpectingError(
IllegalStateException.class,
"Error in Scanner Checker configuration, should be set with via error prone flag: (-XepOpt:Scanner:ConfigPath)");
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public void checkOutputDirIsNonnull() {
.setOutputFileNameAndHeader("Unknown", "Unknown")
.addSourceFile("SampleClassForTest.java")
.setFactory(factory);
tester.doTest(
tester.doTestWithExpectingError(
IllegalArgumentException.class,
"Output path cannot be null, should be set it in config file within <path> tag");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,25 @@ public SerializationTestHelper(Path outputDir) {
this.outputDir = outputDir;
}

/**
* Adds source code to the list of inputs. This method is part of the builder pattern.
*
* @param path Relative path to src directory where the given source code should exist.
* @param lines Lines of source code.
* @return Receiver of the call.
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
public SerializationTestHelper<T> addSourceLines(String path, String... lines) {
compilationTestHelper.addSourceLines(path, lines);
return this;
}

/**
* Adds source code to the list of inputs. This method is part of the builder pattern.
*
* @param path Path to resource.
* @return Receiver of the call.
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
public SerializationTestHelper<T> addSourceFile(String path) {
// This class is inside "tools" package, which means compilationTestHelper will try to use a
Expand All @@ -71,35 +84,68 @@ public SerializationTestHelper<T> addSourceFile(String path) {
return this;
}

/**
* Sets the expected output. Any unseen / unexpected output will result to a failure in the test.
* This method is part of the builder pattern.
*
* @param outputs Expected output.
* @return Receiver of the call.
*/
@SafeVarargs
public final SerializationTestHelper<T> setExpectedOutputs(T... outputs) {
this.expectedOutputs = ImmutableList.copyOf(outputs);
return this;
}

/**
* If called, no output should be expected while running the test. This method is part of the
* builder pattern.
*
* @return Receiver of the call.
*/
public SerializationTestHelper<T> expectNoOutput() {
this.expectedOutputs = ImmutableList.of();
return this;
}

/**
* Creates the actual {@link Scanner} with the given arguments. This method is part of the builder
* pattern and should be called before any other method.
*
* @return Receiver of the call.
*/
public SerializationTestHelper<T> setArgs(List<String> args) {
compilationTestHelper =
CompilationTestHelper.newInstance(Scanner.class, getClass()).setArgs(args);
return this;
}

/**
* Sets factory. This method is part of the builder pattern.
*
* @param factory Factory instance.
* @return Receiver of the call.
*/
public SerializationTestHelper<T> setFactory(DisplayFactory<T> factory) {
this.factory = factory;
return this;
}

/**
* Sets file name and the expected header of output file. This method is part of the builder
* pattern.
*
* @param fileName Output file name.
* @param header Expected header.
* @return Receiver of the call.
*/
public SerializationTestHelper<T> setOutputFileNameAndHeader(String fileName, String header) {
this.fileName = fileName;
this.header = header;
return this;
}

public void prepareTest() {
private void prepareTest() {
Preconditions.checkNotNull(factory, "Factory cannot be null");
Preconditions.checkNotNull(fileName, "File name cannot be null");
outputFilePath = outputDir.resolve(fileName);
Expand All @@ -110,13 +156,21 @@ public void prepareTest() {
}
}

public void doTest(Class<? extends Exception> exception, String expectedErrorMessage) {
/**
* Runs the testing with expecting to encounter a specific error.
*
* @param exception Expected Exception to be raised by running the test.
* @param expectedErrorMessage Expected message to be printed by running the test.
*/
public void doTestWithExpectingError(
Class<? extends Exception> exception, String expectedErrorMessage) {
String fullExpectedMessage = "Caused by: " + exception.getName() + ": " + expectedErrorMessage;
prepareTest();
AssertionError ex = assertThrows(AssertionError.class, () -> compilationTestHelper.doTest());
assert ex.getMessage().contains(fullExpectedMessage);
}

/** Runs the test. */
public void doTest() {
prepareTest();
compilationTestHelper.doTest();
Expand Down

0 comments on commit bb44dac

Please sign in to comment.