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 usingHtmlMimeTypeWithCharsetForIndexFileDoesNotResultInWarning() {
103 project.getFileOption("index.html").setMimeType("text/html; charset=UTF-8");
104 CheckReport checkReport = validateProject(project);
105 assertThat(checkReport.getIssues(), not(hasItem(new Issue("warning.index-not-html", false))));
109 public void notInsertingTheIndexFileAnyFileResultsInError() {
110 project.getFileOption("index.html").setInsert(false);
111 project.getFileOption("index.html").setInsertRedirect(false);
112 CheckReport checkReport = validateProject(project);
113 assertThat(checkReport.getIssues(), hasItem(new Issue("error.index-not-inserted", true)));
117 public void insertingARedirectWithRedirectUrlResultsInError() {
118 project.getFileOption("index.html").setInsert(false);
119 CheckReport checkReport = validateProject(project);
120 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-custom-key", true, "index.html")));
124 public void insertingARedirectEmptyKeyUrlResultsInError() {
125 project.getFileOption("index.html").setInsert(false);
126 project.getFileOption("index.html").setCustomKey(null);
127 CheckReport checkReport = validateProject(project);
128 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-custom-key", true, "index.html")));
132 public void insertingARedirectToCHKUrlResultsInError() {
133 project.getFileOption("index.html").setInsert(false);
134 project.getFileOption("index.html").setCustomKey("CHK@");
135 CheckReport checkReport = validateProject(project);
136 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-custom-key", true, "index.html")));
140 public void notInsertingAnyFileResultsInError() {
141 project.getFileOption("index.html").setInsert(false);
142 project.getFileOption("index.html").setInsertRedirect(false);
143 CheckReport checkReport = validateProject(project);
144 assertThat(checkReport.getIssues(), hasItem(new Issue("error.no-files-to-insert", true)));
148 public void filesWithDuplicateTargetNamesResultInAnError() {
149 project.getFileOption("second.html").setChangedName("index.html");
150 CheckReport checkReport = validateProject(project);
151 assertThat(checkReport.getIssues(), hasItem(new Issue("error.duplicate-file", true, "index.html")));
155 public void filesWithLessThenOrEqualTo2MegabytesDoNotResultInWarning() throws Exception {
156 File largeFile = new File(temporaryFolder.getRoot(), "large.html");
157 Files.write(largeFile.toPath(), new byte[2 * 1048576], StandardOpenOption.CREATE_NEW);
158 CheckReport checkReport = validateProject(project);
159 assertThat(checkReport.getIssues(), not(hasItem(new Issue("warning.site-larger-than-2-mib", false))));
163 public void filesWithSizeOfMoreThan2MegabytesDoNotResultInWarning() throws Exception {
164 File largeFile = new File(temporaryFolder.getRoot(), "large.html");
165 Files.write(largeFile.toPath(), new byte[2 * 1048576 + 1], StandardOpenOption.CREATE_NEW);
166 CheckReport checkReport = validateProject(project);
167 assertThat(checkReport.getIssues(), hasItem(new Issue("warning.site-larger-than-2-mib", false)));
171 public void filesAreCorrectlyIgnoreForSizeCheckIfNotInserted() throws Exception {
172 File largeFile = new File(temporaryFolder.getRoot(), "large.html");
173 Files.write(largeFile.toPath(), new byte[2 * 1048576 + 1], StandardOpenOption.CREATE_NEW);
174 project.getFileOption("large.html").setInsert(false);
175 CheckReport checkReport = validateProject(project);
176 assertThat(checkReport.getIssues(), not(hasItem(new Issue("warning.site-larger-than-2-mib", false))));