=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java' --- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java 2009-09-14 13:52:08 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java 2009-12-19 15:20:41 +0000 @@ -337,7 +337,71 @@ } } } - + + /** + * Test for zip stream signature. + * + * @param instream the BufferedInputStream to test. + */ + public static boolean isZip(BufferedInputStream instream) + { + /* + Signature of zip stream from http://www.pkware.com/documents/casestudies/APPNOTE.TXT + Local file header: + local file header signature 4 bytes (0x04034b50) + */ + instream.mark(4); + byte[] b = new byte[4]; + byte[] zipSig = new byte[4]; + zipSig[0] = 0x50; + zipSig[1] = 0x4b; + zipSig[2] = 0x03; + zipSig[3] = 0x04; + + try { + instream.read(b, 0, 4); + } catch (Exception ex) { + throw new RuntimeException( "Couldn't read header from stream ", ex ); + } + try { + instream.reset(); + } catch (Exception ex) { + throw new RuntimeException( "Couldn't reset stream ", ex ); + } + return Arrays.equals(b, zipSig) ? true : false; + } + + /** + * Test for Gzip stream signature. + * + * @param instream the BufferedInputStream to test. + */ + public static boolean isGZip(BufferedInputStream instream) + { + /* + Signature of gzip stream from RFC 1952: + ID1 (IDentification 1) + ID2 (IDentification 2) + These have the fixed values ID1 = 31 (0x1f, \037), ID2 = 139 + (0x8b, \213), to identify the file as being in gzip format. + */ + instream.mark(2); + byte[] b = new byte[2]; + + try { + instream.read(b, 0, 2); + } catch (Exception ex) { + throw new RuntimeException("Couldn't read header from stream ", ex); + } + try { + instream.reset(); + } catch (Exception ex) { + throw new RuntimeException("Couldn't reset stream ", ex); + } + + return (b[0] == 31 && b[1] == -117) ? true : false; + } + /** * Reads the next ZIP file entry from the ZipInputStream and positions the * stream at the beginning of the entry data. === added file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/StreamUtilsTest.java' --- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/StreamUtilsTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/StreamUtilsTest.java 2009-12-19 15:20:41 +0000 @@ -0,0 +1,83 @@ +package org.hisp.dhis.system.util; + +/* + * Copyright (c) 2004-2005, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of the nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import junit.framework.TestCase; + +/** + * + * @author bobj + * @version created 19-Dec-2009 + */ +public class StreamUtilsTest + extends TestCase { + + public static BufferedInputStream zipStream; + + public static BufferedInputStream gzipStream; + + @Override + public void setUp() + { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + + zipStream = new BufferedInputStream(classLoader.getResourceAsStream( "dxfA.zip" )); + + gzipStream = new BufferedInputStream(classLoader.getResourceAsStream( "Export.xml.gz" )); + } + + @Override + public void tearDown() throws Exception + { + zipStream.close(); + + gzipStream.close(); + } + + public void testZip() + { + assertTrue(StreamUtils.isZip(zipStream)); + + assertFalse(StreamUtils.isGZip(zipStream)); + } + + public void testGZip() + { + assertTrue(StreamUtils.isGZip(gzipStream)); + + assertFalse(StreamUtils.isZip(gzipStream)); + } +} + + === added file 'dhis-2/dhis-support/dhis-support-system/src/test/resources/Export.xml' --- dhis-2/dhis-support/dhis-support-system/src/test/resources/Export.xml 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/test/resources/Export.xml 2009-12-19 15:20:41 +0000 @@ -0,0 +1,627 @@ + + + + + 65 + categoryOptionA + + + 66 + categoryOptionB + + + 67 + categoryOptionC + + + 68 + categoryOptionD + + + + + 65 + categoryA + + + 66 + categoryB + + + + + 65 + categoryComboA + + + 66 + categoryComboB + + + + + 65 + + 65 + categoryComboA + + + + 65 + categoryOptionA + + + 67 + categoryOptionC + + + + + 66 + + 65 + categoryComboA + + + + 66 + categoryOptionB + + + 68 + categoryOptionD + + + + + 67 + + 66 + categoryComboB + + + + 65 + categoryOptionA + + + + + 68 + + 66 + categoryComboB + + + + 66 + categoryOptionB + + + + + + + 65 + 65 + + + 65 + 66 + + + 66 + 68 + + + 66 + 67 + + + + + 65 + 66 + + + 65 + 65 + + + 66 + 65 + + + + + 65 + C3C2E28D-9686-4634-93FD-BE3133935ECA + DataElementA + AlternativeNameA + ShortNameA + CodeA + DescriptionA + true + int + sum + 65 + + + 66 + C3C2E28D-9686-4634-93FD-BE3133935ECB + DataElementB + AlternativeNameB + ShortNameB + CodeB + DescriptionB + true + int + sum + 65 + + + 67 + C3C2E28D-9686-4634-93FD-BE3133935ECC + DataElementC + AlternativeNameC + ShortNameC + CodeC + DescriptionC + true + int + sum + 65 + + + + + 65 + C3C2E28D-9686-4634-93FD-BE3133935ECA + DataElementGroupA + + + 66 + C3C2E28D-9686-4634-93FD-BE3133935ECB + DataElementGroupB + + + 67 + C3C2E28D-9686-4634-93FD-BE3133935ECC + DataElementGroupC + + + + + 65 + 67 + + + 65 + 66 + + + 65 + 65 + + + 66 + 67 + + + 66 + 66 + + + 66 + 65 + + + 67 + 67 + + + 67 + 66 + + + 67 + 65 + + + + + 65 + IndicatorTypeA + 100 + + + 66 + IndicatorTypeB + 100 + + + 67 + IndicatorTypeC + 100 + + + + + 65 + C3C2E28D-9686-4634-93FD-BE3133935ECA + IndicatorA + AlternativeNameA + ShortNameA + CodeA + DescriptionA + false + 65 + Numerator + NumeratorDescription + sum + Denominator + DenominatorDescription + sum + + + 66 + C3C2E28D-9686-4634-93FD-BE3133935ECB + IndicatorB + AlternativeNameB + ShortNameB + CodeB + DescriptionB + false + 65 + Numerator + NumeratorDescription + sum + Denominator + DenominatorDescription + sum + + + 67 + C3C2E28D-9686-4634-93FD-BE3133935ECC + IndicatorC + AlternativeNameC + ShortNameC + CodeC + DescriptionC + false + 65 + Numerator + NumeratorDescription + sum + Denominator + DenominatorDescription + sum + + + + + 65 + C3C2E28D-9686-4634-93FD-BE3133935ECA + IndicatorGroupA + + + 66 + C3C2E28D-9686-4634-93FD-BE3133935ECB + IndicatorGroupB + + + 67 + C3C2E28D-9686-4634-93FD-BE3133935ECC + IndicatorGroupC + + + + + 65 + 66 + + + 65 + 65 + + + 65 + 67 + + + 66 + 66 + + + 66 + 65 + + + 66 + 67 + + + 67 + 66 + + + 67 + 65 + + + 67 + 67 + + + + + 65 + DataSetA + Weekly + + + 66 + DataSetB + Weekly + + + 67 + DataSetC + Weekly + + + + + 65 + 67 + + + 65 + 66 + + + 65 + 65 + + + 66 + 67 + + + 66 + 66 + + + 66 + 65 + + + 67 + 67 + + + 67 + 66 + + + 67 + 65 + + + + + 65 + C3C2E28D-9686-4634-93FD-BE3133935ECA + OrganisationUnitA + ShortNameA + CodeA + 1970-01-01 + 1970-01-01 + true + CommentA + GeoCode + + + 66 + C3C2E28D-9686-4634-93FD-BE3133935ECB + OrganisationUnitB + ShortNameB + CodeB + 1970-01-01 + 1970-01-01 + true + CommentB + GeoCode + + + 67 + C3C2E28D-9686-4634-93FD-BE3133935ECC + OrganisationUnitC + ShortNameC + CodeC + 1970-01-01 + 1970-01-01 + true + CommentC + GeoCode + + + + + 65 + 66 + + + 66 + 67 + + + + + 65 + C3C2E28D-9686-4634-93FD-BE3133935ECA + OrganisationUnitGroupA + + + 66 + C3C2E28D-9686-4634-93FD-BE3133935ECB + OrganisationUnitGroupB + + + 67 + C3C2E28D-9686-4634-93FD-BE3133935ECC + OrganisationUnitGroupC + + + + + 65 + 66 + + + 65 + 67 + + + 65 + 65 + + + 66 + 66 + + + 66 + 67 + + + 66 + 65 + + + 67 + 66 + + + 67 + 67 + + + 67 + 65 + + + + + 65 + OrganisationUnitGroupSetA + DescriptionA + true + true + + + 66 + OrganisationUnitGroupSetB + DescriptionB + true + true + + + 67 + OrganisationUnitGroupSetC + DescriptionC + true + true + + + + + 65 + 67 + + + 65 + 66 + + + 65 + 65 + + + 66 + 67 + + + 66 + 66 + + + 66 + 65 + + + 67 + 67 + + + 67 + 66 + + + 67 + 65 + + + + + ValidationRuleA + DescriptionA + absolute + equal_to + [A.65]+[B.65] + DescriptionA + [A.65]+[B.65] + DescriptionA + + + ValidationRuleB + DescriptionB + absolute + equal_to + [A.65]+[B.65] + DescriptionA + [A.65]+[B.65] + DescriptionA + + + ValidationRuleC + DescriptionC + absolute + equal_to + [A.65]+[B.65] + DescriptionA + [A.65]+[B.65] + DescriptionA + + + + + 65 + Weekly + 1999-12-27 + 2000-01-02 + + + 66 + Weekly + 2000-01-03 + 2000-01-09 + + + 67 + Weekly + 2000-01-10 + 2000-01-16 + + + === added file 'dhis-2/dhis-support/dhis-support-system/src/test/resources/Export.xml.gz' Binary files dhis-2/dhis-support/dhis-support-system/src/test/resources/Export.xml.gz 1970-01-01 00:00:00 +0000 and dhis-2/dhis-support/dhis-support-system/src/test/resources/Export.xml.gz 2009-12-19 15:20:41 +0000 differ === added file 'dhis-2/dhis-support/dhis-support-system/src/test/resources/dxfA.zip' Binary files dhis-2/dhis-support/dhis-support-system/src/test/resources/dxfA.zip 1970-01-01 00:00:00 +0000 and dhis-2/dhis-support/dhis-support-system/src/test/resources/dxfA.zip 2009-12-19 15:20:41 +0000 differ