Commit 4bda4428 authored by jcechace's avatar jcechace
Browse files

Improvements and Test

parent 4965995f
Homework assignment no. 2, RPG Character generator
====================================
**Publication date:** 15th May
**Publication date:** 17th May
**Submission deadline:** 5th June 23:59
**Submission deadline:** 7th June 23:59
General information
-------------------
......@@ -32,7 +32,7 @@ You will have to learn just the basics by replicating what is already present in
### Thymeleaf
Thymeleaf is a complex templating library which allows for templating of various documents from simple text, through html, to javascript files.
In this homework you will be required to fill in a small part of a template. Other than that, you are encouraged to have a look at how to use this library.
In this homework you will be only be required to have a look and understand how to use this library. Any actual work with this library was encapsulated into methods which you can use.
The documentation can be found [here](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf).
......@@ -142,11 +142,12 @@ Complete the implementation of ``Application`` class in order for it to recognis
In order to validate some of the values provided on command line, you will need to read section *4.1. Individual parameter validation* in JCommander's documentation.
Program usage info should be displayed when ``--help`` is specified as command line parameter.
In case something goes wrong you should print to standard error output ```Error: <message>``` where ``<message>`` is a message from exception.
### Character to HTML file
Running the ``main`` method of ``Application`` class should produce a HTML file with character information.
To achieve this you should use the Thymeleaf library already mentioned in this assignment.
To have easier time working with this library, you can use the class ```impl.AbstractHtmlWriter``` and prepared template located in ```src/main/resources```.
However this template is not complete and you will have to fill in few things.
We strongly encourage you to play with this template and try to improve it -- as it's kinda ugly ;).
Pleas notice that ``AbstractHtmlWriter`` is located inside the ```impl``` package, which means that you can modify it to suit your needs.
\ No newline at end of file
package cz.muni.fi.pb162.hw03;
import java.util.Objects;
/**
* Representation of either Skill or Attribute
* @author Jakub Cechacek
......@@ -26,4 +28,19 @@ public class SkillOrAttribute {
public int getLevel() {
return level;
}
@Override public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SkillOrAttribute that = (SkillOrAttribute) o;
return level == that.level && Objects.equals(name, that.name);
}
@Override public int hashCode() {
return Objects.hash(name, level);
}
}
package cz.muni.fi.pb162.hw03.impl;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.SoftAssertions;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Jakub Cechacek
*/
public class ApplicationTest {
private static PrintStream outBcp;
private static PrintStream errBcp;
private static ByteArrayOutputStream out = new ByteArrayOutputStream();
private static PrintStream outStream = new PrintStream(out);
private static ByteArrayOutputStream err = new ByteArrayOutputStream();
private static PrintStream errStream = new PrintStream(err);
private Map<String, String> params = new HashMap<>();
@BeforeClass
public static void setupClass() {
outBcp = System.out;
errBcp = System.err;
System.setOut(outStream);
System.setErr(errStream);
}
@AfterClass
public static void teardownClass() {
System.setOut(outBcp);
System.setErr(errBcp);
}
@Before
public void setup() throws URISyntaxException {
params.put("--name", "Stephen Mercer");
params.put("--age", "42");
params.put("--occupation", "Investigative Journalist" );
params.put("--health", "5");
params.put("--sanity", "5");
params.put("--inventory" , "42");
params.put(
"--attrs", Paths.get(getClass().getResource("/attributes.txt").toURI()).toAbsolutePath().toString()
);
params.put(
"--skills", Paths.get(getClass().getResource("/skills.txt").toURI()).toAbsolutePath().toString()
);
}
public String[] params(String... changes) {
for (int i = 0; i < changes.length - 1; i += 2) {
params.put(changes[i], changes[i+1]);
}
String[] arrayParams = new String[params.size() * 2];
int i = 0;
for (Map.Entry<String, String> e : params.entrySet()) {
arrayParams[i] = e.getKey();
i++;
arrayParams[i] = e.getValue();
i++;
}
return arrayParams;
}
public void assertOutput(ByteArrayOutputStream stream, SoftAssertions assertions, String content) {
assertions.assertThat(stream.toString()).contains(content);
stream.reset();
}
@Test
public void shouldDisplayErrorWhenUnknownParameterIsPresent() {
SoftAssertions assertions = new SoftAssertions();
Application.main(params("--foo", "0"));
assertOutput(err, assertions, "--foo");
assertions.assertAll();
}
@Test
public void shouldDisplayErrorWhenAgeIsNotValid() {
SoftAssertions assertions = new SoftAssertions();
Application.main(params("--age", "0"));
assertOutput(err, assertions, "Parameter --age should be a positive integer");
Application.main(params("--age", "-1"));
assertOutput(err, assertions, "Parameter --age should be a positive integer");
assertions.assertAll();
}
@Test
public void shouldDisplayErrorWhenInventoryIsNotValid() {
SoftAssertions assertions = new SoftAssertions();
Application.main(params("--inventory", "0"));
assertOutput(err, assertions, "Parameter --inventory should be a positive multiplier of 3");
Application.main(params("--inventory", "-1"));
assertOutput(err, assertions, "Parameter --inventory should be a positive multiplier of 3");
Application.main(params("--inventory", "2"));
assertOutput(err, assertions, "Parameter --inventory should be a positive multiplier of 3");
assertions.assertAll();
}
@Test
public void shouldDisplayErrorWhenHealthIsNotValid() {
SoftAssertions assertions = new SoftAssertions();
Application.main(params("--health", "0"));
assertOutput(err, assertions, "Parameter --health should be an integer from 1 to 10");
Application.main(params("--health", "-1"));
assertOutput(err, assertions, "Parameter --health should be an integer from 1 to 10");
Application.main(params("--health", "11"));
assertOutput(err, assertions, "Parameter --health should be an integer from 1 to 10");
assertions.assertAll();
}
@Test
public void shouldDisplayErrorWhenSanityIsNotValid() {
SoftAssertions assertions = new SoftAssertions();
Application.main(params("--sanity", "0"));
assertOutput(err, assertions, "Parameter --sanity should be an integer from 1 to 10");
Application.main(params("--sanity", "-1"));
assertOutput(err, assertions, "Parameter --sanity should be an integer from 1 to 10");
Application.main(params("--sanity", "11"));
assertOutput(err, assertions, "Parameter --sanity should be an integer from 1 to 10");
assertions.assertAll();
}
@Test
public void shouldCreateOutputFileOnCorrectCall() throws IOException {
String name = "character_" + System.currentTimeMillis() + ".html";
Path path = Paths.get(name);
Application.main(params("--out", name));
Assertions.assertThat(Files.exists(path))
.withFailMessage("File " + name + " should be created")
.isTrue();
Files.delete(path);
}
@Test
public void shouldDisplayHelpWhenRequested() {
SoftAssertions assertions = new SoftAssertions();
Application.main(params("--help", ""));
assertOutput(out, assertions, "Usage: char-gen [options]");
assertions.assertAll();
}
}
package cz.muni.fi.pb162.hw03.impl;
import cz.muni.fi.pb162.hw03.SkillOrAttribute;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.SoftAssertions;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
* @author Jakub Cechacek
*/
public class CharacterTest {
private Character.Builder builder;
private final SkillOrAttribute TEST_ATTR = new SkillOrAttribute("Intelligence", 4);
private final SkillOrAttribute TEST_SKILL = new SkillOrAttribute("App Destroying", 5);
private final List<SkillOrAttribute> TEST_ATTRS = List.of(
new SkillOrAttribute("Strength", 1), new SkillOrAttribute("Dexterity", 2), new SkillOrAttribute("Intelligence", 4)
);
private final List<SkillOrAttribute> TEST_SKILLS = List.of(
new SkillOrAttribute("App destroying", 5), new SkillOrAttribute("Hw evaluation", 4)
);
@Before
public void setup() {
builder = Character.newBuilder();
Assertions.assertThat(builder).isNotNull();
}
@Test
public void shouldCorrectlyCreateCharacterWithoutSkillsAndAttributes() {
Character character = builder
.setName("Tom Tester")
.setAge(42)
.setHealth(8)
.setSanity(7)
.setInventorySize(42)
.setOccupation("App Destroyer")
.setAttributes(new ArrayList<>(Collections.singleton(TEST_ATTR)))
.setSkills(new ArrayList<>(Collections.singleton(TEST_SKILL)))
.build();
SoftAssertions assertions = new SoftAssertions();
assertions.assertThat(character.getName()).isEqualTo("Tom Tester");
assertions.assertThat(character.getAge()).isEqualTo(42);
assertions.assertThat(character.getHealth()).isEqualTo(8);
assertions.assertThat(character.getSanity()).isEqualTo(7);
assertions.assertThat(character.getOccupation()).isEqualTo("App Destroyer");
assertions.assertThat(character.getAttributes()).containsExactly(TEST_ATTR);
assertions.assertThat(character.getSkills()).containsExactly(TEST_SKILL);
assertions.assertThatThrownBy(() -> character.getAttributes().add(null))
.withFailMessage("Returned attributes should be in unmodifiable list")
.isInstanceOf(UnsupportedOperationException.class);
assertions.assertThatThrownBy(() -> character.getSkills().add(null))
.withFailMessage("Returned attributes should be in unmodifiable list")
.isInstanceOf(UnsupportedOperationException.class);
assertions.assertAll();;
}
@Test
public void shouldCorrectlyParseAttributes() throws URISyntaxException, IOException {
Character character = builder
.readAttributes( Paths.get(getClass().getResource("/attributes_small.txt").toURI()), StandardCharsets.UTF_8)
.build();
SoftAssertions assertions = new SoftAssertions();
assertions.assertThat(character.getAttributes()).containsExactlyElementsOf(TEST_ATTRS);
assertions.assertAll();
}
@Test
public void shouldCorrectlyParseSkills() throws URISyntaxException, IOException {
Character character = builder
.readAttributes( Paths.get(getClass().getResource("/skills_small.txt").toURI()), StandardCharsets.UTF_8)
.build();
SoftAssertions assertions = new SoftAssertions();
assertions.assertThat(character.getAttributes()).hasSize(TEST_SKILLS.size() + 1);
assertions.assertThat(character.getAttributes()).containsAnyElementsOf(TEST_SKILLS);
assertions.assertThat(character.getAttributes()).anySatisfy(s -> assertions.assertThat(s.getName()).isEqualTo("Exam failing"));
assertions.assertAll();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment