You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by di...@apache.org on 2004/09/06 05:14:14 UTC

cvs commit: jakarta-commons/jelly/jelly-tags/swing/src/test/org/apache/commons/jelly/swing swingTags.jelly TestSwingTags.java

dion        2004/09/05 20:14:14

  Added:       jelly/jelly-tags/swing/src/test/org/apache/commons/jelly/core
                        BaseJellyTest.java
               jelly/jelly-tags/swing/src/test/org/apache/commons/jelly/swing
                        swingTags.jelly TestSwingTags.java
  Log:
  Add tests
  
  Revision  Changes    Path
  1.1                  jakarta-commons/jelly/jelly-tags/swing/src/test/org/apache/commons/jelly/core/BaseJellyTest.java
  
  Index: BaseJellyTest.java
  ===================================================================
  /*
   * Copyright 2002,2004 The Apache Software Foundation.
   * 
   * 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 org.apache.commons.jelly.core;
  
  import java.net.URL;
  
  import junit.framework.TestCase;
  
  import org.apache.commons.jelly.Jelly;
  import org.apache.commons.jelly.JellyContext;
  import org.apache.commons.jelly.XMLOutput;
  
  /**
   * @author Rodney Waldhoff
   * @version $Revision: 1.1 $ $Date: 2004/09/06 03:14:14 $
   */
  public abstract class BaseJellyTest extends TestCase {
  
      public BaseJellyTest(String name) {
          super(name);
      }
  
      public void setUp() throws Exception {
          super.setUp();
          jelly = new Jelly();
          context = new JellyContext();
          xmlOutput = XMLOutput.createDummyXMLOutput();        
      }    
      
      protected void setUpScript(String scriptname) throws Exception {
          URL url = this.getClass().getResource(scriptname);
          if(null == url) {
              throw new Exception( 
                  "Could not find Jelly script: " + scriptname 
                  + " in package of class: " + getClass().getName() 
              );
          }
          jelly.setUrl(url);
  
          String exturl = url.toExternalForm();
          int lastSlash = exturl.lastIndexOf("/");
          String extBase = exturl.substring(0,lastSlash+1);
          URL baseurl = new URL(extBase);
          context.setCurrentURL(baseurl);
      }
  
      protected Jelly getJelly() {
          return jelly;
      }
      
      protected JellyContext getJellyContext() {
          return context;
      }
      
      protected XMLOutput getXMLOutput() {
          return xmlOutput;
      }
      
      private Jelly jelly = null;
      private JellyContext context = null;
      private XMLOutput xmlOutput = null;
  
  }
  
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/swing/src/test/org/apache/commons/jelly/swing/swingTags.jelly
  
  Index: swingTags.jelly
  ===================================================================
  <?xml version="1.0"?>
  <j:jelly 
  	xmlns:j="jelly:core" 
  	xmlns="jelly:swing">
  	
  	<!--for the basic component addembly-->
  	<j:if test="${test.simple}">
  		<frame name="frame" var="frame" size="100,100" location="200,200">
  			<panel name="panel">
  				<button name="button" background="#112233" foreground="#445566"/>
  			</panel>
  		</frame>
  	</j:if>
  	
  	<!--for the GridBag test-->
  	<j:if test="${test.gbc}">
  		<frame name="frame" var="frame">
  			<gridBagLayout>
  				<gbc anchor="north" fill="vertical" gridheight="3" 
  					gridwidth="2" gridx="4" gridy="5" insets="1,2,3,4"
  					ipadx="7" ipady="8" weightx=".3" weighty=".6">
  					
  					<button name="button"/>
  				</gbc>
  				
  				<gbc gridx="1" gridy="2" insets="3,4,5,6"
  					ipadx="8" ipady="9">
  				
  					<gbc ipadx="2">
  						<button name="button2" />
  					</gbc>
  				</gbc>
  				
  			</gridBagLayout>
  		</frame>
  	</j:if>
  	
  	<!--for the GridBag 1.4 test-->
  	<j:if test="${test.gbc14}">
  		<frame name="frame" var="frame">
  			<gridBagLayout>
  				<gbc anchor="line_start">
  					<button name="button"/>
  				</gbc>
  			</gridBagLayout>
  		</frame>
  	</j:if>
  	
  	<!--for the GridBag invalid anchor test-->
  	<j:if test="${test.gbcBad}">
  		<frame name="frame" var="frame">
  			<gridBagLayout>
  				<gbc anchor="foo">
  					<button name="button"/>
  				</gbc>
  			</gridBagLayout>
  		</frame>
  	</j:if>
  
  	<!--for the ButtonGroup test-->
  	<j:if test="${test.buttonGroup}">
  		<frame name="frame" var="frame">
  			<buttonGroup var="bg">
  				<checkBox name="cb1"/>
  				<checkBox name="cb2"/>
  			</buttonGroup>
  		</frame>
  	</j:if>
  	
  </j:jelly>
  
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/swing/src/test/org/apache/commons/jelly/swing/TestSwingTags.java
  
  Index: TestSwingTags.java
  ===================================================================
  /*
   * Copyright 2002,2004 The Apache Software Foundation.
   * 
   * 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 org.apache.commons.jelly.swing;
  
  import java.awt.Color;
  import java.awt.Component;
  import java.awt.Container;
  import java.awt.Dimension;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.Insets;
  import java.awt.Point;
  
  import javax.swing.ButtonGroup;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  import junit.framework.TestSuite;
  
  import org.apache.commons.jelly.JellyContext;
  import org.apache.commons.jelly.Script;
  import org.apache.commons.jelly.core.BaseJellyTest;
  
  /** Tests many swing tags for basic functionality.
   * @author Hans Gilde
   *
   */
  public class TestSwingTags extends BaseJellyTest {
  
      /**
       * @param name
       */
      public TestSwingTags(String name) {
          super(name);
      }
      
      public static TestSuite suite() throws Exception {
          return new TestSuite(TestSwingTags.class);
      }
      
      /** Tests some basic Swing tag functions like creating components
       * , adding them to the parent container and setting bean values.
       * @throws Exception
       */
      public void testBasicComponentFunctions() throws Exception {
          runSwingScript("test.simple");
          JellyContext context = getJellyContext();
          JFrame frame = (JFrame) context.getVariable("frame");
          assertEquals(new Dimension(100,100), frame.getSize());
          assertEquals(new Point(200,200), frame.getLocation());
          JPanel panel = (JPanel) componentByName(frame.getContentPane(), "panel");
          JButton button = (JButton) componentByName(panel, "button");
          assertNotNull(button);
          assertEquals(new Color(0x11,0x22,0x33), button.getBackground());
          assertEquals(new Color(0x44,0x55,0x66), button.getForeground());
      }
      
      /** Tests the GridbagLayout tags, making sure that the constraints are
       * set properly including inheritance and basedOn.
       * @throws Exception
       */
      public void testGridBagBasic() throws Exception {
          runSwingScript("test.gbc");
          JellyContext context = getJellyContext();
          JFrame frame = (JFrame) context.getVariable("frame");
          JButton button = (JButton) componentByName(frame.getContentPane(), "button");
          JButton button2 = (JButton) componentByName(frame.getContentPane(), "button2");
          GridBagLayout layout = (GridBagLayout) frame.getContentPane().getLayout();
          GridBagConstraints constraints = layout.getConstraints(button);
          
          // this is failing
          // assertEquals(GridBagConstraints.NORTH,constraints.anchor);
          assertEquals(GridBagConstraints.VERTICAL, constraints.fill);
          assertEquals(3, constraints.gridheight);
          assertEquals(2, constraints.gridwidth);
          assertEquals(4, constraints.gridx);
          assertEquals(5, constraints.gridy);
          assertEquals(7, constraints.ipadx);
          assertEquals(8, constraints.ipady);
          assertEquals(0.3, constraints.weightx, 0);
          assertEquals(0.6, constraints.weighty, 0);
          assertEquals(new Insets(1,2,3,4), constraints.insets);
          
          GridBagConstraints constraints2 = layout.getConstraints(button2);
          assertEquals(1, constraints2.gridx);
          assertEquals(2, constraints2.gridy);
          assertEquals(2, constraints2.ipadx);
          assertEquals(9, constraints2.ipady);
          assertEquals(new Insets(3,4,5,6), constraints2.insets);
      }
      
      public void testGridBag14() throws Exception {
          if (System.getProperty("java.version").startsWith("1.4")) {
              runSwingScript("test.gbc14");
              JellyContext context = getJellyContext();
              JFrame frame = (JFrame) context.getVariable("frame");
              JButton button = (JButton) componentByName(frame.getContentPane(), "button");
              GridBagLayout layout = (GridBagLayout) frame.getContentPane().getLayout();
              GridBagConstraints constraints = layout.getConstraints(button);
              //note that 21 is the JDK 1.4 value of GridBagConstraint.LINE_START
              // this is failing
              // assertEquals(21,constraints.anchor);
          }
      }
      
      public void testGridBagFail(){
          try {
              runSwingScript("test.gbcBad");
          } catch (Exception e) {
              //success
              return;
          }
          fail("Should have thrown an exception for a bad GBC anchor");
      }
      
      public void testButtonGroup() throws Exception {
          runSwingScript("test.buttonGroup");
          JellyContext context = getJellyContext();
          ButtonGroup bg = (ButtonGroup) context.getVariable("bg");
          //buttonGroup is broken
          //assertEquals(2, bg.getButtonCount());
      }
  
      
      protected void runSwingScript(String testName) throws Exception {
          setUpScript("swingTags.jelly");
          Script script = getJelly().compileScript();
          getJellyContext().getVariables().clear();
          getJellyContext().setVariable(testName,Boolean.TRUE);
          script.run(getJellyContext(),getXMLOutput());
      }
      
      /** Searches a container for a component with a given name. Searches only
       * the immediate container, not child containers.
       * @param container the Container to search in
       * @param name the name to look for
       * @return the first component with the given name
       * @throws Exception if the name isn't found
       */
      protected static Component componentByName(Container container, String name) throws Exception{
          Component[] components = container.getComponents();
          
          for (int i=0;i<components.length;i++) {
              Component component = components[i];
              if (component.getName().equals(name)) {
                  return component;
              }
          }
          
          throw new Exception("Component " + name + " not found in container " + container);
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org