Menu

code to run emma

2009-01-13
2013-05-09
  • Andre Buzzo

    Andre Buzzo - 2009-01-13

    Hi,
    I would like to know if there is any emma API.
    I don't want to run emma from command line or ant.
    I want to run emma from a program (class). Is there any documentation for this?

    Tks,
    Andre

     
    • FRANCISCO CEBRIAN JUAREZ

      Hi
      Excume, but my english is not very good.
      I'm from Spain.
      I think what  isn't any emma Api
      We instrumetation the classes in an ear file, for we use de emma.
      When we uses de application, then it show the message error.

      We have uses normaly emma in our applicatios, but one day left off working.

      I have reset with ctl, but it isn't work.

      Thank you

       
      • Alex Eagle

        Alex Eagle - 2009-01-15

        There isn't a documented API, but it is possible to use EMMA programatically.

        A good place to start is InstrProcessor.java
        http://www.google.com/codesearch/p?hl=en#wGW6mpCcHuw/core/java12/com/vladium/emma/instr/InstrProcessor.java&q=Emma%20lang:java%20instrument

        I can post some sample code tomorrow, if that's helpful.

        -Alex

         
        • Andre Buzzo

          Andre Buzzo - 2009-01-15

          Hi Alex, tks for the tip.
          I took a look on the link. Could you paste the sample code?

          Here is what I want to do: I want to make a program that finds all input data for a method (this is called unit test) for a full coverage (100%) of this method. For this I need to start with some input data, verify the % of coverage, store this input and then try out new values, verity the coverage again, etc.

          So you see, I want to run the program and recheck the coverage in some kind of loop. For this I need some access for EMMA. I could do this calling EMMA in bash from my Java code and then parsing the returned string but this would not be good. A direct access for some kind of API would be better. But now I see that EMMA has no API... so I am accepting any other way.

          This tip you gave me, can help me running EMMA from a Java code?
          Tks!
          Andre

           
          • Alex Eagle

            Alex Eagle - 2009-01-15

            This will probably get formatted like crap, but maybe helpful. This is just to produce an instrumented class, you still need to go execute the resulting bytecode somewhere.

            import com.vladium.emma.data.CoverageOptions;
            import com.vladium.emma.data.DataFactory;
            import com.vladium.emma.data.IMetaData;
            import com.vladium.emma.instr.InstrVisitor;
            import com.vladium.jcd.cls.ClassDef;
            import com.vladium.jcd.compiler.ClassWriter;
            import com.vladium.jcd.parser.ClassDefParser;

            ...

               String[] classNames = [ your classes to instrument ]
                IMetaData metadata = DataFactory.newMetaData(options);
                JarOutputStream resultJar = new JarOutputStream(new FileOutputStream(outputJar));
                for (String className : classNames) {
                  InputStream classStream = classpath.getResourceAsStream(className);
                  resultJar.putNextEntry(new ZipEntry(className));
                  instrument(classStream, resultJar, metadata);
                }
                resultJar.close();
                DataFactory.persist(metadata, metadataFile, false);

              /**
               * Instrument a single class file
               * @param classStream read the class from here
               * @param outputStream writes an instrumented class here
               * @throws IOException if the streams bork
               */
              public void instrument(InputStream classStream, OutputStream outputStream,
                                     IMetaData metadata) throws IOException {
                InstrVisitor visitor = new InstrVisitor(options);
                ClassDef classDef = ClassDefParser.parseClass(classStream);
                InstrVisitor.InstrResult out = new InstrVisitor.InstrResult();
                visitor.process(classDef, true, true, true, out);
                ClassWriter.writeClassTable(classDef, outputStream);
                if (out.m_instrumented) {
                  if (out.m_descriptor == null) {
                    throw new IllegalStateException("No metadata produced for instrumented class");
                  }
                  metadata.add(out.m_descriptor, false);
                }
              }

             
            • Andre Buzzo

              Andre Buzzo - 2009-01-18

              Thats great! Tks!

              Still, do you have any code for the execution part? Or I will have to run in bash mode? The last important think would be read the report.... is there any code for that too?

              Anyway, thks for this tip!

              Andre

               
              • Alex Eagle

                Alex Eagle - 2009-01-19

                I don't have anything on hand to execute the code. Since emma produces instrumented bytecode, you need to run that bytecode with all its dependencies available in the classpath. It produces a coverage.ec file with the actual lines that were covered. Once you have that, Producing a report is pretty easy, just start from the emma report ant task and run the same stuff it does.

                 
    • karthik krishnan

      Thanks for the post, but I have a question about this example: What should be the output jar in this line?

      JarOutputStream resultJar = new JarOutputStream(new FileOutputStream(outputJar));

      I need to generate code coverage programmatically.

       

Log in to post a comment.