Skip to content

Commit b483371

Browse files
Tests for final modifier
1 parent fefc9d8 commit b483371

File tree

6 files changed

+310
-2
lines changed

6 files changed

+310
-2
lines changed

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/access/modifier/FieldEditableMakerForJavaLowerThan18.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ public class FieldEditableMakerForJavaLowerThan18<E> implements FieldAccessModif
7878

7979
private void startMakingEditable()
8080
{
81-
this.field.setAccessible(true);
81+
if(!this.wasAccessible)
82+
{
83+
this.field.setAccessible(true);
84+
}
8285
if(this.wasFinal)
8386
{
8487
if(LOG.isTraceEnabled())
@@ -106,7 +109,10 @@ public void close()
106109
}
107110
MODIFIERS.set(this.field, this.field.getModifiers() | Modifier.FINAL);
108111
}
109-
this.field.setAccessible(this.wasAccessible);
112+
if(!this.wasAccessible)
113+
{
114+
this.field.setAccessible(this.wasAccessible);
115+
}
110116
}
111117

112118
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright © 2023 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.repositories;
17+
18+
import java.util.List;
19+
import java.util.Objects;
20+
21+
22+
public class CustomerWithFinal
23+
{
24+
private final String firstName;
25+
private final String lastName;
26+
27+
public CustomerWithFinal(final String firstName, final String lastName)
28+
{
29+
this.firstName = firstName;
30+
this.lastName = lastName;
31+
}
32+
33+
public String getFirstName()
34+
{
35+
return this.firstName;
36+
}
37+
38+
public String getLastName()
39+
{
40+
return this.lastName;
41+
}
42+
43+
@Override
44+
public String toString()
45+
{
46+
return String.format(
47+
"Customer[firstName='%s', lastName='%s']",
48+
this.firstName, this.lastName);
49+
}
50+
51+
@Override
52+
public boolean equals(final Object o)
53+
{
54+
if(this == o)
55+
{
56+
return true;
57+
}
58+
if(o == null || this.getClass() != o.getClass())
59+
{
60+
return false;
61+
}
62+
final CustomerWithFinal customer = (CustomerWithFinal)o;
63+
return Objects.equals(this.firstName, customer.firstName) && Objects.equals(
64+
this.lastName,
65+
customer.lastName);
66+
}
67+
68+
@Override
69+
public int hashCode()
70+
{
71+
return Objects.hash(this.firstName, this.lastName);
72+
}
73+
74+
@SuppressWarnings("OptionalGetWithoutIsPresent")
75+
public static CustomerWithFinal getCustomerWithFirstName(
76+
final List<CustomerWithFinal> customers,
77+
final String firstName)
78+
{
79+
return customers.stream().filter(customer -> customer.getFirstName().equals(firstName)).findFirst().get();
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright © 2023 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.repositories;
17+
18+
import java.util.List;
19+
import java.util.Objects;
20+
21+
22+
public class CustomerWithFinalChild
23+
{
24+
private final String firstName;
25+
private final String lastName;
26+
27+
private final Child child;
28+
29+
public CustomerWithFinalChild(final String firstName, final String lastName)
30+
{
31+
this.firstName = firstName;
32+
this.lastName = lastName;
33+
this.child = new Child(firstName + "child", lastName + "child");
34+
}
35+
36+
public String getFirstName()
37+
{
38+
return this.firstName;
39+
}
40+
41+
public String getLastName()
42+
{
43+
return this.lastName;
44+
}
45+
46+
public Child getChild()
47+
{
48+
return this.child;
49+
}
50+
51+
@Override
52+
public String toString()
53+
{
54+
return String.format(
55+
"Customer[firstName='%s', lastName='%s']",
56+
this.firstName, this.lastName);
57+
}
58+
59+
@Override
60+
public boolean equals(final Object o)
61+
{
62+
if(this == o)
63+
{
64+
return true;
65+
}
66+
if(o == null || this.getClass() != o.getClass())
67+
{
68+
return false;
69+
}
70+
final CustomerWithFinalChild customer = (CustomerWithFinalChild)o;
71+
return Objects.equals(this.firstName, customer.firstName) && Objects.equals(
72+
this.lastName,
73+
customer.lastName);
74+
}
75+
76+
@Override
77+
public int hashCode()
78+
{
79+
return Objects.hash(this.firstName, this.lastName);
80+
}
81+
82+
@SuppressWarnings("OptionalGetWithoutIsPresent")
83+
public static CustomerWithFinalChild getCustomerWithFirstName(
84+
final List<CustomerWithFinalChild> customers,
85+
final String firstName)
86+
{
87+
return customers.stream().filter(customer -> customer.getFirstName().equals(firstName)).findFirst().get();
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright © 2023 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.repositories;
17+
18+
import org.springframework.data.repository.CrudRepository;
19+
import org.springframework.data.repository.PagingAndSortingRepository;
20+
21+
22+
public interface CustomerWithFinalChildRepository
23+
extends CrudRepository<CustomerWithFinalChild, String>, PagingAndSortingRepository<CustomerWithFinalChild, String>
24+
{
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright © 2023 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.repositories;
17+
18+
import org.springframework.data.repository.CrudRepository;
19+
import org.springframework.data.repository.PagingAndSortingRepository;
20+
21+
22+
public interface CustomerWithFinalRepository
23+
extends CrudRepository<CustomerWithFinal, String>, PagingAndSortingRepository<CustomerWithFinal, String>
24+
{
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright © 2023 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.tests;
17+
18+
import java.util.List;
19+
20+
import jakarta.inject.Inject;
21+
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.Test;
24+
25+
import software.xdev.spring.data.eclipse.store.helper.TestData;
26+
import software.xdev.spring.data.eclipse.store.helper.TestUtil;
27+
import software.xdev.spring.data.eclipse.store.integration.DefaultTestAnnotations;
28+
import software.xdev.spring.data.eclipse.store.integration.repositories.CustomerWithFinal;
29+
import software.xdev.spring.data.eclipse.store.integration.repositories.CustomerWithFinalChild;
30+
import software.xdev.spring.data.eclipse.store.integration.repositories.CustomerWithFinalChildRepository;
31+
import software.xdev.spring.data.eclipse.store.integration.repositories.CustomerWithFinalRepository;
32+
import software.xdev.spring.data.eclipse.store.repository.EclipseStoreStorage;
33+
34+
35+
@DefaultTestAnnotations
36+
class FinalTest
37+
{
38+
@Inject
39+
private CustomerWithFinalRepository repository;
40+
@Inject
41+
private CustomerWithFinalChildRepository withChildRepository;
42+
43+
@Inject
44+
private EclipseStoreStorage storage;
45+
46+
@Test
47+
void testSaveAndFindSingle()
48+
{
49+
final CustomerWithFinal customer = new CustomerWithFinal(TestData.FIRST_NAME, TestData.LAST_NAME);
50+
this.repository.save(customer);
51+
52+
TestUtil.doBeforeAndAfterRestartOfDatastore(
53+
this.storage,
54+
() -> {
55+
final List<CustomerWithFinal> customers = TestUtil.iterableToList(this.repository.findAll());
56+
Assertions.assertEquals(1, customers.size());
57+
Assertions.assertEquals(TestData.FIRST_NAME, customers.get(0).getFirstName());
58+
}
59+
);
60+
}
61+
62+
@Test
63+
void testSaveAndFindSingleWithChild()
64+
{
65+
final CustomerWithFinalChild originalCustomer =
66+
new CustomerWithFinalChild(TestData.FIRST_NAME, TestData.LAST_NAME);
67+
this.withChildRepository.save(originalCustomer);
68+
69+
TestUtil.doBeforeAndAfterRestartOfDatastore(
70+
this.storage,
71+
() -> {
72+
final List<CustomerWithFinalChild> customers =
73+
TestUtil.iterableToList(this.withChildRepository.findAll());
74+
Assertions.assertEquals(1, customers.size());
75+
Assertions.assertEquals(TestData.FIRST_NAME, customers.get(0).getFirstName());
76+
Assertions.assertEquals(
77+
originalCustomer.getChild().getFirstName(),
78+
customers.get(0).getChild().getFirstName());
79+
}
80+
);
81+
}
82+
}

0 commit comments

Comments
 (0)