|
| 1 | +package edu.caltech.ipac.visualize.plot; |
| 2 | + |
| 3 | +import edu.caltech.ipac.firefly.util.FileLoader; |
| 4 | +import nom.tam.fits.*; |
| 5 | +import nom.tam.util.BufferedDataOutputStream; |
| 6 | +import nom.tam.util.Cursor; |
| 7 | +import org.junit.After; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.Before; |
| 11 | + |
| 12 | +import java.io.*; |
| 13 | + |
| 14 | +/** |
| 15 | + * Created by zhang on 10/10/16. |
| 16 | + * This test class is to test the Crop class. The sample file used for testing is f3.fits. It is used to create a output file, |
| 17 | + * out_f3.fits. Both f3.fits and out_f3.fits are stored in the firefly_test_data/edu/caltech/ipac/visualize/plot/. The end to end test is |
| 18 | + * load the f3.fits and creates a cropped file in the fly. The newly created outFits file is compared with out_f3.fits. If the |
| 19 | + * assertion is failed, it means the bugs are introduced after this class is written. |
| 20 | + * |
| 21 | + * It is difficult to do a unit test for this kind cases. We the end to end test is used instead. |
| 22 | + */ |
| 23 | +public class CropTest { |
| 24 | + |
| 25 | + |
| 26 | + private static String fileName = "f3.fits"; |
| 27 | + private Fits inFits=null; |
| 28 | + private String resultFitsName="out_f3.fits"; |
| 29 | + private Fits expectedFits =null; |
| 30 | + private float delta =0.1f-10; |
| 31 | + |
| 32 | + @Before |
| 33 | + /** |
| 34 | + * An one dimensional array is created and it is used to run the unit test for Histogram's public methods |
| 35 | + */ |
| 36 | + public void setUp() throws FitsException, ClassNotFoundException, IOException { |
| 37 | + |
| 38 | + inFits = FileLoader.loadFits(CropTest.class, fileName); |
| 39 | + |
| 40 | + //This file stored the results run when this class is written. All future results are compared with it. |
| 41 | + expectedFits = FileLoader.loadFits(CropTest.class,resultFitsName); |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + } |
| 46 | + @After |
| 47 | + /** |
| 48 | + * Release the memories |
| 49 | + */ |
| 50 | + public void tearDown() { |
| 51 | + inFits=null; |
| 52 | + expectedFits=null; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Validate the header card one by one |
| 57 | + * @param expectedFits |
| 58 | + * @param calculatedFits |
| 59 | + * @throws FitsException |
| 60 | + */ |
| 61 | + private void validHeader (Fits expectedFits, Fits calculatedFits) throws FitsException { |
| 62 | + BasicHDU[] expectedHDUs = expectedFits.read(); |
| 63 | + |
| 64 | + BasicHDU[] outHDUs =calculatedFits.read(); |
| 65 | + for (int i=0; i<expectedHDUs.length; i++){ |
| 66 | + Header expectedHeader = expectedHDUs[i].getHeader(); |
| 67 | + Header calculatedHeader = outHDUs[i].getHeader(); |
| 68 | + Assert.assertEquals(expectedHeader.getNumberOfCards(), calculatedHeader.getNumberOfCards()); |
| 69 | + Cursor expectedIter = expectedHeader.iterator(); |
| 70 | + Cursor calculatedIter = calculatedHeader.iterator(); |
| 71 | + while(expectedIter.hasNext() && calculatedIter.hasNext()){ |
| 72 | + HeaderCard expectedCard = (HeaderCard) expectedIter.next(); |
| 73 | + HeaderCard calculatedCard = (HeaderCard) calculatedIter.next(); |
| 74 | + Assert.assertEquals( expectedCard.getKey(), calculatedCard.getKey()); |
| 75 | + Assert.assertEquals( expectedCard.getValue(), calculatedCard.getValue()); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Validate the data stored in the FITS file |
| 82 | + * @param expectedFits |
| 83 | + * @param calculatedFits |
| 84 | + * @throws FitsException |
| 85 | + */ |
| 86 | + private void validateData(Fits expectedFits, Fits calculatedFits) throws FitsException { |
| 87 | + FitsRead[] fitsReads = FitsRead.createFitsReadArray(calculatedFits); |
| 88 | + FitsRead[] expectedFitsRead = FitsRead.createFitsReadArray(expectedFits); |
| 89 | + for (int i=0; i<fitsReads.length; i++){ |
| 90 | + Assert.assertArrayEquals(fitsReads[i].getDataFloat(), expectedFitsRead[i].getDataFloat(), delta); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + @Test |
| 95 | + /** |
| 96 | + * We use the result wen this class written as a reference. The output is saved to a file, out_f3.fits. The newly calculated |
| 97 | + * data is compared with the data in out_f3.fits. |
| 98 | + */ |
| 99 | + public void endToEndTest() throws FitsException { |
| 100 | + |
| 101 | + int min_x = 47, min_y = 50, max_x = 349, max_y = 435; |
| 102 | + |
| 103 | + Fits outFits = Crop.do_crop(inFits, min_x, min_y, max_x, max_y); |
| 104 | + |
| 105 | + validHeader(expectedFits,outFits); |
| 106 | + |
| 107 | + validateData(expectedFits,outFits); |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + /** |
| 113 | + * This main program does not need to re-run. It ran only once to create the json file. |
| 114 | + * Use main to generate the Histogram output and store in json format. This is end to end test. The json file only needs |
| 115 | + * to generated once. It will be used for all future testing. It ensures that the future changes will not affect the |
| 116 | + * results. If the assertion fails, it means the changes introduce the bugs. |
| 117 | + * |
| 118 | + * @param args |
| 119 | + * @throws Exception |
| 120 | + */ |
| 121 | + public static void main(String[] args) throws Exception { |
| 122 | + |
| 123 | + String dataPath =FileLoader.getDataPath(CropTest.class); |
| 124 | + Fits inFits = FileLoader.loadFits(CropTest.class, fileName); |
| 125 | + String outFitsName =dataPath+ "out_"+fileName.substring(0, fileName.length()-5 )+".fits"; |
| 126 | + |
| 127 | + int min_x = 47, min_y = 50, max_x = 349, max_y = 435; |
| 128 | + |
| 129 | + Fits outFits = Crop.do_crop(inFits, min_x, min_y, max_x, max_y); |
| 130 | + |
| 131 | + FileOutputStream fo = new java.io.FileOutputStream(outFitsName); |
| 132 | + BufferedDataOutputStream o = new BufferedDataOutputStream(fo); |
| 133 | + outFits.write(o); |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments