diff --git a/ChangeLog.md b/ChangeLog.md index 7f9a870113..3ad5e71af0 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -16,6 +16,12 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> +**2026-11-21 Arturo Bernal (abernal AT apache DOT org)** + +* _3.0.2-git-2_ + +* [JSPWIKI-572](https://issues.apache.org/jira/browse/JSPWIKI-572): tests for the `IndexPlugin`: the pages are grouped by their first letter and sorted, no entry is empty, and the pages whose names differ only in the case of a letter are all listed. + **2026-11-19 Arturo Bernal (abernal AT apache DOT org)** * _3.0.2-git-1_ diff --git a/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java b/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java index 26cb6a0efa..ad73c8ffa7 100644 --- a/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java +++ b/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java @@ -69,7 +69,7 @@ public final class Release { *

* If the build identifier is empty, it is not added. */ - public static final String BUILD = "1"; + public static final String BUILD = "2"; /** * This is the generic version string you should use when printing out the version. It is of diff --git a/jspwiki-main/src/test/java/org/apache/wiki/plugin/IndexPluginTest.java b/jspwiki-main/src/test/java/org/apache/wiki/plugin/IndexPluginTest.java new file mode 100644 index 0000000000..3fb580f398 --- /dev/null +++ b/jspwiki-main/src/test/java/org/apache/wiki/plugin/IndexPluginTest.java @@ -0,0 +1,133 @@ +/* + 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.wiki.plugin; + +import org.apache.wiki.HttpMockFactory; +import org.apache.wiki.TestEngine; +import org.apache.wiki.api.core.Context; +import org.apache.wiki.api.spi.Wiki; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.apache.wiki.TestEngine.with; + +class IndexPluginTest { + + private static final String INDEX = "{INSERT org.apache.wiki.plugin.IndexPlugin}"; + private static final Pattern BODY = Pattern.compile( "]*class=\"body\"[^>]*>(.*?)" ); + private static final Pattern ENTRY = Pattern.compile( "]*>([^<]*)" ); + + private static final String[] PAGES = { "IdxAlpha", "IdxApple", "IdxBeta", "IdxMixed", "IDXMIXED", "idxMixed" }; + + private TestEngine engine; + private Context context; + + @BeforeEach + void setUp() { + engine = TestEngine.build( with( "jspwiki.cache.enable", "false" ) ); + context = Wiki.context().create( engine, HttpMockFactory.createHttpRequest(), Wiki.contents().page( engine, "IdxAlpha" ) ); + } + + @AfterEach + void tearDown() { + for( final String page : PAGES ) { + engine.deleteTestPage( page ); + } + } + + /** The names listed by the plugin, section by section, in the order they appear. */ + private List< List< String > > sections( final String html ) { + final List< List< String > > sections = new ArrayList<>(); + final Matcher body = BODY.matcher( html ); + while( body.find() ) { + final List< String > names = new ArrayList<>(); + final Matcher entry = ENTRY.matcher( body.group( 1 ) ); + while( entry.find() ) { + names.add( entry.group( 1 ) ); + } + sections.add( names ); + } + return sections; + } + + @Test + void testPagesAreGroupedByTheirFirstLetter() throws Exception { + engine.saveText( "IdxAlpha", "alpha" ); + engine.saveText( "IdxApple", "apple" ); + engine.saveText( "IdxBeta", "beta" ); + + final List< List< String > > sections = sections( engine.getManager( PluginManager.class ).execute( context, INDEX ) ); + + Assertions.assertEquals( 1, sections.size(), "all the names start with the same letter" ); + final List< String > names = sections.get( 0 ); + Assertions.assertTrue( names.containsAll( List.of( "IdxAlpha", "IdxApple", "IdxBeta" ) ), names.toString() ); + Assertions.assertTrue( names.indexOf( "IdxAlpha" ) < names.indexOf( "IdxApple" ), "sorted: " + names ); + Assertions.assertTrue( names.indexOf( "IdxApple" ) < names.indexOf( "IdxBeta" ), "sorted: " + names ); + } + + @Test + void testThereAreNoEmptyEntries() throws Exception { + engine.saveText( "IdxAlpha", "alpha" ); + engine.saveText( "IdxBeta", "beta" ); + + final String html = engine.getManager( PluginManager.class ).execute( context, INDEX ); + + sections( html ).forEach( names -> names.forEach( name -> Assertions.assertFalse( name.isBlank(), "empty entry in " + names ) ) ); + Assertions.assertFalse( html.contains( ", ," ), html ); + Assertions.assertFalse( html.contains( ", " ), html ); + } + + /** JSPWIKI-572: the pages whose names differ only in the case of a letter are all listed. */ + @Test + void testPagesWhoseNamesDifferOnlyInCaseAreAllListed() throws Exception { + Assumptions.assumeTrue( fileSystemIsCaseSensitive(), "the page files of these names would be the same file" ); + engine.saveText( "IdxMixed", "one" ); + engine.saveText( "IDXMIXED", "two" ); + engine.saveText( "idxMixed", "three" ); + + final List< String > listed = new ArrayList<>(); + sections( engine.getManager( PluginManager.class ).execute( context, INDEX ) ).forEach( listed::addAll ); + + Assertions.assertTrue( listed.containsAll( List.of( "IdxMixed", "IDXMIXED", "idxMixed" ) ), listed.toString() ); + } + + private static boolean fileSystemIsCaseSensitive() throws IOException { + final Path dir = Files.createTempDirectory( "case" ); + final Path lower = dir.resolve( "ab" ); + try { + Files.createFile( lower ); + return !Files.exists( dir.resolve( "AB" ) ); + } finally { + Files.deleteIfExists( lower ); + Files.deleteIfExists( dir ); + } + } + +}