I have two entity.
one is Profile:
package ry.enstudy.entity;

import java.io.Serializable;
import java.util.List;

import javax.jdo.annotations.*;

import com.google.appengine.api.users.User;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Queries(value = {...@query(name = "findByUser", value = "select from
Profile where user == :user")})
public class Profile implements Serializable {

        private static final long serialVersionUID = -5700704235163464277L;

        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value
= "true")
        private String id;

        @Persistent
        private User user;

        @Persistent
        private String test;

        @Order(extensions = @Extension(vendorName = "datanucleus", key =
"list-ordering", value = "createDate asc"))
        @Persistent(defaultFetchGroup = "true")
        private List<Sentence> sentences;


        public User getUser() {
                return user;
        }

        public void setUser(User user) {
                this.user = user;
        }

        public void setSentences(List<Sentence> sentences) {
                this.sentences = sentences;
        }

        public List<Sentence> getSentences() {
                return sentences;
        }

        public String getId() {
                return id;
        }

        public void setId(String id) {
                this.id = id;
        }

        public String getTest() {
                return test;
        }

        public void setTest(String test) {
                this.test = test;
        }
}

the other one is Sentence
package ry.enstudy.entity;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Sentence implements Serializable {

        private static final long serialVersionUID = -1183409823615443436L;

        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value
= "true")
        private String id;

        @Persistent
        private String content;

        @Persistent
        private int rememberState;

        @Persistent
        private Date createDate;

        @Persistent
        private Profile profile;

        public String getContent() {
                return content;
        }

        public void setContent(String content) {
                this.content = content;
        }

        public String getId() {
                return id;
        }

        public void setId(String id) {
                this.id = id;
        }

        public Date getCreateDate() {
                return createDate;
        }

        public void setCreateDate(Date createDate) {
                this.createDate = createDate;
        }

        public int getRememberState() {
                return rememberState;
        }

        public void setRememberState(int rememberState) {
                this.rememberState = rememberState;
        }

        public Profile getProfile() {
                return profile;
        }

        public void setProfile(Profile profile) {
                this.profile = profile;
        }

}

Profile own many Sentence.

This is my unit test:
package ry.enstudy.actions;

import static org.junit.Assert.*;

import java.io.File;
import java.util.Calendar;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.datastore.dev.LocalDatastoreService;
import com.google.appengine.tools.development.ApiProxyLocalImpl;
import com.google.apphosting.api.ApiProxy;

import ry.enstudy.entity.Profile;
import ry.enstudy.entity.Sentence;
import ry.enstudy.test.TestEnvironment;

public class RememberTaskActionTest{

        private static final PersistenceManagerFactory pmfInstance =
JDOHelper
                        .getPersistenceManagerFactory("transactions-optional");

        @Before
        public void beforeClass() {
                ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment());
                ApiProxy.setDelegate(new ApiProxyLocalImpl(new File(".")) {
                });
                ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) 
ApiProxy.getDelegate
();
                proxy.setProperty(LocalDatastoreService.NO_STORAGE_PROPERTY,
                                Boolean.TRUE.toString());
        }

        @After
        public void afterClass() {
                ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) 
ApiProxy.getDelegate
();
                LocalDatastoreService datastoreService = (LocalDatastoreService)
proxy
                                .getService("datastore_v3");
                datastoreService.clearProfiles();
                ApiProxy.setDelegate(null);
                ApiProxy.setEnvironmentForCurrentThread(null);
        }

        @Test
        public void TestAddTask() throws Exception {
                PersistenceManager pm = pmfInstance.getPersistenceManager();

                pm.currentTransaction().begin();
                Profile profile = new Profile();
                profile.setTest("ssss");
                pm.makePersistent(profile);
                Sentence sentence = new Sentence();
                sentence.setContent("This is test.");
                sentence.setCreateDate(Calendar.getInstance().getTime());
                profile.getSentences().add(sentence);
                Profile p = (Profile) pm.getObjectById(Profile.class, 
profile.getId
());
                assertEquals(1, p.getSentences().size()); // pass
                pm.currentTransaction().commit();
                pm.close();

                pm = pmfInstance.getPersistenceManager();
                pm.currentTransaction().begin();
                Sentence s = pm.getObjectById(Sentence.class, sentence.getId());
                assertEquals(s.getContent(), "This is test."); // pass
                pm.currentTransaction().commit();
                pm.close();

                pm = pmfInstance.getPersistenceManager();
                pm.currentTransaction().begin();
                Profile p2 = (Profile) pm.getObjectById(Profile.class, 
profile.getId
());
                assertEquals(1, p2.getSentences().size());// fail
                pm.currentTransaction().commit();
                pm.close();
        }

}

Any one could help me, why the third assert fail? thank you

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-java@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to