1 15 package org.apache.hivemind.util; 16 17 import java.net.URL ; 18 import java.util.Locale ; 19 20 import javax.servlet.ServletContext ; 21 22 import org.apache.hivemind.Resource; 23 import org.apache.hivemind.test.HiveMindTestCase; 24 25 31 public class TestContextResource extends HiveMindTestCase 32 { 33 private ServletContext newContext() 34 { 35 return (ServletContext ) newMock(ServletContext .class); 36 } 37 38 public void testConstructor() 39 { 40 ServletContext context = newContext(); 41 42 replayControls(); 43 44 ContextResource r = new ContextResource(context, "/foo/bar/baz_en.html", Locale.ENGLISH); 45 46 assertEquals("context:/foo/bar/baz_en.html", r.toString()); 47 48 assertEquals("/foo/bar/baz_en.html", r.getPath()); 49 50 assertEquals("baz_en.html", r.getName()); 51 52 assertEquals(Locale.ENGLISH, r.getLocale()); 53 54 verifyControls(); 55 } 56 57 public void testLocalizationExists() throws Exception 58 { 59 ServletContext context = newContext(); 60 61 context.getResource("/foo/bar/baz_en.html"); 62 setReturnValue(context, new URL ("http://foo.com")); 63 64 replayControls(); 65 66 ContextResource r1 = new ContextResource(context, "/foo/bar/baz.html"); 67 68 Resource r2 = r1.getLocalization(Locale.ENGLISH); 69 70 assertEquals("/foo/bar/baz_en.html", r2.getPath()); 71 assertEquals(Locale.ENGLISH, r2.getLocale()); 72 73 verifyControls(); 74 } 75 76 public void testLocalizationSame() throws Exception 77 { 78 ServletContext context = newContext(); 79 80 context.getResource("/foo/bar/baz_en.html"); 81 setReturnValue(context, null); 82 83 context.getResource("/foo/bar/baz.html"); 84 setReturnValue(context, new URL ("http://foo.com")); 85 86 replayControls(); 87 88 ContextResource r1 = new ContextResource(context, "/foo/bar/baz.html"); 89 90 Resource r2 = r1.getLocalization(Locale.ENGLISH); 91 92 assertSame(r2, r1); 93 94 verifyControls(); 95 } 96 97 public void testLocalizationMissing() throws Exception 98 { 99 ServletContext context = newContext(); 100 101 context.getResource("/foo/bar/baz_en.html"); 102 setReturnValue(context, null); 103 104 context.getResource("/foo/bar/baz.html"); 105 setReturnValue(context, null); 106 107 replayControls(); 108 109 ContextResource r1 = new ContextResource(context, "/foo/bar/baz.html"); 110 111 assertNull(r1.getLocalization(Locale.ENGLISH)); 112 113 verifyControls(); 114 } 115 116 public void testGetRelativeResource() 117 { 118 ServletContext context = newContext(); 119 120 replayControls(); 121 122 ContextResource r1 = new ContextResource(context, "/foo/bar/baz.html"); 123 Resource r2 = r1.getRelativeResource("baz.gif"); 124 125 assertEquals("/foo/bar/baz.gif", r2.getPath()); 126 127 verifyControls(); 128 } 129 } | Popular Tags |