Skip to content

Commit

Permalink
@wip #77 mitigate some bugs from sonarcloud reports
Browse files Browse the repository at this point in the history
  • Loading branch information
xcesco committed Nov 23, 2020
1 parent e4d294f commit 967dcc3
Show file tree
Hide file tree
Showing 30 changed files with 1,045 additions and 1,140 deletions.
8 changes: 5 additions & 3 deletions kripton-android-core/kripton-android-core.iml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.reactivex.rxjava2:rxjava:2.2.10" level="project" />
<orderEntry type="module" module-name="kripton-core" />
<orderEntry type="module" module-name="kripton-arch-integration" scope="PROVIDED" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.reactivex.rxjava2:rxjava:2.2.10" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.reactivestreams:reactive-streams:1.0.2" level="project" />
<orderEntry type="module" module-name="kripton-core" />
<orderEntry type="module" module-name="kripton-arch-integration" scope="PROVIDED" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.reactivex.rxjava2:rxjava:2.2.19" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.reactivestreams:reactive-streams:1.0.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.robolectric:robolectric:3.1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.robolectric:robolectric-annotations:3.1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.robolectric:robolectric-resources:3.1.4" level="project" />
Expand Down Expand Up @@ -72,7 +74,7 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: xpp3:xpp3:1.1.4c" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.json:json:20080701" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.android:support-v4:r7" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.unitils:unitils-core:3.4.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: commons-lang:commons-lang:2.3" level="project" />
Expand Down
4 changes: 2 additions & 2 deletions kripton-android-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>com.abubusoft</groupId>
<artifactId>kripton-parent</artifactId>
<version>7.0.0-rc.8</version>
<version>7.0.0-rc.9</version>
<relativePath>../kripton-parent/pom.xml</relativePath>
</parent>

Expand All @@ -25,7 +25,7 @@
<description>Kripton Persistence Library for Android platform - core module for android modules</description>

<properties>
<kripton.version>7.0.0-rc.8</kripton.version>
<kripton.version>7.0.0-rc.9</kripton.version>

<!-- dependencies version -->
<jackson.version>2.11.2</jackson.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,90 +1,91 @@
package com.abubusoft.kripton.androidx.livedata;

import java.util.Collections;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.paging.DataSource;
import androidx.paging.PositionalDataSource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* A {@link PositionalDataSource} that loads entities based on an ObjectBox
* {@link Query} using offset and limit to implement paging support. The data
* source is invalidated if the query results change.
*/
public class KriptonXDataSource<T> extends PositionalDataSource<T> {

private final PagedLiveData<T> query;

private final Observer<T> observer;

public static class Factory<Item> extends DataSource.Factory<Integer, Item> {

private final PagedLiveData<Item> query;

public Factory(PagedLiveData<Item> query) {
this.query = query;
}

@Override
public KriptonXDataSource<Item> create() {
return new KriptonXDataSource<>(query);
}
}

public KriptonXDataSource(PagedLiveData<T> query) {
this.query = query;
observer = new Observer<T>() {

@Override
public void onChanged(T t) {
invalidate();

}
};

query.observeForever(observer);
// observer will be automatically removed once GC'ed
// query. subscribe().onlyChanges().weak().observer(observer);
}

@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<T> callback) {
// note: limiting to int should be fine for Android apps
// TODO ripristinare query.count();
int totalCount = query.getTotalElements();
if (totalCount == 0) {
callback.onResult(Collections.<T>emptyList(), 0, 0);
return;
}

int position = computeInitialLoadPosition(params, totalCount);
int loadSize = computeInitialLoadSize(params, position, totalCount);

List<T> list = loadRange(position, loadSize);
if (list.size() == loadSize) {
callback.onResult(list, position, totalCount);
} else {
invalidate(); // size doesn't match request - DB modified between
// count and load
}
}

@Override
public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback<T> callback) {
callback.onResult(loadRange(params.startPosition, params.loadSize));
}

private List<T> loadRange(int startPosition, int loadCount) {
/* private void loadRange(int startPosition, int loadCount) { */
// note: find interprets loadCount 0 as no limit
//query.moveTo(startPosition, loadCount);
query.createPageRequestBuilder().offset(startPosition).pageSize(loadCount).apply();
return null;
}
private final PagedLiveData<T> query;

private final Observer<T> observer;

public static class Factory<Item> extends DataSource.Factory<Integer, Item> {

private final PagedLiveData<Item> query;

public Factory(PagedLiveData<Item> query) {
this.query = query;
}

@Override
public KriptonXDataSource<Item> create() {
return new KriptonXDataSource<>(query);
}
}

public KriptonXDataSource(PagedLiveData<T> query) {
this.query = query;

observer = new Observer<T>() {

@Override
public void onChanged(T t) {
invalidate();

}
};

query.observeForever(observer);
// observer will be automatically removed once GC'ed
// query. subscribe().onlyChanges().weak().observer(observer);
}

