Romain, thanks for your advices :)
Of course if I remove the transaction code I have to use the entity manager
from the EJB and not the one remains in the test class ^^ (I should have
delete it..)
Anyway the code is working now and I even tried to move it to a Web Service.
works fine..except one thing : after the delete, the final assertion failed
(null pointer) I don't know why ?
Here the code :
*The movie :*
@Entity
public class Movie {
@Id
@GeneratedValue
private long id;
....
*The movies *
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@WebService(endpointInterface = "org.superbiz.altdd.IMoviesWS", serviceName
= "MoviesWebService", targetNamespace = "http://superbiz.org/wsdl")
public class Movies implements IMoviesWS {
@PersistenceContext(unitName = "movie-unit", type =
PersistenceContextType.TRANSACTION)
private EntityManager entityManager;
public void addMovie(Movie movie) throws Exception {
entityManager.persist(movie);
}
public void deleteMovie(Movie movie) throws Exception {
entityManager.remove(entityManager.find(Movie.class,
movie.getId()));
}
@SuppressWarnings("unchecked")
public List<Movie> getMovies() throws Exception {
Query query = entityManager.createQuery("SELECT m from Movie as
m");
return query.getResultList();
}
}
*The interface of the WS :*
@WebService(name = "IMoviesWS", targetNamespace =
"http://superbiz.org/wsdl")
public interface IMoviesWS {
void addMovie(Movie movie) throws Exception;
void deleteMovie(Movie movie) throws Exception;
List<Movie> getMovies() throws Exception;
}
*And the test :*
@LocalClient
public class MoviesTest extends TestCase {
@WebServiceRef(wsdlLocation = "http://127.0.0.1:4204/Movies?wsdl")
private IMoviesWS movies;
private InitialContext initialContext;
public void setUp() throws Exception {
Properties properties = new Properties();
properties.put("movieDatabase",
"new://Resource?type=DataSource");
properties.put("movieDatabase.JdbcDriver",
"org.hsqldb.jdbcDriver");
properties.put("movieDatabase.JdbcUrl",
"jdbc:hsqldb:mem:moviedb");
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.core.LocalInitialContextFactory");
properties.setProperty("openejb.embedded.remotable", "true");
initialContext = new InitialContext(properties);
initialContext.bind("inject", this);
}
public void test() throws Exception {
movies.addMovie(new Movie("Quentin Tarantino", "Reservoir
Dogs", 1992));
movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
movies.addMovie(new Movie("Joel Coen", "The Big Lebowski",
1998));
List<Movie> list = movies.getMovies();
assertEquals("List.size()", 3, list.size());
for (Movie movie : list) {
movies.deleteMovie(movie);
}
/*// do a null pointer exception why?*/
// assertEquals("Movies.getMovies()", 0,
movies.getMovies().size());
}
public void testWS() throws Exception {
Service movieService = Service.create(new URL(
"http://127.0.0.1:4204/Movies?wsdl"), new QName(
"http://superbiz.org/wsdl",
"MoviesWebService"));
assertNotNull(movieService);
IMoviesWS mv = movieService.getPort(IMoviesWS.class);
mv.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs",
1992));
assertEquals("List.size()", 1, movies.getMovies().size());
}
}
thank you for your patience!
And I hope this will help other noobs like me ))
--
View this message in context:
http://openejb.979440.n4.nabble.com/openEJB-fail-on-second-test-tp4401889p4411703.html
Sent from the OpenEJB User mailing list archive at Nabble.com.