From c29e7f0679f60f718eab8d190c624046bacb97a4 Mon Sep 17 00:00:00 2001 From: yohannrub <2856063+yohannrub@users.noreply.github.com> Date: Fri, 28 Jan 2022 21:51:38 +0100 Subject: [PATCH 1/2] Add currently failing tests for issue jknack/handlebars.java#686 --- .../jknack/handlebars/issues/Issue686.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 handlebars/src/test/java/com/github/jknack/handlebars/issues/Issue686.java diff --git a/handlebars/src/test/java/com/github/jknack/handlebars/issues/Issue686.java b/handlebars/src/test/java/com/github/jknack/handlebars/issues/Issue686.java new file mode 100644 index 000000000..8ab69c477 --- /dev/null +++ b/handlebars/src/test/java/com/github/jknack/handlebars/issues/Issue686.java @@ -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())); + } +} From c3fe622d223724c382f329967b3e28a0146e8643 Mon Sep 17 00:00:00 2001 From: yohannrub <2856063+yohannrub@users.noreply.github.com> Date: Fri, 28 Jan 2022 21:53:48 +0100 Subject: [PATCH 2/2] Add proper equals / hashCode implementations for TemplateSource classes --- .../handlebars/io/AbstractTemplateSource.java | 52 ------------------- .../io/ForwardingTemplateSource.java | 18 ++++++- .../io/ReloadableTemplateSource.java | 25 +++++---- .../handlebars/io/StringTemplateSource.java | 19 ++++++- .../handlebars/io/URLTemplateSource.java | 19 ++++++- 5 files changed, 65 insertions(+), 68 deletions(-) delete mode 100644 handlebars/src/main/java/com/github/jknack/handlebars/io/AbstractTemplateSource.java diff --git a/handlebars/src/main/java/com/github/jknack/handlebars/io/AbstractTemplateSource.java b/handlebars/src/main/java/com/github/jknack/handlebars/io/AbstractTemplateSource.java deleted file mode 100644 index 90b4b5d99..000000000 --- a/handlebars/src/main/java/com/github/jknack/handlebars/io/AbstractTemplateSource.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Copyright (c) 2012-2015 Edgar Espina - * - * This file is part of Handlebars.java. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.github.jknack.handlebars.io; - -import org.apache.commons.lang3.builder.HashCodeBuilder; - -/** - * Base class for {@link TemplateSource} with default implementation of {@link #equals(Object)} and - * {@link #hashCode()}. - * - * @author edgar.espina - * @since 0.11.0 - */ -public abstract class AbstractTemplateSource implements TemplateSource { - - @Override - public int hashCode() { - return new HashCodeBuilder().append(filename()).build(); - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj instanceof TemplateSource) { - TemplateSource that = (TemplateSource) obj; - return filename().equals(that.filename()); - } - return false; - } - - @Override - public String toString() { - return filename(); - } -} diff --git a/handlebars/src/main/java/com/github/jknack/handlebars/io/ForwardingTemplateSource.java b/handlebars/src/main/java/com/github/jknack/handlebars/io/ForwardingTemplateSource.java index 0a67c9603..69cbff8ac 100644 --- a/handlebars/src/main/java/com/github/jknack/handlebars/io/ForwardingTemplateSource.java +++ b/handlebars/src/main/java/com/github/jknack/handlebars/io/ForwardingTemplateSource.java @@ -28,7 +28,7 @@ * @author edgar.espina * @since 0.11.0 */ -public class ForwardingTemplateSource extends AbstractTemplateSource { +public class ForwardingTemplateSource implements TemplateSource { /** * The template source. @@ -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(); + } } diff --git a/handlebars/src/main/java/com/github/jknack/handlebars/io/ReloadableTemplateSource.java b/handlebars/src/main/java/com/github/jknack/handlebars/io/ReloadableTemplateSource.java index 9a21d9989..f98216119 100644 --- a/handlebars/src/main/java/com/github/jknack/handlebars/io/ReloadableTemplateSource.java +++ b/handlebars/src/main/java/com/github/jknack/handlebars/io/ReloadableTemplateSource.java @@ -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()}. * @@ -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()); + } } diff --git a/handlebars/src/main/java/com/github/jknack/handlebars/io/StringTemplateSource.java b/handlebars/src/main/java/com/github/jknack/handlebars/io/StringTemplateSource.java index 916ee01eb..96fa83360 100644 --- a/handlebars/src/main/java/com/github/jknack/handlebars/io/StringTemplateSource.java +++ b/handlebars/src/main/java/com/github/jknack/handlebars/io/StringTemplateSource.java @@ -20,6 +20,7 @@ import static org.apache.commons.lang3.Validate.notNull; import java.nio.charset.Charset; +import java.util.Objects; /** * String implementation of {@link TemplateSource}. @@ -27,7 +28,7 @@ * @author edgar.espina * @since 0.11.0 */ -public class StringTemplateSource extends AbstractTemplateSource { +public class StringTemplateSource implements TemplateSource { /** * The template's content. Required. @@ -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); + } } diff --git a/handlebars/src/main/java/com/github/jknack/handlebars/io/URLTemplateSource.java b/handlebars/src/main/java/com/github/jknack/handlebars/io/URLTemplateSource.java index 7c9e82222..12b3230a2 100644 --- a/handlebars/src/main/java/com/github/jknack/handlebars/io/URLTemplateSource.java +++ b/handlebars/src/main/java/com/github/jknack/handlebars/io/URLTemplateSource.java @@ -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; @@ -38,7 +39,7 @@ * @author edgar.espina * @since 0.11.0 */ -public class URLTemplateSource extends AbstractTemplateSource { +public class URLTemplateSource implements TemplateSource { /** * The resource. Required. @@ -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); + } }