Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes cache collisions on TemplateSource objects #944

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author edgar.espina
* @since 0.11.0
*/
public class ForwardingTemplateSource extends AbstractTemplateSource {
public class ForwardingTemplateSource implements TemplateSource {

/**
* The template source.
Expand Down Expand Up @@ -58,4 +58,20 @@ public long lastModified() {
return source.lastModified();
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ForwardingTemplateSource)) {
return false;
}
ForwardingTemplateSource that = (ForwardingTemplateSource) o;
return source.equals(that.source);
}

@Override
public int hashCode() {
return source.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
*/
package com.github.jknack.handlebars.io;

import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.github.jknack.handlebars.cache.TemplateCache;

import java.util.Objects;

/**
* Template source with auto-reload supports. Auto-reload is done via {@link #lastModified()}.
*
Expand All @@ -42,20 +42,19 @@ public ReloadableTemplateSource(final TemplateSource source) {
}

@Override
public int hashCode() {
return new HashCodeBuilder().append(filename()).append(lastModified()).build();
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (obj instanceof TemplateSource) {
TemplateSource that = (TemplateSource) obj;
return filename().equals(that.filename()) && lastModified() == that.lastModified();
if (!(o instanceof ReloadableTemplateSource)) {
return false;
}
return false;
ReloadableTemplateSource that = (ReloadableTemplateSource) o;
return super.equals(that) && lastModified() == that.lastModified();
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), lastModified());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import static org.apache.commons.lang3.Validate.notNull;

import java.nio.charset.Charset;
import java.util.Objects;

/**
* String implementation of {@link TemplateSource}.
*
* @author edgar.espina
* @since 0.11.0
*/
public class StringTemplateSource extends AbstractTemplateSource {
public class StringTemplateSource implements TemplateSource {

/**
* The template's content. Required.
Expand Down Expand Up @@ -70,4 +71,20 @@ public long lastModified() {
return lastModified;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof StringTemplateSource)) {
return false;
}
StringTemplateSource that = (StringTemplateSource) o;
return filename.equals(that.filename) && content.equals(that.content);
}

@Override
public int hashCode() {
return Objects.hash(filename, content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.Objects;

import com.github.jknack.handlebars.Handlebars;

Expand All @@ -38,7 +39,7 @@
* @author edgar.espina
* @since 0.11.0
*/
public class URLTemplateSource extends AbstractTemplateSource {
public class URLTemplateSource implements TemplateSource {

/**
* The resource. Required.
Expand Down Expand Up @@ -150,4 +151,20 @@ private long lastModified(final URL resource) {
}
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof URLTemplateSource)) {
return false;
}
URLTemplateSource that = (URLTemplateSource) o;
return filename.equals(that.filename) && resource.equals(that.resource);
}

@Override
public int hashCode() {
return Objects.hash(filename, resource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.jknack.handlebars.issues;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.cache.ConcurrentMapTemplateCache;
import com.github.jknack.handlebars.io.StringTemplateSource;
import com.github.jknack.handlebars.io.TemplateSource;
import com.github.jknack.handlebars.io.URLTemplateSource;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class Issue686 {

@Test
public void compileStringsWithSameHashCode() throws IOException {
Handlebars handlebars = new Handlebars().with(new ConcurrentMapTemplateCache());

// Strings "Aa" and "BB" have same hashCode
assertEquals("Aa", handlebars.compileInline("Aa").apply(new Object()));
assertEquals("BB", handlebars.compileInline("BB").apply(new Object()));
}

@Test
public void compileTemplateSourcesWithSameFilename() throws IOException {
Handlebars handlebars = new Handlebars().with(new ConcurrentMapTemplateCache());

TemplateSource stringTemplateSource = new StringTemplateSource("filename", "string");
TemplateSource urlTemplateSource = new URLTemplateSource("filename", getClass().getResource(
"/template.hbs"));

assertEquals("string", handlebars.compile(stringTemplateSource).apply(new Object()));
assertEquals("template.hbs", handlebars.compile(urlTemplateSource).apply(new Object()));
}
}