You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Brian H1 <bd...@yahoo.com> on 2008/04/18 20:41:07 UTC

Junit Plugin test fails when tables don't exist


I have a test case in Eclipse than runs successfully when executed as a
Junit test, but fails when run as a Junit Plugin test.  Below is the test
case and supporting classes.  Because I couldn't get the plugin to find the
persistence.xml file during the plugin test, I include all the parameters,
including the classes to be persisted, in a Props class and pass it to the
Persistence.createEntityManagerFactory method.  What's happening is when I
run the test as a junit test, it will create the tables needed for the class
and will persist the classes.  If I run it as a junit plugin test, as long
as the tables exist, it will persist the classes.  But, I get these messages
below.  I get the same messages when using HSQLDB, an in-memory database. 
This is what I really want to use for testcases because it will work on any
machine without having to have a sqlserver server setup.  I'm trying to
figure out why OpenJPA can't load the classes to be persisted when run as a
Junit Plugin test.

I've encountered quite a few of the other problems I've seen on this board
with not being able to find the persistence.xml file in a plugin.  How are
other people solving this problem and other differences encountered when
running in the plugin environment?


Brian

Here's the messages that the junit plugin test kicks out regardless of
whether the tables exist or not:

6  WARN   [main] openjpa.MetaData - The class "sample2.Note" listed in the
openjpa.MetaDataFactory configuration property could not be loaded;
ignoring.
16  WARN   [main] openjpa.MetaData - The class "sample2.Message" listed in
the openjpa.MetaDataFactory configuration property could not be loaded;
ignoring.

If the tables don't exist in the database, the persistence fails with these
messages for Note and a similiar messages for Message:

15  WARN   [main] openjpa.Runtime - The property named "openjpa.Id" was not
recognized and will be ignored, although the name closely matches a valid
property called "openjpa.Id".
46  INFO   [main] openjpa.Runtime - Starting OpenJPA 1.0.1
234  WARN   [main] openjpa.MetaData - The class "sample2.Note" listed in the
openjpa.MetaDataFactory configuration property could not be loaded;
ignoring.
234  WARN   [main] openjpa.MetaData - The class "sample2.Message" listed in
the openjpa.MetaDataFactory configuration property could not be loaded;
ignoring.
249  WARN   [main] openjpa.MetaData - The class "sample2.Note" listed in the
openjpa.MetaDataFactory configuration property could not be loaded;
ignoring.
249  WARN   [main] openjpa.MetaData - The class "sample2.Message" listed in
the openjpa.MetaDataFactory configuration property could not be loaded;
ignoring.
437  TRACE  [main] openjpa.jdbc.SQL - <t 19833309, conn 8713574> executing
prepstmnt 33236179 INSERT INTO Message (id, created, message) VALUES (?, ?,
?) [params=(long) 1208543090088, (Timestamp) 2008-04-18 13:24:50.088,
(String) hello there, from SessionTest2 with cfg]
546  TRACE  [main] openjpa.jdbc.SQL - <t 19833309, conn 8713574> [109 ms]
spent

Here's parts of the exception that's thrown for Message:
<openjpa-1.0.1-r420667:592145 fatal store error>
org.apache.openjpa.persistence.RollbackException: The transaction has been
rolled back.  See the nested exceptions for details on the errors that
occurred.
	at
org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:419)
	at sample2.Session.save(Session.java:27)
	at sample2.SessionTest.testSaveMessageWithCfg(SessionTest.java:44)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	...
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:443)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1169)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1144)
Caused by: <openjpa-1.0.1-r420667:592145 fatal general error>
org.apache.openjpa.persistence.PersistenceException: The transaction has
been rolled back.  See the nested exceptions for details on the errors that
occurred.
	at
org.apache.openjpa.kernel.BrokerImpl.newFlushException(BrokerImpl.java:2107)
	at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1954)
	at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1852)
	at
org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1770)
	at
org.apache.openjpa.kernel.LocalManagedRuntime.commit(LocalManagedRuntime.java:81)
	at org.apache.openjpa.kernel.BrokerImpl.commit(BrokerImpl.java:1292)
	at
org.apache.openjpa.kernel.DelegatingBroker.commit(DelegatingBroker.java:861)
	at
org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:408)
	... 39 more
