KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > test > core > I18nELTestCase


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.test.core;
10
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Locale JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import javax.portlet.PortletConfig;
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23 import org.apache.cactus.JspTestCase;
24 import org.jboss.portal.core.servlet.jsp.PortalJsp;
25 import org.jboss.portal.core.servlet.jsp.taglib.PortalLib;
26 import org.jboss.portal.portlet.impl.PortletConfigImpl;
27 import org.jboss.portal.portlet.metadata.LanguagesMetaData;
28 import org.jboss.portal.portlet.plugins.language.ResourceBundles;
29
30 /**
31  * @author <a HREF="theute@jboss.org">Thomas Heute</a>
32  * $Revision: 1.1 $
33  */

34 public class I18nELTestCase
35       extends JspTestCase
36 {
37    private static final String JavaDoc RESOURCE_BUNDLE_NAME = "MyResourceBundle";
38
39    
40    public void test01() throws ServletException JavaDoc, IOException JavaDoc {
41       PortalJsp jbossJsp = new PortalJsp()
42       {
43          public void _jspService(HttpServletRequest JavaDoc arg0, HttpServletResponse JavaDoc arg1) throws ServletException JavaDoc, IOException JavaDoc
44          {
45             assertEquals("Test_fr", PortalLib.getMessage("test"));
46          }
47       };
48       // Builder loader
49
ResourceClassLoader loader = new ResourceClassLoader(Thread.currentThread().getContextClassLoader());
50       loader.addResource(RESOURCE_BUNDLE_NAME + "_fr.properties", "javax.portlet.title=Title_fr\ntest=Test_fr\n");
51
52       // Build the language metadata
53
LanguagesMetaData.InfoMetaData infoMD = new LanguagesMetaData.InfoMetaData();
54       infoMD.setTitle("Title");
55       infoMD.setShortTitle("ShortTitle");
56       LanguagesMetaData languagesMD = new LanguagesMetaData();
57       languagesMD.setInfo(infoMD);
58       languagesMD.setResourceBundle(RESOURCE_BUNDLE_NAME);
59
60       // Build the bundle
61
ResourceBundles rbs = new ResourceBundles(languagesMD, loader);
62
63       PortletConfig portletConfig = new PortletConfigImpl("testPortlet", null, null, rbs);
64       request.setAttribute("javax.portlet.config", portletConfig);
65       
66       HttpServletRequestWrapperImpl requestWrapper = new HttpServletRequestWrapperImpl(request);
67       requestWrapper.setLocale(Locale.FRENCH);
68       
69       jbossJsp.service(requestWrapper, response);
70       jbossJsp.destroy();
71    }
72
73    private class ResourceClassLoader extends ClassLoader JavaDoc
74    {
75
76       private Map JavaDoc resources = new HashMap JavaDoc();
77
78       public ResourceClassLoader(ClassLoader JavaDoc parent)
79       {
80          super(parent);
81       }
82
83       public void addResource(String JavaDoc name, String JavaDoc content)
84       {
85          if (name == null)
86          {
87             throw new IllegalArgumentException JavaDoc();
88          }
89          if (content == null)
90          {
91             throw new IllegalArgumentException JavaDoc();
92          }
93          resources.put(name, content.getBytes());
94       }
95
96       public InputStream JavaDoc getResourceAsStream(String JavaDoc name)
97       {
98          if (resources.containsKey(name))
99          {
100             return new ByteArrayInputStream JavaDoc((byte[])resources.get(name));
101          }
102          else
103          {
104             return super.getResourceAsStream(name);
105          }
106       }
107    }
108
109 }
110
Popular Tags