rollbackChanges in a unit test

From: Jim Menard (jim..o.com)
Date: Mon Jan 12 2004 - 08:43:00 EST

  • Next message: Jim Menard: "Re: rollbackChanges in a unit test"

    I've written a unit test using JUnit. It's throwing a
    NullPointerException during the call to context.rollbackChanges(). This
    only started happening when I added the code after the line "NPE CAUSED
    BY THIS CODE?". Does anyone have any suggestions as to what I'm doing
    wrong?

    As always, thanks for your help.

    public MetadataTest(String name) {
         super(name);

         Configuration.bootstrapSharedConfiguration(getClass());
         context = Configuration.getSharedConfiguration().getDomain()
             .createDataContext();
    }

    public void setUp() {
         metadata = (Metadata)context.createAndRegisterNewObject("Metadata");
         metadata.setType(new Integer(Metadata.PEOPLE));
    }

    public void tearDown() {
         context.rollbackChanges();
    }

    public void testModifyMetadataItems() {
         metadata.modifyMetadataItems("a, b, and c");
         List items = metadata.getMetadataItems();
         assertEquals(3, items.size());
         List values = getItemValues(); // Calls metadata.getMetadataItems()
         assertTrue("\"a\" is missing", values.contains("a"));
         assertTrue("\"b\" is missing", values.contains("b"));
         assertTrue("\"c\" is missing", values.contains("c"));

            // NPE CAUSED BY THIS CODE?
         // Remove b
         metadata.modifyMetadataItems("a, c");
         items = metadata.getMetadataItems();
         assertEquals(2, items.size());
         values = getItemValues();
         assertTrue("\"a\" is missing", values.contains("a"));
         assertTrue("\"c\" is missing", values.contains("c"));
    }

    /**
      * Returns the list of <var>metadata</var>'s metadata item values.
      */
    protected List getItemValues() {
         List values = new ArrayList();
         for (Iterator iter = metadata.getMetadataItems().iterator();
              iter.hasNext(); )
             values.add(((MetadataItem)iter.next()).getValue());
         return values;
    }

    There was 1 error:
    1) testModifyMetadataItems(com.preclick.photowiki.test.MetadataTest)
    java.lang.NullPointerException
             at
    org.objectstyle.cayenne.access.SnapshotManager.refreshObjectWithSnap\
    shot(SnapshotManager.java:120)
             at
    org.objectstyle.cayenne.access.DataContext.rollbackChanges(DataConte\
    xt.java:733)
             at
    com.preclick.photowiki.test.MetadataTest.tearDown(MetadataTest.java:\
    41)
             at com.preclick.photowiki.test.AllTests.main(AllTests.java:40)

    FAILURES!!!
    Tests run: 29, Failures: 0, Errors: 1

    Jim

    -- 
    Jim Menard, jim..o.com, http://www.io.com/~jimm/
    "Slideshow ... boring. Loosing ... consciousness ..." -- The Tick
    



    This archive was generated by hypermail 2.0.0 : Mon Jan 12 2004 - 08:43:07 EST