← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1854: Improved stream cleaning up code in import service

 

------------------------------------------------------------
revno: 1854
committer: Bob Jolliffe bobjolliffe@xxxxxxxxx
branch nick: trunk
timestamp: Thu 2010-07-15 13:10:45 +0100
message:
  Improved stream cleaning up code in import service
  Added new unit test to xmlreader 
modified:
  dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/DefaultImportService.java
  dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf/GML2DXFTest.java
  dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/reader/XMLReaderTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/DefaultImportService.java'
--- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/DefaultImportService.java	2010-07-14 13:27:39 +0000
+++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/DefaultImportService.java	2010-07-15 12:10:45 +0000
@@ -66,7 +66,6 @@
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
-    
     @Autowired
     private XMLPreConverter preConverter;
 
@@ -145,34 +144,45 @@
 
         XMLReader dxfReader = null;
         File transformOutput = null;
+        FileInputStream dxfInStream = null;
 
         try
         {
             if ( documentRootName.getLocalPart().equals( DXFConverter.DXFROOT ) )
             {
                 // Native DXF stream - no transform required
-                log.info( "Importing dxf native stream" );
+                log.debug( "Importing dxf native stream" );
                 dxfReader = XMLFactory.getXMLReader( xmlDataStream );
             } else
             {
                 log.debug( "Transforming stream" );
+                state.setMessage( "Transforming stream" );
                 transformOutput = File.createTempFile( "TRANSFORM_", ".xml" );
                 Source source = new StreamSource( xmlDataStream );
-                StreamResult result = new StreamResult( transformOutput );
+                FileOutputStream transformOutStream = new FileOutputStream( transformOutput );
+                StreamResult result = new StreamResult( transformOutStream );
                 String xsltIdentifierTag = documentRootName.toString();
                 log.debug( "Tag for transformer: " + xsltIdentifierTag );
-                // String currentUserName = currentUserService.getCurrentUsername();
+
+                //String currentUserName = currentUserService.getCurrentUsername();
                 preConverter.transform( source, result, xsltIdentifierTag, tempZipFile, null );
+                transformOutStream.flush();
+                transformOutStream.close();
+
                 log.debug( "Transform successful" );
-                dxfReader = XMLFactory.getXMLReader( new FileInputStream( transformOutput ) );
+                dxfInStream = new FileInputStream( transformOutput );
+                dxfReader = XMLFactory.getXMLReader( dxfInStream );
             }
 
+            log.debug( "sending dxf to converter" );
             converter.read( dxfReader, params, state );
+
         } catch ( Exception ex )
         {
             throw new ImportException( "Failed to import stream", ex );
         } finally
         {
+            // clean up whatever streams might be open
             if ( dxfReader != null )
             {
                 dxfReader.closeReader();
@@ -181,21 +191,26 @@
             {
                 transformOutput.delete();
             }
-            if ( zipFile != null )
-            {
-                try
-                {
-                    zipFile.close();
-                } catch ( IOException ex )
-                {
-                    // this really shouldn't happen
-                    log.warn("Failed to close zipfile");
-                }
-            }
             if ( tempZipFile != null )
             {
                 tempZipFile.delete();
             }
+
+            try
+            {
+                if ( dxfInStream != null )
+                {
+                    dxfInStream.close();
+                }
+                if ( zipFile != null )
+                {
+                    zipFile.close();
+                }
+            } catch ( IOException ex )
+            {
+                // this really shouldn't happen
+                log.warn( "Failed to close zipfile" );
+            }
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf/GML2DXFTest.java'
--- dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf/GML2DXFTest.java	2010-07-14 19:36:41 +0000
+++ dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf/GML2DXFTest.java	2010-07-15 12:10:45 +0000
@@ -93,10 +93,11 @@
         assertEquals( 9, organisationUnitService.getOrganisationUnitByName( "Port Loko" ).getCoordinatesAsList().size() );
         assertEquals( 1, organisationUnitService.getOrganisationUnitByName( "Tonkolili" ).getCoordinatesAsList().size() );
         assertEquals( 2, organisationUnitService.getOrganisationUnitByName( "Western Area" ).getCoordinatesAsList().size() );
-        
+
         assertEquals( 76, organisationUnitService.getOrganisationUnitByName( "Bo" ).getCoordinatesAsList().get( 0 ).getNumberOfCoordinates() );
         assertEquals( 7, organisationUnitService.getOrganisationUnitByName( "Pujehun" ).getCoordinatesAsList().get( 0 ).getNumberOfCoordinates() );
         assertEquals( 7, organisationUnitService.getOrganisationUnitByName( "Pujehun" ).getCoordinatesAsList().get( 1 ).getNumberOfCoordinates() );
         assertEquals( 159, organisationUnitService.getOrganisationUnitByName( "Pujehun" ).getCoordinatesAsList().get( 2 ).getNumberOfCoordinates() );
+        assertEquals( 189, organisationUnitService.getOrganisationUnitByName( "Bonthe" ).getCoordinatesAsList().get( 1 ).getNumberOfCoordinates() );
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/reader/XMLReaderTest.java'
--- dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/reader/XMLReaderTest.java	2010-02-19 18:38:02 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/reader/XMLReaderTest.java	2010-07-15 12:10:45 +0000
@@ -27,6 +27,7 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
+import java.io.ByteArrayInputStream;
 import javax.xml.stream.XMLStreamException;
 import static junit.framework.Assert.assertEquals;
 
@@ -67,6 +68,8 @@
   private URL relaxNg;
   private String[] specialChars = {"&", "<", ">", "\"", "'"};
 
+
+  private static int BIGTEXTSIZE = 1987; // fails when set to 1988
   // -------------------------------------------------------------------------
   // Fixture
   // -------------------------------------------------------------------------
@@ -348,5 +351,31 @@
           er.next();
         }
     }
+
+  @Test
+  public void testBigTextRead() {
+
+    char[] bigText = new char[BIGTEXTSIZE];
+    for (int i=0; i<BIGTEXTSIZE; ++i) {
+        bigText[i]='A';
+    }
+
+    //ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlBuf);
+
+    StringBuilder xml = new StringBuilder(BIGTEXTSIZE*2+40);
+    xml.append( "<root>");
+    xml.append( "<node>"); xml.append(bigText); xml.append( "</node>");
+    xml.append( "<node>"); xml.append(bigText); xml.append( "</node>");
+    xml.append( "</root>");
+
+    ByteArrayInputStream xmlStream = new ByteArrayInputStream(xml.toString().getBytes());
+    XMLReader reader = XMLFactory.getXMLReader(xmlStream);
+
+    reader.moveToStartElement( "node");
+    assertEquals(BIGTEXTSIZE, reader.getElementValue().length());
+    reader.moveToStartElement( "node");
+    assertEquals(BIGTEXTSIZE, reader.getElementValue().length());
+  }
+
 }