[jboss-svn-commits] JBoss Common SVN: r2144 - in jbossxb/trunk/src/test: java/org/jboss/test/xml resources/org/jboss/test/xml

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Oct 25 09:27:48 EDT 2006


Author: heiko.braun at jboss.com
Date: 2006-10-25 09:27:44 -0400 (Wed, 25 Oct 2006)
New Revision: 2144

Added:
   jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnmarshallerImpl.java
   jbossxb/trunk/src/test/resources/org/jboss/test/xml/XOPUnitTestCase_data.xml
Modified:
   jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnitTestCase.java
   jbossxb/trunk/src/test/resources/org/jboss/test/xml/XOPUnitTestCase.xsd
Log:
Added BeforeSetParentCallback sample for XOP encoded requests

Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnitTestCase.java	2006-10-25 13:10:54 UTC (rev 2143)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnitTestCase.java	2006-10-25 13:27:44 UTC (rev 2144)
@@ -24,14 +24,7 @@
 import org.jboss.xb.binding.Unmarshaller;
 import org.jboss.xb.binding.UnmarshallerFactory;
 import org.jboss.xb.binding.sunday.marshalling.MarshallerImpl;
-import org.jboss.xb.binding.sunday.unmarshalling.DefaultSchemaResolver;
-import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
-import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
-import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
-import org.jboss.xb.binding.sunday.unmarshalling.SequenceBinding;
-import org.jboss.xb.binding.sunday.unmarshalling.TermBeforeSetParentCallback;
-import org.jboss.xb.binding.sunday.unmarshalling.UnmarshallingContext;
-import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder;
+import org.jboss.xb.binding.sunday.unmarshalling.*;
 import org.jboss.xb.binding.sunday.xop.XOPMarshaller;
 import org.jboss.xb.binding.sunday.xop.XOPObject;
 import org.jboss.xb.binding.sunday.xop.XOPUnmarshaller;
@@ -39,6 +32,7 @@
 import org.xml.sax.SAXException;
 
 import javax.xml.transform.Source;
+import javax.xml.namespace.QName;
 import javax.activation.DataSource;
 import java.awt.*;
 import java.awt.image.ImageObserver;
@@ -59,7 +53,8 @@
  */
 public class XOPUnitTestCase
     extends AbstractJBossXBTest
