String javaRuntimeVersion = (String) serializationManager.deserialize(input); boolean cacheJavaClasses = UnderlyingSystemInfo.JAVA_RUNTIME_VERSION.equals(javaRuntimeVersion); Map<String, Collection<String>> fqnWithHashes = (Map<String, Collection<String>>) serializationManager.deserialize(input); for (Entry<String, Collection<String>> entry : fqnWithHashes.entrySet()) { String fqn = entry.getKey();
/** * Tests if the data deserialzied from empty buffer is <code>null</code>. * * @throws SerializationException * Serialization Exception */ @Test public void emptyBufferSerialization() throws SerializationException { // I need to create a new buffer, because clear on the buffer will not actually erase the // data in the buffer, but only move the pointers ByteBuffer newByteBuffer = ByteBuffer.allocateDirect(1024); ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(newByteBuffer); Input input = new Input(byteBufferInputStream); Object data = serializer.deserialize(input); assertThat(data, is(nullValue())); }
@Test public void cacheFileExists() throws Exception { when(configurationStorage.isClassCacheExistsOnCmr()).thenReturn(true); new File(TEST_CACHE_FILE).createNewFile(); Object javaRuntimeVersion = UnderlyingSystemInfo.JAVA_RUNTIME_VERSION; Object hashes = Collections.singletonMap("fqn", Collections.singleton("hash")); when(serializationManager.deserialize(Matchers.<Input> any())).thenReturn(javaRuntimeVersion).thenReturn(hashes); helper.afterPropertiesSet(); verify(prototypesProvider, times(1)).createSerializer(); verify(serializationManager, times(2)).deserialize(Matchers.<Input> any()); verify(executorService, times(1)).scheduleAtFixedRate(Matchers.<Runnable> any(), anyLong(), anyLong(), Matchers.<TimeUnit> any()); assertThat(helper.isEmpty(), is(false)); }
@Test public void cacheFileExistsCacheOnCmrNot() throws Exception { when(configurationStorage.isClassCacheExistsOnCmr()).thenReturn(false); new File(TEST_CACHE_FILE).createNewFile(); Object hashes = Collections.singleton("hash"); when(serializationManager.deserialize(Matchers.<Input> any())).thenReturn(hashes); helper.afterPropertiesSet(); verify(executorService, times(1)).scheduleAtFixedRate(Matchers.<Runnable> any(), anyLong(), anyLong(), Matchers.<TimeUnit> any()); assertThat(helper.isEmpty(), is(true)); assertThat(new File(TEST_CACHE_FILE).exists(), is(false)); verifyZeroInteractions(serializationManager); }
@Test public void cacheFileExistsJavaNotCached() throws Exception { when(configurationStorage.isClassCacheExistsOnCmr()).thenReturn(true); new File(TEST_CACHE_FILE).createNewFile(); Object javaRuntimeVersion = "some_other_version"; Object hashes = Collections.singletonMap("java.lang.String", Collections.singleton("hash")); when(serializationManager.deserialize(Matchers.<Input> any())).thenReturn(javaRuntimeVersion).thenReturn(hashes); helper.afterPropertiesSet(); verify(prototypesProvider, times(1)).createSerializer(); verify(serializationManager, times(2)).deserialize(Matchers.<Input> any()); verify(executorService, times(1)).scheduleAtFixedRate(Matchers.<Runnable> any(), anyLong(), anyLong(), Matchers.<TimeUnit> any()); assertThat(helper.isEmpty(), is(true)); }
@Test public void cacheFileExistsJavaCached() throws Exception { when(configurationStorage.isClassCacheExistsOnCmr()).thenReturn(true); new File(TEST_CACHE_FILE).createNewFile(); Object javaRuntimeVersion = UnderlyingSystemInfo.JAVA_RUNTIME_VERSION; Object hashes = Collections.singletonMap("java.lang.String", Collections.singleton("hash")); when(serializationManager.deserialize(Matchers.<Input> any())).thenReturn(javaRuntimeVersion).thenReturn(hashes); helper.afterPropertiesSet(); verify(prototypesProvider, times(1)).createSerializer(); verify(serializationManager, times(2)).deserialize(Matchers.<Input> any()); verify(executorService, times(1)).scheduleAtFixedRate(Matchers.<Runnable> any(), anyLong(), anyLong(), Matchers.<TimeUnit> any()); assertThat(helper.isEmpty(), is(false)); }
/** * Tests if the data de-serialzed from buffer with random data is <code>null</code>. * * @throws SerializationException * Serialization Exception */ @Test public void radomBufferDataSerialization() throws SerializationException { for (int i = 0; i < 64; i++) { byteBuffer.putInt(i); } byteBuffer.flip(); ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(byteBuffer); Input input = new Input(byteBufferInputStream); Object data = serializer.deserialize(input); assertThat(data, is(nullValue())); }
@Test public void load() throws Exception { String fqn = "fqn"; when(configurationStorage.isClassCacheExistsOnCmr()).thenReturn(true); new File(TEST_CACHE_FILE).createNewFile(); Object javaRuntimeVersion = UnderlyingSystemInfo.JAVA_RUNTIME_VERSION; Object hashes = Collections.singletonMap(fqn, Collections.emptyList()); when(serializationManager.deserialize(Matchers.<Input> any())).thenReturn(javaRuntimeVersion).thenReturn(hashes); helper.afterPropertiesSet(); boolean analyzed = helper.isAnalyzed(fqn); assertThat(analyzed, is(true)); } }
@Test public void load() throws Exception { String fqn = "fqn"; String hash = "hash"; when(configurationStorage.isClassCacheExistsOnCmr()).thenReturn(true); new File(TEST_CACHE_FILE).createNewFile(); Object javaRuntimeVersion = UnderlyingSystemInfo.JAVA_RUNTIME_VERSION; Object hashes = Collections.singletonMap(fqn, Collections.singleton(hash)); when(serializationManager.deserialize(Matchers.<Input> any())).thenReturn(javaRuntimeVersion).thenReturn(hashes); helper.afterPropertiesSet(); boolean sent = helper.isSent(fqn, hash); assertThat(sent, is(true)); } }
@Test public void cacheFileExistsException() throws Exception { when(configurationStorage.isClassCacheExistsOnCmr()).thenReturn(true); new File(TEST_CACHE_FILE).createNewFile(); when(serializationManager.deserialize(Matchers.<Input> any())).thenThrow(new SerializationException()); helper.afterPropertiesSet(); verify(prototypesProvider, times(1)).createSerializer(); verify(executorService, times(1)).scheduleAtFixedRate(Matchers.<Runnable> any(), anyLong(), anyLong(), Matchers.<TimeUnit> any()); assertThat(helper.isEmpty(), is(true)); }
/** * Performs the serialization of the given object to bytes and then performs de-serialization * from those bytes and returns the de-serialized object back. * * @param original * Original object. * @return De-serialized objects from bytes gotten from the serialization of original. * @throws SerializationException * If serialization fails. */ @SuppressWarnings("unchecked") private <T> T serializeBackAndForth(Object original) throws SerializationException { ByteBufferOutputStream byteBufferOutputStream = new ByteBufferOutputStream(byteBuffer); Output output = new Output(byteBufferOutputStream); serializer.serialize(original, output); byteBuffer.flip(); ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(byteBuffer); Input input = new Input(byteBufferInputStream); return (T) serializer.deserialize(input); }
/** * Performs the serialization of the given object to bytes and then performs de-serialization * from those bytes and returns the de-serialized object back. * * @param original * Original object. * @return De-serialized objects from bytes gotten from the serialization of original. * @throws SerializationException * If serialization fails. */ @SuppressWarnings("unchecked") private <T> T serializeBackAndForth(Object original) throws SerializationException { ByteBufferOutputStream byteBufferOutputStream = new ByteBufferOutputStream(byteBuffer); Output output = new Output(byteBufferOutputStream); serializer.serialize(original, output); byteBuffer.flip(); ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(byteBuffer); Input input = new Input(byteBufferInputStream); return (T) serializer.deserialize(input); }