This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit 812bfbc470faafca96a766ad0c03bc5440c3e9d7 Author: juanpablo <[email protected]> AuthorDate: Fri Dec 6 23:23:43 2019 +0100 move getRequiredProperty from WikiEngine to TextUtil --- .../src/main/java/org/apache/wiki/WikiEngine.java | 18 -- .../test/java/org/apache/wiki/WikiEngineTest.java | 21 -- .../main/java/org/apache/wiki/util/TextUtil.java | 19 ++ .../java/org/apache/wiki/util/TextUtilTest.java | 317 ++++++++------------- 4 files changed, 134 insertions(+), 241 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java index 4ab6b73..c7fe788 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java @@ -1843,24 +1843,6 @@ public class WikiEngine } /** - * Throws an exception if a property is not found. - * - * @param props A set of properties to search the key in. - * @param key The key to look for. - * @return The required property - * - * @throws NoRequiredPropertyException If the search key is not in the property set. - * @since 2.0.26 (on TextUtils, moved To WikiEngine on 2.11.0-M1) - */ - public String getRequiredProperty( Properties props, String key ) throws NoRequiredPropertyException { - String value = TextUtil.getStringProperty( props, key, null ); - if( value == null ) { - throw new NoRequiredPropertyException( "Required property not found", key ); - } - return value; - } - - /** * Returns the current PageManager which is responsible for storing * and managing WikiPages. * diff --git a/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java b/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java index c5bde8c..6faa84f 100644 --- a/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java +++ b/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java @@ -847,26 +847,5 @@ public class WikiEngineTest Assertions.assertEquals( TextUtil.normalizePostData( "" ), m_engine.getText( name ), "wrong content" ); } - - @Test - public void testGetRequiredProperty() throws Exception - { - String[] vals = { "foo", " this is a property ", "bar", "60" }; - Properties props = TextUtil.createProperties(vals); - Assertions.assertEquals( "60", m_engine.getRequiredProperty( props, "bar" ) ); - } - - @Test - public void testGetRequiredPropertyNRPE() - { - String[] vals = { "foo", " this is a property ", "bar", "60" }; - Properties props = TextUtil.createProperties(vals); - try - { - m_engine.getRequiredProperty( props, "ber" ); - Assertions.fail( "NoRequiredPropertyException should've been thrown!" ); - } - catch (NoRequiredPropertyException nrpe) {} - } } diff --git a/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java b/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java index 26c109a..ba46853 100644 --- a/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java +++ b/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.SecureRandom; +import java.util.NoSuchElementException; import java.util.Properties; import java.util.Random; @@ -392,6 +393,24 @@ public final class TextUtil { } /** + * Throws an exception if a property is not found. + * + * @param props A set of properties to search the key in. + * @param key The key to look for. + * @return The required property + * + * @throws NoSuchElementException If the search key is not in the property set. + * @since 2.0.26 (on TextUtils, moved To WikiEngine on 2.11.0-M1 and back to TextUtils on 2.11.0-M6) + */ + public static String getRequiredProperty( Properties props, String key ) throws NoSuchElementException { + String value = getStringProperty( props, key, null ); + if( value == null ) { + throw new NoSuchElementException( "Required property not found: " + key ); + } + return value; + } + + /** * Fetches a file path property from the set of Properties. * * Before inspecting the props, we first check if there is a Java System Property with the same name, if it exists diff --git a/jspwiki-util/src/test/java/org/apache/wiki/util/TextUtilTest.java b/jspwiki-util/src/test/java/org/apache/wiki/util/TextUtilTest.java index 265e943..f3d6375 100644 --- a/jspwiki-util/src/test/java/org/apache/wiki/util/TextUtilTest.java +++ b/jspwiki-util/src/test/java/org/apache/wiki/util/TextUtilTest.java @@ -18,215 +18,167 @@ */ package org.apache.wiki.util; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + import java.io.File; +import java.util.NoSuchElementException; import java.util.Properties; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -public class TextUtilTest -{ +public class TextUtilTest { + @Test - public void testGenerateRandomPassword() - { - for (int i=0; i<1000; i++) { + public void testGenerateRandomPassword() { + for( int i = 0; i < 1000; i++ ) { Assertions.assertEquals( TextUtil.PASSWORD_LENGTH, TextUtil.generateRandomPassword().length(), "pw" ); } } @Test - public void testEncodeName_1() - { - String name = "Hello/World"; - + public void testEncodeName_1() { + final String name = "Hello/World"; Assertions.assertEquals( "Hello/World", TextUtil.urlEncode(name,"ISO-8859-1") ); } @Test - public void testEncodeName_2() - { - String name = "Hello~World"; - + public void testEncodeName_2() { + final String name = "Hello~World"; Assertions.assertEquals( "Hello%7EWorld", TextUtil.urlEncode(name,"ISO-8859-1") ); } @Test - public void testEncodeName_3() - { - String name = "Hello/World ~"; - + public void testEncodeName_3() { + final String name = "Hello/World ~"; Assertions.assertEquals( "Hello/World+%7E", TextUtil.urlEncode(name,"ISO-8859-1") ); } @Test - public void testDecodeName_1() - throws Exception - { - String name = "Hello/World+%7E+%2F"; - + public void testDecodeName_1() { + final String name = "Hello/World+%7E+%2F"; Assertions.assertEquals( "Hello/World ~ /", TextUtil.urlDecode(name,"ISO-8859-1") ); } @Test - public void testEncodeNameUTF8_1() - { - String name = "\u0041\u2262\u0391\u002E"; - + public void testEncodeNameUTF8_1() { + final String name = "\u0041\u2262\u0391\u002E"; Assertions.assertEquals( "A%E2%89%A2%CE%91.", TextUtil.urlEncodeUTF8(name) ); } @Test - public void testEncodeNameUTF8_2() - { - String name = "\uD55C\uAD6D\uC5B4"; - + public void testEncodeNameUTF8_2() { + final String name = "\uD55C\uAD6D\uC5B4"; Assertions.assertEquals( "%ED%95%9C%EA%B5%AD%EC%96%B4", TextUtil.urlEncodeUTF8(name) ); } @Test - public void testEncodeNameUTF8_3() - { - String name = "\u65E5\u672C\u8A9E"; - + public void testEncodeNameUTF8_3() { + final String name = "\u65E5\u672C\u8A9E"; Assertions.assertEquals( "%E6%97%A5%E6%9C%AC%E8%AA%9E", TextUtil.urlEncodeUTF8(name) ); } @Test - public void testEncodeNameUTF8_4() - { - String name = "Hello World"; - + public void testEncodeNameUTF8_4() { + final String name = "Hello World"; Assertions.assertEquals( "Hello+World", TextUtil.urlEncodeUTF8(name) ); } @Test - public void testDecodeNameUTF8_1() - { - String name = "A%E2%89%A2%CE%91."; - + public void testDecodeNameUTF8_1() { + final String name = "A%E2%89%A2%CE%91."; Assertions.assertEquals( "\u0041\u2262\u0391\u002E", TextUtil.urlDecodeUTF8(name) ); } @Test - public void testDecodeNameUTF8_2() - { - String name = "%ED%95%9C%EA%B5%AD%EC%96%B4"; - + public void testDecodeNameUTF8_2() { + final String name = "%ED%95%9C%EA%B5%AD%EC%96%B4"; Assertions.assertEquals( "\uD55C\uAD6D\uC5B4", TextUtil.urlDecodeUTF8(name) ); } @Test - public void testDecodeNameUTF8_3() - { - String name = "%E6%97%A5%E6%9C%AC%E8%AA%9E"; - + public void testDecodeNameUTF8_3() { + final String name = "%E6%97%A5%E6%9C%AC%E8%AA%9E"; Assertions.assertEquals( "\u65E5\u672C\u8A9E", TextUtil.urlDecodeUTF8(name) ); } @Test - public void testReplaceString1() - { - String text = "aabacaa"; - + public void testReplaceString1() { + final String text = "aabacaa"; Assertions.assertEquals( "ddbacdd", TextUtil.replaceString( text, "aa", "dd" ) ); } @Test - public void testReplaceString4() - { - String text = "aabacaafaa"; - + public void testReplaceString4() { + final String text = "aabacaafaa"; Assertions.assertEquals( "ddbacddfdd", TextUtil.replaceString( text, "aa", "dd" ) ); } @Test - public void testReplaceString5() - { - String text = "aaabacaaafaa"; - + public void testReplaceString5() { + final String text = "aaabacaaafaa"; Assertions.assertEquals( "dbacdfaa", TextUtil.replaceString( text, "aaa", "d" ) ); } @Test - public void testReplaceString2() - { - String text = "abcde"; - + public void testReplaceString2() { + final String text = "abcde"; Assertions.assertEquals( "fbcde", TextUtil.replaceString( text, "a", "f" ) ); } @Test - public void testReplaceString3() - { - String text = "ababab"; - + public void testReplaceString3() { + final String text = "ababab"; Assertions.assertEquals( "afafaf", TextUtil.replaceString( text, "b", "f" ) ); } @Test - public void testReplaceStringCaseUnsensitive1() - { - String text = "aABcAa"; - + public void testReplaceStringCaseUnsensitive1() { + final String text = "aABcAa"; Assertions.assertEquals( "ddBcdd", TextUtil.replaceStringCaseUnsensitive( text, "aa", "dd" ) ); } @Test - public void testReplaceStringCaseUnsensitive2() - { - String text = "Abcde"; - + public void testReplaceStringCaseUnsensitive2() { + final String text = "Abcde"; Assertions.assertEquals( "fbcde", TextUtil.replaceStringCaseUnsensitive( text, "a", "f" ) ); } @Test - public void testReplaceStringCaseUnsensitive3() - { - String text = "aBAbab"; - + public void testReplaceStringCaseUnsensitive3() { + final String text = "aBAbab"; Assertions.assertEquals( "afAfaf", TextUtil.replaceStringCaseUnsensitive( text, "b", "f" ) ); } @Test - public void testReplaceStringCaseUnsensitive4() - { - String text = "AaBAcAAfaa"; - + public void testReplaceStringCaseUnsensitive4() { + final String text = "AaBAcAAfaa"; Assertions.assertEquals( "ddBAcddfdd", TextUtil.replaceStringCaseUnsensitive( text, "aa", "dd" ) ); } @Test - public void testReplaceStringCaseUnsensitive5() - { - String text = "aAaBaCAAafaa"; - + public void testReplaceStringCaseUnsensitive5() { + final String text = "aAaBaCAAafaa"; Assertions.assertEquals( "dBaCdfaa", TextUtil.replaceStringCaseUnsensitive( text, "aaa", "d" ) ); } // Pure UNIX. @Test - public void testNormalizePostdata1() - { - String text = "ab\ncd"; - + public void testNormalizePostdata1() { + final String text = "ab\ncd"; Assertions.assertEquals( "ab\r\ncd\r\n", TextUtil.normalizePostData( text ) ); } // Pure MSDOS. @Test - public void testNormalizePostdata2() - { - String text = "ab\r\ncd"; - + public void testNormalizePostdata2() { + final String text = "ab\r\ncd"; Assertions.assertEquals( "ab\r\ncd\r\n", TextUtil.normalizePostData( text ) ); } // Pure Mac @Test - public void testNormalizePostdata3() - { - String text = "ab\rcd"; - + public void testNormalizePostdata3() { + final String text = "ab\rcd"; Assertions.assertEquals( "ab\r\ncd\r\n", TextUtil.normalizePostData( text ) ); } @@ -234,43 +186,34 @@ public class TextUtilTest @Test public void testNormalizePostdata4() { - String text = "ab\ncd\r\n\r\n\r"; - + final String text = "ab\ncd\r\n\r\n\r"; Assertions.assertEquals( "ab\r\ncd\r\n\r\n\r\n", TextUtil.normalizePostData( text ) ); } // Multiple newlines @Test - public void testNormalizePostdata5() - { - String text = "ab\ncd\n\n\n\n"; - + public void testNormalizePostdata5() { + final String text = "ab\ncd\n\n\n\n"; Assertions.assertEquals( "ab\r\ncd\r\n\r\n\r\n\r\n", TextUtil.normalizePostData( text ) ); } // Empty. @Test - public void testNormalizePostdata6() - { - String text = ""; - + public void testNormalizePostdata6() { + final String text = ""; Assertions.assertEquals( "\r\n", TextUtil.normalizePostData( text ) ); } // Just a newline. @Test - public void testNormalizePostdata7() - { - String text = "\n"; - + public void testNormalizePostdata7() { + final String text = "\n"; Assertions.assertEquals( "\r\n", TextUtil.normalizePostData( text ) ); } @Test - public void testGetBooleanProperty() - { - Properties props = new Properties(); - + public void testGetBooleanProperty() { + final Properties props = new Properties(); props.setProperty("foobar.0", "YES"); props.setProperty("foobar.1", "true"); props.setProperty("foobar.2", "false"); @@ -281,140 +224,110 @@ public class TextUtilTest Assertions.assertTrue( TextUtil.getBooleanProperty( props, "foobar.0", false ), "foobar.0" ); Assertions.assertTrue( TextUtil.getBooleanProperty( props, "foobar.1", false ), "foobar.1" ); - Assertions.assertFalse( TextUtil.getBooleanProperty( props, "foobar.2", true ), "foobar.2" ); Assertions.assertFalse( TextUtil.getBooleanProperty( props, "foobar.3", true ), "foobar.3" ); Assertions.assertTrue( TextUtil.getBooleanProperty( props, "foobar.4", false ), "foobar.4" ); - Assertions.assertFalse( TextUtil.getBooleanProperty( props, "foobar.5", true ), "foobar.5" ); - Assertions.assertFalse( TextUtil.getBooleanProperty( props, "foobar.6", true ), "foobar.6" ); } @Test - public void testGetSection1() - throws Exception - { - String src = "Single page."; + public void testGetSection1() { + final String src = "Single page."; Assertions.assertEquals( src, TextUtil.getSection(src,1), "section 1" ); - - try - { - TextUtil.getSection( src, 5 ); - Assertions.fail("Did not get exception for 2"); - } - catch( IllegalArgumentException e ) {} - - try - { - TextUtil.getSection( src, -1 ); - Assertions.fail("Did not get exception for -1"); - } - catch( IllegalArgumentException e ) {} + Assertions.assertThrows( IllegalArgumentException.class, () -> TextUtil.getSection( src, 5 ) ); + Assertions.assertThrows( IllegalArgumentException.class, () -> TextUtil.getSection( src, -1 ) ); } @Test - public void testGetSection2() - throws Exception - { - String src = "First section\n----\nSecond section\n\n----\n\nThird section"; + public void testGetSection2() { + final String src = "First section\n----\nSecond section\n\n----\n\nThird section"; Assertions.assertEquals( "First section\n", TextUtil.getSection(src,1), "section 1" ); Assertions.assertEquals( "\nSecond section\n\n", TextUtil.getSection(src,2), "section 2" ); Assertions.assertEquals( "\n\nThird section", TextUtil.getSection(src,3), "section 3" ); - - try - { - TextUtil.getSection( src, 4 ); - Assertions.fail("Did not get exception for section 4"); - } - catch( IllegalArgumentException e ) {} + Assertions.assertThrows( IllegalArgumentException.class, () -> TextUtil.getSection( src, 4 ) ); } @Test - public void testGetSection3() - throws Exception - { - String src = "----\nSecond section\n----"; - + public void testGetSection3() { + final String src = "----\nSecond section\n----"; Assertions.assertEquals( "", TextUtil.getSection(src,1), "section 1" ); Assertions.assertEquals( "\nSecond section\n", TextUtil.getSection(src,2), "section 2" ); Assertions.assertEquals( "", TextUtil.getSection(src,3), "section 3" ); - - try - { - TextUtil.getSection( src, 4 ); - Assertions.fail("Did not get exception for section 4"); - } - catch( IllegalArgumentException e ) {} + Assertions.assertThrows( IllegalArgumentException.class, () -> TextUtil.getSection( src, 4 ) ); } @Test - public void testGetSectionWithMoreThanFourDashes() throws Exception - { - String src = "----------------\nSecond section\n----"; - + public void testGetSectionWithMoreThanFourDashes() { + final String src = "----------------\nSecond section\n----"; Assertions.assertEquals( "\nSecond section\n", TextUtil.getSection(src, 2), "section 2" ); } @Test - public void testBooleanParameter() - throws Exception - { - Assertions.assertEquals( true, TextUtil.isPositive(" true "), "1" ); - Assertions.assertEquals( false, TextUtil.isPositive(" fewqkfow kfpokwe "), "2" ); - Assertions.assertEquals( true, TextUtil.isPositive("on"), "3" ); - Assertions.assertEquals( true, TextUtil.isPositive("\t\ton"), "4" ); + public void testBooleanParameter() { + Assertions.assertTrue( TextUtil.isPositive(" true "), "1" ); + Assertions.assertFalse( TextUtil.isPositive(" fewqkfow kfpokwe "), "2" ); + Assertions.assertTrue( TextUtil.isPositive("on"), "3" ); + Assertions.assertTrue( TextUtil.isPositive("\t\ton"), "4" ); } @Test - public void testTrimmedProperty() - { - String[] vals = { "foo", " this is a property ", "bar", "60" }; - - Properties props = TextUtil.createProperties(vals); + public void testTrimmedProperty() { + final String[] vals = { "foo", " this is a property ", "bar", "60" }; + final Properties props = TextUtil.createProperties(vals); Assertions.assertEquals( "this is a property", TextUtil.getStringProperty(props,"foo",""), "foo" ); Assertions.assertEquals( 60, TextUtil.getIntegerProperty(props,"bar",0), "bar" ); } @Test - public void testGetStringProperty() - { - String[] vals = { "foo", " this is a property " }; - Properties props = TextUtil.createProperties(vals); + public void testGetStringProperty() { + final String[] vals = { "foo", " this is a property " }; + final Properties props = TextUtil.createProperties(vals); Assertions.assertEquals( "this is a property", TextUtil.getStringProperty( props, "foo", "err" ) ); } @Test - public void testGetStringPropertyDefaultValue() - { - String defaultValue = System.getProperty( "user.home" ) + File.separator + "jspwiki-files"; - String[] vals = { "foo", " this is a property " }; - Properties props = TextUtil.createProperties(vals); + public void testGetStringPropertyDefaultValue() { + final String defaultValue = System.getProperty( "user.home" ) + File.separator + "jspwiki-files"; + final String[] vals = { "foo", " this is a property " }; + final Properties props = TextUtil.createProperties(vals); Assertions.assertEquals( defaultValue, TextUtil.getStringProperty( props, "bar", defaultValue ) ); } @Test - public void testGetCanonicalFilePathProperty() - { - String[] values = { "jspwiki.fileSystemProvider.pageDir", " ." + File.separator + "data" + File.separator + "private " }; - Properties props = TextUtil.createProperties(values); - String path = TextUtil.getCanonicalFilePathProperty(props, "jspwiki.fileSystemProvider.pageDir", "NA"); + public void testGetCanonicalFilePathProperty() { + final String[] values = { "jspwiki.fileSystemProvider.pageDir", " ." + File.separator + "data" + File.separator + "private " }; + final Properties props = TextUtil.createProperties(values); + final String path = TextUtil.getCanonicalFilePathProperty(props, "jspwiki.fileSystemProvider.pageDir", "NA"); Assertions.assertTrue( path.endsWith( File.separator + "data" + File.separator + "private" ) ); Assertions.assertFalse( path.endsWith( "." + File.separator + "data" + File.separator + "private" ) ); } @Test - public void testGetCanonicalFilePathPropertyDefaultValue() - { - String defaultValue = System.getProperty( "user.home" ) + File.separator + "jspwiki-files"; - String[] values = {}; - Properties props = TextUtil.createProperties(values); - String path = TextUtil.getCanonicalFilePathProperty(props, "jspwiki.fileSystemProvider.pageDir", defaultValue); + public void testGetCanonicalFilePathPropertyDefaultValue() { + final String defaultValue = System.getProperty( "user.home" ) + File.separator + "jspwiki-files"; + final String[] values = {}; + final Properties props = TextUtil.createProperties(values); + final String path = TextUtil.getCanonicalFilePathProperty(props, "jspwiki.fileSystemProvider.pageDir", defaultValue); Assertions.assertTrue(path.endsWith("jspwiki-files")); } + @Test + public void testGetRequiredProperty() { + final String[] vals = { "foo", " this is a property ", "bar", "60" }; + final Properties props = TextUtil.createProperties( vals ); + Assertions.assertEquals( "60", TextUtil.getRequiredProperty( props, "bar" ) ); + } + + @Test + public void testGetRequiredPropertyNSEE() { + final String[] vals = { "foo", " this is a property ", "bar", "60" }; + final Properties props = TextUtil.createProperties(vals); + Assertions.assertThrows( NoSuchElementException.class, () -> TextUtil.getRequiredProperty( props, "ber" ) ); + } + }