-{   
+{
+
    public static final TestSuite suite()
    {
       return new TestSuite(XOPUnitTestCase.class);
@@ -188,6 +183,13 @@
             xopObject = new XOPObject("octets".getBytes());
             xopObject.setContentType("application/octet-stream");
          }
+         else if(cid.endsWith("xopContent"))
+         {
+            // The XOPUnmarshaller returns an object
+            // that doesn't match that actual java property
+            xopObject = new XOPObject("xopContent".getBytes());
+            xopObject.setContentType("application/octet-stream");
+         }
          else
          {
             try
@@ -254,13 +256,13 @@
             {
                ElementBinding e = (ElementBinding) ctx.getParticle().getTerm();
                Class propType = ctx.resolvePropertyType();
-               
+
                String localPart = e.getQName().getLocalPart();
                if("image".equals(localPart) ||
-                     "sig".equals(localPart) ||
-                     "imageWithContentType".equals(localPart) ||
-                     "octets".equals(localPart) ||
-                     "jpeg".equals(localPart))
+                   "sig".equals(localPart) ||
+                   "imageWithContentType".equals(localPart) ||
+                   "octets".equals(localPart) ||
+                   "jpeg".equals(localPart))
                {
                   assertEquals("expected " + byte[].class + " for " + localPart, byte[].class, propType);
                }
@@ -290,8 +292,65 @@
          {
             ParticleBinding particle = (ParticleBinding) i.next();
             ElementBinding child = (ElementBinding) particle.getTerm();
-            child.setBeforeSetParentCallback(callback);
+            if(! "xopContent".equals( child.getQName().getLocalPart()))
+               child.setBeforeSetParentCallback(callback);
          }
+
+         // ---------------------------------------------------
+
+         TermBeforeSetParentCallback interceptXOPUnmarshallerResults = new TermBeforeSetParentCallback()
+         {
+            public Object beforeSetParent(Object o, UnmarshallingContext ctx)
+            {
+
+               ElementBinding e = (ElementBinding) ctx.getParticle().getTerm();
+               Class propType = ctx.resolvePropertyType();
+
+               assertNotNull("Failed to resolve property type for "+e.getQName(), propType);
+
+               String localPart = e.getQName().getLocalPart();
+               if("xopContent".equals(localPart))
+               {
+                  System.out.println("! xopContent handle on "+ e.getQName());
+                  assertEquals(String.class, propType);
+               }
+               else if("Include".equals(localPart))
+               {
+                  System.out.println("! include handle on "+ e.getQName());
+                  assertEquals(String.class, propType);
+
+                  assertTrue( (o instanceof byte[]));
+                  
+                  // Type conversion required
+                  if(propType.equals(String.class))
+                     o = new String( (byte[])o);
+               }
+
+               return o;
+            }
+         };
+
+         // xmime complex types
+         TypeBinding xmimeBase64Type = SCHEMA.getType(new QName("http://www.w3.org/2005/05/xmlmime", "base64Binary"));
+         if(xmimeBase64Type!=null)
+         {
+            xmimeBase64Type.setBeforeSetParentCallback( interceptXOPUnmarshallerResults );
+
+            // xop:Include
+            // Uncomment the following lines in order to intercept the
+            // XOPUnmarshaller result _before_ the actual setter is invoked
+
+            /*
+            ModelGroupBinding modelGroup = (ModelGroupBinding)xmimeBase64Type.getParticle().getTerm();
+            ParticleBinding particle = (ParticleBinding)modelGroup.getParticles().iterator().next();
+            ElementBinding xopInclude = (ElementBinding)particle.getTerm();
+
+            if(! xopInclude.getQName().equals(new QName("http://www.w3.org/2004/08/xop/include", "Include")))
+               throw new RuntimeException("Looks like the JBossXB XOP implementation has changed, please open a JIRA issue");
+
+            xopInclude.setBeforeSetParentCallback(interceptXOPUnmarshallerResults);
+            */
+         }
       }
 
       if(NON_OPT_XML == null)
@@ -509,6 +568,23 @@
       assertEquals("string", e.string);
    }
 
+   public void testUnmarshalStringWithTypeConversion() throws Exception
+   {
+      SCHEMA.setXopUnmarshaller(XOP_ENABLED_UNMARSH);
+
+      String xml = getOptimizedXml("xopContent");
+
+      Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
+      Object o = unmarshaller.unmarshal(new StringReader(xml), SCHEMA);
+
+      assertNotNull(o);
+      assertTrue(o instanceof E);
+
+      E e = (E)o;
+      assertNotNull(e.xopContent);
+      assertEquals("xopContent", e.xopContent);
+   }
+
    public void testTopLevelUnmarshalling() throws Exception
    {
       String xsd =
@@ -601,5 +677,8 @@
       public Source source;
       public String string;
       public byte[] octets;
+
+      public String xopContent;
    }
+
 }

