Skip to content

Commit

Permalink
[SCA] Minor SCA-based code improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Oct 30, 2020
1 parent f050fdf commit a6059e9
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public static Class<?> getMapGenericValueType(Field field) {
final Type actualType = valueType.getActualTypeArguments()[0];
if (Class.class.isAssignableFrom(actualType.getClass())) {
// For Map<?, Collection<String>>
return (Class) actualType;
return (Class<?>) actualType;
}
// For Map<?, Collection<?>>
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ private enum LiteralType {
BOOLEAN, NUMBER, STRING, TEMPORAL
}

public static LiteralNode createLiteralNode(Object value) {
public static LiteralNode<?> createLiteralNode(Object value) {
return createLiteralNode(null, value);
}

public static LiteralNode createLiteralNode(String name, Object value) {
public static LiteralNode<?> createLiteralNode(String name, Object value) {
final LiteralType type = determineLiteralType(value);
LiteralNode node = null;
LiteralNode<?> node = null;
switch (type) {
case BOOLEAN:
node = name != null ? new BooleanLiteralNode(name, (Boolean) value) : new BooleanLiteralNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private TemporalNodeFactory() {
throw new AssertionError();
}

static LiteralNode createLiteralNode(String name, Object value) {
static LiteralNode<Long> createLiteralNode(String name, Object value) {
assert value != null;
if (value instanceof Date) {
final Date date = (Date) value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void createNewInstanceThrowsBeanProcessingExceptionWhenNoArgConstructorIsMissing
}

private static class ClassWithoutPublicCtor {
private String name;
private final String name;

public ClassWithoutPublicCtor(String name) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ void deserializationSupportsPluralMultilingualAttributes() throws Exception {
void deserializationSupportsOptimisticTargetTypeResolution() throws Exception {
final Configuration config = sut.configuration();
config.set(ConfigParam.ENABLE_OPTIMISTIC_TARGET_TYPE_RESOLUTION, Boolean.toString(true));
config.set(ConfigParam.SCAN_PACKAGE, "cz.cvut.kbss.jsonld.environment.model");
this.sut = ExpandedJsonLdDeserializer.createExpandedDeserializer(config);
final Object input = readAndExpand("objectWithPluralOptimisticallyTypedReference.json");
final StudyOnPersons result = sut.deserialize(input, StudyOnPersons.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void createCollectionFromArrayCreatesSetNodeWithoutName() {

@Test
void createLiteralNodeCreatesBooleanNodeFromBooleanValueWithName() {
final LiteralNode node = JsonNodeFactory.createLiteralNode(NAME, false);
final LiteralNode<?> node = JsonNodeFactory.createLiteralNode(NAME, false);
assertThat(node, instanceOf(BooleanLiteralNode.class));
assertFalse(node.isValueNode());
assertEquals(NAME, node.getName());
Expand All @@ -88,7 +88,7 @@ void createLiteralNodeCreatesBooleanNodeFromBooleanValueWithName() {

@Test
void createLiteralNodeCreatesBooleanNodeFromBooleanValueWithoutName() {
final LiteralNode node = JsonNodeFactory.createLiteralNode(true);
final LiteralNode<?> node = JsonNodeFactory.createLiteralNode(true);
assertThat(node, instanceOf(BooleanLiteralNode.class));
assertTrue(node.isValueNode());
assertTrue((Boolean) node.getValue());
Expand All @@ -97,7 +97,7 @@ void createLiteralNodeCreatesBooleanNodeFromBooleanValueWithoutName() {
@Test
void createLiteralNodeCreatesNumericNodeFromNumberValueWithName() {
final long value = System.currentTimeMillis();
final LiteralNode node = JsonNodeFactory.createLiteralNode(NAME, value);
final LiteralNode<?> node = JsonNodeFactory.createLiteralNode(NAME, value);
assertThat(node, instanceOf(NumericLiteralNode.class));
assertFalse(node.isValueNode());
assertEquals(value, node.getValue());
Expand All @@ -106,7 +106,7 @@ void createLiteralNodeCreatesNumericNodeFromNumberValueWithName() {
@Test
void createLiteralNodeCreatesNumericNodeFromNumberValueWithoutName() {
final double value = Double.MIN_VALUE;
final LiteralNode node = JsonNodeFactory.createLiteralNode(value);
final LiteralNode<?> node = JsonNodeFactory.createLiteralNode(value);
assertThat(node, instanceOf(NumericLiteralNode.class));
assertTrue(node.isValueNode());
assertEquals(value, node.getValue());
Expand All @@ -115,7 +115,7 @@ void createLiteralNodeCreatesNumericNodeFromNumberValueWithoutName() {
@Test
void createLiteralNodeCreatesStringNodeFromStringWithName() {
final String value = "test";
final LiteralNode node = JsonNodeFactory.createLiteralNode(NAME, value);
final LiteralNode<?> node = JsonNodeFactory.createLiteralNode(NAME, value);
assertThat(node, instanceOf(StringLiteralNode.class));
assertFalse(node.isValueNode());
assertEquals(NAME, node.getName());
Expand All @@ -125,7 +125,7 @@ void createLiteralNodeCreatesStringNodeFromStringWithName() {
@Test
void createLiteralNodeCreatesStringNodeFromStringWithoutName() {
final String value = "test2";
final LiteralNode node = JsonNodeFactory.createLiteralNode(value);
final LiteralNode<?> node = JsonNodeFactory.createLiteralNode(value);
assertThat(node, instanceOf(StringLiteralNode.class));
assertTrue(node.isValueNode());
assertEquals(value, node.getValue());
Expand All @@ -134,23 +134,23 @@ void createLiteralNodeCreatesStringNodeFromStringWithoutName() {
@Test
void createLiteralNodeCreatesNumberNodeFromDateInstance() {
final Date value = new Date();
final LiteralNode node = JsonNodeFactory.createLiteralNode(value);
final LiteralNode<?> node = JsonNodeFactory.createLiteralNode(value);
assertThat(node, instanceOf(NumericLiteralNode.class));
assertEquals(value.getTime(), node.getValue());
}

@Test
void createLiteralNodeCreatesStringNodeContainingIsoFormattedValueForLocalDateTime() {
final LocalDateTime value = LocalDateTime.now();
final LiteralNode result = JsonNodeFactory.createLiteralNode(value);
final LiteralNode<?> result = JsonNodeFactory.createLiteralNode(value);
assertThat(result, instanceOf(StringLiteralNode.class));
assertEquals(value.toString(), result.getValue());
}

@Test
void createLiteralNodeCreatesStringNodeContainingIsoFormattedValueForZonedDateTime() {
final ZonedDateTime value = ZonedDateTime.now();
final LiteralNode result = JsonNodeFactory.createLiteralNode(value);
final LiteralNode<?> result = JsonNodeFactory.createLiteralNode(value);
assertThat(result, instanceOf(StringLiteralNode.class));
assertEquals(value.toString(), result.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cz.cvut.kbss.jsonld.serialization.model;

import cz.cvut.kbss.jsonld.serialization.JsonGenerator;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

Expand All @@ -23,6 +24,7 @@ public abstract class AbstractNodeTest {
@Mock
JsonGenerator serializerMock;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.serialization.model;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -23,11 +20,6 @@

public class BooleanLiteralNodeTest extends AbstractNodeTest {

@BeforeEach
public void setUp() {
super.setUp();
}

@Test
void writeValueWritesTheValueAsBoolean() throws IOException {
final String name = "test";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.serialization.model;

import cz.cvut.kbss.jsonld.exception.JsonLdSerializationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;

Expand All @@ -26,11 +23,6 @@

public class JsonNodeTest extends AbstractNodeTest {

@BeforeEach
public void setUp() {
super.setUp();
}

@Test
void writeWritesFirstKeyAndThenValueOfTheNode() throws Exception {
final String name = "test";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package cz.cvut.kbss.jsonld.serialization.model;

import cz.cvut.kbss.jsonld.JsonLd;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;
import org.mockito.Mockito;

class LangStringNodeTest extends AbstractNodeTest {

@BeforeEach
public void setUp() {
super.setUp();
}

@Test
void writeValueWritesValueAndLanguageAsStrings() throws Exception {
final String value = "test";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.serialization.model;

import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.environment.Generator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;

Expand All @@ -27,11 +24,6 @@

public class ListNodeTest extends AbstractNodeTest {

@BeforeEach
public void setUp() {
super.setUp();
}

@Test
void writeOutputsItemsInAnObjectWithSingleAttributeOfNameListAndValueAnArray() throws Exception {
final CollectionNode node = new ListNode("http://krizik.felk.cvut.cz/ontologies/jsonld#list");
Expand All @@ -45,7 +37,7 @@ void writeOutputsItemsInAnObjectWithSingleAttributeOfNameListAndValueAnArray() t
inOrder.verify(serializerMock).writeFieldName(JsonLd.LIST);
inOrder.verify(serializerMock).writeArrayStart();
for (JsonNode item : items) {
inOrder.verify(serializerMock).writeNumber((Number) ((NumericLiteralNode) item).getValue());
inOrder.verify(serializerMock).writeNumber(((NumericLiteralNode<?>) item).getValue());
}
inOrder.verify(serializerMock).writeArrayEnd();
inOrder.verify(serializerMock).writeObjectEnd();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.serialization.model;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.verify;

public class NumericLiteralNodeTest extends AbstractNodeTest {

@BeforeEach
public void setUp() {
super.setUp();
}

@Test
void writeValueWritesTheValueAsNumber() throws Exception {
final String name = "test";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.serialization.model;

import cz.cvut.kbss.jsonld.environment.Generator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;

import static org.mockito.Mockito.inOrder;

public class ObjectIdNodeTest extends AbstractNodeTest {

@BeforeEach
public void setUp() {
super.setUp();
}

@Test
void writeValueOutputsTheValueAsObjectWithIdFieldAndStringValue() throws Exception {
final String name = "test";
Expand Down
Loading

0 comments on commit a6059e9

Please sign in to comment.