Search in sources :

Example 1 with Person

use of org.infinispan.test.data.Person in project infinispan by infinispan.

the class JavaSerializationTranscoderTest method setUp.

@BeforeClass(alwaysRun = true)
public void setUp() {
    dataSrc = new Person("Joe");
    Address address = new Address();
    address.setCity("London");
    dataSrc.setAddress(address);
    transcoder = new JavaSerializationTranscoder(new ClassAllowList(Collections.singletonList(".*")));
    supportedMediaTypes = transcoder.getSupportedMediaTypes();
}
Also used : Address(org.infinispan.test.data.Address) JavaSerializationTranscoder(org.infinispan.encoding.impl.JavaSerializationTranscoder) ClassAllowList(org.infinispan.commons.configuration.ClassAllowList) Person(org.infinispan.test.data.Person) BeforeClass(org.testng.annotations.BeforeClass)

Example 2 with Person

use of org.infinispan.test.data.Person in project infinispan by infinispan.

the class DataConversionTest method testConversionWithListeners.

@Test
public void testConversionWithListeners() {
    ConfigurationBuilder cfg = new ConfigurationBuilder();
    withCacheManager(new CacheManagerCallable(createCacheManager(TestDataSCI.INSTANCE, cfg)) {

        @Override
        public void call() {
            Cache<String, Person> cache = cm.getCache();
            cm.getClassAllowList().addClasses(Person.class);
            // Obtain cache with custom valueEncoder
            Cache storeMarshalled = cache.getAdvancedCache().withEncoding(JavaSerializationEncoder.class);
            // Add a listener
            SimpleListener simpleListener = new SimpleListener();
            storeMarshalled.addListener(simpleListener);
            Person value = new Person();
            storeMarshalled.put("1", value);
            // Assert values returned are passed through the valueEncoder
            assertEquals(simpleListener.events.size(), 1);
            assertEquals(simpleListener.events.get(0).getKey(), "1");
            assertEquals(simpleListener.events.get(0).getValue(), value);
        }
    });
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) CacheManagerCallable(org.infinispan.test.CacheManagerCallable) Person(org.infinispan.test.data.Person) Cache(org.infinispan.Cache) AdvancedCache(org.infinispan.AdvancedCache) JavaSerializationEncoder(org.infinispan.commons.dataconversion.JavaSerializationEncoder) Test(org.testng.annotations.Test) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest)

Example 3 with Person

use of org.infinispan.test.data.Person in project infinispan by infinispan.

the class DataConversionTest method testReadUnencoded.

@Test
public void testReadUnencoded() {
    ConfigurationBuilder cfg = new ConfigurationBuilder();
    cfg.memory().storage(StorageType.OFF_HEAP);
    withCacheManager(new CacheManagerCallable(createCacheManager(TestDataSCI.INSTANCE, cfg)) {

        private final EncoderRegistry registry = TestingUtil.extractGlobalComponent(cm, EncoderRegistry.class);

        public Object asStored(Object object) {
            return registry.convert(object, APPLICATION_OBJECT, APPLICATION_PROTOSTREAM);
        }

        @Override
        public void call() {
            cm.getClassAllowList().addClasses(Person.class);
            Cache<String, Person> cache = cm.getCache();
            Person value = new Person();
            cache.put("1", value);
            // Read using default valueEncoder
            assertEquals(cache.get("1"), value);
            // Read unencoded
            Cache<?, ?> unencodedCache = cache.getAdvancedCache().withStorageMediaType();
            assertEquals(unencodedCache.get(asStored("1")), asStored(value));
        }
    });
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) CacheManagerCallable(org.infinispan.test.CacheManagerCallable) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) Person(org.infinispan.test.data.Person) Cache(org.infinispan.Cache) AdvancedCache(org.infinispan.AdvancedCache) Test(org.testng.annotations.Test) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest)

Example 4 with Person

use of org.infinispan.test.data.Person in project infinispan by infinispan.

the class ProtostreamTranscoderTest method testToFromObject.

@Test
public void testToFromObject() throws IOException {
    Person objectContent = new Person("value");
    final byte[] marshalled = toWrappedByteArray(ctx, objectContent);
    byte[] marshalledHex = Base16Codec.encode(marshalled).getBytes(UTF_8);
    // Converting from Object to protostream with different binary encodings
    Object result = transcoder.transcode(objectContent, APPLICATION_OBJECT, APPLICATION_PROTOSTREAM);
    assertArrayEquals(marshalled, (byte[]) result);
    result = transcoder.transcode(objectContent, APPLICATION_OBJECT, APPLICATION_PROTOSTREAM.withEncoding("hex"));
    assertEquals(Base16Codec.encode(marshalled), result);
    result = transcoder.transcode(objectContent, APPLICATION_OBJECT, APPLICATION_PROTOSTREAM.withEncoding("hex").withClassType(String.class));
    assertEquals(Base16Codec.encode(marshalled), result);
    // convert from protostream with different encodings back to object
    result = transcoder.transcode(marshalled, APPLICATION_PROTOSTREAM, APPLICATION_OBJECT);
    assertEquals(objectContent, result);
    result = transcoder.transcode(marshalledHex, APPLICATION_PROTOSTREAM.withEncoding("hex"), APPLICATION_OBJECT);
    assertEquals(objectContent, result);
}
Also used : Person(org.infinispan.test.data.Person) Test(org.testng.annotations.Test) AbstractTranscoderTest(org.infinispan.test.dataconversion.AbstractTranscoderTest)

