[gatein-commits] gatein SVN: r7085 - in portal/trunk/component/web/resources/src: test/java/org/exoplatform/portal/resource and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Sat Aug 13 23:19:26 EDT 2011


Author: ndkhoiits
Date: 2011-08-13 23:19:26 -0400 (Sat, 13 Aug 2011)
New Revision: 7085

Added:
   portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/TestJavascriptConfigService.java
   portal/trunk/component/web/resources/src/test/resources/conf/js-service-configuration.xml
Modified:
   portal/trunk/component/web/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java
   portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/AbstractWebResourceTest.java
   portal/trunk/component/web/resources/src/test/resources/mockwebapp/gatein-resources.xml
Log:
GTNPORTAL-2005 JUnit Tests for JavascriptConfigService

Modified: portal/trunk/component/web/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java
===================================================================
--- portal/trunk/component/web/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java	2011-08-14 02:48:49 UTC (rev 7084)
+++ portal/trunk/component/web/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java	2011-08-14 03:19:26 UTC (rev 7085)
@@ -23,7 +23,6 @@
 import org.exoplatform.container.ExoContainerContext;
 import org.exoplatform.portal.resource.compressor.ResourceCompressor;
 import org.exoplatform.portal.resource.compressor.ResourceType;
-import org.exoplatform.portal.resource.compressor.impl.ResourceCompressorService;
 import org.gatein.common.logging.Logger;
 import org.gatein.common.logging.LoggerFactory;
 import org.gatein.wci.impl.DefaultServletContainerFactory;
@@ -369,6 +368,8 @@
       extendedJavascripts.remove(path);
       jsBytes = null;
    }
+   
+   
 
    /**
     * Start service.

Modified: portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/AbstractWebResourceTest.java
===================================================================
--- portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/AbstractWebResourceTest.java	2011-08-14 02:48:49 UTC (rev 7084)
+++ portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/AbstractWebResourceTest.java	2011-08-14 03:19:26 UTC (rev 7085)
@@ -29,6 +29,7 @@
  */
 @ConfiguredBy({
    @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/resource-compressor-service-configuration.xml"),
+   @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/js-service-configuration.xml"),
    @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/skin-service-configuration.xml")
 })
 public abstract class AbstractWebResourceTest extends AbstractKernelTest

Added: portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/TestJavascriptConfigService.java
===================================================================
--- portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/TestJavascriptConfigService.java	                        (rev 0)
+++ portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/TestJavascriptConfigService.java	2011-08-14 03:19:26 UTC (rev 7085)
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.portal.resource;
+
+import org.exoplatform.container.PortalContainer;
+import org.exoplatform.test.mocks.servlet.MockServletContext;
+import org.exoplatform.web.application.javascript.JavascriptConfigParser;
+import org.exoplatform.web.application.javascript.JavascriptConfigService;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
+/**
+ * @author <a href="mailto:phuong.vu at exoplatform.com">Vu Viet Phuong</a>
+ * @version $Id$
+ *
+ */
+public class TestJavascriptConfigService extends AbstractWebResourceTest
+{
+   private JavascriptConfigService jsService;
+
+   private ServletContext mockServletContext;
+   
+   private static final String exModule = "extended.js.test";
+   
+   private static final String exPath = "/extended/js/test.js";
+   
+   private static final ServletContext exContext = new MockJSServletContext("extendedWebApp", null);
+
+   @Override
+   protected void setUp() throws Exception
+   {
+      final PortalContainer portalContainer = getContainer();
+      jsService = (JavascriptConfigService)portalContainer.getComponentInstanceOfType(JavascriptConfigService.class);
+            
+      Map<String, String> resources = new HashMap<String, String>(4);
+      resources.put("/js/test1.js", "aaa;");
+      resources.put("/js/test2.js", "bbb;");
+      resources.put("/js/test3.js", "ccc;");
+      resources.put("/js/test4.js", "ddd;");      
+      mockServletContext = new MockJSServletContext("mockwebapp", resources);
+      
+      URL url = portalContainer.getPortalClassLoader().getResource("mockwebapp/gatein-resources.xml");
+      JavascriptConfigParser.processConfigResource(url.openStream(), jsService, mockServletContext);
+   }
+
+   public void testAvailableScripts()
+   {
+      assertEquals(4, jsService.getAvailableScripts().size());
+      assertTrue(jsService.isModuleLoaded("js.test1"));
+      assertTrue(jsService.isModuleLoaded("js.test2"));
+      assertTrue(jsService.isModuleLoaded("js.test3"));
+      assertTrue(jsService.isModuleLoaded("js.test4"));
+      assertFalse(jsService.isModuleLoaded("js.test5"));            
+   }
+
+   public void testPriority()
+   {
+      Iterator<String> availPaths = jsService.getAvailableScriptsPaths().iterator();      
+      assertEquals(mockServletContext.getContextPath() + "/js/test2.js", availPaths.next());
+      assertEquals(mockServletContext.getContextPath() + "/js/test4.js", availPaths.next());
+      assertEquals(mockServletContext.getContextPath() + "/js/test1.js", availPaths.next());
+      assertEquals(mockServletContext.getContextPath() + "/js/test3.js", availPaths.next());
+      assertFalse(availPaths.hasNext());
+   }
+   
+   public void testExtendedJS() throws Exception
+   {
+      jsService.addExtendedJavascript(exModule, exPath, exContext, "extendedJS;");
+      assertTrue(jsService.isModuleLoaded(exModule));
+      assertTrue(jsService.getAvailableScriptsPaths().contains(exContext.getContextPath() + exPath));
+      assertEquals("\nbbb;ddd;aaa;ccc;extendedJS;", new String(jsService.getMergedJavascript()));
+  
+      long first = jsService.getLastModified();
+      assertTrue(first < System.currentTimeMillis());            
+            
+      jsService.removeExtendedJavascript(exModule, exPath, exContext);
+      
+      assertFalse(jsService.isModuleLoaded(exModule));
+      assertFalse(jsService.getAvailableScriptsPaths().contains(exContext.getContextPath() + exPath));
+      Thread.sleep(1000); //Wait to make sure we can compare lastModified properly
+      assertEquals("\nbbb;ddd;aaa;ccc;", new String(jsService.getMergedJavascript()));
+      
+      long second = jsService.getLastModified();
+      assertTrue(first < second);
+      assertTrue(second < System.currentTimeMillis());
+   }
+   
+   public void testMergedJS()
+   {      
+      String mergedJS = new String(jsService.getMergedJavascript());
+      assertEquals("\nbbb;ddd;aaa;ccc;", mergedJS);            
+      assertTrue(jsService.getLastModified() < System.currentTimeMillis());
+   }   
+   
+   public void testRemoveJS()
+   {
+      assertEquals(4, jsService.getAvailableScripts().size());
+      assertEquals(4, jsService.getAvailableScriptsPaths().size());            
+      jsService.remove(mockServletContext);
+      assertEquals(0, jsService.getAvailableScripts().size());
+      assertEquals(0, jsService.getAvailableScriptsPaths().size());      
+      
+      assertTrue(jsService.getMergedJavascript().length > 0);
+      jsService.refreshMergedJavascript();      
+      //bug of service
+//      assertTrue(jsService.getMergedJavascript().length == 0);
+   }
+
+   @Override
+   protected void tearDown() throws Exception
+   {
+      jsService.remove(mockServletContext);
+      jsService.removeExtendedJavascript(exModule, exPath, exContext);
+   }
+   
+   private static class MockJSServletContext extends MockServletContext
+   {
+      private Map<String, String> resources;
+      
+      public MockJSServletContext(String contextName, Map<String, String> resources)
+      {
+         super(contextName);
+         this.resources = resources;
+      }
+      
+      @Override
+      public String getContextPath()
+      {
+         return "/" + getServletContextName();
+      }
+      
+      @Override
+      public InputStream getResourceAsStream(String s)
+      {
+         return new ByteArrayInputStream(resources.get(s).getBytes());
+      }
+   }
+}

