Skip to content

Commit

Permalink
Merge pull request #298 from ancho/feature/test-documents-renderer
Browse files Browse the repository at this point in the history
Add tests for DocumentsRenderer
  • Loading branch information
jonbullock committed Sep 26, 2016
2 parents ce73f32 + 6b63198 commit bfe655c
Show file tree
Hide file tree
Showing 21 changed files with 375 additions and 332 deletions.
79 changes: 40 additions & 39 deletions src/main/java/org/jbake/app/ContentStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import org.jbake.model.DocumentAttributes;
import org.jbake.model.DocumentTypes;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jbake.model.DocumentAttributes;
import org.jbake.model.DocumentTypes;

/**
*
* @author jdlee
*/
public class ContentStore {
Expand Down Expand Up @@ -79,7 +78,7 @@ public long getLimit() {
public void setLimit(int limit) {
this.limit = limit;
}

public void resetPagination() {
this.start = -1;
this.limit = -1;
Expand Down Expand Up @@ -113,48 +112,48 @@ public long getDocumentCount(String docType) {
return db.countClass(docType);
}

public List<ODocument> getDocumentStatus(String docType, String uri) {
public DocumentList getDocumentStatus(String docType, String uri) {
return query("select sha1,rendered from " + docType + " where sourceuri=?", uri);

}

public List<ODocument> getPublishedPosts() {
public DocumentList getPublishedPosts() {
return getPublishedContent("post");
}

public List<ODocument> getPublishedPostsByTag(String tag) {
public DocumentList getPublishedPostsByTag(String tag) {
return query("select * from post where status='published' and ? in tags order by date desc", tag);
}

public List<ODocument> getPublishedPages() {
public DocumentList getPublishedPages() {
return getPublishedContent("page");
}

public List<ODocument> getPublishedContent(String docType) {
public DocumentList getPublishedContent(String docType) {
String query = "select * from " + docType + " where status='published'";
if ((start >= 0) && (limit > -1)) {
query += " SKIP " + start + " LIMIT " + limit;
}
return query(query + " order by date desc");
}

public List<ODocument> getAllContent(String docType) {
public DocumentList getAllContent(String docType) {
String query = "select * from " + docType;
if ((start >= 0) && (limit > -1)) {
query += " SKIP " + start + " LIMIT " + limit;
}
return query(query + " order by date desc");
}

public List<ODocument> getAllTagsFromPublishedPosts() {
public DocumentList getAllTagsFromPublishedPosts() {
return query("select tags from post where status='published'");
}

public List<ODocument> getSignaturesForTemplates() {
public DocumentList getSignaturesForTemplates() {
return query("select sha1 from Signatures where key='templates'");
}

public List<ODocument> getUnrenderedContent(String docType) {
public DocumentList getUnrenderedContent(String docType) {
return query("select * from " + docType + " where rendered=false");
}

Expand All @@ -178,40 +177,42 @@ public void insertSignature(String currentTemplatesSignature) {
executeCommand("insert into Signatures(key,sha1) values('templates',?)", currentTemplatesSignature);
}

private List<ODocument> query(String sql) {
return db.query(new OSQLSynchQuery<ODocument>(sql));
private DocumentList query(String sql) {
List<ODocument> results = db.query(new OSQLSynchQuery<ODocument>(sql));
return DocumentList.wrap(results.iterator());
}

private List<ODocument> query(String sql, Object... args) {
return db.command(new OSQLSynchQuery<ODocument>(sql)).execute(args);

private DocumentList query(String sql, Object... args) {
List<ODocument> results = db.command(new OSQLSynchQuery<ODocument>(sql)).execute(args);
return DocumentList.wrap(results.iterator());
}

private void executeCommand(String query, Object... args) {
db.command(new OCommandSQL(query)).execute(args);
}

public Set<String> getTags() {
List<ODocument> docs = this.getAllTagsFromPublishedPosts();
Set<String> result = new HashSet<String>();
for (ODocument document : docs) {
String[] tags = DBUtil.toStringArray(document.field(Crawler.Attributes.TAGS));
Collections.addAll(result, tags);
}
return result;
}
DocumentList docs = this.getAllTagsFromPublishedPosts();
Set<String> result = new HashSet<String>();
for (Map<String, Object> document : docs) {
String[] tags = DBUtil.toStringArray(document.get(Crawler.Attributes.TAGS));
Collections.addAll(result, tags);
}
return result;
}

public Set<String> getAllTags() {
Set<String> result = new HashSet<String>();
for (String docType : DocumentTypes.getDocumentTypes()) {
List<ODocument> docs = query("select tags from " + docType + " where status='published'");
for (ODocument document : docs) {
String[] tags = DBUtil.toStringArray(document.field("tags"));
Collections.addAll(result, tags);
}
}
return result;
}
Set<String> result = new HashSet<String>();
for (String docType : DocumentTypes.getDocumentTypes()) {
DocumentList docs = query("select tags from " + docType + " where status='published'");
for (Map<String, Object> document : docs) {
String[] tags = DBUtil.toStringArray(document.get(Crawler.Attributes.TAGS));
Collections.addAll(result, tags);
}
}
return result;
}

private static void createDocType(final OSchema schema, final String doctype) {
OClass page = schema.createClass(doctype);
page.createProperty(String.valueOf(DocumentAttributes.SHA1), OType.STRING).setNotNull(true);
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/org/jbake/app/Crawler.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package org.jbake.app;


import com.orientechnologies.orient.core.record.impl.ODocument;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.io.FilenameUtils;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.app.Crawler.Attributes.Status;
import org.jbake.model.DocumentAttributes;
import org.jbake.model.DocumentStatus;
import org.jbake.model.DocumentTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.io.File.separator;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;

import com.orientechnologies.orient.core.record.impl.ODocument;
import static java.io.File.separator;

/**
* Crawls a file system looking for content.
Expand Down Expand Up @@ -128,10 +126,10 @@ private String buildHash(final File sourceFile) {
}
return sha1;
}

private String buildURI(final File sourceFile) {
String uri = FileUtil.asPath(sourceFile.getPath()).replace(FileUtil.asPath( contentPath), "");

boolean noExtensionUri = config.getBoolean(Keys.URI_NO_EXTENSION);
String noExtensionUriPrefix = config.getString(Keys.URI_NO_EXTENSION_PREFIX);
if (noExtensionUri && noExtensionUriPrefix != null && noExtensionUriPrefix.length() > 0) {
Expand All @@ -142,7 +140,7 @@ private String buildURI(final File sourceFile) {
} else {
uri = uri.substring(0, uri.lastIndexOf(".")) + config.getString(Keys.OUTPUT_EXTENSION);
}

// strip off leading / to enable generating non-root based sites
if (uri.startsWith("/")) {
uri = uri.substring(1, uri.length());
Expand Down Expand Up @@ -173,11 +171,11 @@ private void crawlSourceFile(final File sourceFile, final String sha1, final Str
}
}
}

if (config.getBoolean(Keys.URI_NO_EXTENSION)) {
fileContents.put(Attributes.NO_EXTENSION_URI, uri.replace("/index.html", "/"));
}

ODocument doc = new ODocument(documentType);
doc.fields(fileContents);
boolean cached = fileContents.get(DocumentAttributes.CACHED) != null ? Boolean.valueOf((String)fileContents.get(DocumentAttributes.CACHED)):true;
Expand All @@ -204,11 +202,11 @@ public String getPathToRoot(File sourceFile) {
}

private DocumentStatus findDocumentStatus(String docType, String uri, String sha1) {
List<ODocument> match = db.getDocumentStatus(docType, uri);
DocumentList match = db.getDocumentStatus(docType, uri);
if (!match.isEmpty()) {
ODocument entries = match.get(0);
String oldHash = entries.field(String.valueOf(DocumentAttributes.SHA1));
if (!(oldHash.equals(sha1)) || Boolean.FALSE.equals(entries.field(String.valueOf(DocumentAttributes.RENDERED)))) {
Map entries = match.get(0);
String oldHash = (String) entries.get(String.valueOf(DocumentAttributes.SHA1));
if (!(oldHash.equals(sha1)) || Boolean.FALSE.equals(entries.get(String.valueOf(DocumentAttributes.RENDERED)))) {
return DocumentStatus.UPDATED;
} else {
return DocumentStatus.IDENTICAL;
Expand Down
29 changes: 0 additions & 29 deletions src/main/java/org/jbake/app/DocumentIterator.java

This file was deleted.

24 changes: 12 additions & 12 deletions src/main/java/org/jbake/app/Oven.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package org.jbake.app;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.model.DocumentAttributes;
import org.jbake.model.DocumentTypes;
import org.jbake.render.RenderingTool;
import org.jbake.template.ModelExtractorsDocumentTypeListener;
import org.jbake.template.RenderingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
Expand All @@ -10,16 +20,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.orientechnologies.orient.core.record.impl.ODocument;
import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.model.DocumentTypes;
import org.jbake.render.RenderingTool;
import org.jbake.template.ModelExtractorsDocumentTypeListener;
import org.jbake.template.RenderingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* All the baking happens in the Oven!
*
Expand Down Expand Up @@ -196,15 +196,15 @@ private void updateDocTypesFromConfiguration() {
private void clearCacheIfNeeded(final ContentStore db) {
boolean needed = isClearCache;
if (!needed) {
List<ODocument> docs = db.getSignaturesForTemplates();
DocumentList docs = db.getSignaturesForTemplates();
String currentTemplatesSignature;
try {
currentTemplatesSignature = FileUtil.sha1(templatesPath);
} catch (Exception e) {
currentTemplatesSignature = "";
}
if (!docs.isEmpty()) {
String sha1 = docs.get(0).field("sha1");
String sha1 = (String) docs.get(0).get(String.valueOf(DocumentAttributes.SHA1));
needed = !sha1.equals(currentTemplatesSignature);
if (needed) {
db.updateSignatures(currentTemplatesSignature);
Expand Down
59 changes: 29 additions & 30 deletions src/main/java/org/jbake/render/DocumentsRenderer.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
package org.jbake.render;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ContentStore;
import org.jbake.app.DocumentIterator;
import org.jbake.app.DocumentList;
import org.jbake.app.Renderer;
import org.jbake.model.DocumentTypes;
import org.jbake.template.RenderingException;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class DocumentsRenderer implements RenderingTool {

@Override
public int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException {
int renderedCount = 0;
final List<String> errors = new LinkedList<String>();
for (String docType : DocumentTypes.getDocumentTypes()) {
DocumentIterator pagesIt = new DocumentIterator(db.getUnrenderedContent(docType).iterator());
while (pagesIt.hasNext()) {
Map<String, Object> page = pagesIt.next();
try {
renderer.render(page);
renderedCount++;
} catch (Exception e) {
errors.add(e.getMessage());
}
}
}
@Override
public int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException {
int renderedCount = 0;
final List<String> errors = new LinkedList<String>();
for (String docType : DocumentTypes.getDocumentTypes()) {
DocumentList documentList = db.getUnrenderedContent(docType);
for (Map<String, Object> page : documentList) {
try {
renderer.render(page);
renderedCount++;
} catch (Exception e) {
errors.add(e.getMessage());
}
}
}
if (!errors.isEmpty()) {
StringBuilder sb = new StringBuilder();
sb.append("Failed to render documents. Cause(s):");
for(String error: errors) {
sb.append("\n" + error);
}
throw new RenderingException(sb.toString());
StringBuilder sb = new StringBuilder();
sb.append("Failed to render documents. Cause(s):");
for (String error : errors) {
sb.append("\n" + error);
}
throw new RenderingException(sb.toString());
} else {
return renderedCount;
return renderedCount;
}
}
}
}
Loading

0 comments on commit bfe655c

Please sign in to comment.