public void testMapMarshalling() throws Exception { Map m1 = new HashMap(); Map m2 = new TreeMap(); Map m3 = new HashMap(); Map<Integer, GlobalTransaction> m4 = new FastCopyHashMap<Integer, GlobalTransaction>(); for (int i = 0; i < 10; i++) { JGroupsAddress jGroupsAddress = new JGroupsAddress(new IpAddress(1000 * i)); GlobalTransaction gtx = gtf.newGlobalTransaction(jGroupsAddress, false); m1.put(1000 * i, gtx); m2.put(1000 * i, gtx); m4.put(1000 * i, gtx); } Map m5 = Immutables.immutableMapWrap(m3); marshallAndAssertEquality(m1); marshallAndAssertEquality(m2); byte[] bytes = marshaller.objectToByteBuffer(m4); Map<Integer, GlobalTransaction> m4Read = (Map<Integer, GlobalTransaction>) marshaller.objectFromByteBuffer(bytes); for (Map.Entry<Integer, GlobalTransaction> entry : m4.entrySet()) { assert m4Read.get(entry.getKey()).equals(entry.getValue()) : "Writen[" + entry.getValue() + "] and read[" + m4Read.get(entry.getKey()) + "] objects should be the same"; } marshallAndAssertEquality(m5); }
public void testMarshalledValueGetMarshalling() throws Exception { Pojo ext = new Pojo(); MarshalledValue mv = new MarshalledValue(ext, true, marshaller); byte[] bytes = marshaller.objectToByteBuffer(mv); MarshalledValue rmv = (MarshalledValue) marshaller.objectFromByteBuffer(bytes); assert rmv.equals(mv) : "Writen[" + mv + "] and read[" + rmv + "] objects should be the same"; assert rmv.get() instanceof Pojo; }
public void testMIMECacheEntryMarshalling() throws Exception { MIMECacheEntry entry = new MIMECacheEntry("rm", new byte[] {1, 2, 3}); byte[] bytes = marshaller.objectToByteBuffer(entry); MIMECacheEntry rEntry = (MIMECacheEntry) marshaller.objectFromByteBuffer(bytes); assert Arrays.equals(rEntry.data, entry.data); assert rEntry.contentType.equals(entry.contentType); assert rEntry.lastModified == entry.lastModified; }
public void testExceptionResponse() throws Exception { ExceptionResponse er = new ExceptionResponse(new TimeoutException()); byte[] bytes = marshaller.objectToByteBuffer(er); ExceptionResponse rer = (ExceptionResponse) marshaller.objectFromByteBuffer(bytes); assert rer.getException().getClass().equals(er.getException().getClass()) : "Writen[" + er.getException().getClass() + "] and read[" + rer.getException().getClass() + "] objects should be the same"; }
public void testBucketMarshalling() throws Exception { ImmortalCacheEntry entry1 = (ImmortalCacheEntry) TestInternalCacheEntryFactory.create( "key", "value", System.currentTimeMillis() - 1000, -1, System.currentTimeMillis(), -1); MortalCacheEntry entry2 = (MortalCacheEntry) TestInternalCacheEntryFactory.create( "key", "value", System.currentTimeMillis() - 1000, 200000, System.currentTimeMillis(), -1); TransientCacheEntry entry3 = (TransientCacheEntry) TestInternalCacheEntryFactory.create( "key", "value", System.currentTimeMillis() - 1000, -1, System.currentTimeMillis(), 4000000); TransientMortalCacheEntry entry4 = (TransientMortalCacheEntry) TestInternalCacheEntryFactory.create( "key", "value", System.currentTimeMillis() - 1000, 200000, System.currentTimeMillis(), 4000000); Bucket b = new Bucket(); b.setBucketId(0); b.addEntry(entry1); b.addEntry(entry2); b.addEntry(entry3); b.addEntry(entry4); byte[] bytes = marshaller.objectToByteBuffer(b); Bucket rb = (Bucket) marshaller.objectFromByteBuffer(bytes); assert rb.getEntries().equals(b.getEntries()) : "Writen[" + b.getEntries() + "] and read[" + rb.getEntries() + "] objects should be the same"; }
public void testErrorUnmarshallInputStreamAvailable() throws Exception { byte[] bytes = marshaller.objectToByteBuffer("23"); Object o = marshaller.objectFromInputStream( new ByteArrayInputStream(bytes) { @Override public int available() { return 0; } }); assertEquals("23", o); }
public void testErrorUnmarshalling() throws Exception { Pojo pojo = new PojoWhichFailsOnUnmarshalling(); byte[] bytes = marshaller.objectToByteBuffer(pojo); try { marshaller.objectFromByteBuffer(bytes); } catch (IOException e) { log.info("Log exception for output format verification", e); TraceInformation inf = (TraceInformation) e.getCause(); assert inf.toString() .contains( "in object of type org.infinispan.marshall.VersionAwareMarshallerTest$PojoWhichFailsOnUnmarshalling"); } }
public void testNonSerializable() throws Exception { try { marshaller.objectToByteBuffer(new Object()); } catch (NotSerializableException e) { log.info("Log exception for output format verification", e); TraceInformation inf = (TraceInformation) e.getCause(); assert inf.toString().contains("in object java.lang.Object@"); } }
@Inject public void inject( GlobalConfiguration globalCfg, Configuration cfg, InvocationContextContainer icc, ExternalizerTable extTable) { super.inject(extTable); this.marshaller = createMarshaller(globalCfg, cfg.getClassLoader()); this.marshaller.inject(cfg, null, icc); }
public void testNestedNonSerializable() throws Exception { PutKeyValueCommand cmd = new PutKeyValueCommand("k", new Object(), false, null, 0, 0, Collections.<Flag>emptySet()); try { marshaller.objectToByteBuffer(cmd); } catch (NotSerializableException e) { log.info("Log exception for output format verification", e); TraceInformation inf = (TraceInformation) e.getCause(); assert inf.toString().contains("in object java.lang.Object@"); assert inf.toString().contains("in object org.infinispan.commands.write.PutKeyValueCommand@"); } }
public void testAtomicHashMap() throws Exception { AtomicHashMap<String, String> m = new AtomicHashMap<String, String>(); m.initForWriting(); m.put("k1", "v1"); m.put("k1", "v2"); m.put("k1", "v3"); assert m.size() == 1; byte[] bytes = marshaller.objectToByteBuffer(m); m = (AtomicHashMap<String, String>) marshaller.objectFromByteBuffer(bytes); for (Map.Entry<String, String> entry : m.entrySet()) { assert m.get(entry.getKey()).equals(entry.getValue()); } assert m.size() == 1; m = new AtomicHashMap<String, String>(); assert m.isEmpty(); bytes = marshaller.objectToByteBuffer(m); m = (AtomicHashMap<String, String>) marshaller.objectFromByteBuffer(bytes); assert m.isEmpty(); m = new AtomicHashMap<String, String>(); m.initForWriting(); m.put("k1", "v1"); m.put("k2", "v2"); m.put("k3", "v3"); m.remove("k1"); assert m.size() == 2; bytes = marshaller.objectToByteBuffer(m); m = (AtomicHashMap<String, String>) marshaller.objectFromByteBuffer(bytes); for (Map.Entry<String, String> entry : m.entrySet()) { assert m.get(entry.getKey()).equals(entry.getValue()); } assert m.size() == 2; m = new AtomicHashMap<String, String>(); m.initForWriting(); m.put("k5", "v1"); m.put("k5", "v2"); m.put("k5", "v3"); m.clear(); assert m.isEmpty(); bytes = marshaller.objectToByteBuffer(m); m = (AtomicHashMap<String, String>) marshaller.objectFromByteBuffer(bytes); for (Map.Entry<String, String> entry : m.entrySet()) { assert m.get(entry.getKey()).equals(entry.getValue()); } assert m.isEmpty(); }
public void testStateTransferControlCommand() throws Exception { Cache<Object, Object> cache = cm.getCache(); String cacheName = EmbeddedCacheManager.DEFAULT_CACHE_NAME; ImmortalCacheEntry entry1 = (ImmortalCacheEntry) TestInternalCacheEntryFactory.create( "key", "value", System.currentTimeMillis() - 1000, -1, System.currentTimeMillis(), -1); Collection<InternalCacheEntry> state = new ArrayList<InternalCacheEntry>(); state.add(entry1); Address a1 = new JGroupsAddress(UUID.randomUUID()); Address a2 = new JGroupsAddress(UUID.randomUUID()); Address a3 = new JGroupsAddress(UUID.randomUUID()); List<Address> oldAddresses = new ArrayList<Address>(); oldAddresses.add(a1); oldAddresses.add(a2); DefaultConsistentHashFactory chf = new DefaultConsistentHashFactory(); DefaultConsistentHash oldCh = chf.create(new MurmurHash3(), 2, 3, oldAddresses); List<Address> newAddresses = new ArrayList<Address>(); newAddresses.add(a1); newAddresses.add(a2); newAddresses.add(a3); DefaultConsistentHash newCh = chf.create(new MurmurHash3(), 2, 3, newAddresses); StateRequestCommand c14 = new StateRequestCommand( cacheName, StateRequestCommand.Type.START_STATE_TRANSFER, a1, 99, null); byte[] bytes = marshaller.objectToByteBuffer(c14); marshaller.objectFromByteBuffer(bytes); bytes = marshaller.objectToByteBuffer(c14); marshaller.objectFromByteBuffer(bytes); bytes = marshaller.objectToByteBuffer(c14); marshaller.objectFromByteBuffer(bytes); }
@Override @Stop(priority = 11) // Stop after RPCManager to avoid send/receive and marshaller not being ready public void stop() { super.stop(); }
protected void marshallAndAssertEquality(Object writeObj) throws Exception { byte[] bytes = marshaller.objectToByteBuffer(writeObj); Object readObj = marshaller.objectFromByteBuffer(bytes); assert readObj.equals(writeObj) : "Writen[" + writeObj + "] and read[" + readObj + "] objects should be the same"; }
public void testReplicableCommandsMarshalling() throws Exception { String cacheName = EmbeddedCacheManager.DEFAULT_CACHE_NAME; ClusteredGetCommand c2 = new ClusteredGetCommand("key", cacheName, Collections.<Flag>emptySet()); marshallAndAssertEquality(c2); // SizeCommand does not have an empty constructor, so doesn't look to be one that is // marshallable. GetKeyValueCommand c4 = new GetKeyValueCommand("key", null, Collections.<Flag>emptySet()); byte[] bytes = marshaller.objectToByteBuffer(c4); GetKeyValueCommand rc4 = (GetKeyValueCommand) marshaller.objectFromByteBuffer(bytes); assert rc4.getCommandId() == c4.getCommandId() : "Writen[" + c4.getCommandId() + "] and read[" + rc4.getCommandId() + "] objects should be the same"; assert Arrays.equals(rc4.getParameters(), c4.getParameters()) : "Writen[" + c4.getParameters() + "] and read[" + rc4.getParameters() + "] objects should be the same"; PutKeyValueCommand c5 = new PutKeyValueCommand("k", "v", false, null, 0, 0, Collections.<Flag>emptySet()); marshallAndAssertEquality(c5); RemoveCommand c6 = new RemoveCommand("key", null, null, Collections.<Flag>emptySet()); marshallAndAssertEquality(c6); // EvictCommand does not have an empty constructor, so doesn't look to be one that is // marshallable. InvalidateCommand c7 = new InvalidateCommand(null, null, "key1", "key2"); bytes = marshaller.objectToByteBuffer(c7); InvalidateCommand rc7 = (InvalidateCommand) marshaller.objectFromByteBuffer(bytes); assert rc7.getCommandId() == c7.getCommandId() : "Writen[" + c7.getCommandId() + "] and read[" + rc7.getCommandId() + "] objects should be the same"; assert Arrays.equals(rc7.getParameters(), c7.getParameters()) : "Writen[" + c7.getParameters() + "] and read[" + rc7.getParameters() + "] objects should be the same"; InvalidateCommand c71 = new InvalidateL1Command( false, null, null, null, null, Collections.<Flag>emptySet(), "key1", "key2"); bytes = marshaller.objectToByteBuffer(c71); InvalidateCommand rc71 = (InvalidateCommand) marshaller.objectFromByteBuffer(bytes); assert rc71.getCommandId() == c71.getCommandId() : "Writen[" + c71.getCommandId() + "] and read[" + rc71.getCommandId() + "] objects should be the same"; assert Arrays.equals(rc71.getParameters(), c71.getParameters()) : "Writen[" + c71.getParameters() + "] and read[" + rc71.getParameters() + "] objects should be the same"; ReplaceCommand c8 = new ReplaceCommand("key", "oldvalue", "newvalue", null, 0, 0, Collections.EMPTY_SET); marshallAndAssertEquality(c8); ClearCommand c9 = new ClearCommand(); bytes = marshaller.objectToByteBuffer(c9); ClearCommand rc9 = (ClearCommand) marshaller.objectFromByteBuffer(bytes); assert rc9.getCommandId() == c9.getCommandId() : "Writen[" + c9.getCommandId() + "] and read[" + rc9.getCommandId() + "] objects should be the same"; assert Arrays.equals(rc9.getParameters(), c9.getParameters()) : "Writen[" + c9.getParameters() + "] and read[" + rc9.getParameters() + "] objects should be the same"; Map m1 = new HashMap(); for (int i = 0; i < 10; i++) { GlobalTransaction gtx = gtf.newGlobalTransaction(new JGroupsAddress(new IpAddress(1000 * i)), false); m1.put(1000 * i, gtx); } PutMapCommand c10 = new PutMapCommand(m1, null, 0, 0, Collections.<Flag>emptySet()); marshallAndAssertEquality(c10); Address local = new JGroupsAddress(new IpAddress(12345)); GlobalTransaction gtx = gtf.newGlobalTransaction(local, false); PrepareCommand c11 = new PrepareCommand(cacheName, gtx, true, c5, c6, c8, c10); marshallAndAssertEquality(c11); CommitCommand c12 = new CommitCommand(cacheName, gtx); marshallAndAssertEquality(c12); RollbackCommand c13 = new RollbackCommand(cacheName, gtx); marshallAndAssertEquality(c13); MultipleRpcCommand c99 = new MultipleRpcCommand( Arrays.<ReplicableCommand>asList(c2, c5, c6, c8, c10, c12, c13), cacheName); marshallAndAssertEquality(c99); }
public void testMarshallingNestedSerializableSubclass() throws Exception { Child1 child1Obj = new Child1(1234, "1234"); Child2 child2Obj = new Child2(2345, "2345", child1Obj); byte[] bytes = marshaller.objectToByteBuffer(child2Obj); marshaller.objectFromByteBuffer(bytes); }
public void testInternalCacheValueMarshalling() throws Exception { ImmortalCacheValue value1 = (ImmortalCacheValue) TestInternalCacheEntryFactory.createValue( "value", System.currentTimeMillis() - 1000, -1, System.currentTimeMillis(), -1); byte[] bytes = marshaller.objectToByteBuffer(value1); ImmortalCacheValue rvalue1 = (ImmortalCacheValue) marshaller.objectFromByteBuffer(bytes); assert rvalue1.getValue().equals(value1.getValue()) : "Writen[" + rvalue1.getValue() + "] and read[" + value1.getValue() + "] objects should be the same"; MortalCacheValue value2 = (MortalCacheValue) TestInternalCacheEntryFactory.createValue( "value", System.currentTimeMillis() - 1000, 200000, System.currentTimeMillis(), -1); bytes = marshaller.objectToByteBuffer(value2); MortalCacheValue rvalue2 = (MortalCacheValue) marshaller.objectFromByteBuffer(bytes); assert rvalue2.getValue().equals(value2.getValue()) : "Writen[" + rvalue2.getValue() + "] and read[" + value2.getValue() + "] objects should be the same"; TransientCacheValue value3 = (TransientCacheValue) TestInternalCacheEntryFactory.createValue( "value", System.currentTimeMillis() - 1000, -1, System.currentTimeMillis(), 4000000); bytes = marshaller.objectToByteBuffer(value3); TransientCacheValue rvalue3 = (TransientCacheValue) marshaller.objectFromByteBuffer(bytes); assert rvalue3.getValue().equals(value3.getValue()) : "Writen[" + rvalue3.getValue() + "] and read[" + value3.getValue() + "] objects should be the same"; TransientMortalCacheValue value4 = (TransientMortalCacheValue) TestInternalCacheEntryFactory.createValue( "value", System.currentTimeMillis() - 1000, 200000, System.currentTimeMillis(), 4000000); bytes = marshaller.objectToByteBuffer(value4); TransientMortalCacheValue rvalue4 = (TransientMortalCacheValue) marshaller.objectFromByteBuffer(bytes); assert rvalue4.getValue().equals(value4.getValue()) : "Writen[" + rvalue4.getValue() + "] and read[" + value4.getValue() + "] objects should be the same"; }
@Start(priority = 7) // should start before RPCManager public void start() { super.start(); }