Skip to Content
Author's profile photo Stefan Grube

Testing and Debugging Java Mapping in Developer Studio

In some forum entries, I found a question about debugging Java Mapping. There are a lot of Blogs about Java Mappings like this one of Thorsten Søbirk:
Using JAXP to both parse and emit XML in XI Java mapping programs

Now, how should you debug your code before a upload it to the XI? It is quite simple: Add the method “main” to your Java Mapping class.

Here is a template for a Java Mapping class with the method “main”:

 

package sample;

import java.io.*;
import com.sap.aii.mapping.api.*;


public class MyJavaMapping extends AbstractTransformation {

  DynamicConfiguration conf; // used for dynamic configuration 
                             // (adapter-specific message attributes) 
  InputParameters param;     // used for input parameters


  // the method "transform" is called by Java Mapping Runtime
  public void transform (TransformationInput arg0, TransformationOutput arg1)  
                        throws StreamTransformationException {

    conf = arg0.getDynamicConfiguration(); // provide dynamic configuration 
    param = arg0.getInputParameters();     // provide input parameters
    this.execute(arg0.getInputPayload().getInputStream(), 
                 arg1.getOutputPayload().getOutputStream());

  }

  // the method "execute" is called by "transform" and "main"
  public void execute (InputStream in, OutputStream out)  
                      throws StreamTransformationException {


    // Add your code here

  }

  // reading dynamic values
  String getDynamicValue(String namespace, String name){

    if (conf == null) // in test mode, dynamic parameters cannot be used. 
      return "";      // Use default values instead
    DynamicConfigurationKey key = 
                 DynamicConfigurationKey.create(namespace, name);
    return conf.get(key);

  }
 
  // reading input parameters
  String getInputParameter(String name){

    if (param == null){  // in test mode, input parameters cannot be used. 
      return "";         // Use default values instead
    } else {
      return (String) param.getValue(name);
    }
  }

  // the method "main" is using files for input and output
  public static void main(String[] args) {

    try {
      InputStream in = new FileInputStream(new File("in.xml"));
      OutputStream out = new FileOutputStream(new File("out.xml"));
      MyJavaMapping myMapping = new MyJavaMapping();
      myMapping.execute(in, out);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

 

All you have to do is placing an xml file “in.xml” in the working directory of your Java program, set a breakpoint and start the program.

API Documentation

https://help.sap.com/doc/javadocs_pi_sp3_xpi/7.1.3/en-US/index.html

Additional Information

See also this Weblog for Debugging Java Mappings using SAP Netweaver Developer Studio

Examples for Java Mapping (scroll down to III-Java Mapping)

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member
      Hi Stefan

      If you want to unit test your Java mappings actually running inside the WAS, you can do so using Detroubulator, a unit test tool written by myself and Mr Thorsten Søbirk. More information here:

      http://www.applicon.dk/detroubulator

      The tool is open source (GPL license) and available for download today.

      Kind regards,

      Morten Wittrock

      Author's profile photo Former Member
      Former Member
      Hi stefhan,

      i tried your code and i am getting package not found. even though i imported the packages.  can you tell me how and where i need to copy the packages so as to avoid the above specified error

      cheers
      ram

      Author's profile photo Stefan Grube
      Stefan Grube
      Blog Post Author
      Hi Ram,

      you have to attach the library aii_map_api.jar to your project.

      Regards
      Stefan

      Author's profile photo Former Member
      Former Member
      Thank you , i really found it usefull.

      It is similar to java code testing.

      Author's profile photo Mayur Patel
      Mayur Patel
      Hello Stefan,

      This a great way to summarize two different, old and new, way of doing java mapping. On my project I am using new PI 7.1 API. Could you show me how do i access MessageID, Namespace, Service Interface, etc.

      Thanks!

      Author's profile photo Former Member
      Former Member
      Hi Stefan,

      great idea with the main method, but why do you have a try/catch statement in main() ?  Any why do you print the stack-trace on stdout?  Why not this much simpler and much cleaner way:

      public static void main(final String[] args)
        throws Exception
      {
        doSomethingWhichMightThrowAnException();
      }

      Author's profile photo Stefan Grube
      Stefan Grube
      Blog Post Author
      As you have stated in the headline: Everyone does this. I have copied the code from elsewhere.
      The result is the same, you can use both versions, whichever you prefer.

      Regards
      Stefan