1 package de.todesbaum.jsite.application.validation;
3 import static de.todesbaum.jsite.application.validation.ProjectValidator.validateProject;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.empty;
6 import static org.hamcrest.Matchers.hasItem;
7 import static org.hamcrest.Matchers.not;
10 import java.nio.file.Files;
11 import java.nio.file.StandardOpenOption;
13 import de.todesbaum.jsite.application.Project;
14 import org.junit.Before;
15 import org.junit.Rule;
16 import org.junit.Test;
17 import org.junit.rules.TemporaryFolder;
20 * Unit test for {@link ProjectValidator}.
22 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
24 public class ProjectValidatorTest {
27 public final TemporaryFolder temporaryFolder = new TemporaryFolder();
29 private Project project = new Project();
32 public void setupProject() throws Exception {
33 project.setLocalPath(temporaryFolder.getRoot().getPath());
34 project.setPath("temp");
35 project.setIndexFile("index.html");
36 temporaryFolder.newFile("index.html").createNewFile();
40 public void completelyValidProjectDoesNotGenerateAnyIssues() {
41 CheckReport checkReport = validateProject(project);
42 assertThat(checkReport.getIssues(), empty());
46 public void missingLocalPathResultsInError() {
47 project.setLocalPath(null);
48 CheckReport checkReport = validateProject(project);
49 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-local-path", true)));
53 public void emptyLocalPathResultsInError() {
54 project.setLocalPath(" ");
55 CheckReport checkReport = validateProject(project);
56 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-local-path", true)));
60 public void missingPathResultsInError() {
61 project.setPath(null);
62 CheckReport checkReport = validateProject(project);
63 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-path", true)));
67 public void emptyPathResultsInError() {
69 CheckReport checkReport = validateProject(project);
70 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-path", true)));
74 public void missingIndexFileResultsInWarning() {
75 project.setIndexFile(null);
76 CheckReport checkReport = validateProject(project);
77 assertThat(checkReport.getIssues(), hasItem(new Issue("warning.empty-index", false)));
81 public void emptyIndexFileResultsInWarning() {
82 project.setIndexFile("");
83 CheckReport checkReport = validateProject(project);
84 assertThat(checkReport.getIssues(), hasItem(new Issue("warning.empty-index", false)));
88 public void settingIndexFileWithoutExistingFileResultsInWarning() {
89 project.setIndexFile("test.html");
90 CheckReport checkReport = validateProject(project);
91 assertThat(checkReport.getIssues(), hasItem(new Issue("error.index-missing", true)));
95 public void usingNonHtmlMimeTypeForIndexFileResultsInWarning() {
96 project.getFileOption("index.html").setMimeType("text/plain");
97 CheckReport checkReport = validateProject(project);
98 assertThat(checkReport.getIssues(), hasItem(new Issue("warning.index-not-html", false)));
102 public void notInsertingTheIndexFileAnyFileResultsInError() {
103 project.getFileOption("index.html").setInsert(false);
104 project.getFileOption("index.html").setInsertRedirect(false);
105 CheckReport checkReport = validateProject(project);
106 assertThat(checkReport.getIssues(), hasItem(new Issue("error.index-not-inserted", true)));
110 public void insertingARedirectWithRedirectUrlResultsInError() {
111 project.getFileOption("index.html").setInsert(false);
112 CheckReport checkReport = validateProject(project);
113 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-custom-key", true, "index.html")));
117 public void insertingARedirectEmptyKeyUrlResultsInError() {
118 project.getFileOption("index.html").setInsert(false);
119 project.getFileOption("index.html").setCustomKey(null);
120 CheckReport checkReport = validateProject(project);
121 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-custom-key", true, "index.html")));
125 public void insertingARedirectToCHKUrlResultsInError() {
126 project.getFileOption("index.html").setInsert(false);
127 project.getFileOption("index.html").setCustomKey("CHK@");
128 CheckReport checkReport = validateProject(project);
129 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-custom-key", true, "index.html")));
133 public void notInsertingAnyFileResultsInError() {
134 project.getFileOption("index.html").setInsert(false);
135 project.getFileOption("index.html").setInsertRedirect(false);
136 CheckReport checkReport = validateProject(project);
137 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-files-to-insert", true)));
141 public void filesWithDuplicateTargetNamesResultInAnError() {
142 project.getFileOption("second.html").setChangedName("index.html");
143 CheckReport checkReport = validateProject(project);
144 assertThat(checkReport.getIssues(), hasItem(new Issue("error.duplicate-file", true, "index.html")));
148 public void filesWithLessThenOrEqualTo2MegabytesDoNotResultInWarning() throws Exception {
149 File largeFile = new File(temporaryFolder.getRoot(), "large.html");
150 Files.write(largeFile.toPath(), new byte[2 * 1048576], StandardOpenOption.CREATE_NEW);
151 CheckReport checkReport = validateProject(project);
152 assertThat(checkReport.getIssues(), not(hasItem(new Issue("warning.site-larger-than-2-mib", false))));
156 public void filesWithSizeOfMoreThan2MegabytesDoNotResultInWarning() throws Exception {
157 File largeFile = new File(temporaryFolder.getRoot(), "large.html");
158 Files.write(largeFile.toPath(), new byte[2 * 1048576 + 1], StandardOpenOption.CREATE_NEW);
159 CheckReport checkReport = validateProject(project);
160 assertThat(checkReport.getIssues(), hasItem(new Issue("warning.site-larger-than-2-mib", false)));
164 public void filesAreCorrectlyIgnoreForSizeCheckIfNotInserted() throws Exception {
165 File largeFile = new File(temporaryFolder.getRoot(), "large.html");
166 Files.write(largeFile.toPath(), new byte[2 * 1048576 + 1], StandardOpenOption.CREATE_NEW);
167 project.getFileOption("large.html").setInsert(false);
168 CheckReport checkReport = validateProject(project);
169 assertThat(checkReport.getIssues(), not(hasItem(new Issue("warning.site-larger-than-2-mib", false))));