From 324aebf011439f67b8be66b64eff5b6568afaa4a Mon Sep 17 00:00:00 2001 From: Michael Fross Date: Tue, 18 Jul 2023 13:45:17 -0500 Subject: [PATCH] Added a few tests for the xPath lookup class --- .../java/org/fross/quoter/XPathLookup.java | 36 +++--- .../org/fross/quoter/XPathLookupTest.java | 103 ++++++++++++++++++ 2 files changed, 121 insertions(+), 18 deletions(-) create mode 100644 src/test/java/org/fross/quoter/XPathLookupTest.java diff --git a/src/main/java/org/fross/quoter/XPathLookup.java b/src/main/java/org/fross/quoter/XPathLookup.java index 8dcd3ff..731d146 100644 --- a/src/main/java/org/fross/quoter/XPathLookup.java +++ b/src/main/java/org/fross/quoter/XPathLookup.java @@ -3,19 +3,19 @@ * * Quoter is a command line program that display stock quotes and index data. * - * * Copyright (c) 2019-2022 Michael Fross + * Copyright (c) 2019-2022 Michael Fross * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER @@ -39,7 +39,7 @@ public class XPathLookup { */ public XPathLookup() { // --------------------------------------------------------------------------------- - // Symbols with market CLOSED + // xPaths: Symbols with market CLOSED // --------------------------------------------------------------------------------- symbolClosed.put("latestPrice", "/html/body/div[3]/div[2]/div[3]/div/div[4]/table/tbody/tr/td[1]"); symbolClosed.put("change", "/html/body/div[3]/div[2]/div[3]/div/div[4]/table/tbody/tr/td[2]"); @@ -52,7 +52,7 @@ public XPathLookup() { symbolClosed.put("fullname", "/html/body/div[3]/div[2]/div[2]/div/div[2]/h1"); // --------------------------------------------------------------------------------- - // Symbols with market OPEN + // xPaths: Symbols with market OPEN // --------------------------------------------------------------------------------- symbolOpen.put("latestPrice", "/html/body/div[3]/div[2]/div[3]/div/div[2]/h2/bg-quote"); symbolOpen.put("change", "/html/body/div[3]/div[2]/div[3]/div/div[2]/bg-quote/span[1]/bg-quote"); @@ -65,7 +65,7 @@ public XPathLookup() { symbolOpen.put("fullname", "/html/body/div[3]/div[2]/div[2]/div/div[2]/h1"); // --------------------------------------------------------------------------------- - // Indexes with market CLOSED + // xPaths: Indexes with market CLOSED // --------------------------------------------------------------------------------- indexClosed.put("latestPrice", "/html/body/div[3]/div[2]/div[3]/div/div[2]/h2/span"); indexClosed.put("change", "/html/body/div[3]/div[2]/div[3]/div/div[2]/bg-quote/span[1]"); @@ -76,7 +76,7 @@ public XPathLookup() { indexClosed.put("timeStamp", "/html/body/div[3]/div[2]/div[3]/div/div[1]/span/bg-quote"); // --------------------------------------------------------------------------------- - // Indexes with market OPEN + // xPaths: Indexes with market OPEN // --------------------------------------------------------------------------------- indexOpen.put("latestPrice", "/html/body/div[3]/div[2]/div[3]/div/div[2]/h2/bg-quote"); indexOpen.put("change", "/html/body/div[3]/div[2]/div[3]/div/div[2]/bg-quote/span[1]/bg-quote"); @@ -91,7 +91,7 @@ public XPathLookup() { } /** - * lookupSymbolOpen(): Returns the xPath string as maped to the provided key + * lookupSymbolOpen(): Returns the xPath string as mapped to the provided key * * @param key * @return @@ -101,7 +101,7 @@ public String lookupSymbolOpen(String key) { } /** - * lookupSymbolClosed(): Returns the xPath string as maped to the provided key + * lookupSymbolClosed(): Returns the xPath string as mapped to the provided key * * @param key * @return @@ -111,7 +111,7 @@ public String lookupSymbolClosed(String key) { } /** - * lookupIndexOpen(): Returns the xPath string as maped to the provided key + * lookupIndexOpen(): Returns the xPath string as mapped to the provided key * * @param key * @return @@ -121,7 +121,7 @@ public String lookupIndexOpen(String key) { } /** - * lookupIndexClosed(): Returns the xPath string as maped to the provided key + * lookupIndexClosed(): Returns the xPath string as mapped to the provided key * * @param key * @return diff --git a/src/test/java/org/fross/quoter/XPathLookupTest.java b/src/test/java/org/fross/quoter/XPathLookupTest.java new file mode 100644 index 0000000..268f00d --- /dev/null +++ b/src/test/java/org/fross/quoter/XPathLookupTest.java @@ -0,0 +1,103 @@ +/************************************************************************************************************** + * Quoter.jar + * + * Quoter is a command line program that display stock quotes and index data. + * + * Copyright (c) 2019-2022 Michael Fross + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + ***************************************************************************************************************/ +package org.fross.quoter; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +class XPathLookupTest { + + // Simple test - make sure each of the xPath hash maps have the right number of fields + @Test + void fieldTest() { + XPathLookup xPathLookup = new XPathLookup(); + + assertEquals(9, xPathLookup.symbolClosed.size()); + assertEquals(9, xPathLookup.symbolOpen.size()); + assertEquals(7, xPathLookup.indexClosed.size()); + assertEquals(8, xPathLookup.indexOpen.size()); + } + + // Ensure that some text exists within each lookup value + @Test + void contentTest() { + XPathLookup xPathLookup = new XPathLookup(); + + // lookupSymbolClosed + for (String value : xPathLookup.symbolClosed.keySet()) + assertFalse(xPathLookup.symbolClosed.get(value).isEmpty()); + + // lookupSymbolOpen + for (String value : xPathLookup.symbolOpen.keySet()) + assertFalse(xPathLookup.symbolOpen.get(value).isEmpty()); + + // lookupIndexClosed + for (String value : xPathLookup.indexClosed.keySet()) + assertFalse(xPathLookup.indexClosed.get(value).isEmpty()); + + // lookupIndexOpen + for (String value : xPathLookup.indexOpen.keySet()) + assertFalse(xPathLookup.indexOpen.get(value).isEmpty()); + + } + + // Do a few lookups and just make sure we get non-null back + @Test + void lookupTests() { + XPathLookup xPathLookup = new XPathLookup(); + + // lookupSymbolClosed + assertNotNull(xPathLookup.lookupSymbolClosed("latestPrice")); + assertNotNull(xPathLookup.lookupSymbolClosed("fullname")); + assertNull(xPathLookup.lookupSymbolClosed("xxx")); + assertNull(xPathLookup.lookupSymbolClosed("yyy")); + + // lookupSymbolOpen + assertNotNull(xPathLookup.lookupSymbolOpen("change")); + assertNotNull(xPathLookup.lookupSymbolOpen("52weekRange")); + assertNull(xPathLookup.lookupSymbolOpen("xxx")); + assertNull(xPathLookup.lookupSymbolOpen("yyy")); + + // lookupIndexClosed + assertNotNull(xPathLookup.lookupIndexClosed("oneYearChangePercent")); + assertNotNull(xPathLookup.lookupIndexClosed("timeStamp")); + assertNull(xPathLookup.lookupIndexClosed("xxx")); + assertNull(xPathLookup.lookupIndexClosed("yyy")); + + // lookupIndexOpen + assertNotNull(xPathLookup.lookupIndexOpen("marketStatus")); + assertNotNull(xPathLookup.lookupIndexOpen("52weekRange")); + assertNull(xPathLookup.lookupIndexOpen("xxx")); + assertNull(xPathLookup.lookupIndexOpen("yyy")); + + } + +}