/** * {@inheritDoc} */ @Override public void serialize(Object object, Output output) throws SerializationException { serialize(object, output, Collections.emptyMap()); }
/** * Creates a deep copy of the given object. * * @param object * object to clone * @param <T> * the class of the given object * @return the new object */ private synchronized <T> T copy(T object) { return serializationManager.copy(object); }
/** * {@inheritDoc} */ @Override public void afterPropertiesSet() throws Exception { initKryo(); }
/** * Instantiates the {@link SerializationManager}. * * @throws IOException * If {@link IOException} occurs. */ @BeforeMethod public void initSerializer() throws IOException { schemaManager.setSchemaListFile(new ClassPathResource(ClassSchemaManager.SCHEMA_DIR + "/" + ClassSchemaManager.SCHEMA_LIST_FILE, schemaManager.getClass().getClassLoader())); schemaManager.loadSchemasFromLocations(); serializer = new SerializationManager(); serializer.hibernateUtil = new HibernateUtil(); serializer.setSchemaManager(schemaManager); serializer.setKryoNetNetwork(new KryoNetNetwork()); serializer.initKryo(); }
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();
/** * 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); }
/** * Instantiates the {@link SerializationManager}. * * @throws IOException * If {@link IOException} occurs. */ @BeforeMethod public void initSerializer() throws IOException { schemaManager.setSchemaListFile(new ClassPathResource(ClassSchemaManager.SCHEMA_DIR + "/" + ClassSchemaManager.SCHEMA_LIST_FILE, schemaManager.getClass().getClassLoader())); schemaManager.loadSchemasFromLocations(); serializer = new SerializationManager(); serializer.setSchemaManager(schemaManager); serializer.setKryoNetNetwork(new KryoNetNetwork()); serializer.initKryo(); SerializationManagerPostProcessor postProcessor = new SerializationManagerPostProcessor(); postProcessor.postProcessAfterInitialization(serializer, "serializerTest"); }
/** * 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())); }
/** * 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); }
@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); }
/** * Creates the cloned {@link HttpTimerData} by using the kryo and {@link #serializationManager}. * Sets id of the clone to zero. * * @param original * Data to be cloned. * @return Cloned {@link HttpTimerData} with id zero. * @throws SerializationException * If serialization fails. */ private synchronized HttpTimerData getClone(HttpTimerData original) throws SerializationException { HttpTimerData httpTimerData = serializationManager.copy(original); httpTimerData.setId(0L); return httpTimerData; }
@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)); }
@Test(dataProvider = "classProvider") public void copy(Class<?> testingClass) throws IllegalArgumentException, IllegalAccessException, InstantiationException { Object object = getInstanceWithPrimitiveFieldsSet(testingClass); Object copy = serializer.copy(object); assertThat(copy, is(equalTo(object))); assertThat(copy == object, is(false)); }
@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)); }
when(httpTimerData.getHttpInfo()).thenReturn(originalInfo); HttpTimerData clone = mock(HttpTimerData.class); when(serializationManager.copy(Matchers.<HttpTimerData> any())).thenReturn(clone); HttpInfo httpInfo = mock(HttpInfo.class); when(query.getResultList()).thenReturn(Collections.singletonList(httpInfo));
/** * 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 deleteProfileRemovedFromEnvironment() throws Exception { Profile profile = new Profile(); profile.setName("test"); profile.setProfileData(new SensorAssignmentProfileData()); profile = manager.createProfile(profile); Environment environment = new Environment(); environment.setName("Test"); environment = manager.createEnvironment(environment); environment.setProfileIds(new HashSet<>(Collections.singleton(profile.getId()))); environment = manager.updateEnvironment(environment, true); when(serializationManager.copy(any(Environment.class))).thenAnswer(new Answer<Environment>() { @Override public Environment answer(InvocationOnMock invocation) throws Throwable { Environment env = (Environment) invocation.getArguments()[0]; Environment copy = new Environment(); copy.setId(env.getId()); copy.setName(env.getName()); copy.setRevision(env.getRevision()); copy.setProfileIds(new HashSet<>(env.getProfileIds())); return copy; } }); manager.deleteProfile(profile); assertThat(manager.getEnvironment(environment.getId()).getProfileIds(), is(not(equalTo(environment.getProfileIds())))); assertThat(manager.getEnvironment(environment.getId()).getProfileIds(), is(empty())); }
@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 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)); } }