diff --git a/src/main/java/jpos/config/simple/AbstractRegPopulator.java b/src/main/java/jpos/config/simple/AbstractRegPopulator.java index 4d35e34..b568977 100644 --- a/src/main/java/jpos/config/simple/AbstractRegPopulator.java +++ b/src/main/java/jpos/config/simple/AbstractRegPopulator.java @@ -408,34 +408,39 @@ 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 )) + 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 ) ); + return is; + } + } + } + finally { + if (is == null) { + zipFile.close(); + } + } } catch( Exception e ) { tracer.println( "findInJarZipFiles: Exception.message=" + e.getMessage() ); } - - if( is != null ) break; } return is; 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()); diff --git a/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java index da7b259..96e4a20 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("Custom XML populator file 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 ) + { + fail( "Unexpected exception message = " + e.getMessage() ); + } + + restorePropFile(); + + } + //------------------------------------------------------------------------- // Instance variables //