Search in sources :

Example 31 with Serializable

use of java.io.Serializable in project jersey by jersey.

the class ReadWriteLockDataStore method get.

public <T extends Serializable> T get(String key, Class<T> type) {
    requireNonNull(key, "The parameter 'key' must not be null");
    requireNonNull(type, "The parameter 'type' must not be null");
    readerLock.lock();
    try {
        Serializable object = datastore.get(key);
        return type.cast(object);
    } finally {
        readerLock.unlock();
    }
}
Also used : Serializable(java.io.Serializable)

Example 32 with Serializable

use of java.io.Serializable in project jersey by jersey.

the class CombinedFeedService method getAll.

@Override
public List<CombinedFeed> getAll() {
    //TODO ugly, for purposes of CRUD controller
    if (datastore instanceof ReadWriteLockDataStore) {
        ReadWriteLockDataStore rwDatastore = (ReadWriteLockDataStore) datastore;
        Collection<Serializable> entities = rwDatastore.getAll();
        return entities.parallelStream().filter(entity -> entity instanceof CombinedFeed).map(CombinedFeed.class::cast).collect(Collectors.toList());
    }
    return new ArrayList<>();
}
Also used : Serializable(java.io.Serializable) CombinedFeed(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed) ArrayList(java.util.ArrayList) ReadWriteLockDataStore(org.glassfish.jersey.examples.feedcombiner.store.ReadWriteLockDataStore)

Example 33 with Serializable

use of java.io.Serializable in project jersey by jersey.

the class ReadWriteLockDataStore method put.

public <T extends Serializable> Serializable put(String key, T data) {
    requireNonNull(key, "The parameter 'key' must not be null");
    writerLock.lock();
    try {
        if (data == null) {
            observers.forEach(observer -> observer.remove(key));
            return datastore.remove(key);
        }
        Serializable previousEntity = datastore.put(key, data);
        if (previousEntity == null) {
            observers.forEach(observer -> observer.save(data));
        }
        return previousEntity;
    } finally {
        writerLock.unlock();
    }
}
Also used : Serializable(java.io.Serializable)

Example 34 with Serializable

use of java.io.Serializable in project jersey by jersey.

the class ReadWriteLockDataStoreTest method testLoadDatastore.

@Test
public void testLoadDatastore() {
    // Insert new combined Feed
    String id = "1";
    CombinedFeed feed = new CombinedFeed.CombinedFeedBuilder(id, "http://localhost").title("title").description("description").refreshPeriod(5L).build();
    // call save mock
    observer.save(feed);
    // Create a datastore with one Combined Feed and serialize it
    String id2 = "2";
    CombinedFeed deserializedFeed = new CombinedFeed.CombinedFeedBuilder(id2, "http://localhost").title("deserialized_title").description("deserialized_description").refreshPeriod(5L).build();
    HashMap<String, Serializable> datastore = new HashMap<>();
    datastore.put(id2, deserializedFeed);
    // call mocks after a load of the datastore
    Capture<Set<String>> captureRemoveAll = EasyMock.newCapture();
    Capture<Collection<Serializable>> captureSaveAll = EasyMock.newCapture();
    observer.removeAll(capture(captureRemoveAll));
    observer.saveAll(capture(captureSaveAll));
    replayAll();
    // Insert feed into the old datastore
    testedClass.put(id, feed);
    byte[] serializedDatastore = SerializationUtils.serialize(datastore);
    // Load the serialized datastore
    ByteArrayInputStream input = new ByteArrayInputStream(serializedDatastore);
    try {
        testedClass.load(input);
        // Test that the new datastore does not contain old Combined Feed
        CombinedFeed previousCombinedFeed = testedClass.get(id, CombinedFeed.class);
        if (previousCombinedFeed != null) {
            fail("The previous combined feed should be deleted.");
        }
        // Test that the new datastore contains new Combined Feed
        CombinedFeed newCombinedFeed = testedClass.get(id2, CombinedFeed.class);
        assertEquals(deserializedFeed, newCombinedFeed);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    verifyAll();
    // Verify captured values
    // Check whether the registered observer was called because of removing entities
    Set<String> previousKeySet = captureRemoveAll.getValue();
    if (!previousKeySet.contains(id)) {
        fail("The previous keys should be deleted.");
    }
    // Check whether the registered observer was called because of saving entities
    Collection<Serializable> newlySavedEntities = captureSaveAll.getValue();
    assertEquals(1, newlySavedEntities.size());
    boolean exists = newlySavedEntities.stream().map(CombinedFeed.class::cast).anyMatch(entity -> Objects.equals(entity.getId(), id2));
    if (!exists) {
        fail("The new stored CombinedFeed was not found.");
    }
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) HashMap(java.util.HashMap) CombinedFeed(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) Collection(java.util.Collection) Test(org.junit.Test)

Example 35 with Serializable

use of java.io.Serializable in project jersey by jersey.

the class ReadWriteLockDataStoreTest method testRemoveEntity.

@Test
public void testRemoveEntity() {
    String id = "1";
    CombinedFeed feed = getCombinedFeed(id);
    observer.save(feed);
    observer.remove(id);
    replayAll();
    // save entity
    testedClass.put(id, feed);
    Serializable removedEntity = testedClass.put(id, null);
    verifyAll();
    if (!(removedEntity instanceof CombinedFeed)) {
        fail("The previous entity is not an instance of CombinedFeed");
    }
    assertEquals(feed, removedEntity);
}
Also used : Serializable(java.io.Serializable) CombinedFeed(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed) Test(org.junit.Test)

Aggregations

Serializable (java.io.Serializable)3100 Test (org.junit.Test)906 HashMap (java.util.HashMap)789 ArrayList (java.util.ArrayList)420 Map (java.util.Map)370 List (java.util.List)208 QName (org.alfresco.service.namespace.QName)197 IOException (java.io.IOException)174 Metacard (ddf.catalog.data.Metacard)145 NodeRef (org.alfresco.service.cmr.repository.NodeRef)135 LinkedHashMap (java.util.LinkedHashMap)128 HashSet (java.util.HashSet)122 Date (java.util.Date)114 ByteArrayInputStream (java.io.ByteArrayInputStream)82 ParserContext (org.mvel2.ParserContext)82 Set (java.util.Set)80 File (java.io.File)75 ObjectInputStream (java.io.ObjectInputStream)65 Session (org.hibernate.Session)65 URI (java.net.URI)64