本文整理汇总了Java中org.spearal.DefaultSpearalFactory类的典型用法代码示例。如果您正苦于以下问题:Java DefaultSpearalFactory类的具体用法?Java DefaultSpearalFactory怎么用?Java DefaultSpearalFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DefaultSpearalFactory类属于org.spearal包,在下文中一共展示了DefaultSpearalFactory类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testIsProxy
import org.spearal.DefaultSpearalFactory; //导入依赖的package包/类
@Test
public void testIsProxy() throws IOException {
SpearalFactory clientFactory = new DefaultSpearalFactory(false);
SpearalFactory serverFactory = new DefaultSpearalFactory();
Person serverPerson1 = new Person("Bla", "Bla");
EntityManager entityManager = entityManagerFactory.createEntityManager();
PartialEntityResolver resolver = new PartialEntityResolver(entityManager);
Assert.assertTrue("Simple object", resolver.introspect(serverPerson1).isEmpty());
Contact serverContact1 = new Contact(serverPerson1, "bla", "06");
serverPerson1.getContacts().add(serverContact1);
Assert.assertTrue("Graph object", resolver.introspect(serverPerson1).isEmpty());
Person clientPerson2 = new Person("Bruce", "Willis");
Person serverPerson2 = clientEncodeServerDecode(clientFactory, serverFactory, clientPerson2, Person.class, "id", "version", "firstName");
PartialEntityMap map = resolver.introspect(serverPerson2);
Assert.assertFalse("Proxy object", map.isEmpty());
serverPerson2 = (Person)resolver.resolve(serverPerson2, map);
map = resolver.introspect(serverPerson2);
Assert.assertTrue("Non Proxy object", map.isEmpty());
Person clientPerson3 = new Person("Bruce", "Willis");
Contact clientContact3 = new Contact(clientPerson3, "blo", "06");
clientPerson3.getContacts().add(clientContact3);
Person serverPerson3 = clientEncodeServerDecode(clientFactory, serverFactory, clientPerson3, Contact.class, "id", "version", "person", "mobile");
Assert.assertFalse("Non proxy graph root", serverPerson3 instanceof PartialObjectProxy);
map = resolver.introspect(serverPerson3);
Assert.assertFalse("Proxy graph", map.isEmpty());
serverPerson3 = (Person)resolver.resolve(serverPerson3, map);
map = resolver.introspect(serverPerson3);
Assert.assertTrue("Non Proxy graph", map.isEmpty());
entityManager.close();
}
示例2: testIsProxy2
import org.spearal.DefaultSpearalFactory; //导入依赖的package包/类
@Test
public void testIsProxy2() throws Exception {
SpearalFactory serverFactory = new DefaultSpearalFactory();
Person serverPerson2 = new Person("Blo", "Blo");
serverPerson2 = mergeEntity(serverPerson2);
Person serverPerson1 = new Person("Bla", "Bla");
serverPerson1.setBestFriend(serverPerson2);
serverPerson1 = mergeEntity(serverPerson1);
serverPerson1 = findEntity(Person.class, serverPerson1.getId());
EntityManager entityManager = entityManagerFactory.createEntityManager();
PartialEntityResolver resolver = new PartialEntityResolver(entityManager);
Assert.assertTrue("Simple object", resolver.introspect(serverPerson1).isEmpty());
entityManager.close();
Person incomingPerson1 = new Person("Bla", "Bla");
incomingPerson1.setId(serverPerson1.getId());
Property[] properties = new Property[2];
properties[0] = new AnyProperty("id", AbstractEntity.class.getDeclaredField("id"), Person.class.getMethod("getId"), Person.class.getMethod("setId", Long.class));
properties[1] = new StringProperty("lastName", Person.class.getDeclaredField("lastName"), Person.class.getMethod("getLastName"), Person.class.getMethod("setLastName", String.class));
Person incomingPerson2 = (Person)new JavassistPartialObjectFactory().instantiatePartial(serverFactory.getContext(), Person.class, properties);
incomingPerson2.setId(serverPerson2.getId());
incomingPerson2.setLastName("Toto");
incomingPerson1.setBestFriend(incomingPerson2);
mergeEntity(incomingPerson1);
serverPerson1 = findEntity(Person.class, serverPerson1.getId());
Assert.assertEquals("Value updated", "Toto", serverPerson1.getBestFriend().getLastName());
}
示例3: testEmbeddedId
import org.spearal.DefaultSpearalFactory; //导入依赖的package包/类
@Test
public void testEmbeddedId() throws Exception {
Assert.assertNotNull(entityManagerFactory);
EntityWithEmbeddedId entity = new EntityWithEmbeddedId();
entity.setId(new EntityEmbeddedId("Joe", "Smith"));
entity.setAge(12);
entity.setPhones(Arrays.asList("0123456789", "9876543210"));
persistEntity(entity);
entity = new EntityWithEmbeddedId();
entity.setId(new EntityEmbeddedId("Joe", "Smith"));
entity.setAge(24);
SpearalFactory clientFactory = new DefaultSpearalFactory(false);
SpearalFactory serverFactory = new DefaultSpearalFactory();
entity = clientEncodeServerDecode(clientFactory, serverFactory, entity, EntityWithEmbeddedId.class, "id", "age");
Assert.assertTrue(entity instanceof PartialObjectProxy);
Assert.assertFalse(((PartialObjectProxy)entity).$isDefined("phones"));
try {
entity.getPhones();
Assert.fail("Should throw a UndefinedPropertyException");
}
catch (UndefinedPropertyException e) {
}
entity = mergeEntity(entity);
Assert.assertFalse(entity instanceof PartialObjectProxy);
Assert.assertNotNull(entity.getPhones());
Assert.assertFalse(entityManagerFactory.getPersistenceUnitUtil().isLoaded(entity.getPhones()));
entity = findEntity(EntityWithEmbeddedId.class, new EntityEmbeddedId("Joe", "Smith"), "getPhones");
Assert.assertEquals(24, entity.getAge());
Assert.assertEquals(Arrays.asList("0123456789", "9876543210"), entity.getPhones());
}
示例4: testNoPartial
import org.spearal.DefaultSpearalFactory; //导入依赖的package包/类
@Test
public void testNoPartial() throws IOException {
byte[] data = encode(new ChildBean());
SpearalFactory factory = new DefaultSpearalFactory();
SpearalDecoder decoder = factory.newDecoder(new ByteArrayInputStream(data));
decoder.readAny();
Assert.assertFalse(decoder.containsPartialObjects());
}
示例5: testIdClass
import org.spearal.DefaultSpearalFactory; //导入依赖的package包/类
@Test
public void testIdClass() throws Exception {
Assert.assertNotNull(entityManagerFactory);
ManagedType<?> managedType = entityManagerFactory.getMetamodel().managedType(EntityWithIdClass.class);
int idsCount = 0;
for (SingularAttribute<?, ?> attribute : managedType.getSingularAttributes()) {
if (attribute.isId())
idsCount++;
}
Assert.assertEquals(2, idsCount);
EntityWithIdClass entity = new EntityWithIdClass();
entity.setFirstName("Jim");
entity.setLastName("Hall");
entity.setAge(12);
entity.setPhones(Arrays.asList("0123456789", "9876543210"));
persistEntity(entity);
entity = new EntityWithIdClass();
entity.setFirstName("Jim");
entity.setLastName("Hall");
entity.setAge(24);
SpearalFactory clientFactory = new DefaultSpearalFactory(false);
SpearalFactory serverFactory = new DefaultSpearalFactory();
entity = clientEncodeServerDecode(clientFactory, serverFactory, entity, EntityWithIdClass.class, "firstName", "lastName", "age");
Assert.assertTrue(entity instanceof PartialObjectProxy);
Assert.assertFalse(((PartialObjectProxy)entity).$isDefined("phones"));
try {
entity.getPhones();
Assert.fail("Should throw a UndefinedPropertyException");
}
catch (UndefinedPropertyException e) {
}
entity = mergeEntity(entity);
Assert.assertFalse(entity instanceof PartialObjectProxy);
Assert.assertNotNull(entity.getPhones());
Assert.assertFalse(entityManagerFactory.getPersistenceUnitUtil().isLoaded(entity.getPhones()));
entity = findEntity(EntityWithIdClass.class, new EntityIdClass("Jim", "Hall"), "getPhones");
Assert.assertEquals(24, entity.getAge());
Assert.assertEquals(Arrays.asList("0123456789", "9876543210"), entity.getPhones());
}
示例6: initDefaultSpearalFactory
import org.spearal.DefaultSpearalFactory; //导入依赖的package包/类
@PostConstruct
private void initDefaultSpearalFactory() {
if (spearalFactory == null || spearalFactory.isUnsatisfied())
this.defaultFactory = new DefaultSpearalFactory();
}
示例7: testClassNotFoundBean
import org.spearal.DefaultSpearalFactory; //导入依赖的package包/类
@SuppressWarnings("boxing")
@Test
public void testClassNotFoundBean() throws IOException {
SimpleBean bean = new SimpleBean(true, 1, 0.1, "blabla");
SpearalFactory serverFactory = new DefaultSpearalFactory();
serverFactory.getContext().configure(new AliasStrategy() {
@Override
public String alias(Class<?> cls) {
return "org.error." + cls.getSimpleName();
}
@Override
public String unalias(String aliasedClassName) {
return aliasedClassName;
}
});
SpearalFactory clientFactory = new DefaultSpearalFactory();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
SpearalEncoder encoder = serverFactory.newEncoder(baos);
encoder.writeAny(bean);
byte[] bytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
SpearalDecoder decoder = clientFactory.newDecoder(bais);
decoder.printAny(clientFactory.newPrinter(printStream));
printStream.println();
bais.reset();
decoder = clientFactory.newDecoder(bais);
ClassNotFound clientBean = (ClassNotFound)decoder.readAny();
Assert.assertEquals(
clientBean.getClassNotFoundDescription(),
"org.error.SimpleBean#booleanValue,doubleValue,intValue,stringValue"
);
Assert.assertEquals(bean.isBooleanValue(), clientBean.get("booleanValue"));
Assert.assertEquals(Long.valueOf(bean.getIntValue()), clientBean.get("intValue"));
Assert.assertEquals(bean.getDoubleValue(), clientBean.get("doubleValue"));
Assert.assertEquals(bean.getStringValue(), clientBean.get("stringValue"));
bais.reset();
decoder = clientFactory.newDecoder(bais);
SimpleBean copy = decoder.readAny(SimpleBean.class);
Assert.assertEquals(bean, copy);
}
示例8: encode
import org.spearal.DefaultSpearalFactory; //导入依赖的package包/类
protected byte[] encode(Object o) throws IOException {
return encode(new DefaultSpearalFactory(), null, o);
}
示例9: decode
import org.spearal.DefaultSpearalFactory; //导入依赖的package包/类
protected Object decode(byte[] bytes) throws IOException {
return decode(new DefaultSpearalFactory(), bytes, null);
}
示例10: testMergeProxy
import org.spearal.DefaultSpearalFactory; //导入依赖的package包/类
@Test
public void testMergeProxy() throws Exception {
Assert.assertNotNull(entityManagerFactory);
Person clientPerson = new Person("Bruce", "Willis");
// Do not load any services here (pseudo-client application).
SpearalFactory clientFactory = new DefaultSpearalFactory(false);
SpearalFactory serverFactory = new DefaultSpearalFactory();
Person serverPerson = clientEncodeServerDecodeMerge(clientFactory, serverFactory, clientPerson, Person.class, "id", "version", "firstName");
Assert.assertNotNull("Id not null", serverPerson.getId());
Assert.assertEquals("Version 0", Long.valueOf(0L), serverPerson.getVersion());
serverPerson = findEntity(Person.class, serverPerson.getId());
Assert.assertEquals("Person firstName", clientPerson.getFirstName(), serverPerson.getFirstName());
clientPerson.setId(serverPerson.getId());
clientPerson.backdoorSetVersion(serverPerson.getVersion());
clientPerson.setFirstName("John");
clientPerson.setLastName("McLane");
serverPerson = clientEncodeServerDecodeMerge(clientFactory, serverFactory, clientPerson, Person.class, "id", "version", "firstName");
Assert.assertEquals("Person firstName", clientPerson.getFirstName(), serverPerson.getFirstName());
serverPerson = findEntity(Person.class, serverPerson.getId());
Assert.assertEquals("Person firstName", clientPerson.getFirstName(), serverPerson.getFirstName());
Assert.assertEquals("Version 1", Long.valueOf(1L), serverPerson.getVersion());
Person clientFriend = new Person("Hans", "Gruber");
clientPerson.backdoorSetVersion(serverPerson.getVersion());
clientPerson.setBestFriend(clientFriend);
serverPerson = clientEncodeServerDecodeMerge(clientFactory, serverFactory, clientPerson, Person.class, "id", "version", "firstName", "bestFriend");
Assert.assertEquals("Friend firstName", clientFriend.getFirstName(), serverPerson.getBestFriend().getFirstName());
Assert.assertEquals("Version 0", Long.valueOf(0L), serverPerson.getBestFriend().getVersion());
serverPerson = findEntity(Person.class, serverPerson.getId());
Assert.assertEquals("Version 2", Long.valueOf(2L), serverPerson.getVersion());
}