Hello, i have the following testCase (inspired by some code found online):

what it does is to read the xml from string, create a digester, and load the
object: very simple stuff, but i get the following stacktrace:

28-dic-2005 14.59.26 org.apache.commons.digester.Digester startElement
GRAVE: Begin event threw exception
java.lang.InstantiationException: jfrr.mapping.StudentDigester$Student
    at java.lang.Class.newInstance0(Class.java:335)
    at java.lang.Class.newInstance(Class.java:303)
    at org.apache.commons.digester.ObjectCreateRule.begin(
ObjectCreateRule.java:205)
    at org.apache.commons.digester.Rule.begin(Rule.java:152)
    at org.apache.commons.digester.Digester.startElement(Digester.java:1361)
    at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown
Source)
    at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown
Source)
    at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
Source)
    at 
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
Source)
    at org.apache.commons.digester.Digester.parse(Digester.java:1685)
    at jfrr.mapping.StudentDigester$DigestStudents.digest(
StudentDigester.java:53)
    at jfrr.mapping.StudentDigester$DigestStudents.access$0(
StudentDigester.java:32)
    at jfrr.mapping.StudentDigester.testAddBeanPropertySetter(
StudentDigester.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
RemoteTestRunner.java:478)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
RemoteTestRunner.java:344)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
RemoteTestRunner.java:196)
java.lang.InstantiationException: jfrr.mapping.StudentDigester$Student
    at org.apache.commons.digester.Digester.createSAXException(Digester.java
:2919)
    at org.apache.commons.digester.Digester.createSAXException(Digester.java
:2945)
    at org.apache.commons.digester.Digester.startElement(Digester.java:1364)
    at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown
Source)
    at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown
Source)
    at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
Source)
    at 
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
Source)
    at org.apache.commons.digester.Digester.parse(Digester.java:1685)
    at jfrr.mapping.StudentDigester$DigestStudents.digest(
StudentDigester.java:53)
    at jfrr.mapping.StudentDigester$DigestStudents.access$0(
StudentDigester.java:32)
    at jfrr.mapping.StudentDigester.testAddBeanPropertySetter(
StudentDigester.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
RemoteTestRunner.java:478)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
RemoteTestRunner.java:344)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
RemoteTestRunner.java:196)


The code I used is the following:

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.ExtendedBaseRules;

public class StudentDigester extends TestCase {

    protected String studentsXML = "<?xml version=\"1.0\"?>"
            + "<students><student>" + "<name>Java Boy</name>"
            + "<course>JSP</course>" + "<age>10</age>" + "</student>" +
"</students>";

    public void testAddBeanPropertySetter() {

        DigestStudents ds = new DigestStudents();
        ds.digest();

    }

    private class DigestStudents {
        List<Student> students;

        public DigestStudents() {
            students = new ArrayList<Student>();
        }

        private void digest() {
            try {
                Digester digester = new Digester();
                digester.setRules(new ExtendedBaseRules());
                //Push the current object onto the stack
                digester.push(this);

                //Creates a new instance of the Student class
                digester.addObjectCreate("students/student", Student.class);

                //Uses setName method of the Student instance
                //Uses tag name as the property name
                digester.addBeanPropertySetter("students/student/?");

                //Uses setCourse method of the Student instance
                //Explicitly specify property name as 'course'
                //digester.addBeanPropertySetter( "students/student/course",
"course" );

                //Move to next student
                digester.addSetNext("students/student", "addStudent");

                DigestStudents ds = (DigestStudents) digester.parse(new
StringReader(
                        studentsXML));

                //Print the contents of the Vector
                System.out.println("Students List " + ds.students);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        public void addStudent(Student stud) {
            //Add a new Student instance to the Vector
            students.add(stud);
        }
    }
    public class Student {
    private String name;
    private String course;
    private int age;

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String newCourse) {
        course = newCourse;
    }
    public String toString() {
        return("Name="+this.name + " & Course=" +  this.course +" &
Age="+this.age);
    }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
}
}

If i move the public internal class Student outside the testcase class, it
works as supposed. IS this normal behaviour ?
thanks,
valerio
--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs

Reply via email to