Example 5 with Person

use of org.infinispan.test.data.Person in project infinispan by infinispan.

the class BaseStoreFunctionalTest method testPreloadStoredAsBinary.

public void testPreloadStoredAsBinary() {
    ConfigurationBuilder cb = getDefaultCacheConfiguration();
    createCacheStoreConfig(cb.persistence(), "testPreloadStoredAsBinary", true).memory().storageType(StorageType.BINARY);
    TestingUtil.defineConfiguration(cacheManager, "testPreloadStoredAsBinary", cb.build());
    Cache<String, Person> cache = cacheManager.getCache("testPreloadStoredAsBinary");
    cache.start();
    assert cache.getCacheConfiguration().persistence().preload();
    assertEquals(StorageType.BINARY, cache.getCacheConfiguration().memory().storageType());
    byte[] pictureBytes = new byte[] { 1, 82, 123, 19 };
    cache.put("k1", new Person("1"));
    cache.put("k2", new Person("2", null, pictureBytes, null, null, false), 111111, TimeUnit.MILLISECONDS);
    cache.put("k3", new Person("3", null, null, Sex.MALE, null, false), -1, TimeUnit.MILLISECONDS, 222222, TimeUnit.MILLISECONDS);
    cache.put("k4", new Person("4", new Address("Street", "City", 12345), null, null, null, true), 333333, TimeUnit.MILLISECONDS, 444444, TimeUnit.MILLISECONDS);
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("EST"));
    calendar.set(2009, Calendar.MARCH, 18, 3, 22, 57);
    // Chop off the last milliseconds as some databases don't have that high of accuracy
    calendar.setTimeInMillis((calendar.getTimeInMillis() / 1000) * 1000);
    Date infinispanBirthDate = calendar.getTime();
    Person infinispanPerson = createEmptyPerson("Infinispan");
    infinispanPerson.setBirthDate(infinispanBirthDate);
    infinispanPerson.setAcceptedToS(true);
    cache.put("Infinispan", infinispanPerson);
    // Just to prove nothing in memory even with stop
    cache.getAdvancedCache().getDataContainer().clear();
    assertEquals(createEmptyPerson("1"), cache.get("k1"));
    Person person2 = createEmptyPerson("2");
    person2.setPicture(pictureBytes);
    assertEquals(person2, cache.get("k2"));
    Person person3 = createEmptyPerson("3");
    person3.setSex(Sex.MALE);
    assertEquals(person3, cache.get("k3"));
    assertEquals(new Person("4", new Address("Street", "City", 12345), null, null, null, true), cache.get("k4"));
    assertEquals(infinispanPerson, cache.get("Infinispan"));
    cache.stop();
    cache.start();
    assertEquals(5, cache.entrySet().size());
    assertEquals(createEmptyPerson("1"), cache.get("k1"));
    assertEquals(person2, cache.get("k2"));
    assertEquals(person3, cache.get("k3"));
    assertEquals(new Person("4", new Address("Street", "City", 12345), null, null, null, true), cache.get("k4"));
    assertEquals(infinispanPerson, cache.get("Infinispan"));
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) PersistenceConfigurationBuilder(org.infinispan.configuration.cache.PersistenceConfigurationBuilder) Address(org.infinispan.test.data.Address) Calendar(java.util.Calendar) Person(org.infinispan.test.data.Person) Date(java.util.Date)

Aggregations

Person (org.infinispan.test.data.Person)42 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)10 Test (org.testng.annotations.Test)10 Address (org.infinispan.test.data.Address)9 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)8 Marshaller (org.infinispan.commons.marshall.Marshaller)5 PersistenceMarshaller (org.infinispan.marshall.persistence.PersistenceMarshaller)5 TransactionManager (javax.transaction.TransactionManager)4 Cache (org.infinispan.Cache)4 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)4 FunctionalTestUtils.await (org.infinispan.functional.FunctionalTestUtils.await)4 AssertJUnit.assertTrue (org.testng.AssertJUnit.assertTrue)4 BeforeClass (org.testng.annotations.BeforeClass)4 Properties (java.util.Properties)3 AdvancedCache (org.infinispan.AdvancedCache)3 GenericJBossMarshaller (org.infinispan.jboss.marshalling.commons.GenericJBossMarshaller)3 DelegatingUserMarshaller (org.infinispan.marshall.core.impl.DelegatingUserMarshaller)3 CacheManagerCallable (org.infinispan.test.CacheManagerCallable)3 String.format (java.lang.String.format)2 ArrayList (java.util.ArrayList)2