Caused by: <openjpa-1.0.1-r420667:592145 nonfatal general error>
org.apache.openjpa.persistence.PersistenceException: Invalid object name
'Message'. {prepstmnt 33236179 INSERT INTO Message (id, created, message)
VALUES (?, ?, ?) [params=(long) 1208543090088, (Timestamp) 2008-04-18
13:24:50.088, (String) hello there, from SessionTest2 with cfg]} [code=208,
state=S0002]
FailedObject: sample2.Message@19c5048
	at
org.apache.openjpa.jdbc.sql.DBDictionary.newStoreException(DBDictionary.java:3938)
	at
org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:97)
	at
org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:67)
	at
org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushInternal(PreparedStatementManagerImpl.java:108)
	...
	... 53 more



package sample2;

import java.util.*;

import junit.framework.*;


public class SessionTest extends TestCase {
	
	private Session sessionNoCfgProps;
	private Session sessionWithCfgProps;
	private Properties props;

	public void setUp(){
		
		props = new Properties();
		
		// SqlServer configuration
		props.put("openjpa.ConnectionURL",
		
"jdbc:sqlserver://localhost:1433;DatabaseName=OpenJPA_Test;selectMethod=cursor;create=true");
	
props.put("openjpa.ConnectionDriverName","com.microsoft.sqlserver.jdbc.SQLServerDriver");
		props.put("openjpa.ConnectionUserName", "sa");
		props.put("openjpa.ConnectionPassword", "sa");
		
		
		// HSQLDB configuration
		/*props.put("openjpa.ConnectionURL","jdbc:hsqldb:mem:TestDb;create=true");
		props.put("openjpa.ConnectionDriverName","org.hsqldb.jdbcDriver");
		props.put("openjpa.ConnectionUserName", "sa");
		props.put("openjpa.ConnectionPassword", "");*/
		
		props.put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
		props.put("openjpa.Log", "DefaultLevel=WARN, Runtime=INFO, Tool=INFO,
SQL=TRACE");
		props.put("openjpa.MetaDataFactory",
"jpa(Types=sample2.Note;sample2.Message)");
		
		sessionWithCfgProps = new Session(props);
		
	}
	
	public void testSaveMessageWithCfg(){
		
		Message msg = new Message();
		msg.setMessage("hello there, from SessionTest2 with cfg");
		sessionWithCfgProps.save(msg);
	}
	
	public void testSaveNoteWithCfg(){
		
		Note note = new Note();
		note.setNote("hello note");
		sessionWithCfgProps.save(note);
	}
	

}

The Message class:
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package sample2;

import java.util.*;
import javax.persistence.*;


/** 
 * A very simple persistent entity that holds a "message", has a
 * "created" field that is initialized to the time at which the
 * object was created, and an id field that is initialized to the
 * current time.
 */
@Entity
public class Message {
    @Id
    private long id = System.currentTimeMillis();

    @Basic
    private String message;

    @Basic
    private Date created = new Date();

    public Message() {
    }

    public Message(String msg) {
        message = msg;
    }

    public void setId(long val) {
        id = val;
    }

    public long getId() {
        return id;
    }

    public void setMessage(String msg) {
        message = msg;
    }

    public String getMessage() {
        return message;
    }

    public void setCreated(Date date) {
        created = date;
    }

    public Date getCreated() {
        return created;
    }
}

The Session class:
/**
 * 
 */
package sample2;

import java.net.*;
import java.util.*;

import javax.persistence.*;

public class Session {
	
	private Properties configurationProps;
	
	public Session(){
	}
	
	public Session(Properties configProps){
		this.configurationProps = configProps;
	}

	public Object save(Object obj) {
		
		EntityManager entityMgr = this.getEntityManager();
		entityMgr.getTransaction().begin();
		entityMgr.persist(obj);
		entityMgr.getTransaction().commit();
		return obj;
		
	}
	
	public void setConfigurationProps(Properties configProps) {
		this.configurationProps = configProps;
	}

	public EntityManager getEntityManager() {

		EntityManagerFactory factory = null;
		
		if (this.configurationProps == null){
			System.out.println("without config props");
			factory = Persistence.
			createEntityManagerFactory("sample");
		} else {
			System.out.println("with config props");
			factory = Persistence.
			createEntityManagerFactory("sample", this.configurationProps);
		}
		
        return factory.createEntityManager();
	}

}


-- 
View this message in context: http://www.nabble.com/Junit-Plugin-test-fails-when-tables-don%27t-exist-tp16764005p16764005.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.