Added: portal/trunk/component/web/resources/src/test/resources/conf/js-service-configuration.xml
===================================================================
--- portal/trunk/component/web/resources/src/test/resources/conf/js-service-configuration.xml	                        (rev 0)
+++ portal/trunk/component/web/resources/src/test/resources/conf/js-service-configuration.xml	2011-08-14 03:19:26 UTC (rev 7085)
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+
+    Copyright (C) 2009 eXo Platform SAS.
+    
+    This is free software; you can redistribute it and/or modify it
+    under the terms of the GNU Lesser General Public License as
+    published by the Free Software Foundation; either version 2.1 of
+    the License, or (at your option) any later version.
+    
+    This software is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+    Lesser General Public License for more details.
+    
+    You should have received a copy of the GNU Lesser General Public
+    License along with this software; if not, write to the Free
+    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+    02110-1301 USA, or see the FSF site: http://www.fsf.org.
+
+-->
+
+<configuration
+   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+   xsi:schemaLocation="http://www.exoplaform.org/xml/ns/kernel_1_1.xsd http://www.exoplaform.org/xml/ns/kernel_1_1.xsd"
+   xmlns="http://www.exoplaform.org/xml/ns/kernel_1_1.xsd">
+   <component>
+		<key>org.exoplatform.web.application.javascript.JavascriptConfigService</key>
+		<type>org.exoplatform.web.application.javascript.JavascriptConfigService</type>
+	</component>
+</configuration>

Modified: portal/trunk/component/web/resources/src/test/resources/mockwebapp/gatein-resources.xml
===================================================================
--- portal/trunk/component/web/resources/src/test/resources/mockwebapp/gatein-resources.xml	2011-08-14 02:48:49 UTC (rev 7084)
+++ portal/trunk/component/web/resources/src/test/resources/mockwebapp/gatein-resources.xml	2011-08-14 03:19:26 UTC (rev 7085)
@@ -118,4 +118,31 @@
 		</style-theme>
 	</window-style>
 	
+	<javascript>
+		<param>
+			<js-module>js.test1</js-module>
+			<js-path>/js/test1.js</js-path>	
+			<js-priority>-1</js-priority>		
+		</param>
+	</javascript>	
+	<javascript>
+		<param>
+			<js-module>js.test2</js-module>
+			<js-path>/js/test2.js</js-path>
+			<js-priority>1</js-priority>
+		</param>
+	</javascript>	
+	<javascript>
+		<param>
+			<js-module>js.test3</js-module>
+			<js-path>/js/test3.js</js-path>
+		</param>
+	</javascript>	
+	<javascript>
+		<param>
+			<js-module>js.test4</js-module>
+			<js-path>/js/test4.js</js-path>
+			<js-priority>2</js-priority>
+		</param>
+	</javascript>
 </gatein-resources>



More information about the gatein-commits mailing list