Skip to content

Commit

Permalink
ISSUE-28 default value recognizing for kotlin. types (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-5 committed Aug 26, 2019
1 parent d414bd3 commit 8706e3a
Show file tree
Hide file tree
Showing 16 changed files with 405 additions and 54 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cache:
- $HOME/.gradle/wrapper/
before_install:
- chmod +x gradlew
- chmod +x gradle/release-patch.sh
jobs:
include:
- stage: build
Expand All @@ -36,6 +37,9 @@ jobs:
- script: ./gradlew gdx-fireapp-html:jar
- stage: test
script: ./gradlew test
- stage: release
script: ./gradle/release-patch.sh
if: branch = master && pull_request = "false"
- stage: deploy
script: ./gradlew bintrayUpload -Pversion=$TRAVIS_BRANCH
env:
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ buildscript {
classpath 'org.codehaus.groovy:groovy-all:2.4.15'
}
}
apply plugin: 'base'

allprojects {
apply plugin: "eclipse"
Expand Down
7 changes: 7 additions & 0 deletions e2e/core/src/pl/mk5/gdx/fireapp/e2e/GdxFireappE2ETests.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import pl.mk5.gdx.fireapp.e2e.tests.DatabaseReadValue2Test;
import pl.mk5.gdx.fireapp.e2e.tests.DatabaseReadValueTest;
import pl.mk5.gdx.fireapp.e2e.tests.DatabaseSetValueTest;
import pl.mk5.gdx.fireapp.e2e.tests.DatabaseTransactionDefaultDoubleValueTest;
import pl.mk5.gdx.fireapp.e2e.tests.DatabaseTransactionDefaultLongValueTest;
import pl.mk5.gdx.fireapp.e2e.tests.DatabaseTransactionDefaultStringValueTest;
import pl.mk5.gdx.fireapp.e2e.tests.DatabaseTransactionValue2Test;
import pl.mk5.gdx.fireapp.e2e.tests.DatabaseTransactionValueTest;
import pl.mk5.gdx.fireapp.e2e.tests.GdxFirebaseUserTest;
Expand Down Expand Up @@ -72,6 +75,9 @@ public GdxFireappE2ETests() throws Exception {
e2ETestRunner.addNext(DatabaseChildEventTest.class, 30);
e2ETestRunner.addNext(DatabaseTransactionValueTest.class, 30);
e2ETestRunner.addNext(DatabaseTransactionValue2Test.class, 30);
e2ETestRunner.addNext(DatabaseTransactionDefaultDoubleValueTest.class, 120);
e2ETestRunner.addNext(DatabaseTransactionDefaultLongValueTest.class, 30);
e2ETestRunner.addNext(DatabaseTransactionDefaultStringValueTest.class, 30);


e2ETestRunner.onFinish(new Runnable() {
Expand All @@ -86,6 +92,7 @@ public void run() {
public void create() {
batch = new SpriteBatch();
GdxFIRApp.inst().configure();
// e2ETestRunner.only(DatabaseTransactionDefaultDoubleValueTest.class, DatabaseTransactionDefaultLongValueTest.class, DatabaseTransactionDefaultStringValueTest.class);
e2ETestRunner.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface E2ETestRunner {

void onFinish(Runnable runnable);

void only(Class<? extends E2ETest> testType);
void only(Class<? extends E2ETest>... testType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class E2ETestRunnerImpl implements E2ETestRunner {
private final ObjectMap<Class<? extends E2ETest>, Float> testTimeout = new ObjectMap<>();
private float state = 0f;
private Runnable onFinish;
private Class<? extends E2ETest> onlyType;
private final Array<Class<? extends E2ETest>> onlyTypes = new Array<>();

@Override
public void addNext(Class<? extends E2ETest> testType) throws ReflectionException {
Expand All @@ -37,10 +37,12 @@ public void start() {
if (testsStable.size == 0) {
throw new IllegalStateException("No tests to run.");
}
if (onlyType != null) {
if (!onlyTypes.isEmpty()) {
testsStable.clear();
try {
addNext(onlyType, testTimeout.get(onlyType));
for (Class<? extends E2ETest> testType : onlyTypes) {
addNext(testType, testTimeout.get(testType));
}
} catch (ReflectionException e) {
Gdx.app.error(E2ETestRunnerImpl.class.getSimpleName(), e.getMessage(), e);
}
Expand All @@ -66,8 +68,8 @@ public void onFinish(Runnable runnable) {
}

@Override
public void only(Class<? extends E2ETest> testType) {
this.onlyType = testType;
public void only(Class<? extends E2ETest>... testType) {
this.onlyTypes.addAll(testType);
}

private void startTest(E2ETest test) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2019 mk
*
* 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 License for the specific language governing permissions and
* limitations under the License.
*/

package pl.mk5.gdx.fireapp.e2e.tests;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Batch;

import pl.mk5.gdx.fireapp.GdxFIRDatabase;
import pl.mk5.gdx.fireapp.e2e.runner.E2ETest;
import pl.mk5.gdx.fireapp.functional.BiConsumer;
import pl.mk5.gdx.fireapp.functional.Consumer;
import pl.mk5.gdx.fireapp.functional.Function;

public class DatabaseTransactionDefaultDoubleValueTest extends E2ETest {

@Override
public void action() {
final String reference = "/test-trans";
Consumer<Double> successConsumer = new Consumer<Double>() {
@Override
public void accept(Double i) {
if (i == 1) {
success();
}
}
};

GdxFIRDatabase.promise()
.then(GdxFIRDatabase.inst().inReference(reference).removeValue())
.then(GdxFIRDatabase.inst().inReference(reference)
.transaction(Double.class, new Function<Double, Double>() {
@Override
public Double apply(Double num) {
return num + 1;
}
}))
.then(GdxFIRDatabase.inst().inReference(reference).readValue(Double.class))
.then(successConsumer)
.fail(new BiConsumer<String, Throwable>() {
@Override
public void accept(String s, Throwable throwable) {
Gdx.app.log("Error", s);
}
});
}

@Override
public void draw(Batch batch) {
}

@Override
public void update(float dt) {

}

@Override
public void dispose() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2019 mk
*
* 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 License for the specific language governing permissions and
* limitations under the License.
*/

package pl.mk5.gdx.fireapp.e2e.tests;

import com.badlogic.gdx.graphics.g2d.Batch;

import pl.mk5.gdx.fireapp.GdxFIRDatabase;
import pl.mk5.gdx.fireapp.e2e.runner.E2ETest;
import pl.mk5.gdx.fireapp.functional.Consumer;
import pl.mk5.gdx.fireapp.functional.Function;

public class DatabaseTransactionDefaultLongValueTest extends E2ETest {

@Override
public void action() {
final String reference = "/test-trans";

GdxFIRDatabase.promise()
.then(GdxFIRDatabase.inst().inReference(reference).removeValue())
.then(GdxFIRDatabase.inst().inReference(reference)
.transaction(Long.class, new Function<Long, Long>() {
@Override
public Long apply(Long num) {
return num + 1;
}
}))
.then(GdxFIRDatabase.inst().inReference(reference).readValue(Long.class))
.then(new Consumer<Long>() {
@Override
public void accept(Long i) {
if (i == 1) {
success();
}
}
});
}

@Override
public void draw(Batch batch) {
}

@Override
public void update(float dt) {

}

@Override
public void dispose() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2019 mk
*
* 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 License for the specific language governing permissions and
* limitations under the License.
*/

package pl.mk5.gdx.fireapp.e2e.tests;

import com.badlogic.gdx.graphics.g2d.Batch;

import pl.mk5.gdx.fireapp.GdxFIRDatabase;
import pl.mk5.gdx.fireapp.e2e.runner.E2ETest;
import pl.mk5.gdx.fireapp.functional.Consumer;
import pl.mk5.gdx.fireapp.functional.Function;

public class DatabaseTransactionDefaultStringValueTest extends E2ETest {

@Override
public void action() {
final String reference = "/test-trans";

GdxFIRDatabase.promise()
.then(GdxFIRDatabase.inst().inReference(reference).removeValue())
.then(GdxFIRDatabase.inst().inReference(reference)
.transaction(String.class, new Function<String, String>() {
@Override
public String apply(String str) {
return str + "abc";
}
}))
.then(GdxFIRDatabase.inst().inReference(reference).readValue(String.class))
.then(new Consumer<String>() {
@Override
public void accept(String i) {
if (i.equals("abc")) {
success();
}
}
});
}

@Override
public void draw(Batch batch) {
}

@Override
public void update(float dt) {

}

@Override
public void dispose() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@ public <T, R extends T> ListenerPromise<R> onChildChange(final Class<T> dataType
FilteringStateEnsurer.checkFilteringState(filters, orderByClause, dataType);
return ConverterPromise.whenWithConvert(new DatabaseConsumer<ConverterPromise<T, R>>(databasePath, orderByClause, filters) {
@Override
@SuppressWarnings("unchecked")
public void accept(ConverterPromise<T, R> trConverterPromise) {
trConverterPromise.with(GdxFIRDatabase.instance().getMapConverter(), dataType);
new QueryOnChildChange<>(Database.this, getDatabasePath())
.with(getFilters())
.with(getOrderByClause())
.with((FuturePromise<Object>) trConverterPromise)
.with((FuturePromise) trConverterPromise)
.withArgs(dataType, eventsType)
.execute();
}
Expand Down Expand Up @@ -245,7 +246,6 @@ public <T, R extends T> Promise<Void> transaction(final Class<T> dataType, final
return FuturePromise.when(new DatabaseConsumer<FuturePromise<Void>>(databasePath, orderByClause, filters) {
@Override
public void accept(FuturePromise<Void> voidFuturePromise) {
// TODO - converter
new QueryRunTransaction(Database.this, getDatabasePath())
.withArgs(dataType, transaction)
.with(voidFuturePromise)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package pl.mk5.gdx.fireapp.android.database;

import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.MutableData;
Expand All @@ -27,6 +26,7 @@
import pl.mk5.gdx.fireapp.functional.Function;
import pl.mk5.gdx.fireapp.promises.FuturePromise;
import pl.mk5.gdx.fireapp.reflection.AnnotationFinder;
import pl.mk5.gdx.fireapp.reflection.DefaultTypeRecognizer;

/**
* Provides transactionFunction invocation
Expand Down Expand Up @@ -58,7 +58,7 @@ class TransactionHandler<R> implements Transaction.Handler {
public Transaction.Result doTransaction(MutableData mutableData) {
try {
if (mutableData.getValue() == null) {
mutableData.setValue(defaultValueForDataType());
mutableData.setValue(transactionFunction.apply((R) DefaultTypeRecognizer.getDefaultValue(dataType)));
return Transaction.success(mutableData);
}
MapConversion mapConversionAnnotation = null;
Expand Down Expand Up @@ -91,12 +91,4 @@ public void onComplete(DatabaseError databaseError, boolean committed, DataSnaps
}
}
}

private Object defaultValueForDataType() {
if (ClassReflection.isAssignableFrom(Number.class, dataType)) {
return 0;
} else {
return "";
}
}
}
7 changes: 0 additions & 7 deletions gdx-fireapp-core/src/pl/mk5/gdx/fireapp/GdxFIRDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,6 @@ public Promise<Void> updateChildren(Map<String, Object> data) {
*/
@Override
public <T, R extends T> Promise<Void> transaction(Class<T> dataType, Function<R, R> transaction) {
// TransactionMitmConverter<T, R> mitmConverter = new TransactionMitmConverter<T, R>(dataType, transactionCallback, mapConverter);
// if (mitmConverter.isPojo(dataType)) {
// platformObject.transaction(Map.class, mitmConverter.getPojoCallback(), completeCallback);
// } else {
// platformObject.transaction(dataType, mitmConverter.getGenericCallback(), completeCallback);
// }
// maybe conversion is not needed? TODO - check data types in transactions
return platformObject.transaction(dataType, transaction);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import pl.mk5.gdx.fireapp.functional.Consumer;
import pl.mk5.gdx.fireapp.functional.Function;
import pl.mk5.gdx.fireapp.reflection.AnnotationFinder;
import pl.mk5.gdx.fireapp.reflection.DefaultTypeRecognizer;

/**
* Promise implementation with conversion before {@link #doComplete(Object)}
Expand Down Expand Up @@ -81,6 +82,12 @@ public synchronized void doComplete(Object object) {
}
}
}
if (object != null &&
DefaultTypeRecognizer.isLongNumberType(object.getClass()) &&
DefaultTypeRecognizer.isFloatingPointNumberType(wantedDataType)
) {
object = Double.valueOf(Long.valueOf(object.toString()));
}
super.doComplete((R) object);
} catch (Exception e) {
doFail(e);
Expand Down
Loading

0 comments on commit 8706e3a

Please sign in to comment.