package com.hoo.test.morphia;
import java.net.UnknownHostException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.code.morphia.Datastore;
import com.google.code.morphia.EntityInterceptor;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.mapping.MappedClass;
import com.google.code.morphia.mapping.Mapper;
import com.google.code.morphia.mapping.cache.EntityCache;
import com.hoo.entity.User;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**
* <b>function:</b> morphia对象的简单测试
* @author hoojo
* @createDate 2011-5-31上午11:30:20
* @file MorphiaTest.java
* @package com.hoo.test
* @project Morphia
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class MorphiaTest {
private Mongo mongo;
private Morphia morphia;
@Before
public void init() {
try {
mongo = new Mongo();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
morphia = new Morphia();
}
private void print(Object o) {
if (o != null) {
System.out.println(o.toString());
}
}
/**
* <b>function:</b> morphia对象的简单测试
* @author hoojo
*/
@SuppressWarnings("deprecation")
@Test
public void testMorphia() {
// 创建一个Datastore,过时的方法不推荐使用
Datastore ds = morphia.createDatastore("myTestDB");
print("createDatastore: " + ds);
// 创建ds
ds = morphia.createDatastore(mongo, "myTestDB");
print("createDatastore: " + ds);
// 创建一个带用户名、密码的ds
//ds = morphia.createDatastore(mongo, "myTestDB", "admin", new char[] { '1', '2', '3', '4', '5', '6' });
// 设置操作资源对象,这里设置User.class 就可以完成对User的一系列操作
//morphia.map(User.class);
//morphia.mapPackage("com.hoo.entity");//会把整个包下面的类都加载进来
// 将对象转成DBObject
print("toDBObject: " + morphia.toDBObject(new User(System.currentTimeMillis(), "jackson", true, 22, null)));
// 将参数2转换成参数1的类型
print("fromDBObject: " + morphia.fromDBObject(User.class, BasicDBObjectBuilder.start("sex", true).get()));
print("getMapper: " + morphia.getMapper());
print("isMapped: " + morphia.isMapped(User.class));
}
/**
* <b>function:</b> 对Mapper对象相关操作
* @author hoojo
* @createDate 2012-2-16下午10:20:38
*/
@Test
public void testMapper() {
Mapper mapper = morphia.getMapper();
// 添加对象映射
print("addMappedClass: " + mapper.addMappedClass(User.class));
//print(mapper.addMappedClass(mapper.addMappedClass(User.class));
// 创建实体缓存
print("createEntityCache: " + mapper.createEntityCache());
print(mapper.getCollectionName("myTestDB"));
print(mapper.getConverters());
User user = new User(System.currentTimeMillis(), "jackson", true, 22, null);
user.setId(1306814012734L);
print(mapper.getId(user));
for (EntityInterceptor ei : mapper.getInterceptors()) {
System.out.println("EntityInterceptor: " + ei);
}
// 查询主键
print("getKey: " + mapper.getKey(user));
// 所有已经映射的class
for (MappedClass mc : mapper.getMappedClasses()) {
System.out.println("getMappedClasses: " + mc);
}
print("mcMap: " + mapper.getMCMap());
print("getOptions: " + mapper.getOptions());
print("keyToRef: " + mapper.keyToRef(mapper.getKey(user)));
print("refToKey: " + mapper.refToKey(mapper.keyToRef(mapper.getKey(user))));
}
/**
* <b>function:</b> 实体缓存
* @author hoojo
*/
@Test
public void testEntityCache() {
EntityCache ec = morphia.getMapper().createEntityCache();
print("EntityCache: " + ec);
Datastore ds = morphia.createDatastore(mongo, "myTestDB");
User user = new User(System.currentTimeMillis(), "jackson", true, 22, null);
user.setId(1306814012734L);
// 添加实体
ec.putEntity(ds.getKey(user), user);
// 代理
ec.putProxy(ds.getKey(user), user);
print("getKey: " + ds.getKey(user));
print("getProxy: " + ec.getProxy(ds.getKey(user)));
print("getEntity: " + ec.getEntity(ds.getKey(user)));
print(ec.exists(ds.getKey(user)));
print("stats: " + ec.stats());
}
@After
public void destory() {
mongo = null;
morphia = null;
System.gc();
}
}