BasicResourceFactoryTest.java
/***************************************************************************
Copyright 2015 Emily Estes
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 net.metanotion.simpletemplate;
import java.util.HashMap;
import java.util.Map;
import net.metanotion.scripting.ObjectServer;
import net.metanotion.web.RequestObject;
public final class BasicResourceFactoryTest {
private static void assertTrue(final boolean result) {
if(!result) { throw new AssertionError("Expected true"); }
}
public static final class MapResources implements ResourceFactory {
private final Map<String,CResource> resources;
public MapResources(final Map<String,CResource> resources) { this.resources = resources; }
@Override public Resource get(final String urn) {
final CResource r = this.resources.get(urn);
if(r==null) {
throw new RuntimeException(urn + " not found");
} else {
return r.get(urn);
}
}
}
public static final class CResource implements Resource {
public final String c;
public final String urn;
public CResource(final String c) { this(c, null); }
public CResource(final String c, final String urn) {
this.c = c;
this.urn = urn;
}
public Resource get(final String urn) { return new CResource(this.c, urn); }
@Override public Object skin(final ObjectServer os, final RequestObject ro) { return (this.c + ":" + this.urn); }
}
public static void main(final String[] args) throws Exception {
final HashMap<String,CResource> set1 = new HashMap<>();
set1.put("/pre/test", new CResource("A"));
final MapResources map1 = new MapResources(set1);
set1.put("/a_test", new CResource("A"));
final PrefixedResources prefixed = new PrefixedResources("/pre", map1);
final Resource res1 = prefixed.get("/test");
assertTrue("A:/pre/test".equals(res1.skin(null, null)));
try {
prefixed.get("/test1");
throw new AssertionError("That resource was not in the map.");
} catch (final RuntimeException ex) {
// Expected.
}
final HashMap<String,CResource> set2 = new HashMap<>();
set2.put("/test2", new CResource("B"));
final MapResources map2 = new MapResources(set2);
final MountPointResources mountPt = new MountPointResources(map1, "/pre", map2);
final Resource res2 = mountPt.get("/test2");
assertTrue("B:/test2".equals(res2.skin(null, null)));
try {
mountPt.get("/pre/test");
throw new AssertionError("That resource was not in the map.");
} catch (final RuntimeException ex) {
// Expected.
}
final Resource res3 = mountPt.get("/pre/a_test");
assertTrue("A:/a_test".equals(res3.skin(null, null)));
final ConstantResourceFactory crf = new ConstantResourceFactory("Test");
assertTrue("Test".equals(crf.get("/test1").skin(null, null)));
assertTrue("Test".equals(crf.get("a.b.caweft1").skin(null, null)));
assertTrue("Test".equals(crf.get("http://127.0.0.1/!#$!").skin(null, null)));
try {
NullTemplateHelper.INSTANCE.get("/test");
throw new AssertionError("Should fail since that's what NullTemplateHelper always does.");
} catch (final RuntimeException ex) {
assertTrue("Resource '/test' could not be found.".equals(ex.getMessage()));
}
try {
NullTemplateHelper.INSTANCE.skin(null, null);
throw new AssertionError("Should fail since that's what NullTemplateHelper always does.");
} catch (final RuntimeException ex) {
assertTrue("Bad Resource".equals(ex.getMessage()));
}
final ConstantDynamicResourceFactory cdrf = new ConstantDynamicResourceFactory(new CResource("A", "B"));
final Resource res4 = cdrf.get("/test");
assertTrue("A:B".equals(res4.skin(null, null)));
set2.put("/pre/test", new CResource("B"));
final ChainedResourceFactory chained = new ChainedResourceFactory(map1, map2);
final Resource res5 = chained.get("/pre/test");
assertTrue("A:/pre/test".equals(res5.skin(null, null)));
final Resource res6 = chained.get("/test2");
assertTrue("B:/test2".equals(res6.skin(null, null)));
try {
chained.get("/missing");
throw new AssertionError("Not in either factory");
} catch (final RuntimeException ex) {
// Expected.
}
final DefaultServer def = new DefaultServer(map1);
assertTrue("A:/pre/test".equals(def.get("/pre/test")));
assertTrue("A:/a_test".equals(def.get("/a_test")));
try {
def.get("/missing");
throw new AssertionError("Bad url factory");
} catch (final RuntimeException ex) {
// Expected.
}
}
}