Search in sources :

Example 1 with TrayRuntimeException

use of net.grandcentrix.tray.core.TrayRuntimeException in project tray by grandcentrix.

the class ContentProviderStorage method put.

/**
     * same as {@link #put(String, Object)} but with an additional migration key to save where the
     * data came from. Putting data twice with the same param migraionKey does not override the
     * already saved data. This should prevent migrating data multiple times while the data my be
     * edited with {@link #put(String, Object)}.
     *
     * @param key          where to save
     * @param migrationKey where the data came from
     * @param data         what to save
     * @return whether the put was successful
     */
@Override
public boolean put(@NonNull final String key, @Nullable final String migrationKey, @Nullable final Object data) {
    if (getType() == Type.UNDEFINED) {
        throw new TrayRuntimeException("writing data into a storage with type UNDEFINED is forbidden. Only Read and delete is allowed.");
    }
    final String value = data == null ? null : String.valueOf(data);
    final Uri uri = mTrayUri.builder().setType(getType()).setModule(getModuleName()).setKey(key).build();
    return mProviderHelper.persist(uri, value, migrationKey);
}
Also used : TrayRuntimeException(net.grandcentrix.tray.core.TrayRuntimeException) Uri(android.net.Uri)

Example 2 with TrayRuntimeException

use of net.grandcentrix.tray.core.TrayRuntimeException in project tray by grandcentrix.

the class ContentProviderStorageTest method testUndefinedTypeAccessErrors.

/**
     * writing data and version should fail for an undefined storage type
     */
public void testUndefinedTypeAccessErrors() throws Exception {
    final ContentProviderStorage storage = new ContentProviderStorage(getProviderMockContext(), "undefined", TrayStorage.Type.UNDEFINED);
    // put
    try {
        storage.put(TEST_KEY2, TEST_STRING);
        fail();
    } catch (TrayRuntimeException e) {
        assertTrue(e.getMessage().contains("UNDEFINED"));
    }
    try {
        storage.put(new TrayItem("undefined", TEST_KEY2, null, TEST_STRING, null, null));
        fail();
    } catch (TrayRuntimeException e) {
        assertTrue(e.getMessage().contains("UNDEFINED"));
    }
    try {
        final ContentProviderStorage someModule = new ContentProviderStorage(getProviderMockContext(), "someModule", TrayStorage.Type.USER);
        // without value -> no data reading -> no exception
        someModule.put(TEST_KEY, TEST_STRING);
        storage.annex(someModule);
        fail();
    } catch (TrayRuntimeException e) {
        assertTrue(e.getMessage().contains("UNDEFINED"));
    }
    // setVersion
    try {
        storage.setVersion(10);
        fail();
    } catch (TrayRuntimeException e) {
        assertTrue(e.getMessage().contains("UNDEFINED"));
    }
    assertEquals(TrayStorage.Type.UNDEFINED, storage.getType());
    storage.get(TEST_KEY);
    storage.getAll();
    storage.getVersion();
    storage.getModuleName();
}
Also used : TrayRuntimeException(net.grandcentrix.tray.core.TrayRuntimeException) TrayItem(net.grandcentrix.tray.core.TrayItem)

Aggregations

TrayRuntimeException (net.grandcentrix.tray.core.TrayRuntimeException)2 Uri (android.net.Uri)1 TrayItem (net.grandcentrix.tray.core.TrayItem)1