Added: jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnmarshallerImpl.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnmarshallerImpl.java	2006-10-25 13:10:54 UTC (rev 2143)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnmarshallerImpl.java	2006-10-25 13:27:44 UTC (rev 2144)
@@ -0,0 +1,152 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* 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.jboss.test.xml;
+
+import org.jboss.test.AbstractTestCase;
+import org.jboss.xb.binding.sunday.xop.XOPObject;
+import org.jboss.xb.binding.sunday.xop.XOPUnmarshaller;
+
+import javax.xml.transform.stream.StreamSource;
+import java.awt.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+/**
+ * @author Heiko Braun <heiko.braun at jboss.com>
+ * @version $Id$
+ * @since Oct 5, 2006
+ */
+public class XOPUnmarshallerImpl implements XOPUnmarshaller {
+
+   private AbstractTestCase callback;
+   private String resourceDir;
+
+   public XOPUnmarshallerImpl(AbstractTestCase callback, String resourceDir) {
+      this.callback = callback;
+      this.resourceDir = resourceDir;
+   }
+
+   public boolean isXOPPackage()
+   {
+      return true;
+   }
+
+   public XOPObject getAttachmentAsDataHandler(String cid)
+   {
+      XOPObject xopObject;
+
+      if(cid.endsWith("string"))
+      {
+         xopObject = new XOPObject("A plain text attachment");
+         xopObject.setContentType("text/plain");
+      }
+      else if (cid.endsWith("source"))
+      {
+         StreamSource source = createTestSource();
+
+         xopObject = new XOPObject(source);
+         xopObject.setContentType("application/xml");
+
+      }
+      else if(cid.endsWith("jpeg") || cid.endsWith("image"))
+      {
+         Image image = createTestImage();
+
+         xopObject = new XOPObject(image);
+         xopObject.setContentType("image/jpeg");
+      }
+      else if(cid.endsWith("octets"))
+      {
+         xopObject = new XOPObject("octets".getBytes());
+         xopObject.setContentType("application/octet-stream");
+      }
+      else if (cid.endsWith("applxml"))
+      {
+         xopObject = new XOPObject("applxml".getBytes());
+         xopObject.setContentType("application/octet-stream");
+      }
+      else
+      {
+         throw new RuntimeException("Unmapped content: " + cid);
+      }
+
+      return xopObject;
+   }
+
+   public byte[] getAttachmentAsByteArray(String cid)
+   {
+      return cid.substring(4).getBytes();
+   }
+   
+   public StreamSource createTestSource()
+   {
+      String filename = callback.getResource(resourceDir+"_data.xml").getFile();
+      FileInputStream stream = null;
+      try
+      {
+         stream = new FileInputStream(filename);
+      }
+      catch (FileNotFoundException e)
+      {
+         throw new RuntimeException("Failed to load file: " + filename);
+      }
+
+      return new StreamSource(stream);
+   }
+
+   public Image createTestImage() {
+      String filename = callback.getResource(resourceDir+"_data.jpg")!= null ?
+          callback.getResource(resourceDir+"_data.jpg").getFile() : null;
+
+      if(null == filename)
+         throw new IllegalArgumentException("Failed to create image");
+
+      URL url = null;
+      try
+      {
+         url = new File(filename).toURL();
+      }
+      catch (MalformedURLException e)
+      {
+         throw new RuntimeException("Failed to load file: " + filename);
+      }
+
+      // On Linux the X11 server must be installed properly to create images successfully.
+      // If the image cannot be created in the test VM, we assume it cannot be done on the
+      // server either, so we just skip the test
+      Image image = null;
+      try
+      {
+         image = Toolkit.getDefaultToolkit().createImage(url);
+      }
+      catch (Throwable th)
+      {
+         //log.warn("Cannot create Image: " + th);
+      }
+      return image;
+   }
+
+
+}


Property changes on: jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnmarshallerImpl.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: jbossxb/trunk/src/test/resources/org/jboss/test/xml/XOPUnitTestCase.xsd
===================================================================
--- jbossxb/trunk/src/test/resources/org/jboss/test/xml/XOPUnitTestCase.xsd	2006-10-25 13:10:54 UTC (rev 2143)
+++ jbossxb/trunk/src/test/resources/org/jboss/test/xml/XOPUnitTestCase.xsd	2006-10-25 13:27:44 UTC (rev 2144)
@@ -28,6 +28,8 @@
 
         <xs:element name="octets" type="xs:base64Binary" minOccurs="0"/>
 
+        <xs:element name="xopContent" type="xmime:base64Binary" minOccurs="0"/>       
+
       </xs:sequence>
     </xs:complexType>
   </xs:element>

Added: jbossxb/trunk/src/test/resources/org/jboss/test/xml/XOPUnitTestCase_data.xml
===================================================================
--- jbossxb/trunk/src/test/resources/org/jboss/test/xml/XOPUnitTestCase_data.xml	2006-10-25 13:10:54 UTC (rev 2143)
+++ jbossxb/trunk/src/test/resources/org/jboss/test/xml/XOPUnitTestCase_data.xml	2006-10-25 13:27:44 UTC (rev 2144)
@@ -0,0 +1,17 @@
+<?xml version="1.0 "encoding="UTF-8"?>
+
+<!DOCTYPE html PUBLIC "-//JBoss//DTD Test 1.0 Strict//EN"
+ "http://www.jboss.org/test/xml/xop">
+    
+<plugin>
+   <artifactId>maven-source-plugin</artifactId>
+   <version>2.0.1</version>
+   <executions>
+      <execution>
+         <goals>
+            <goal>jar</goal>
+         </goals>
+      </execution>
+   </executions>
+   <inherited>true</inherited>
+</plugin>
\ No newline at end of file


Property changes on: jbossxb/trunk/src/test/resources/org/jboss/test/xml/XOPUnitTestCase_data.xml
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF




More information about the jboss-svn-commits mailing list