Skip to content

Commit

Permalink
chore: apply sonarqube suggestions (#20786)
Browse files Browse the repository at this point in the history
* chore: apply sonarqube suggestions

* additional fixes

* additional fixes

* additional fixes

* additional fixes
  • Loading branch information
mcollovati authored Jan 3, 2025
1 parent cca0105 commit d7258e0
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private static native boolean hasPromise(Element element, int promiseId)
public void testClientCallableMethodInDom() {
assertServerEventHandlerMethodInDom(
NodeFeatures.CLIENT_DELEGATE_HANDLERS,
element -> assertPublishedMethods(element,
el -> assertPublishedMethods(el,
new String[] { "publishedMethod" }),
"publishedMethod");
}
Expand Down Expand Up @@ -240,15 +240,6 @@ private JsArray<String> getPublishedServerMethods(Element element) {
}
}

private JsArray<String> getPublishedServerMethods(
ServerEventObject object) {
if (object == null) {
return JsCollections.array();
} else {
return object.getMethods();
}
}

private void assertPublishedMethods(Element element, String[] expected) {
assertEventHandlerMethods(() -> getPublishedServerMethods(element),
expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

public class ComponentTest {

private UI ui;
private UI testUI;

@After
public void checkThreadLocal() {
Expand Down Expand Up @@ -158,7 +158,6 @@ public static class TestOtherButton extends Component {
private Component child2InputComponent;
private Component shadowRootParent;
private Component shadowChild;
private UI testUI;
private MockServletServiceSessionSetup mocks;
private VaadinSession session;

Expand Down Expand Up @@ -187,7 +186,7 @@ default void assertDetachEvents(int detachEvents) {
}
}

public static abstract class TracksAttachDetachComponent extends Component
public abstract static class TracksAttachDetachComponent extends Component
implements TracksAttachDetach {

private AtomicInteger attachEvents = new AtomicInteger();
Expand Down Expand Up @@ -274,9 +273,6 @@ public BrokenComponent() {

static class TestComponentContainer extends TestComponent {

public TestComponentContainer() {
}

public void add(Component c) {
getElement().appendChild(c.getElement());
}
Expand All @@ -287,14 +283,14 @@ public void remove(Component c) {
}

private UI createMockedUI() {
UI ui = new UI() {
UI mockUI = new UI() {
@Override
public VaadinSession getSession() {
return session;
}
};
ui.getInternals().setSession(session);
return ui;
mockUI.getInternals().setSession(session);
return mockUI;
}

@Before
Expand All @@ -313,9 +309,9 @@ public void setup() throws Exception {
mocks = new MockServletServiceSessionSetup();

session = mocks.getSession();
ui = createMockedUI();
testUI = createMockedUI();

UI.setCurrent(ui);
UI.setCurrent(testUI);
}

@After
Expand Down Expand Up @@ -440,8 +436,7 @@ public void defaultGetChildrenDirectlyAttached() {

public static void assertChildren(Component parent,
Component... expectedChildren) {
List<Component> children = parent.getChildren()
.collect(Collectors.toList());
List<Component> children = parent.getChildren().toList();
Assert.assertArrayEquals(expectedChildren, children.toArray());
for (Component c : children) {
Assert.assertEquals(c.getParent().get(), parent);
Expand Down Expand Up @@ -491,20 +486,16 @@ public void defaultGetChildrenDirectlyDeepElementHierarchy() {
child2.getElement(),
new Element("level1b").appendChild(child3.getElement()));

List<Component> children = parent.getChildren()
.collect(Collectors.toList());
Assert.assertArrayEquals(new Component[] { child1, child2, child3 },
children.toArray());
parent.getChildren().toArray());

}

@Test
public void defaultGetChildrenNoChildren() {
List<Component> children = parentDivComponent.getChildren()
.collect(Collectors.toList());
Assert.assertArrayEquals(
new Component[] { child1SpanComponent, child2InputComponent },
children.toArray());
parentDivComponent.getChildren().toArray());

}

Expand Down Expand Up @@ -742,8 +733,7 @@ public void testDetachListener_eventOrder_childFirst() {
public void testDetach_failingListeners_allListenersInvokedAndExceptionHandled() {
Set<Throwable> expectedExceptions = new HashSet<>();
Set<Throwable> handledExceptions = new HashSet<>();
VaadinSession session = new AlwaysLockedVaadinSession(
new MockVaadinServletService());
session = new AlwaysLockedVaadinSession(new MockVaadinServletService());
session.setErrorHandler(
event -> handledExceptions.add(event.getThrowable()));
VaadinSession.setCurrent(session);
Expand Down Expand Up @@ -908,7 +898,7 @@ public void testUIInitialAttach() {
});

MockDeploymentConfiguration config = new MockDeploymentConfiguration();
VaadinSession session = new AlwaysLockedVaadinSession(
session = new AlwaysLockedVaadinSession(
new MockVaadinServletService(config));
ui.getInternals().setSession(session);
Assert.assertTrue(initialAttach.get());
Expand Down Expand Up @@ -1459,7 +1449,7 @@ private Map<String, Dependency> getDependenciesMap(
@Test // 3818
public void enabledStateChangeOnAttachCalledForParentState() {
enabledStateChangeOnAttachCalledForParentState(false,
(parent, child) -> parent.add(child));
HasComponents::add);
}

@Test // 7085
Expand Down Expand Up @@ -1533,8 +1523,7 @@ public void onEnabledStateChanged(boolean enabled) {

@Test
public void enabledStateChangeOnParentDetachReturnsOldState() {
enabledStateChangeOnParentDetachReturnsOldState(
(parent, child) -> parent.add(child));
enabledStateChangeOnParentDetachReturnsOldState(HasComponents::add);
}

@Test
Expand Down Expand Up @@ -1970,16 +1959,17 @@ public void onEnabledStateChanged(boolean enabled) {
@Test
public void scrollIntoView() {
EnabledDiv div = new EnabledDiv();
ui.add(div);
testUI.add(div);
div.scrollIntoView();

assertPendingJs("scrollIntoView()");
}

private void assertPendingJs(String expectedJs) {
ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
testUI.getInternals().getStateTree()
.runExecutionsBeforeClientResponse();

List<PendingJavaScriptInvocation> pendingJs = ui.getInternals()
List<PendingJavaScriptInvocation> pendingJs = testUI.getInternals()
.dumpPendingJavaScriptInvocations();
Assert.assertEquals(1, pendingJs.size());
JavaScriptInvocation inv = pendingJs.get(0).getInvocation();
Expand All @@ -1990,7 +1980,7 @@ private void assertPendingJs(String expectedJs) {
@Test
public void scrollIntoViewSmooth() {
EnabledDiv div = new EnabledDiv();
ui.add(div);
testUI.add(div);
div.scrollIntoView(new ScrollOptions(Behavior.SMOOTH));

assertPendingJs("scrollIntoView({\"behavior\":\"smooth\"})");
Expand All @@ -1999,7 +1989,7 @@ public void scrollIntoViewSmooth() {
@Test
public void scrollIntoViewAllParams() {
EnabledDiv div = new EnabledDiv();
ui.add(div);
testUI.add(div);
div.scrollIntoView(new ScrollOptions(Behavior.SMOOTH, Alignment.END,
Alignment.CENTER));

Expand All @@ -2015,7 +2005,7 @@ public void cannotMoveComponentsToOtherUI() {
otherUI.add(button);

IllegalStateException ex = Assert.assertThrows(
IllegalStateException.class, () -> ui.add(button));
IllegalStateException.class, () -> testUI.add(button));
Assert.assertTrue(ex.getMessage(), ex.getMessage().startsWith(
"Can't move a node from one state tree to another. If this is "
+ "intentional, first remove the node from its current "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;

Expand Down Expand Up @@ -139,8 +138,7 @@ public void collectEmptyStream() {
@Test
public void testStream() {
JsonArray array = createTestArray1();
List<JsonValue> list = JsonUtils.stream(array)
.collect(Collectors.toList());
List<JsonValue> list = JsonUtils.stream(array).toList();

Assert.assertEquals(2, list.size());
Assert.assertEquals("foo", list.get(0).asString());
Expand All @@ -153,8 +151,7 @@ public void testObjectStream() {
JsonArray array = Stream.of(Json.createObject(), createTestObject1(),
createTestObject2()).collect(JsonUtils.asArray());

List<JsonObject> objects = JsonUtils.objectStream(array)
.collect(Collectors.toList());
List<JsonObject> objects = JsonUtils.objectStream(array).toList();

Assert.assertEquals(3, objects.size());
Assert.assertTrue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.vaadin.flow.router.internal;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -34,9 +35,7 @@
import net.jcip.annotations.NotThreadSafe;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

Expand Down Expand Up @@ -81,7 +80,6 @@
import com.vaadin.flow.server.MockVaadinServletService;
import com.vaadin.flow.server.MockVaadinSession;
import com.vaadin.flow.server.RouteRegistry;
import com.vaadin.flow.server.ServiceException;
import com.vaadin.flow.server.WrappedSession;
import com.vaadin.flow.server.menu.AvailableViewInfo;
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;
Expand Down Expand Up @@ -162,9 +160,6 @@ public ProxyableView() {

private Router router;

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Before
public void init() {
RouteRegistry registry = ApplicationRouteRegistry
Expand All @@ -173,7 +168,7 @@ public void init() {
}

@Test
public void getRouterLayoutForSingle() throws Exception {
public void getRouterLayoutForSingle() {
NavigationStateRenderer childRenderer = new NavigationStateRenderer(
navigationStateFromTarget(RouteParentLayout.class));

Expand All @@ -186,7 +181,7 @@ public void getRouterLayoutForSingle() throws Exception {
}

@Test
public void getRouterLayoutForSingleParent() throws Exception {
public void getRouterLayoutForSingleParent() {
NavigationStateRenderer childRenderer = new NavigationStateRenderer(
navigationStateFromTarget(SingleView.class));
RouteConfiguration.forRegistry(router.getRegistry())
Expand All @@ -202,7 +197,7 @@ public void getRouterLayoutForSingleParent() throws Exception {
}

@Test
public void getRouterLayoutForMulipleLayers() throws Exception {
public void getRouterLayoutForMulipleLayers() {
NavigationStateRenderer childRenderer = new NavigationStateRenderer(
navigationStateFromTarget(ChildConfiguration.class));
RouteConfiguration.forRegistry(router.getRegistry())
Expand All @@ -220,7 +215,7 @@ public void getRouterLayoutForMulipleLayers() throws Exception {
}

@Test
public void instantiatorUse() throws ServiceException {
public void instantiatorUse() {

MockVaadinServletService service = new MockVaadinServletService();
service.init(new MockInstantiator() {
Expand Down Expand Up @@ -568,7 +563,7 @@ public void handle_preserveOnRefreshView_routerLayoutIsPreserved_oldUiIsClosed()
session.setConfiguration(new MockDeploymentConfiguration());

// given a NavigationStateRenderer mapping to PreservedNestedView
Router router = session.getService().getRouter();
router = session.getService().getRouter();
NavigationStateRenderer renderer = new NavigationStateRenderer(
new NavigationStateBuilder(router)
.withTarget(PreservedNestedView.class)
Expand Down Expand Up @@ -676,7 +671,7 @@ public void handle_preserveOnRefreshView_refreshCurrentRouteRecreatesComponents(
session.setConfiguration(new MockDeploymentConfiguration());

// given a NavigationStateRenderer mapping to PreservedNestedView
Router router = session.getService().getRouter();
router = session.getService().getRouter();
NavigationStateRenderer renderer = new NavigationStateRenderer(
new NavigationStateBuilder(router)
.withTarget(PreservedNestedView.class)
Expand Down Expand Up @@ -734,7 +729,7 @@ public void handle_normalView_refreshCurrentRouteRecreatesComponents() {
session.setConfiguration(new MockDeploymentConfiguration());

// given a NavigationStateRenderer mapping to PreservedNestedView
Router router = session.getService().getRouter();
router = session.getService().getRouter();
NavigationStateRenderer renderer = new NavigationStateRenderer(
new NavigationStateBuilder(router).withTarget(SingleView.class)
.withPath("single").build());
Expand Down Expand Up @@ -784,7 +779,7 @@ public void handle_clientNavigation_withMatchingFlowRoute() {
session.setConfiguration(new MockDeploymentConfiguration());

// given a NavigationStateRenderer mapping to PreservedNestedView
Router router = session.getService().getRouter();
router = session.getService().getRouter();
NavigationStateRenderer renderer = new NavigationStateRenderer(
new NavigationStateBuilder(router)
.withTarget(RootRouteWithParam.class).withPath("")
Expand Down Expand Up @@ -833,7 +828,7 @@ public void handle_refreshRoute_modalComponentsDetached() {
session.setConfiguration(new MockDeploymentConfiguration());

// given a NavigationStateRenderer mapping to PreservedNestedView
Router router = session.getService().getRouter();
router = session.getService().getRouter();
NavigationStateRenderer renderer = new NavigationStateRenderer(
new NavigationStateBuilder(router)
.withTarget(RootRouteWithParam.class).withPath("")
Expand Down Expand Up @@ -876,8 +871,10 @@ private MockVaadinServletService createMockServiceWithInstantiator() {
public <T extends HasElement> T createRouteTarget(
Class<T> routeTargetType, NavigationEvent event) {
try {
return routeTargetType.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
return routeTargetType.getDeclaredConstructor()
.newInstance();
} catch (InstantiationException | IllegalAccessException
| NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
Expand Down
Loading

0 comments on commit d7258e0

Please sign in to comment.