From adfdb14ffcc29b15d5afb1da1347955dbe4db14d Mon Sep 17 00:00:00 2001 From: Arturas Matusak Date: Fri, 14 Feb 2025 15:05:04 +0000 Subject: [PATCH 1/6] Fix JAR resource loading - Load defined populator file - Switch to manual ZIP resource handling in search function --- .../config/simple/AbstractRegPopulator.java | 39 ++++++++++++------- .../config/simple/xml/JavaxRegPopulator.java | 2 +- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/main/java/jpos/config/simple/AbstractRegPopulator.java b/src/main/java/jpos/config/simple/AbstractRegPopulator.java index 4d35e34..8a6a9fe 100644 --- a/src/main/java/jpos/config/simple/AbstractRegPopulator.java +++ b/src/main/java/jpos/config/simple/AbstractRegPopulator.java @@ -412,22 +412,31 @@ private InputStream findFileInJarZipFiles( String fileName, List jarZipF { String jarZipFileName = jarZipFilesList.get( i ); - try (ZipFile zipFile = new ZipFile( jarZipFileName )) + try { - Enumeration zipEntries = zipFile.entries(); - - while( zipEntries.hasMoreElements() ) - { - ZipEntry zipEntry = zipEntries.nextElement(); - String entryName = zipEntry.getName(); - - if( entryName.endsWith( fileName ) ) - { - is = new BufferedInputStream( zipFile. - getInputStream( zipEntry ) ); - break; - } - } + ZipFile zipFile = new ZipFile( jarZipFileName ); + try { + + Enumeration zipEntries = zipFile.entries(); + + while( zipEntries.hasMoreElements() ) + { + ZipEntry zipEntry = zipEntries.nextElement(); + String entryName = zipEntry.getName(); + + if( entryName.endsWith( fileName ) ) + { + is = new BufferedInputStream( zipFile. + getInputStream( zipEntry ) ); + break; + } + } + } + finally { + if (is == null) { + zipFile.close(); + } + } } catch( Exception e ) { diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index 57d0787..059e96c 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -133,7 +133,7 @@ public void save(@SuppressWarnings("rawtypes") Enumeration entries, String fileN @Override public void load() { - try (InputStream is = isPopulatorFileDefined() ? new FileInputStream(DEFAULT_XML_FILE_NAME) : getPopulatorFileIS()) { + try (InputStream is = isPopulatorFileDefined() ? getPopulatorFileIS() : new FileInputStream(DEFAULT_XML_FILE_NAME) ) { load(is); } catch (Exception e) { tracer.println("Error while loading populator file Exception.message: " + e.getMessage()); From de2d676ffb41d3a99d7b4a4df8efdc40934d413a Mon Sep 17 00:00:00 2001 From: Arturas Matusak Date: Mon, 24 Feb 2025 11:24:38 +0000 Subject: [PATCH 2/6] PR Fixes: - Use return for flow control --- .../java/jpos/config/simple/AbstractRegPopulator.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/java/jpos/config/simple/AbstractRegPopulator.java b/src/main/java/jpos/config/simple/AbstractRegPopulator.java index 8a6a9fe..b568977 100644 --- a/src/main/java/jpos/config/simple/AbstractRegPopulator.java +++ b/src/main/java/jpos/config/simple/AbstractRegPopulator.java @@ -408,10 +408,8 @@ private InputStream findFileInJarZipFiles( String fileName, List jarZipF { InputStream is = null; - for( int i = 0; i < jarZipFilesList.size(); ++i ) + for (String jarZipFileName : jarZipFilesList) { - String jarZipFileName = jarZipFilesList.get( i ); - try { ZipFile zipFile = new ZipFile( jarZipFileName ); @@ -426,9 +424,9 @@ private InputStream findFileInJarZipFiles( String fileName, List jarZipF if( entryName.endsWith( fileName ) ) { - is = new BufferedInputStream( zipFile. + is = new BufferedInputStream( zipFile. getInputStream( zipEntry ) ); - break; + return is; } } } @@ -443,8 +441,6 @@ private InputStream findFileInJarZipFiles( String fileName, List jarZipF tracer.println( "findInJarZipFiles: Exception.message=" + e.getMessage() ); } - - if( is != null ) break; } return is; From a0f04a922b829918a65a2f97396da18ec6e1cf61 Mon Sep 17 00:00:00 2001 From: Arturas Matusak Date: Mon, 24 Feb 2025 16:39:15 +0000 Subject: [PATCH 3/6] PR: Attempt to add test cases - Added cases to cover load method --- .../simple/xml/JavaxRegPopulatorTestCase.java | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java index da7b259..cd659ed 100644 --- a/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java +++ b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java @@ -19,7 +19,9 @@ import jpos.config.*; import jpos.config.simple.*; +import jpos.loader.JposServiceLoader; import jpos.test.*; +import jpos.util.JposPropertiesConst; /** * A JUnit TestCase for the Loading/saving XML entries @@ -287,6 +289,108 @@ public void testGetLastLoadException() emptyTest(); } + public void testLoadDefault() throws Exception + { + Properties jclProps = new Properties(); + jclProps.put( "jpos.util.tracing.TurnOnAllNamedTracers", JPOS_UTIL_TRACING_VALUE ); + createPropFile( jclProps ); + + JposServiceLoader.getManager().getProperties().loadJposProperties(); + + javaxRegPopulator.load(); + + assertTrue("Default jpos.xml is not found", + javaxRegPopulator.getLastLoadException() instanceof FileNotFoundException); + + restorePropFile(); + + } + + public void testLoadCustomNonExisting() throws Exception + { + Properties jclProps = new Properties(); + jclProps.put( "jpos.util.tracing.TurnOnAllNamedTracers", JPOS_UTIL_TRACING_VALUE ); + jclProps.put(JposPropertiesConst.JPOS_POPULATOR_FILE_PROP_NAME, "" + System.currentTimeMillis() + ".xml" ); + createPropFile( jclProps ); + + JposServiceLoader.getManager().getProperties().loadJposProperties(); + + javaxRegPopulator.load(); + + assertTrue("Default jpos.xml is not found", + javaxRegPopulator.getLastLoadException() instanceof FileNotFoundException); + + restorePropFile(); + + } + + public void testLoadCustomPopulator() throws Exception + { + Properties jclProps = new Properties(); + jclProps.put( "jpos.util.tracing.TurnOnAllNamedTracers", JPOS_UTIL_TRACING_VALUE ); + jclProps.put(JposPropertiesConst.JPOS_POPULATOR_FILE_PROP_NAME, JCL_JUNIT_XML_FILE_NAME ); + createPropFile( jclProps ); + + JposServiceLoader.getManager().getProperties().loadJposProperties(); + + List v1 = new ArrayList<>(); + try + { + javaxRegPopulator.save( Collections.enumeration(v1), JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.load(); + + assertNull(javaxRegPopulator.getLastLoadException()); + + @SuppressWarnings("unchecked") + Enumeration entries = javaxRegPopulator.getEntries(); + + assertTrue( "Expected an empty set of entries...", JUnitUtility.isIdentical( entries, Collections.enumeration(v1) ) ); + assertTrue( "Expected an empty set of entries...", JUnitUtility.isEquals( entries, Collections.enumeration(v1) ) ); + + //Add some entries and save and load + JposEntry entry1 = createJposEntry( "entry1", "com.xyz.jpos.XyzJposServiceInstanceFactory", + "com.xyz.jpos.LineDisplayService", "Xyz, Corp.", + "http://www.javapos.com", "LineDisplay", "1.4a", + "Virtual LineDisplay JavaPOS Service", + "Example virtual LineDisplay JavaPOS Service from virtual Xyz Corporation", + "http://www.javapos.com" ); + + + v1.clear(); + v1.add( entry1 ); + + javaxRegPopulator.save( Collections.enumeration(v1) ); + javaxRegPopulator.load( ); + + assertNull(javaxRegPopulator.getLastLoadException()); + + @SuppressWarnings("unchecked") + Enumeration entries2 = javaxRegPopulator.getEntries(); + + assertTrue( "Expected 1 entry...", JUnitUtility.isEquals( entries2, Collections.enumeration(v1) ) ); + + //Remove entries save and load reg + v1.remove( entry1 ); + + javaxRegPopulator.save( Collections.enumeration(v1) ); + javaxRegPopulator.load( ); + + assertNull(javaxRegPopulator.getLastLoadException()); + + @SuppressWarnings("unchecked") + Enumeration entries3 = javaxRegPopulator.getEntries(); + + assertTrue( "Expected 1 entries...", JUnitUtility.isEquals( entries3, Collections.enumeration(v1) ) ); + } + catch( Exception e ) + { + assertTrue( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage(), true ); + } + + restorePropFile(); + + } + //------------------------------------------------------------------------- // Instance variables // From 5527ebc7534e040cb5bf9cd3f22bc4afd49ce36e Mon Sep 17 00:00:00 2001 From: art-and-co Date: Mon, 24 Feb 2025 16:59:57 +0000 Subject: [PATCH 4/6] Added Manual Runner --- .github/workflows/check.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 2623f79..b41f2a7 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -2,10 +2,11 @@ name: Check PR on: pull_request: + workflow_dispatch: jobs: javapos-workflow: uses: JavaPOSWorkingGroup/javapos-workflow/.github/workflows/check.yml@v2 with: github-event-action: ${{github.event.action}} - secrets: inherit \ No newline at end of file + secrets: inherit From f8ba81bd8d8e58dea3472d7038b2ee97ff856a48 Mon Sep 17 00:00:00 2001 From: Arturas Matusak Date: Tue, 25 Feb 2025 09:38:14 +0000 Subject: [PATCH 5/6] Revert "Added Manual Runner" This reverts commit 5527ebc7534e040cb5bf9cd3f22bc4afd49ce36e. --- .github/workflows/check.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index b41f2a7..2623f79 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -2,11 +2,10 @@ name: Check PR on: pull_request: - workflow_dispatch: jobs: javapos-workflow: uses: JavaPOSWorkingGroup/javapos-workflow/.github/workflows/check.yml@v2 with: github-event-action: ${{github.event.action}} - secrets: inherit + secrets: inherit \ No newline at end of file From 7366d0f7117b7a4123ead1aa096f90d89eab6c2b Mon Sep 17 00:00:00 2001 From: Arturas Matusak Date: Wed, 26 Feb 2025 10:30:18 +0000 Subject: [PATCH 6/6] Small corrections in new unit tests. --- .../jpos/config/simple/xml/JavaxRegPopulatorTestCase.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java index cd659ed..96e4a20 100644 --- a/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java +++ b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java @@ -317,7 +317,7 @@ public void testLoadCustomNonExisting() throws Exception javaxRegPopulator.load(); - assertTrue("Default jpos.xml is not found", + assertTrue("Custom XML populator file not found", javaxRegPopulator.getLastLoadException() instanceof FileNotFoundException); restorePropFile(); @@ -384,7 +384,7 @@ public void testLoadCustomPopulator() throws Exception } catch( Exception e ) { - assertTrue( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage(), true ); + fail( "Unexpected exception message = " + e.getMessage() ); } restorePropFile();