Skip to content

Commit fe77349

Browse files
committed
Some new unit tests
1 parent cc0036a commit fe77349

File tree

7 files changed

+245
-19
lines changed

7 files changed

+245
-19
lines changed

handlebars/src/main/java/com/github/jknack/handlebars/cache/ConcurrentMapCache.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package com.github.jknack.handlebars.cache;
1919

20+
import static org.apache.commons.lang3.Validate.notNull;
21+
2022
import java.util.concurrent.ConcurrentHashMap;
2123
import java.util.concurrent.ConcurrentMap;
2224

@@ -31,11 +33,6 @@
3133
*/
3234
public class ConcurrentMapCache implements TemplateCache {
3335

34-
/**
35-
* NULL object.
36-
*/
37-
private static final Object NULL = new Object();
38-
3936
/**
4037
* The object storage.
4138
*/
@@ -54,12 +51,13 @@ public void evict(final Object key) {
5451

5552
@Override
5653
public Template get(final Object key) {
57-
Template value = this.store.get(key);
58-
return value == NULL ? null : value;
54+
return store.get(key);
5955
}
6056

6157
@Override
6258
public void put(final Object key, final Template template) {
59+
notNull(key, "The key is required.");
60+
notNull(template, "The template is required.");
6361
store.put(key, template);
6462
}
6563

handlebars/src/main/java/com/github/jknack/handlebars/helper/EachHelper.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,4 @@ private CharSequence iterableContext(final Iterable<Object> context, final Optio
124124
return buffer.toString();
125125
}
126126

127-
/**
128-
* Retrieve the next element available.
129-
*
130-
* @param parent The parent context.
131-
* @param iterator The element iterator.
132-
* @param index The nth position of this element. Zero base.
133-
* @return The next element available.
134-
*/
135-
protected Object next(final Context parent, final Iterator<Object> iterator,
136-
final int index) {
137-
return iterator.next();
138-
}
139127
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.jknack.handlebars;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
7+
import org.junit.Test;
8+
9+
public class EmptyTemplateTest {
10+
11+
@Test
12+
public void text() {
13+
assertEquals("", Template.EMPTY.text());
14+
}
15+
16+
@Test
17+
public void apply() throws IOException {
18+
assertEquals("", Template.EMPTY.apply((Object) null));
19+
assertEquals("", Template.EMPTY.apply((Context) null));
20+
}
21+
22+
@Test
23+
public void applyWithWriter() throws IOException {
24+
Template.EMPTY.apply((Object) null, null);
25+
Template.EMPTY.apply((Context) null, null);
26+
}
27+
28+
@Test
29+
public void toJs() throws IOException {
30+
assertEquals("", Template.EMPTY.toJavaScript());
31+
}
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.jknack.handlebars.cache;
2+
3+
import static org.easymock.EasyMock.createMock;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertNull;
6+
7+
import org.junit.Test;
8+
9+
import com.github.jknack.handlebars.Template;
10+
import com.github.jknack.handlebars.TemplateCache;
11+
12+
public class ConcurrentMapCacheTest {
13+
14+
@Test
15+
public void putAndRemove() {
16+
TemplateCache cache = new ConcurrentMapCache();
17+
Template template = createMock(Template.class);
18+
String key = "key";
19+
cache.put(key, template);
20+
assertEquals(template, cache.get(key));
21+
cache.evict(key);
22+
assertNull(cache.get(key));
23+
}
24+
25+
@Test
26+
public void clear() {
27+
TemplateCache cache = new ConcurrentMapCache();
28+
Template template = createMock(Template.class);
29+
String key = "key";
30+
cache.put(key, template);
31+
assertEquals(template, cache.get(key));
32+
cache.clear();
33+
assertNull(cache.get(key));
34+
}
35+
}

handlebars/src/test/java/com/github/jknack/handlebars/io/ClasspathLocatorTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void locate() throws IOException {
4141
TemplateLoader locator = new ClassPathTemplateLoader();
4242
Reader reader = locator.load(URI.create("template"));
4343
assertNotNull(reader);
44+
reader.close();
4445
}
4546

4647
@Test
@@ -49,6 +50,7 @@ public void subFolder() throws IOException {
4950
locator.setSuffix(".yml");
5051
Reader reader = locator.load(URI.create("mustache/specs/comments"));
5152
assertNotNull(reader);
53+
reader.close();
5254
}
5355

5456
@Test
@@ -57,6 +59,7 @@ public void subFolderwithDashAtBeginning() throws IOException {
5759
locator.setSuffix(".yml");
5860
Reader reader = locator.load(URI.create("/mustache/specs/comments"));
5961
assertNotNull(reader);
62+
reader.close();
6063
}
6164

6265
@Test(expected = FileNotFoundException.class)
@@ -70,12 +73,14 @@ public void setBasePath() throws IOException {
7073
TemplateLoader locator = new ClassPathTemplateLoader("/mustache/specs", ".yml");
7174
Reader reader = locator.load(URI.create("comments"));
7275
assertNotNull(reader);
76+
reader.close();
7377
}
7478

7579
@Test
7680
public void setBasePathWithDashDash() throws IOException {
7781
TemplateLoader locator = new ClassPathTemplateLoader("/mustache/specs/", ".yml");
7882
Reader reader = locator.load(URI.create("comments"));
7983
assertNotNull(reader);
84+
reader.close();
8085
}
8186
}

handlebars/src/test/java/com/github/jknack/handlebars/io/FileLocatorTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public void locate() throws IOException {
4343
new FileTemplateLoader(new File("src/test/resources"));
4444
Reader reader = locator.load(URI.create("template"));
4545
assertNotNull(reader);
46+
reader.close();
4647
}
4748

4849
@Test
@@ -51,6 +52,7 @@ public void subFolder() throws IOException {
5152
new FileTemplateLoader(new File("src/test/resources"), ".yml");
5253
Reader reader = locator.load(URI.create("mustache/specs/comments"));
5354
assertNotNull(reader);
55+
reader.close();
5456
}
5557

5658
@Test
@@ -59,6 +61,7 @@ public void subFolderwithDashAtBeginning() throws IOException {
5961
new FileTemplateLoader(new File("src/test/resources"), ".yml");
6062
Reader reader = locator.load(URI.create("mustache/specs/comments"));
6163
assertNotNull(reader);
64+
reader.close();
6265
}
6366

6467
@Test(expected = FileNotFoundException.class)
@@ -74,6 +77,7 @@ public void setBasePath() throws IOException {
7477
new FileTemplateLoader(new File("src/test/resources/mustache/specs"), ".yml");
7578
Reader reader = locator.load(URI.create("comments"));
7679
assertNotNull(reader);
80+
reader.close();
7781
}
7882

7983
@Test
@@ -82,6 +86,7 @@ public void setBasePathWithDash() throws IOException {
8286
new FileTemplateLoader(new File("src/test/resources/mustache/specs/"), ".yml");
8387
Reader reader = locator.load(URI.create("comments"));
8488
assertNotNull(reader);
89+
reader.close();
8590
}
8691

8792
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
* Copyright (c) 2012 Edgar Espina
3+
*
4+
* This file is part of Handlebars.java.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.github.jknack.handlebars.io;
19+
20+
import static org.easymock.EasyMock.createMock;
21+
import static org.easymock.EasyMock.expect;
22+
import static org.easymock.EasyMock.expectLastCall;
23+
import static org.easymock.EasyMock.replay;
24+
import static org.easymock.EasyMock.verify;
25+
import static org.junit.Assert.assertNotNull;
26+
27+
import java.io.FileNotFoundException;
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.io.Reader;
31+
import java.net.URI;
32+
33+
import javax.servlet.ServletContext;
34+
35+
import org.apache.commons.io.IOUtils;
36+
import org.junit.Test;
37+
38+
import com.github.jknack.handlebars.TemplateLoader;
39+
40+
public class ServletContextTemplateLoaderTest {
41+
42+
@Test
43+
public void defaultLoad() throws IOException {
44+
InputStream is = createMock(InputStream.class);
45+
is.close();
46+
expectLastCall();
47+
48+
ServletContext servletContext = createMock(ServletContext.class);
49+
expect(servletContext.getResourceAsStream("/template.hbs")).andReturn(is);
50+
51+
replay(servletContext, is);
52+
53+
TemplateLoader locator = new ServletContextTemplateLoader(servletContext);
54+
Reader reader = locator.load(URI.create("template"));
55+
assertNotNull(reader);
56+
IOUtils.closeQuietly(reader);
57+
58+
verify(servletContext, is);
59+
}
60+
61+
@Test
62+
public void subFolder() throws IOException {
63+
InputStream is = createMock(InputStream.class);
64+
is.close();
65+
expectLastCall();
66+
67+
ServletContext servletContext = createMock(ServletContext.class);
68+
expect(servletContext.getResourceAsStream("/mustache/specs/comments.yml")).andReturn(is);
69+
70+
replay(servletContext, is);
71+
72+
TemplateLoader locator = new ServletContextTemplateLoader(servletContext);
73+
74+
locator.setSuffix(".yml");
75+
Reader reader = locator.load(URI.create("mustache/specs/comments"));
76+
assertNotNull(reader);
77+
78+
IOUtils.closeQuietly(reader);
79+
80+
verify(servletContext, is);
81+
82+
}
83+
84+
@Test
85+
public void subFolderwithDashAtBeginning() throws IOException {
86+
InputStream is = createMock(InputStream.class);
87+
is.close();
88+
expectLastCall();
89+
90+
ServletContext servletContext = createMock(ServletContext.class);
91+
expect(servletContext.getResourceAsStream("/mustache/specs/comments.yml")).andReturn(is);
92+
93+
replay(servletContext, is);
94+
95+
TemplateLoader locator = new ServletContextTemplateLoader(servletContext);
96+
locator.setSuffix(".yml");
97+
Reader reader = locator.load(URI.create("/mustache/specs/comments"));
98+
assertNotNull(reader);
99+
100+
IOUtils.closeQuietly(reader);
101+
102+
verify(servletContext, is);
103+
}
104+
105+
@Test(expected = FileNotFoundException.class)
106+
public void failLocate() throws IOException {
107+
ServletContext servletContext = createMock(ServletContext.class);
108+
expect(servletContext.getResourceAsStream("/notExist.hbs")).andReturn(null);
109+
110+
replay(servletContext);
111+
112+
TemplateLoader locator = new ServletContextTemplateLoader(servletContext);
113+
114+
locator.load(URI.create("notExist"));
115+
116+
verify(servletContext);
117+
118+
}
119+
120+
@Test
121+
public void setBasePath() throws IOException {
122+
InputStream is = createMock(InputStream.class);
123+
is.close();
124+
expectLastCall();
125+
126+
ServletContext servletContext = createMock(ServletContext.class);
127+
expect(servletContext.getResourceAsStream("/mustache/specs/comments.yml")).andReturn(is);
128+
129+
replay(servletContext, is);
130+
131+
TemplateLoader locator = new ServletContextTemplateLoader(servletContext, "/mustache/specs",
132+
".yml");
133+
134+
Reader reader = locator.load(URI.create("comments"));
135+
assertNotNull(reader);
136+
137+
IOUtils.closeQuietly(reader);
138+
139+
verify(servletContext, is);
140+
}
141+
142+
@Test
143+
public void setBasePathWithDashDash() throws IOException {
144+
InputStream is = createMock(InputStream.class);
145+
is.close();
146+
expectLastCall();
147+
148+
ServletContext servletContext = createMock(ServletContext.class);
149+
expect(servletContext.getResourceAsStream("/mustache/specs/comments.yml")).andReturn(is);
150+
151+
replay(servletContext, is);
152+
153+
TemplateLoader locator = new ServletContextTemplateLoader(servletContext, "/mustache/specs",
154+
".yml");
155+
156+
Reader reader = locator.load(URI.create("comments"));
157+
assertNotNull(reader);
158+
159+
IOUtils.closeQuietly(reader);
160+
161+
verify(servletContext, is);
162+
}
163+
}

0 commit comments

Comments
 (0)