@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<T> callback) {
// note: limiting to int should be fine for Android apps
// TODO ripristinare query.count();
int totalCount = query.getTotalElements();
if (totalCount == 0) {
callback.onResult(Collections.<T>emptyList(), 0, 0);
return;
}

int position = computeInitialLoadPosition(params, totalCount);
int loadSize = computeInitialLoadSize(params, position, totalCount);

List<T> list = loadRange(position, loadSize);
if (list != null && list.size() == loadSize) {
callback.onResult(list, position, totalCount);
} else {
invalidate(); // size doesn't match request - DB modified between
// count and load
}
}

@Override
public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback<T> callback) {
callback.onResult(loadRange(params.startPosition, params.loadSize));
}

private List<T> loadRange(int startPosition, int loadCount) {
/* private void loadRange(int startPosition, int loadCount) { */
// note: find interprets loadCount 0 as no limit
//query.moveTo(startPosition, loadCount);

query.createPageRequestBuilder().offset(startPosition).pageSize(loadCount).apply();

return new ArrayList<>();
}

}
4 changes: 2 additions & 2 deletions kripton-android-library/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>com.abubusoft</groupId>
<artifactId>kripton-parent</artifactId>
<version>7.0.0-rc.8</version>
<version>7.0.0-rc.9</version>
<relativePath>../kripton-parent/pom.xml</relativePath>
</parent>

Expand All @@ -31,7 +31,7 @@
<rx.version>2.2.19</rx.version>
<reactive.version>1.0.3</reactive.version>

<kripton.version>7.0.0-rc.8</kripton.version>
<kripton.version>7.0.0-rc.9</kripton.version>
<robolectric.version>3.1.4</robolectric.version>
<junit.version>4.13.1</junit.version>
<unitils.version>3.4.2</unitils.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright 2016-2019 Francesco Benincasa (info@abubusoft.com)
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Expand All @@ -15,70 +15,55 @@
******************************************************************************/
package sqlite.kripton84;

import java.io.IOException;
import java.util.List;

import base.BaseAndroidTest;
import com.abubusoft.kripton.android.sqlite.TransactionResult;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import com.abubusoft.kripton.android.sqlite.TransactionResult;

import base.BaseAndroidTest;
import sqlite.kripton84.BindBean84ADataSource.Transaction;
import java.io.IOException;
import java.util.List;


// TODO: Auto-generated Javadoc

/**
* The Class Test84RuntimeA.
*
* @author Francesco Benincasa (info@abubusoft.com)
*/
@Config(manifest=Config.NONE)
@Config(manifest = Config.NONE)
@RunWith(RobolectricTestRunner.class)
public class Test84RuntimeA extends BaseAndroidTest {

/**
* Test run sqlite.
*
* @throws IOException Signals that an I/O exception has occurred.
* @throws InstantiationException the instantiation exception
* @throws IllegalAccessException the illegal access exception
*/
@Test
public void testRunSqlite() throws IOException, InstantiationException, IllegalAccessException {
Assert.assertNotNull(Bean84ATable.class.getName() != null);
Assert.assertNotNull(Bean84ADaoImpl.class.getName() != null);


BindBean84ADataSource dataSource = BindBean84ADataSource.getInstance();
//dataSource.openWritableDatabase();

dataSource.execute(new Transaction() {

@Override
public TransactionResult onExecute(BindBean84ADaoFactory daoFactory) {
Bean84ADaoImpl dao = daoFactory.getBean84ADao();
/**
* Test run sqlite.
*
* @throws IOException Signals that an I/O exception has occurred.
* @throws InstantiationException the instantiation exception
* @throws IllegalAccessException the illegal access exception
*/
@Test
public void testRunSqlite() {
BindBean84ADataSource dataSource = BindBean84ADataSource.getInstance();

Bean84A bean = new Bean84A();
bean.valueString = "hello";
dataSource.execute(daoFactory -> {
Bean84ADaoImpl dao = daoFactory.getBean84ADao();

dao.insertAll(bean);
List<Bean84A> list = dao.selectById(bean.id);
Assert.assertEquals("not list ", 1, list.size());
Bean84A bean = new Bean84A();
bean.valueString = "hello";

Assert.assertEquals("not map", 1, list.size());
dao.insertAll(bean);
List<Bean84A> list = dao.selectById(bean.id);
Assert.assertEquals("not list ", 1, list.size());

// Assert.assertEquals("not set", 1,
// list.get(0).valueSetString.size());
Assert.assertEquals("not map", 1, list.size());

return TransactionResult.COMMIT;
}

});
return TransactionResult.COMMIT;
});

}
}

}

0 comments on commit 967dcc3

Please sign in to comment.