"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorTest.java" between
vaadin-flow-4.0.5.tar.gz and vaadin-flow-4.0.6.tar.gz

About: Vaadin flow is a Java framework binding Vaadin web components to Java.

HierarchicalCommunicatorTest.java  (vaadin-flow-4.0.5):HierarchicalCommunicatorTest.java  (vaadin-flow-4.0.6)
skipping to change at line 18 skipping to change at line 18
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.vaadin.flow.data.provider.hierarchy; package com.vaadin.flow.data.provider.hierarchy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Mockito; import org.mockito.Mockito;
import com.vaadin.flow.component.Component; import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Tag; import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.UI; import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.internal.UIInternals; import com.vaadin.flow.component.internal.UIInternals;
import com.vaadin.flow.data.provider.CompositeDataGenerator; import com.vaadin.flow.data.provider.CompositeDataGenerator;
import com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.Hierarch icalUpdate; import com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.Hierarch icalUpdate;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.function.SerializableConsumer; import com.vaadin.flow.function.SerializableConsumer;
import com.vaadin.flow.internal.StateNode; import com.vaadin.flow.internal.StateNode;
import com.vaadin.flow.internal.StateTree; import com.vaadin.flow.internal.StateTree;
import com.vaadin.flow.internal.nodefeature.ComponentMapping; import com.vaadin.flow.internal.nodefeature.ComponentMapping;
import com.vaadin.flow.internal.nodefeature.ElementData;
import elemental.json.JsonArray;
import elemental.json.JsonObject;
import elemental.json.JsonValue; import elemental.json.JsonValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
public class HierarchicalCommunicatorTest { public class HierarchicalCommunicatorTest {
private static final String ROOT = "ROOT"; private static final String ROOT = "ROOT";
private static final String FOLDER = "FOLDER"; private static final String FOLDER = "FOLDER";
private static final String LEAF = "LEAF"; private static final String LEAF = "LEAF";
private TreeDataProvider<String> dataProvider; private TreeDataProvider<String> dataProvider;
private HierarchicalDataCommunicator<String> communicator; private HierarchicalDataCommunicator<String> communicator;
private TreeData<String> treeData; private TreeData<String> treeData;
private UI ui; private UI ui;
private UIInternals uiInternals; private UIInternals uiInternals;
private StateTree stateTree; private StateTree stateTree;
private final int pageSize = 50; private final int pageSize = 50;
private StateNode stateNode; private StateNode stateNode;
private List<String> enqueueFunctions = new ArrayList<>(); private List<String> enqueueFunctions = new ArrayList<>();
private Map<String, Serializable[]> enqueueFunctionsWithParams =
new HashMap<>();
private class UpdateQueue implements HierarchicalUpdate { private class UpdateQueue implements HierarchicalUpdate {
@Override @Override
public void clear(int start, int length) { public void clear(int start, int length) {
} }
@Override @Override
public void set(int start, List<JsonValue> items) { public void set(int start, List<JsonValue> items) {
} }
@Override @Override
skipping to change at line 97 skipping to change at line 101
@Override @Override
public void commit(int updateId, String parentKey, int levelSize) { public void commit(int updateId, String parentKey, int levelSize) {
} }
@Override @Override
public void commit() { public void commit() {
} }
} }
private class UpdateQueueWithArguments extends UpdateQueue {
@Override
public void enqueue(String name, Serializable... arguments) {
enqueueFunctionsWithParams.put(name, arguments);
}
}
private final HierarchicalArrayUpdater arrayUpdater = new HierarchicalArrayU pdater() { private final HierarchicalArrayUpdater arrayUpdater = new HierarchicalArrayU pdater() {
@Override @Override
public HierarchicalUpdate startUpdate(int sizeChange) { public HierarchicalUpdate startUpdate(int sizeChange) {
return new UpdateQueue(); return new UpdateQueue();
} }
@Override @Override
public void initialize() { public void initialize() {
} }
}; };
private final HierarchicalArrayUpdater arrayUpdaterWithArguments =
new HierarchicalArrayUpdater() {
@Override
public HierarchicalUpdate startUpdate(int sizeChange) {
return new UpdateQueueWithArguments();
}
@Override
public void initialize() {
}
};
@Before @Before
public void setUp() { public void setUp() {
ui = Mockito.mock(UI.class); ui = Mockito.mock(UI.class);
uiInternals = Mockito.mock(UIInternals.class); uiInternals = Mockito.mock(UIInternals.class);
stateTree = Mockito.mock(StateTree.class); stateTree = Mockito.mock(StateTree.class);
Mockito.when(ui.getInternals()).thenReturn(uiInternals); Mockito.when(ui.getInternals()).thenReturn(uiInternals);
Mockito.when(uiInternals.getStateTree()).thenReturn(stateTree); Mockito.when(uiInternals.getStateTree()).thenReturn(stateTree);
treeData = new TreeData<>(); treeData = new TreeData<>();
treeData.addItems(null, ROOT); treeData.addItems(null, ROOT);
skipping to change at line 166 skipping to change at line 189
communicator.expand(FOLDER); communicator.expand(FOLDER);
// Put the item into client queue // Put the item into client queue
communicator.refresh(item); communicator.refresh(item);
treeData.removeItem(item); treeData.removeItem(item);
if (refreshAll) { if (refreshAll) {
dataProvider.refreshAll(); dataProvider.refreshAll();
} else { } else {
dataProvider.refreshItem(item); dataProvider.refreshItem(item);
} }
int number = refreshAll ? 6 : 5; int number = refreshAll ? 7 : 6;
ArgumentCaptor<SerializableConsumer> attachCaptor = ArgumentCaptor ArgumentCaptor<SerializableConsumer> attachCaptor = ArgumentCaptor
.forClass(SerializableConsumer.class); .forClass(SerializableConsumer.class);
Mockito.verify(stateNode, Mockito.times(number)) Mockito.verify(stateNode, Mockito.times(number))
.runWhenAttached(attachCaptor.capture()); .runWhenAttached(attachCaptor.capture());
attachCaptor.getAllValues().forEach(consumer -> consumer.accept(ui)); attachCaptor.getAllValues().forEach(consumer -> consumer.accept(ui));
Mockito.verify(stateTree, Mockito.times(number)) Mockito.verify(stateTree, Mockito.times(number))
.beforeClientResponse(Mockito.any(), Mockito.any()); .beforeClientResponse(Mockito.any(), Mockito.any());
skipping to change at line 206 skipping to change at line 229
enqueueFunctions.clear(); enqueueFunctions.clear();
// The communicator is just initialized with a data provider and has no // The communicator is just initialized with a data provider and has no
// any data controllers // any data controllers
communicator.reset(); communicator.reset();
Assert.assertEquals(1, enqueueFunctions.size()); Assert.assertEquals(1, enqueueFunctions.size());
Assert.assertEquals("$connector.ensureHierarchy", Assert.assertEquals("$connector.ensureHierarchy",
enqueueFunctions.get(0)); enqueueFunctions.get(0));
} }
@Test
public void reset_expandSomeItems_hierarchicalUpdateContainsExpandItems() {
enqueueFunctions.clear();
communicator.expand(ROOT);
communicator.reset();
// One expandItems for calling expand(...)
// One expandItems and one ensureHierarchy for calling reset()
Assert.assertEquals(3, enqueueFunctions.size());
Assert.assertEquals("$connector.expandItems",
enqueueFunctions.get(0));
Assert.assertEquals("$connector.ensureHierarchy",
enqueueFunctions.get(1));
Assert.assertEquals("$connector.expandItems",
enqueueFunctions.get(2));
}
@Test
public void reset_expandSomeItems_updateContainsProperJsonObjectsToExpand()
{
enqueueFunctionsWithParams = new HashMap<>();
TreeData<String> hierarchyTreeData = new TreeData<>();
hierarchyTreeData.addItem(null, "root");
hierarchyTreeData.addItem("root", "first-1");
hierarchyTreeData.addItem("root", "first-2");
hierarchyTreeData.addItem("first-1", "second-1-1");
hierarchyTreeData.addItem("first-2", "second-2-1");
TreeDataProvider<String> treeDataProvider =
new TreeDataProvider<>(hierarchyTreeData);
HierarchicalDataCommunicator<String> dataCommunicator =
new HierarchicalDataCommunicator<String>(
Mockito.mock(CompositeDataGenerator.class),
arrayUpdaterWithArguments,
json -> {}, Mockito.mock(StateNode.class), () -> null);
dataCommunicator.setDataProvider(treeDataProvider, null);
dataCommunicator.expand("root");
dataCommunicator.expand("first-1");
dataCommunicator.reset();
Assert.assertTrue(enqueueFunctionsWithParams
.containsKey("$connector.expandItems"));
JsonArray arguments = (JsonArray) enqueueFunctionsWithParams
.get("$connector.expandItems")[0];
Assert.assertNotNull(arguments);
Assert.assertEquals(2, arguments.length());
JsonObject first1 = arguments.getObject(0);
JsonObject root = arguments.getObject(1);
Assert.assertNotNull(first1);
Assert.assertNotNull(root);
Assert.assertTrue(first1.hasKey("key"));
Assert.assertTrue(root.hasKey("key"));
Assert.assertEquals(dataCommunicator.getKeyMapper().key("first-1"),
first1.getString("key"));
Assert.assertEquals(dataCommunicator.getKeyMapper().key("root"),
root.getString("key"));
}
@Tag("test") @Tag("test")
public static class TestComponent extends Component { public static class TestComponent extends Component {
} }
} }
 End of changes. 13 change blocks. 
7 lines changed or deleted 98 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)