This is an automated email from the ASF dual-hosted git repository. sseifert pushed a commit to branch experimental/SLING-12359-record-unit-test in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
commit c3db4ad3172c852cf401a6f94ac619f3b1a88131 Author: Stefan Seifert <stefanseif...@users.noreply.github.com> AuthorDate: Mon Jun 24 12:01:38 2024 +0200 SLING-12359 Experimental test case with Java 17 using a Record-based model --- pom.xml | 1 + .../apache/sling/models/impl/ConstructorTest.java | 22 ++++++++++++++- .../models/testmodels/classes/RecordModel.java | 28 +++++++++++++++++++ .../classes/RecordNamedAttributesModel.java | 31 ++++++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 2dd843c..81b7550 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,7 @@ <url>https://github.com/apache/sling-org-apache-sling-models-impl.git</url> </scm> <properties> + <sling.java.version>17</sling.java.version> <project.build.outputTimestamp>2023-05-30T08:54:10Z</project.build.outputTimestamp> </properties> diff --git a/src/test/java/org/apache/sling/models/impl/ConstructorTest.java b/src/test/java/org/apache/sling/models/impl/ConstructorTest.java index 7bfc250..510c686 100644 --- a/src/test/java/org/apache/sling/models/impl/ConstructorTest.java +++ b/src/test/java/org/apache/sling/models/impl/ConstructorTest.java @@ -34,6 +34,8 @@ import org.apache.sling.models.impl.injectors.RequestAttributeInjector; import org.apache.sling.models.impl.injectors.SelfInjector; import org.apache.sling.models.impl.via.BeanPropertyViaProvider; import org.apache.sling.models.testmodels.classes.InvalidConstructorModel; +import org.apache.sling.models.testmodels.classes.RecordModel; +import org.apache.sling.models.testmodels.classes.RecordNamedAttributesModel; import org.apache.sling.models.testmodels.classes.SuperclassConstructorModel; import org.apache.sling.models.testmodels.classes.WithOneConstructorModel; import org.apache.sling.models.testmodels.classes.WithThreeConstructorsModel; @@ -86,7 +88,9 @@ public class ConstructorTest { InvalidConstructorModel.class, WithThreeConstructorsOneInjectModel.class, NoNameModel.class, - ViaRequestSuffixModel.class); + ViaRequestSuffixModel.class, + RecordModel.class, + RecordNamedAttributesModel.class); } @Test @@ -211,4 +215,20 @@ public class ConstructorTest { assertThat(model, Matchers.notNullValue()); assertThat(model.getSuffix(), Matchers.is("/the/suffix")); } + + @Test + public void testRecordImplicitConstructor() { + RecordModel model = factory.getAdapter(request, RecordModel.class); + assertNotNull(model); + assertEquals(INT_VALUE, model.attribute()); + assertEquals(STRING_VALUE, model.attribute2()); + } + + @Test + public void testRecordNamedAttributesImplicitConstructor() { + RecordNamedAttributesModel model = factory.getAdapter(request, RecordNamedAttributesModel.class); + assertNotNull(model); + assertEquals(INT_VALUE, model.attribute()); + assertEquals(STRING_VALUE, model.attribute2()); + } } diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/RecordModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/RecordModel.java new file mode 100644 index 0000000..579f1d7 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testmodels/classes/RecordModel.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.sling.models.testmodels.classes; + +import javax.inject.Inject; + +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; +import org.apache.sling.models.annotations.Model; + +@Model(adaptables = SlingHttpServletRequest.class) +public record RecordModel(@Inject int attribute, @Inject @Default(values = "unknown") String attribute2) {} diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/RecordNamedAttributesModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/RecordNamedAttributesModel.java new file mode 100644 index 0000000..3d29219 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testmodels/classes/RecordNamedAttributesModel.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.sling.models.testmodels.classes; + +import javax.inject.Inject; +import javax.inject.Named; + +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; +import org.apache.sling.models.annotations.Model; + +@Model(adaptables = SlingHttpServletRequest.class) +public record RecordNamedAttributesModel( + @Inject @Named("attribute") int attribute, + @Inject @Named("attribute2") @Default(values = "unknown") String attribute2) {}