-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reimplement @RouteScoped to keep beans for PreserveOnRefresh (#370
) * feat: reimplement @RouteScoped to keep beans for PreserveOnRefresh - presere beans for PreserveOnRefresh views - reimplement absent RouteScopeOwner semantic - do not allow to use (route) scope when it's not available fixes #369 * chore: make minor corrections and add ITs * chore: correct test class
- Loading branch information
Denis
committed
Jun 7, 2021
1 parent
8bb1172
commit c4c6d37
Showing
29 changed files
with
1,203 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
vaadin-cdi-itest/src/main/java/com/vaadin/cdi/itest/routecontext/BeanNoOwner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2000-2021 Vaadin Ltd. | ||
* | ||
* 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 com.vaadin.cdi.itest.routecontext; | ||
|
||
import java.util.UUID; | ||
|
||
import com.vaadin.cdi.annotation.RouteScoped; | ||
|
||
@RouteScoped | ||
public class BeanNoOwner extends AbstractCountedBean { | ||
|
||
public BeanNoOwner() { | ||
setData(UUID.randomUUID().toString()); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
vaadin-cdi-itest/src/main/java/com/vaadin/cdi/itest/routecontext/ChildNoOwnerView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2000-2021 Vaadin Ltd. | ||
* | ||
* 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 com.vaadin.cdi.itest.routecontext; | ||
|
||
import javax.enterprise.inject.Instance; | ||
import javax.inject.Inject; | ||
|
||
import com.vaadin.flow.component.AttachEvent; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.router.Route; | ||
import com.vaadin.flow.router.RouterLink; | ||
|
||
@Route(value = "child-no-owner", layout = ParentNoOwnerView.class) | ||
public class ChildNoOwnerView extends Div { | ||
|
||
@Inject | ||
private Instance<BeanNoOwner> instance; | ||
|
||
@Override | ||
protected void onAttach(AttachEvent attachEvent) { | ||
if (attachEvent.isInitialAttach()) { | ||
RouterLink link = new RouterLink("parent", ParentNoOwnerView.class); | ||
link.setId("to-parent"); | ||
add(link); | ||
|
||
Div div = new Div(); | ||
div.setId("child-info"); | ||
div.getElement().getStyle().set("display", "block"); | ||
div.setText(instance.get().getData()); | ||
add(div); | ||
|
||
NativeButton button = new NativeButton("Reset bean instance", | ||
event -> div.setText(instance.get().getData())); | ||
add(button); | ||
button.setId("reset"); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
vaadin-cdi-itest/src/main/java/com/vaadin/cdi/itest/routecontext/CustomException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2000-2021 Vaadin Ltd. | ||
* | ||
* 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 com.vaadin.cdi.itest.routecontext; | ||
|
||
public class CustomException extends RuntimeException { | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...n-cdi-itest/src/main/java/com/vaadin/cdi/itest/routecontext/CustomExceptionSubButton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2000-2021 Vaadin Ltd. | ||
* | ||
* 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 com.vaadin.cdi.itest.routecontext; | ||
|
||
import java.util.UUID; | ||
|
||
import com.vaadin.cdi.annotation.RouteScopeOwner; | ||
import com.vaadin.cdi.annotation.RouteScoped; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
|
||
@RouteScoped | ||
@RouteScopeOwner(ErrorHandlerView.class) | ||
public class CustomExceptionSubButton extends AbstractCountedView { | ||
|
||
public CustomExceptionSubButton() { | ||
NativeButton button = new NativeButton(); | ||
button.setId("custom-exception-button"); | ||
button.setText(UUID.randomUUID().toString()); | ||
add(button); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
vaadin-cdi-itest/src/main/java/com/vaadin/cdi/itest/routecontext/CustomExceptionSubDiv.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2000-2021 Vaadin Ltd. | ||
* | ||
* 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 com.vaadin.cdi.itest.routecontext; | ||
|
||
import java.util.UUID; | ||
|
||
import com.vaadin.cdi.annotation.RouteScopeOwner; | ||
import com.vaadin.cdi.annotation.RouteScoped; | ||
|
||
@RouteScoped | ||
@RouteScopeOwner(ErrorHandlerView.class) | ||
public class CustomExceptionSubDiv extends AbstractCountedView { | ||
|
||
public CustomExceptionSubDiv() { | ||
setId("custom-exception-div"); | ||
setText(UUID.randomUUID().toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
vaadin-cdi-itest/src/main/java/com/vaadin/cdi/itest/routecontext/InvalidView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2000-2021 Vaadin Ltd. | ||
* | ||
* 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 com.vaadin.cdi.itest.routecontext; | ||
|
||
import javax.inject.Inject; | ||
|
||
import com.vaadin.cdi.annotation.RouteScopeOwner; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.router.Route; | ||
|
||
@Route("invalid-injection") | ||
public class InvalidView extends Div { | ||
|
||
@Inject | ||
@RouteScopeOwner(ErrorParentView.class) | ||
/* | ||
* There is no a ErrorParentView in navigation: this injection has no scope. | ||
* Pseudo-scope @RouteScoped is used here with the component to immediately | ||
* get an exception, for normal scope proxy is created and the exception | ||
* won't be thrown immediately. | ||
*/ | ||
private ErrorParentView bean; | ||
|
||
public InvalidView() { | ||
setId("invalid-injection"); | ||
setText("This view should not be shown since the " | ||
+ "injection has no scope and an exception should be thrown"); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
vaadin-cdi-itest/src/main/java/com/vaadin/cdi/itest/routecontext/MainLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2000-2021 Vaadin Ltd. | ||
* | ||
* 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 com.vaadin.cdi.itest.routecontext; | ||
|
||
import com.vaadin.flow.component.AttachEvent; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.Label; | ||
import com.vaadin.flow.router.RouterLayout; | ||
import com.vaadin.flow.router.RouterLink; | ||
|
||
public class MainLayout extends Div implements RouterLayout { | ||
|
||
public static final String UIID = "UIID"; | ||
|
||
public static final String PRESERVE = "preserve"; | ||
public static final String INVALID = "invalid"; | ||
public static final String PARENT_NO_OWNER = "parent-no-owner"; | ||
public static final String CHILD_NO_OWNER = "child-no-owner"; | ||
|
||
private Label uiIdLabel; | ||
|
||
public MainLayout() { | ||
add(new RouterLink(PRESERVE, PreserveOnRefreshView.class), | ||
new RouterLink(INVALID, InvalidView.class), | ||
new RouterLink(PARENT_NO_OWNER, ParentNoOwnerView.class), | ||
new RouterLink(CHILD_NO_OWNER, ChildNoOwnerView.class)); | ||
; | ||
} | ||
|
||
@Override | ||
protected void onAttach(AttachEvent attachEvent) { | ||
if (uiIdLabel != null) { | ||
remove(uiIdLabel); | ||
} | ||
uiIdLabel = new Label(attachEvent.getUI().getUIId() + ""); | ||
uiIdLabel.setId(UIID); | ||
add(uiIdLabel); | ||
} | ||
} |
Oops, something went wrong.