diff --git a/sed-builder/src/test/java/cfa/vo/sed/science/stacker/AbstractSAMPIntegrationTest.java b/sed-builder/src/test/java/cfa/vo/sed/science/stacker/AbstractSAMPIntegrationTest.java new file mode 100644 index 00000000..f24c4097 --- /dev/null +++ b/sed-builder/src/test/java/cfa/vo/sed/science/stacker/AbstractSAMPIntegrationTest.java @@ -0,0 +1,97 @@ +package cfa.vo.sed.science.stacker; + +import java.util.logging.Logger; + +import org.junit.Assert; +import cfa.vo.interop.SAMPController; +import cfa.vo.interop.SAMPFactory; +import cfa.vo.iris.interop.SedSAMPController; + +/** + * Abstract class for integration testing of SAMP integration. Tests will fail if they are + * unable to connect to the SAMP hub. + * + */ +public abstract class AbstractSAMPIntegrationTest { + + private static final Logger logger = Logger.getLogger(AbstractSAMPIntegrationTest.class.getName()); + + private static final int SAMP_CONN_RETRIES = 3; + protected static final double EPSILON = 0.000001; + + protected double[] x1; + protected double[] y1; + protected double[] yerr1; + protected double[] x2; + protected double[] y2; + protected double[] yerr2; + protected double[] x3; + protected double[] y3; + protected double[] yerr3; + + protected SegmentPayload segment1; + protected SegmentPayload segment2; + protected SegmentPayload segment3; + + protected SAMPController controller; + + + protected void initialize() throws Exception { + x1 = new double[] { 5, 1, 10, 15, 50, 100 }; + y1 = new double[] { 0.5, 0.1, 1.0, 1.5, 5.0, 10.0 }; + yerr1 = new double[] { 0.05, 0.01, 0.1, 0.15, 0.5, 1.0 }; + + x2 = new double[] { 2, 4, 5, 8, 10 }; + y2 = new double[] { 1, 2, 3, 4, 5 }; + yerr2 = new double[] { 0.1, 0.2, 0.3, 0.4, 0.5 }; + + x3 = new double[] { 0.5, 3.0, 1.5, 5.0, 10.5, 21.0 }; + y3 = new double[] { 5.0, 7.0, 15.0, 4.5, 13.5, 10.5 }; + yerr3 = new double[] { 0.5, 0.7, 1.5, 0.45, 1.35, 1.05 }; + + segment1 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); + segment1.setX(x1); + segment1.setY(y1); + segment1.setYerr(yerr1); + segment1.setZ(0.1); + segment1.setId("Sed1"); + + segment2 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); + segment2.setX(x2); + segment2.setY(y2); + segment2.setYerr(yerr2); + segment2.setZ(0.2); + segment2.setId("Sed2"); + + segment3 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); + segment3.setX(x3); + segment3.setY(y3); + segment3.setYerr(yerr3); + segment3.setZ(0.3); + segment3.setId("Sed3"); + + // Start the SAMP controller + controller = new SedSAMPController("SEDStacker", "SEDStacker", this.getClass().getResource("/tools_tiny.png") + .toString()); + controller.setAutoRunHub(false); + controller.start(false); + + // Wait for start + Thread.sleep(2000); + + int count = 0; + while (!controller.isConnected()) { + if (++count > SAMP_CONN_RETRIES) { + String msg = "Failed to connect to SAMP, failing Unit tests"; + logger.info(msg); + Assert.fail(msg); + } + logger.info("waiting connection"); + Thread.sleep(1000); + } + } + + protected void terminate() { + controller.stop(); + } +} diff --git a/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerNormalizerTest.java b/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerNormalizerIT.java similarity index 65% rename from sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerNormalizerTest.java rename to sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerNormalizerIT.java index c9f298e6..e7e7c425 100644 --- a/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerNormalizerTest.java +++ b/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerNormalizerIT.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Smithsonian Astrophysical Observatory - *

+ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

+ * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,12 +21,9 @@ */ package cfa.vo.sed.science.stacker; -import cfa.vo.interop.SAMPController; import cfa.vo.interop.SAMPFactory; import cfa.vo.interop.SAMPMessage; -import cfa.vo.iris.interop.SedSAMPController; import cfa.vo.iris.sed.ExtSed; - import static cfa.vo.sed.science.stacker.SedStackerAttachments.NORM_CONSTANT; import cfa.vo.iris.utils.Default; @@ -36,7 +33,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.logging.Logger; import org.astrogrid.samp.Response; import org.junit.After; @@ -47,88 +43,28 @@ import static org.junit.Assert.*; /** - * + * * @author jbudynk */ -public class SedStackerNormalizerTest { - - private static final Logger logger = Logger.getLogger(SedStackerNormalizerTest.class.getName()); - - double[] x1; - double[] y1; - double[] yerr1; - double[] x2; - double[] y2; - double[] yerr2; - double[] x3; - double[] y3; - double[] yerr3; - - SegmentPayload segment1; - SegmentPayload segment2; - SegmentPayload segment3; +public class SedStackerNormalizerIT extends AbstractSAMPIntegrationTest { SedStackerNormalizePayload payload; - private SAMPController controller; - @Before - public void setUp() { - x1 = new double[]{1, 5, 10, 15, 50, 100}; - y1 = new double[]{0.1, 0.5, 1.0, 1.5, 5.0, 10.0}; - yerr1 = new double[]{0.01, 0.05, 0.1, 0.15, 0.5, 1.0}; - - x2 = new double[]{2, 4, 5, 8, 10}; - y2 = new double[]{1, 2, 3, 4, 5}; - yerr2 = new double[]{0.1, 0.2, 0.3, 0.4, 0.5}; - - x3 = new double[]{0.5, 1.5, 3.0, 5.0, 10.5, 21.0}; - y3 = new double[]{5.0, 15.0, 7.0, 4.5, 13.5, 10.5}; - yerr3 = new double[]{0.5, 1.5, 0.7, 0.45, 1.35, 1.05}; - - segment1 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); - segment1.setX(x1); - segment1.setY(y1); - segment1.setYerr(yerr1); - segment1.setZ(0.1); - segment1.setId("Sed1"); - - segment2 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); - segment2.setX(x2); - segment2.setY(y2); - segment2.setYerr(yerr2); - segment2.setZ(0.2); - segment2.setId("Sed2"); - - segment3 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); - segment3.setX(x3); - segment3.setY(y3); - segment3.setYerr(yerr3); - segment3.setZ(0.3); - segment3.setId("Sed3"); + public void before() throws Exception { + super.initialize(); } - + @After public void tearDown() { + super.terminate(); } @Ignore("need sherpa-samp running") @Test public void testNormalize() throws Exception { - - // Start the SAMP controller - controller = new SedSAMPController("SEDStacker", "SEDStacker", this.getClass().getResource("/tools_tiny.png").toString()); - controller.setAutoRunHub(false); - controller.start(false); - - Thread.sleep(2000); - - while (!controller.isConnected()) { - logger.info("waiting connection"); - Thread.sleep(1000); - } - - payload = (SedStackerNormalizePayload) SAMPFactory.get(SedStackerNormalizePayload.class); + payload = (SedStackerNormalizePayload) SAMPFactory + .get(SedStackerNormalizePayload.class); // Setup the normalization payload payload.addSegment(segment1); @@ -143,17 +79,20 @@ public void testNormalize() throws Exception { payload.setIntegrate(Boolean.TRUE); // Setup and send SAMP message - SAMPMessage message = SAMPFactory.createMessage("stack.normalize", payload, SedStackerNormalizePayload.class); + SAMPMessage message = SAMPFactory.createMessage("stack.normalize", + payload, SedStackerNormalizePayload.class); SherpaClient client = new SherpaClient(controller); - Response rspns = controller.callAndWait(client.findSherpa(), message.get(), 10); + Response rspns = controller.callAndWait(client.findSherpa(), + message.get(), 10); if (client.isException(rspns)) { Exception ex = client.getException(rspns); throw ex; } - SedStackerNormalizePayload response = (SedStackerNormalizePayload) SAMPFactory.get(rspns.getResult(), SedStackerNormalizePayload.class); + SedStackerNormalizePayload response = (SedStackerNormalizePayload) SAMPFactory + .get(rspns.getResult(), SedStackerNormalizePayload.class); // get response values double[] resy1 = response.getSegments().get(0).getY(); @@ -180,18 +119,6 @@ public void testNormalize() throws Exception { @Ignore("need sherpa-samp running") @Test public void testNormalizer() throws Exception { - // Start the SAMP controller - controller = new SedSAMPController("SEDStacker", "SEDStacker", this.getClass().getResource("/tools_tiny.png").toString()); - controller.setAutoRunHub(false); - controller.start(false); - - Thread.sleep(2000); - - while (!controller.isConnected()) { - logger.info("waiting connection"); - Thread.sleep(1000); - } - ExtSed sed1 = new ExtSed("Sed1"); ExtSed sed2 = new ExtSed("Sed2"); ExtSed sed3 = new ExtSed("Sed3"); @@ -220,7 +147,8 @@ public void testNormalizer() throws Exception { Segment seg3 = new Segment(); seg3.setFluxAxisValues(y3); - //convert the values in x3 to nm so I can test the unit conversions too. + // convert the values in x3 to nm so I can test the unit conversions + // too. for (int k = 0; k < x3.length; k++) { x3[k] = x3[k] * 0.1; } @@ -250,8 +178,8 @@ public void testNormalizer() throws Exception { SedStackerNormalizer normalizer = new SedStackerNormalizer(controller, Default.getInstance().getUnitsManager()); normalizer.normalize(stack, config); - List xs = new ArrayList(); - List ys = new ArrayList(); + List xs = new ArrayList<>(); + List ys = new ArrayList<>(); xs.add(x1); xs.add(x2); xs.add(x3); @@ -265,48 +193,42 @@ public void testNormalizer() throws Exception { double[] x = xs.get(j); double[] y = ys.get(j); - for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0).getLength(); i++) { - double xOrigValue = origSed.getSegment(0).getSpectralAxisValues()[i]; + for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0) + .getLength(); i++) { + double xOrigValue = origSed.getSegment(0) + .getSpectralAxisValues()[i]; double yOrigValue = origSed.getSegment(0).getFluxAxisValues()[i]; - assertEquals(xOrigValue, x[i]); - assertEquals(yOrigValue, y[i]); + assertEquals(xOrigValue, x[i], .0001); + assertEquals(yOrigValue, y[i], .0001); } } for (int j = 0; j < stack.getSed(0).getSegment(0).getLength(); j++) - assertEquals(0.49234923 * y1[j], stack.getSed(0).getSegment(0).getFluxAxisValues()[j], 0.00001 * 0.49234923 * y1[j]); + assertEquals(0.49234923 * y1[j], stack.getSed(0).getSegment(0) + .getFluxAxisValues()[j], 0.00001 * 0.49234923 * y1[j]); for (int j = 0; j < stack.getSed(1).getSegment(0).getLength(); j++) - assertEquals(9.846 * y2[j], stack.getSed(1).getSegment(0).getFluxAxisValues()[j], 0.00001); + assertEquals(9.846 * y2[j], stack.getSed(1).getSegment(0) + .getFluxAxisValues()[j], 0.00001); - assertEquals(1.1529274, Double.valueOf(stack.getSed(2).getAttachment(NORM_CONSTANT).toString()), 0.00001); - - controller.stop(); + assertEquals( + 1.1529274, + Double.valueOf(stack.getSed(2).getAttachment(NORM_CONSTANT) + .toString()), 0.00001); } @Ignore("need sherpa-samp running") @Test public void testNormalizerOutsideRange() throws Exception { - // Start the SAMP controller - controller = new SedSAMPController("SEDStacker", "SEDStacker", this.getClass().getResource("/tools_tiny.png").toString()); - controller.setAutoRunHub(false); - controller.start(false); - - Thread.sleep(2000); - - while (!controller.isConnected()) { - Thread.sleep(1000); - } - ExtSed sed1 = new ExtSed("Sed1"); ExtSed sed2 = new ExtSed("Sed2"); ExtSed sed3 = new ExtSed("Sed3"); Segment seg1 = new Segment(); -// for (int k=0; k xs = new ArrayList(); - List ys = new ArrayList(); + List xs = new ArrayList<>(); + List ys = new ArrayList<>(); xs.add(x1); xs.add(x2); xs.add(x3); @@ -370,22 +293,26 @@ public void testNormalizerOutsideRange() throws Exception { double[] x = xs.get(j); double[] y = ys.get(j); - for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0).getLength(); i++) { - double xOrigValue = origSed.getSegment(0).getSpectralAxisValues()[i]; + for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0) + .getLength(); i++) { + double xOrigValue = origSed.getSegment(0) + .getSpectralAxisValues()[i]; double yOrigValue = origSed.getSegment(0).getFluxAxisValues()[i]; - assertEquals(xOrigValue, x[i]); - assertEquals(yOrigValue, y[i]); + assertEquals(xOrigValue, x[i], .0001); + assertEquals(yOrigValue, y[i], .0001); } } for (int j = 0; j < stack.getSed(0).getSegment(0).getLength(); j++) - assertEquals(y1[j], stack.getSed(0).getSegment(0).getFluxAxisValues()[j], 0.00001 * y1[j]); + assertEquals(y1[j], stack.getSed(0).getSegment(0) + .getFluxAxisValues()[j], 0.00001 * y1[j]); for (int j = 0; j < stack.getSed(1).getSegment(0).getLength(); j++) - assertEquals(0.0625 * y2[j], stack.getSed(1).getSegment(0).getFluxAxisValues()[j], 0.00001); + assertEquals(0.0625 * y2[j], stack.getSed(1).getSegment(0) + .getFluxAxisValues()[j], 0.00001); - assertEquals(0.035714285714, Double.valueOf(stack.getSed(2).getAttachment(NORM_CONSTANT).toString()), 0.00001); - - controller.stop(); + assertEquals( + 0.035714285714, + Double.valueOf(stack.getSed(2).getAttachment(NORM_CONSTANT) + .toString()), 0.00001); } - } diff --git a/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerRedshifterTest.java b/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerRedshifterIT.java similarity index 63% rename from sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerRedshifterTest.java rename to sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerRedshifterIT.java index d61d70ff..59ec4bee 100644 --- a/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerRedshifterTest.java +++ b/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerRedshifterIT.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Smithsonian Astrophysical Observatory - *

+ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

+ * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,12 +21,9 @@ */ package cfa.vo.sed.science.stacker; -import cfa.vo.interop.SAMPController; import cfa.vo.interop.SAMPFactory; import cfa.vo.interop.SAMPMessage; -import cfa.vo.iris.interop.SedSAMPController; import cfa.vo.iris.sed.ExtSed; - import static cfa.vo.sed.science.stacker.SedStackerAttachments.REDSHIFT; import cfa.vo.iris.utils.Default; @@ -36,7 +33,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.logging.Logger; import org.astrogrid.samp.Response; import org.junit.After; @@ -47,23 +43,11 @@ import static org.junit.Assert.*; /** - * + * * @author jbudynk */ -public class SedStackerRedshifterTest { +public class SedStackerRedshifterIT extends AbstractSAMPIntegrationTest { - private static final Logger logger = Logger.getLogger(SedStackerRedshifterTest.class.getName()); - - double[] x1; - double[] y1; - double[] yerr1; - double[] x2; - double[] y2; - double[] yerr2; - double[] x3; - double[] y3; - double[] yerr3; - double[] controlY1; double[] controlX1; double[] controlYerr1; @@ -71,75 +55,35 @@ public class SedStackerRedshifterTest { double[] controlX1WithExtraSegment; double[] controlY1WithExtraSegment; - SegmentPayload segment1; - SegmentPayload segment2; - SegmentPayload segment3; - SedStackerRedshiftPayload payload; - - private SAMPController controller; private SedStackerRedshifter redshifter; @Before - public void setUp() { - x1 = new double[]{1, 5, 10, 15, 50, 100}; - y1 = new double[]{0.1, 0.5, 1.0, 1.5, 5.0, 10.0}; - yerr1 = new double[]{0.01, 0.05, 0.1, 0.15, 0.5, 1.0}; - - x2 = new double[]{2, 4, 5, 8, 10}; - y2 = new double[]{1, 2, 3, 4, 5}; - yerr2 = new double[]{0.1, 0.2, 0.3, 0.4, 0.5}; - - x3 = new double[]{0.5, 1.5, 3.0, 5.0, 10.5, 21.0}; - y3 = new double[]{5.0, 15.0, 7.0, 4.5, 13.5, 10.5}; - yerr3 = new double[]{0.5, 1.5, 0.7, 0.45, 1.35, 1.05}; - - segment1 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); - segment1.setX(x1); - segment1.setY(y1); - segment1.setYerr(yerr1); - segment1.setZ(0.1); - segment1.setId("Sed1"); - - segment2 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); - segment2.setX(x2); - segment2.setY(y2); - segment2.setYerr(yerr2); - segment2.setZ(0.2); - segment2.setId("Sed2"); - - segment3 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); - segment3.setX(x3); - segment3.setY(y3); - segment3.setYerr(yerr3); - segment3.setZ(0.3); - segment3.setId("Sed3"); - - controlY1 = new double[]{0.1, 0.5, 1.0, 1.5, 5.0, 10.0}; - controlX1 = new double[]{0.90909091, 4.54545455, 9.09090909, 13.63636364, 45.45454545, 90.90909091}; - controlYerr1 = new double[]{0.01, 0.05, 0.1, 0.15, 0.5, 1.0}; - controlX2 = new double[]{1.66666667, 3.33333333, 4.16666667, 6.66666667, 8.33333333}; - - controlX1WithExtraSegment = new double[]{0.90909091, 4.54545455, 9.09090909, 13.63636364, 18.181818182, 45.45454545, 90.90909091}; - controlY1WithExtraSegment = new double[]{0.1, 0.5, 1.0, 1.5, 2.0, 5.0, 10.0}; + public void before() throws Exception { + super.initialize(); + controlY1 = new double[] { 0.1, 0.5, 1.0, 1.5, 5.0, 10.0 }; + controlX1 = new double[] { 0.90909091, 4.54545455, 9.09090909, + 13.63636364, 45.45454545, 90.90909091 }; + controlYerr1 = new double[] { 0.01, 0.05, 0.1, 0.15, 0.5, 1.0 }; + controlX2 = new double[] { 1.66666667, 3.33333333, 4.16666667, + 6.66666667, 8.33333333 }; + + controlX1WithExtraSegment = new double[] { 0.90909091, 4.54545455, + 9.09090909, 13.63636364, 18.181818182, 45.45454545, 90.90909091 }; + controlY1WithExtraSegment = new double[] { 0.1, 0.5, 1.0, 1.5, 2.0, + 5.0, 10.0 }; + } + + @After + public void terminate() { + super.terminate(); } @Ignore("need sherpa-samp running") @Test public void testRedshift() throws Exception { - // Start the SAMP controller - controller = new SedSAMPController("SEDStacker", "SEDStacker", this.getClass().getResource("/tools_tiny.png").toString()); - controller.setAutoRunHub(false); - controller.start(false); - - Thread.sleep(2000); - - while (!controller.isConnected()) { - logger.info("waiting connection"); - Thread.sleep(1000); - } - - payload = (SedStackerRedshiftPayload) SAMPFactory.get(SedStackerRedshiftPayload.class); + payload = (SedStackerRedshiftPayload) SAMPFactory + .get(SedStackerRedshiftPayload.class); // Setup the stack payload payload.addSegment(segment1); @@ -149,17 +93,20 @@ public void testRedshift() throws Exception { payload.setZ0(0.0); // Setup and send SAMP message - SAMPMessage message = SAMPFactory.createMessage("stack.redshift", payload, SedStackerRedshiftPayload.class); + SAMPMessage message = SAMPFactory.createMessage("stack.redshift", + payload, SedStackerRedshiftPayload.class); SherpaClient client = new SherpaClient(controller); - Response rspns = controller.callAndWait(client.findSherpa(), message.get(), 10); + Response rspns = controller.callAndWait(client.findSherpa(), + message.get(), 10); if (client.isException(rspns)) { Exception ex = client.getException(rspns); throw ex; } - SedStackerRedshiftPayload response = (SedStackerRedshiftPayload) SAMPFactory.get(rspns.getResult(), SedStackerRedshiftPayload.class); + SedStackerRedshiftPayload response = (SedStackerRedshiftPayload) SAMPFactory + .get(rspns.getResult(), SedStackerRedshiftPayload.class); // get response values SegmentPayload seg = response.getSegments().get(0); @@ -169,41 +116,22 @@ public void testRedshift() throws Exception { // tests for (int i = 0; i < seg.getY().length; i++) { - assertEquals(controlY1[i], seg.getY()[i], 0.00001); + assertEquals(controlY1[i], seg.getY()[i], EPSILON); } for (int i = 0; i < seg.getY().length; i++) { - assertEquals(controlX1[i], seg.getX()[i], 0.00001); + assertEquals(controlX1[i], seg.getX()[i], EPSILON); } for (int i = 0; i < seg.getY().length; i++) { - assertEquals(controlYerr1[i], seg.getYerr()[i], 0.00001); + assertEquals(controlYerr1[i], seg.getYerr()[i], EPSILON); } for (int i = 0; i < seg2.getX().length; i++) { - assertEquals(controlX2[i], seg2.getX()[i], 0.00001); + assertEquals(controlX2[i], seg2.getX()[i], EPSILON); } - - controller.stop(); - } - - @After - public void tearDown() { } @Ignore("need sherpa-samp running") @Test public void testRedshifter() throws Exception { - - // Start the SAMP controller - controller = new SedSAMPController("SEDStacker", "SEDStacker", this.getClass().getResource("/tools_tiny.png").toString()); - controller.setAutoRunHub(false); - controller.start(false); - - Thread.sleep(2000); - - while (!controller.isConnected()) { - logger.info("waiting connection"); - Thread.sleep(1000); - } - ExtSed sed1 = new ExtSed("Sed1"); ExtSed sed2 = new ExtSed("Sed2"); ExtSed sed3 = new ExtSed("Sed3"); @@ -215,13 +143,14 @@ public void testRedshifter() throws Exception { seg1.setSpectralAxisUnits("Angstrom"); seg1.setDataValues(yerr1, UTYPE.FLUX_STAT_ERROR); sed1.addSegment(seg1); -// Segment seg11 = new Segment(); -// seg11.setFluxAxisValues(new double[] {2.0}); -// seg11.setSpectralAxisValues(new double[] {20}); -// seg11.setFluxAxisUnits("Jy"); -// seg11.setSpectralAxisUnits("nm"); -// seg11.setDataValues(yerr1, UTYPEs.FLUX_STAT_ERROR); -// sed1.addSegment(seg11); + + // Segment seg11 = new Segment(); + // seg11.setFluxAxisValues(new double[] {2.0}); + // seg11.setSpectralAxisValues(new double[] {20}); + // seg11.setFluxAxisUnits("Jy"); + // seg11.setSpectralAxisUnits("nm"); + // seg11.setDataValues(yerr1, UTYPEs.FLUX_STAT_ERROR); + // sed1.addSegment(seg11); sed1.addAttachment(REDSHIFT, 0.1); Segment seg2 = new Segment(); @@ -273,11 +202,13 @@ public void testRedshifter() throws Exception { double[] x = xs.get(j); double[] y = ys.get(j); - for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0).getLength(); i++) { - double xOrigValue = origSed.getSegment(0).getSpectralAxisValues()[i]; + for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0) + .getLength(); i++) { + double xOrigValue = origSed.getSegment(0) + .getSpectralAxisValues()[i]; double yOrigValue = origSed.getSegment(0).getFluxAxisValues()[i]; - assertEquals(xOrigValue, x[i]); - assertEquals(yOrigValue, y[i]); + assertEquals(xOrigValue, x[i], EPSILON); + assertEquals(yOrigValue, y[i], EPSILON); } } @@ -287,35 +218,21 @@ public void testRedshifter() throws Exception { for (int i = 0; i < shiftedSed1.getSegment(0).getLength(); i++) { double xValue = shiftedSed1.getSegment(0).getSpectralAxisValues()[i]; double yValue = shiftedSed1.getSegment(0).getFluxAxisValues()[i]; - assertEquals(controlX1[i], xValue, 0.00001); - assertEquals(controlY1[i], yValue, 0.00001); - assertEquals(controlYerr1[i], yerrValues[i], 0.00001); + assertEquals(controlX1[i], xValue, EPSILON); + assertEquals(controlY1[i], yValue, EPSILON); + assertEquals(controlYerr1[i], yerrValues[i], EPSILON); } ExtSed shiftedSed2 = stack.getSeds().get(1); for (int i = 0; i < shiftedSed2.getSegment(0).getLength(); i++) { double xValue = shiftedSed2.getSegment(0).getSpectralAxisValues()[i]; - assertEquals(xValue, controlX2[i], 0.00001); + assertEquals(xValue, controlX2[i], EPSILON); } - controller.stop(); } @Ignore("need sherpa-samp running") @Test public void testRedshifterNoZ() throws Exception { - - // Start the SAMP controller - controller = new SedSAMPController("SEDStacker", "SEDStacker", this.getClass().getResource("/tools_tiny.png").toString()); - controller.setAutoRunHub(false); - controller.start(false); - - Thread.sleep(2000); - - while (!controller.isConnected()) { - logger.info("waiting connection"); - Thread.sleep(1000); - } - ExtSed sed1 = new ExtSed("Sed1"); ExtSed sed2 = new ExtSed("Sed2"); ExtSed sed3 = new ExtSed("Sed3"); @@ -376,11 +293,13 @@ public void testRedshifterNoZ() throws Exception { ExtSed origSed = stack.getOrigSeds().get(j); double[] x = xs.get(j); double[] y = ys.get(j); - for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0).getLength(); i++) { - double xOrigValue = origSed.getSegment(0).getSpectralAxisValues()[i]; + for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0) + .getLength(); i++) { + double xOrigValue = origSed.getSegment(0) + .getSpectralAxisValues()[i]; double yOrigValue = origSed.getSegment(0).getFluxAxisValues()[i]; - assertEquals(xOrigValue, x[i]); - assertEquals(yOrigValue, y[i]); + assertEquals(xOrigValue, x[i], EPSILON); + assertEquals(yOrigValue, y[i], EPSILON); } } @@ -390,18 +309,17 @@ public void testRedshifterNoZ() throws Exception { for (int i = 0; i < shiftedSed1.getSegment(0).getLength(); i++) { double xValue = shiftedSed1.getSegment(0).getSpectralAxisValues()[i]; double yValue = shiftedSed1.getSegment(0).getFluxAxisValues()[i]; - assertEquals(controlX1[i], xValue, 0.00001); - assertEquals(controlY1[i], yValue, 0.00001); - assertEquals(controlYerr1[i], yerrValues[i], 0.00001); + assertEquals(controlX1[i], xValue, EPSILON); + assertEquals(controlY1[i], yValue, EPSILON); + assertEquals(controlYerr1[i], yerrValues[i], EPSILON); } - // since sed2 doesn't have a redshift, the spectral values shouldn't change. + // since sed2 doesn't have a redshift, the spectral values shouldn't + // change. ExtSed shiftedSed2 = stack.getSeds().get(1); for (int i = 0; i < shiftedSed2.getSegment(0).getLength(); i++) { double xValue = shiftedSed2.getSegment(0).getSpectralAxisValues()[i]; - assertEquals(xValue, x2[i], 0.00001); + assertEquals(xValue, x2[i], EPSILON); } - controller.stop(); } - } diff --git a/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerStackerIT.java b/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerStackerIT.java new file mode 100644 index 00000000..e4ce0454 --- /dev/null +++ b/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerStackerIT.java @@ -0,0 +1,229 @@ +/** + * Copyright (C) 2015 Smithsonian Astrophysical Observatory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package cfa.vo.sed.science.stacker; + +import cfa.vo.interop.SAMPFactory; +import cfa.vo.interop.SAMPMessage; +import cfa.vo.iris.sed.ExtSed; +import cfa.vo.iris.units.UnitsManager; +import cfa.vo.iris.utils.UTYPE; + +import static cfa.vo.sed.science.stacker.SedStackerAttachments.COUNTS; +import cfa.vo.sedlib.Segment; +import cfa.vo.sherpa.SherpaClient; +import java.util.ArrayList; +import java.util.List; +import org.astrogrid.samp.Response; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author jbudynk + */ +public class SedStackerStackerIT extends AbstractSAMPIntegrationTest { + + SedStackerStackPayload payload; + + private SedStackerStacker stacker; + + @Before + public void before() throws Exception { + super.initialize(); + } + + @After + public void tearDown() { + super.terminate(); + } + + @Ignore("need sherpa-samp running") + @Test + public void testStackAvg() throws Exception { + + payload = (SedStackerStackPayload) SAMPFactory.get(SedStackerStackPayload.class); + + // Setup the stack payload + payload.addSegment(segment1); + payload.addSegment(segment2); + payload.addSegment(segment3); + payload.setBinsize(2.0); + payload.setLogBin(false); + payload.setSmooth(false); + payload.setSmoothBinsize(5.0); + payload.setStatistic("avg"); + + // Setup and send SAMP message + SAMPMessage message = SAMPFactory.createMessage("stack.stack", payload, SedStackerStackPayload.class); + + SherpaClient client = new SherpaClient(controller); + + Response rspns = controller.callAndWait(client.findSherpa(), message.get(), 10); + if (client.isException(rspns)) { + Exception ex = client.getException(rspns); + throw ex; + } + + SedStackerStackPayload response = (SedStackerStackPayload) SAMPFactory.get(rspns.getResult(), + SedStackerStackPayload.class); + + // get response values + SegmentPayload seg = response.getSegments().get(0); + double[] controlY = new double[] { 5.0, 5.36666667, 4.5, 2.66666667, 4.0, 6.5, 1.5, 10.5, 5.0, 10.0 }; + double[] controlX = new double[] { 0., 2., 4., 6., 8., 10., 16., 22., 50., 100. }; + // double[] controlYerr = new double[] {0.5, 0.501120965, 0.3640055, + // 0.18104634, 0.4, 0.48102899, 0.15, 1.05, 0.5, 1.0}; // calculated + // from errors [sqrt(sum(errors^2))/N] + double[] controlYerr = new double[] { 0, 6.82169741, 2.5, 1.64991582, 0., 5.21216526, 0., 0., 0., 0. }; // calculated + // from + // stddev(flux) + double[] controlCounts = new double[] { 1, 3, 2, 3, 1, 3, 1, 1, 1, 1 }; + + assertEquals(response.getSegments().size(), 1); + + // tests + for (int i = 0; i < seg.getY().length; i++) { + assertEquals(controlY[i], seg.getY()[i], EPSILON); + } + for (int i = 0; i < seg.getY().length; i++) { + assertEquals(controlX[i], seg.getX()[i], EPSILON); + } + for (int i = 0; i < seg.getY().length; i++) { + assertEquals(controlYerr[i], seg.getYerr()[i], EPSILON); + } + for (int i = 0; i < seg.getY().length; i++) { + assertEquals(controlCounts[i], seg.getCounts()[i], EPSILON); + } + } + + @Ignore("need sherpa-samp running") + @Test + public void testStacker() throws Exception { + ExtSed sed1 = new ExtSed("Sed1"); + ExtSed sed2 = new ExtSed("Sed2"); + ExtSed sed3 = new ExtSed("Sed3"); + + Segment seg1 = new Segment(); + + for (int k = 0; k < x1.length; k++) { + y1[k] = y1[k] * 1e23; + yerr1[k] = yerr1[k] * 1e23; + } + seg1.setFluxAxisValues(y1); + seg1.setSpectralAxisValues(x1); + seg1.setFluxAxisUnits("Jy"); + seg1.setSpectralAxisUnits("Angstrom"); + seg1.setDataValues(yerr1, UTYPE.FLUX_STAT_ERROR); + sed1.addSegment(seg1); + + Segment seg2 = new Segment(); + seg2.setFluxAxisValues(y2); + seg2.setSpectralAxisValues(x2); + seg2.setFluxAxisUnits("erg/s/cm2/Hz"); + seg2.setSpectralAxisUnits("Angstrom"); + seg2.setDataValues(yerr2, UTYPE.FLUX_STAT_ERROR); + sed2.addSegment(seg2); + + Segment seg3 = new Segment(); + seg3.setFluxAxisValues(y3); + + // convert the values in x3 to nm so I can test the unit conversions + // too. + int k = 0; + for (double x : x3) { + x3[k] = x * 0.1; + k++; + } + seg3.setSpectralAxisValues(x3); + seg3.setFluxAxisUnits("erg/s/cm2/Hz"); + seg3.setSpectralAxisUnits("nm"); + seg3.setDataValues(yerr3, UTYPE.FLUX_STAT_ERROR); + sed3.addSegment(seg3); + + SedStack stack = new SedStack("Stack"); + stack.add(sed1); + stack.add(sed2); + stack.add(sed3); + + // setup the stacking configuration + StackConfiguration config = new StackConfiguration(); + config.setBinsize(2.0); + config.setBinsizeUnit("Angstrom"); + config.setLogbin(false); + config.setSmooth(false); + config.setSmoothBinsize(20.); + config.setStatistic("avg"); + config.setYUnits("erg/s/cm2/Hz"); + + // stack + stacker = new SedStackerStacker(controller, (UnitsManager) null); + ExtSed result = stacker.stack(stack, config); + + List xs = new ArrayList<>(); + List ys = new ArrayList<>(); + xs.add(x1); + xs.add(x2); + xs.add(x3); + ys.add(y1); + ys.add(y2); + ys.add(y3); + + // stack.getOrigSeds() should return original seds + for (int j = 0; j < stack.getOrigSeds().size(); j++) { + ExtSed origSed = stack.getOrigSeds().get(j); + double[] x = xs.get(j); + double[] y = ys.get(j); + + for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0).getLength(); i++) { + double xOrigValue = origSed.getSegment(0).getSpectralAxisValues()[i]; + double yOrigValue = origSed.getSegment(0).getFluxAxisValues()[i]; + assertEquals(xOrigValue, x[i], EPSILON); + assertEquals(yOrigValue, y[i], EPSILON); + } + } + + double[] controlY = new double[] { 5.0, 5.36666667, 4.5, 2.66666667, 4.0, 6.5, 1.5, 10.5, 5.0, 10.0 }; + double[] controlX = new double[] { 0., 2., 4., 6., 8., 10., 16., 22., 50., 100. }; + // double[] controlYerr = new double[] {0.5, 0.501120965, 0.3640055, + // 0.18104634, 0.4, 0.48102899, 0.15, 1.05, 0.5, 1.0}; // calculated + // from errors [sqrt(sum(errors^2))/N] + double[] controlYerr = new double[] { 0, 6.82169741, 2.5, 1.64991582, 0., 5.21216526, 0., 0., 0., 0. }; // calculated + // from + // stddev(flux) + double[] controlCounts = new double[] { 1, 3, 2, 3, 1, 3, 1, 1, 1, 1 }; + + // test values of stacked Sed + double[] yerrValues = (double[]) result.getSegment(0).getDataValues(UTYPE.FLUX_STAT_ERROR); + double[] counts = (double[]) result.getAttachment(COUNTS); + for (int i = 0; i < result.getSegment(0).getLength(); i++) { + double xValue = result.getSegment(0).getSpectralAxisValues()[i]; + double yValue = result.getSegment(0).getFluxAxisValues()[i]; + assertEquals(controlX[i], xValue, EPSILON); + assertEquals(controlY[i], yValue, EPSILON); + assertEquals(controlYerr[i], yerrValues[i], EPSILON); + assertEquals(controlCounts[i], counts[i], EPSILON); + } + } +} diff --git a/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerStackerTest.java b/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerStackerTest.java index 942afa80..17a534e2 100644 --- a/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerStackerTest.java +++ b/sed-builder/src/test/java/cfa/vo/sed/science/stacker/SedStackerStackerTest.java @@ -22,9 +22,6 @@ package cfa.vo.sed.science.stacker; import cfa.vo.interop.SAMPController; -import cfa.vo.interop.SAMPFactory; -import cfa.vo.interop.SAMPMessage; -import cfa.vo.iris.interop.SedSAMPController; import cfa.vo.iris.sed.ExtSed; import static cfa.vo.sed.science.stacker.SedStackerAttachments.COUNTS; @@ -34,265 +31,307 @@ import cfa.vo.sedlib.Segment; import cfa.vo.sherpa.SherpaClient; +import static org.junit.Assert.*; + +import java.awt.Component; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; -import java.util.logging.Logger; - +import java.util.Map; import org.astrogrid.samp.Response; -import org.junit.After; +import org.astrogrid.samp.client.SampException; +import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; -import static org.junit.Assert.*; +import cfa.vo.sedlib.common.SedNoDataException; -/** - * - * @author jbudynk - */ public class SedStackerStackerTest { - private static final Logger logger = Logger.getLogger(SedStackerStackerTest.class.getName()); - - double[] x1; - double[] y1; - double[] yerr1; - double[] x2; - double[] y2; - double[] yerr2; - double[] x3; - double[] y3; - double[] yerr3; - - SegmentPayload segment1; - SegmentPayload segment2; - SegmentPayload segment3; + protected static double[] x1 = new double[] { 5, 1, 10, 15, 50, 100 }; + protected static double[] y1 = new double[] { 0.5, 0.1, 1.0, 1.5, 5.0, 10.0 }; + protected static double[] yerr1 = new double[] { 0.05, 0.01, 0.1, 0.15, 0.5, 1.0 }; - SedStackerStackPayload payload; + protected static double[] x2 = new double[] { 2, 4, 5, 8, 10 }; + protected static double[] y2 = new double[] { 1, 2, 3, 4, 5 }; + protected static double[] yerr2 = new double[] { 0.1, 0.2, 0.3, 0.4, 0.5 }; - private SAMPController controller; + protected static double[] x3 = new double[] { 0.5, 3.0, 1.5, 5.0, 10.5, 21.0 }; + protected static double[] y3 = new double[] { 5.0, 7.0, 15.0, 4.5, 13.5, 10.5 }; + protected static double[] yerr3 = new double[] { 0.5, 0.7, 1.5, 0.45, 1.35, 1.05 }; + + Segment segment1; + Segment segment2; + Segment segment3; + + ExtSed sed1; + ExtSed sed2; + ExtSed sed3; + + private SedStack sedStack; + private SedStackerStacker stacker; - + + private StackerPayloadStub response; + + protected SherpaClientStub client; + protected SAMPControllerStub controller; + @Before - public void setUp() { - x1 = new double[]{5, 1, 10, 15, 50, 100}; - y1 = new double[]{0.5, 0.1, 1.0, 1.5, 5.0, 10.0}; - yerr1 = new double[]{0.05, 0.01, 0.1, 0.15, 0.5, 1.0}; - - x2 = new double[]{2, 4, 5, 8, 10}; - y2 = new double[]{1, 2, 3, 4, 5}; - yerr2 = new double[]{0.1, 0.2, 0.3, 0.4, 0.5}; - - x3 = new double[]{0.5, 3.0, 1.5, 5.0, 10.5, 21.0}; - y3 = new double[]{5.0, 7.0, 15.0, 4.5, 13.5, 10.5}; - yerr3 = new double[]{0.5, 0.7, 1.5, 0.45, 1.35, 1.05}; - - segment1 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); - segment1.setX(x1); - segment1.setY(y1); - segment1.setYerr(yerr1); - segment1.setZ(0.1); - - segment2 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); - segment2.setX(x2); - segment2.setY(y2); - segment2.setYerr(yerr2); - segment2.setZ(0.2); - - segment3 = (SegmentPayload) SAMPFactory.get(SegmentPayload.class); - segment3.setX(x3); - segment3.setY(y3); - segment3.setYerr(yerr3); - segment3.setZ(0.3); + public void setUp() throws Exception { + this.controller = new SAMPControllerStub("name", "description", "url"); + this.client = new SherpaClientStub(controller); + + this.stacker = new SedStackerStacker(controller, client) { + @Override + protected void showMessageDialog(Component parent, Object msg, String title, int type) { + logger.info("Expected message dialogue: " + msg); + } + @Override + protected SedStackerStackPayload translateResponse(Response rspns) throws Exception { + return response; + } + }; + } + + private void initialize() throws Exception { + segment1 = new Segment(); + segment1.setFluxAxisValues(y1); + segment1.setFluxAxisUnits("Jy"); + segment1.setSpectralAxisValues(x1); + segment1.setSpectralAxisUnits("Angstrom"); + + segment2 = new Segment(); + segment2.setFluxAxisValues(y2); + segment2.setFluxAxisUnits("erg/s/cm2/Angstrom"); + segment2.setSpectralAxisValues(x2); + segment2.setSpectralAxisUnits("Hz"); + + segment3 = new Segment(); + segment3.setFluxAxisValues(y3); + segment3.setFluxAxisUnits("mJy"); + segment3.setSpectralAxisValues(x3); + segment3.setSpectralAxisUnits("m"); + + sed1 = new ExtSed("1"); + sed1.addSegment(segment1); + + sed2 = new ExtSed("2"); + sed2.addSegment(segment2); + + sed3 = new ExtSed("3"); + sed3.addSegment(segment3); + + List seds = new LinkedList(); + seds.add(sed1); + seds.add(sed2); + seds.add(sed3); + + sedStack = new SedStack("test", seds); + + controller.rspns = new Response(); + + response = new StackerPayloadStub(); } - @Ignore("need sherpa-samp running") @Test - public void testStackAvg() throws Exception { - // Start the SAMP controller - controller = new SedSAMPController("SEDStacker", "SEDStacker", this.getClass().getResource("/tools_tiny.png").toString()); - controller.setAutoRunHub(false); - controller.start(false); - - Thread.sleep(2000); - - while (!controller.isConnected()) { - logger.info("waiting connection"); - Thread.sleep(1000); - } - - payload = (SedStackerStackPayload) SAMPFactory.get(SedStackerStackPayload.class); - - // Setup the stack payload - payload.addSegment(segment1); - payload.addSegment(segment2); - payload.addSegment(segment3); - payload.setBinsize(2.0); - payload.setLogBin(false); - payload.setSmooth(false); - payload.setSmoothBinsize(5.0); - payload.setStatistic("avg"); - - // Setup and send SAMP message - SAMPMessage message = SAMPFactory.createMessage("stack.stack", payload, SedStackerStackPayload.class); - - SherpaClient client = new SherpaClient(controller); - - Response rspns = controller.callAndWait(client.findSherpa(), message.get(), 10); - if (client.isException(rspns)) { - Exception ex = client.getException(rspns); - throw ex; - } - - SedStackerStackPayload response = (SedStackerStackPayload) SAMPFactory.get(rspns.getResult(), SedStackerStackPayload.class); - - // get response values - SegmentPayload seg = response.getSegments().get(0); - double[] controlY = new double[]{5.0, 5.36666667, 4.5, 2.66666667, 4.0, 6.5, 1.5, 10.5, 5.0, 10.0}; - double[] controlX = new double[]{0., 2., 4., 6., 8., 10., 16., 22., 50., 100.}; - //double[] controlYerr = new double[] {0.5, 0.501120965, 0.3640055, 0.18104634, 0.4, 0.48102899, 0.15, 1.05, 0.5, 1.0}; // calculated from errors [sqrt(sum(errors^2))/N] - double[] controlYerr = new double[]{0, 6.82169741, 2.5, 1.64991582, 0., 5.21216526, 0., 0., 0., 0.}; // calculated from stddev(flux) - double[] controlCounts = new double[]{1, 3, 2, 3, 1, 3, 1, 1, 1, 1}; - - assertEquals(response.getSegments().size(), 1); - - // tests - for (int i = 0; i < seg.getY().length; i++) { - assertEquals(controlY[i], seg.getY()[i], 0.00001); + public void testEmptyStack() throws Exception { + // pushing through an empty stack we expect an SedNoDataException and a message dialogue. + // Note that the message dialogue is stubbed out of the call. + SedStack stack = new SedStack(null); + stack.setSeds(new ArrayList()); + + try { + stacker.stack(stack); + } catch(SedNoDataException ex) { + return; } - for (int i = 0; i < seg.getY().length; i++) { - assertEquals(controlX[i], seg.getX()[i], 0.00001); + Assert.fail(); + } + + @Test + public void testSherpaException() throws Exception { + // The check for a sherpa client should fail. + initialize(); + client.findSherpa = false; + try { + stacker.stack(sedStack); + } catch(Exception ex) { + // Tied to message, would be better if we had a more specific exception thrown. + assertTrue(ex.getMessage().contains("Sherpa not found")); + return; + } finally { + client.findSherpa = true; } - for (int i = 0; i < seg.getY().length; i++) { - assertEquals(controlYerr[i], seg.getYerr()[i], 0.00001); + Assert.fail(); + } + + @Test + public void testUnitConversion() throws Exception { + // The convert units method should ensure that all SEDs in a stack + // have the same units + initialize(); + + stacker.convertUnits(sedStack, "Angstrom", "erg/s/cm2/Angstrom"); + + assertEquals(sedStack.getSeds().get(0).getSegment(0).getFluxAxisUnits(), + sedStack.getSeds().get(1).getSegment(0).getFluxAxisUnits()); + assertEquals(sedStack.getSeds().get(0).getSegment(0).getFluxAxisUnits(), + sedStack.getSeds().get(2).getSegment(0).getFluxAxisUnits()); + + assertEquals(sedStack.getSeds().get(0).getSegment(0).getSpectralAxisUnits(), + sedStack.getSeds().get(1).getSegment(0).getSpectralAxisUnits()); + assertEquals(sedStack.getSeds().get(0).getSegment(0).getSpectralAxisUnits(), + sedStack.getSeds().get(2).getSegment(0).getSpectralAxisUnits()); + } + + @Test + public void checkMemory() throws Exception { + initialize(); + + // Normal request should pass just fine + stacker.checkMemory(sedStack); + + // Send in a request that should barf on memory requirements + double[] breakStuff = new double[] {1, 1000000000}; + segment1.setSpectralAxisValues(breakStuff); + try { + stacker.checkMemory(sedStack); + } catch (StackException ex) { + return; } - for (int i = 0; i < seg.getY().length; i++) { - assertEquals(controlCounts[i], seg.getCounts()[i], 0.00001); + fail(); + } + + @Test + public void testSAMPException() throws Exception { + initialize(); + + // Simulating a sherpa client exception, verify it propagates to the caller. + client.hasException = true; + try { + stacker.stack(sedStack); + } catch (RuntimeException ex) { + assertTrue(ex.getMessage().contains("client")); + return; + } finally { + client.hasException = false; } - - controller.stop(); + fail(); } - - @Ignore("need sherpa-samp running") + @Test - public void testStacker() throws Exception { - // Start the SAMP controller - controller = new SedSAMPController("SEDStacker", "SEDStacker", this.getClass().getResource("/tools_tiny.png").toString()); - controller.setAutoRunHub(false); - controller.start(false); - - Thread.sleep(2000); - - while (!controller.isConnected()) { - logger.info("waiting connection"); - Thread.sleep(1000); + public void testSuccessful() throws Exception { + initialize(); + + response.segments = new LinkedList(); + SegmentPayload payload = new SegmentPayloadStub(); + response.segments.add(payload); + + stacker.stack(sedStack); + } + + // + // + // Stubs, use these to set expectations + // + // + + private static class SAMPControllerStub extends SAMPController { + public SAMPControllerStub(String name, String description, String iconUrl) { + super(name, description, iconUrl); } - - ExtSed sed1 = new ExtSed("Sed1"); - ExtSed sed2 = new ExtSed("Sed2"); - ExtSed sed3 = new ExtSed("Sed3"); - - Segment seg1 = new Segment(); - - for (int k = 0; k < x1.length; k++) { - y1[k] = y1[k] * 1e23; - yerr1[k] = yerr1[k] * 1e23; + + public Response rspns; + + @SuppressWarnings("rawtypes") + @Override + public Response callAndWait(String arg0, Map arg1, int arg2) throws SampException { + return rspns; } - seg1.setFluxAxisValues(y1); - seg1.setSpectralAxisValues(x1); - seg1.setFluxAxisUnits("Jy"); - seg1.setSpectralAxisUnits("Angstrom"); - seg1.setDataValues(yerr1, UTYPE.FLUX_STAT_ERROR); - sed1.addSegment(seg1); - - Segment seg2 = new Segment(); - seg2.setFluxAxisValues(y2); - seg2.setSpectralAxisValues(x2); - seg2.setFluxAxisUnits("erg/s/cm2/Hz"); - seg2.setSpectralAxisUnits("Angstrom"); - seg2.setDataValues(yerr2, UTYPE.FLUX_STAT_ERROR); - sed2.addSegment(seg2); - - Segment seg3 = new Segment(); - seg3.setFluxAxisValues(y3); - - //convert the values in x3 to nm so I can test the unit conversions too. - int k = 0; - for (double x : x3) { - x3[k] = x * 0.1; - k++; + } + + private static class SherpaClientStub extends SherpaClient { + + public SherpaClientStub(SAMPController controller) { + super(controller); } - seg3.setSpectralAxisValues(x3); - seg3.setFluxAxisUnits("erg/s/cm2/Hz"); - seg3.setSpectralAxisUnits("nm"); - seg3.setDataValues(yerr3, UTYPE.FLUX_STAT_ERROR); - sed3.addSegment(seg3); - - SedStack stack = new SedStack("Stack"); - stack.add(sed1); - stack.add(sed2); - stack.add(sed3); - - // setup the stacking configuration - StackConfiguration config = new StackConfiguration(); - config.setBinsize(2.0); - config.setBinsizeUnit("Angstrom"); - config.setLogbin(false); - config.setSmooth(false); - config.setSmoothBinsize(20.); - config.setStatistic("avg"); - config.setYUnits("erg/s/cm2/Hz"); - - // stack - stacker = new SedStackerStacker(controller, Default.getInstance().getUnitsManager()); - ExtSed result = stacker.stack(stack, config); - - List xs = new ArrayList(); - List ys = new ArrayList(); - xs.add(x1); - xs.add(x2); - xs.add(x3); - ys.add(y1); - ys.add(y2); - ys.add(y3); - // stack.getOrigSeds() should return original seds - for (int j = 0; j < stack.getOrigSeds().size(); j++) { - ExtSed origSed = stack.getOrigSeds().get(j); - double[] x = xs.get(j); - double[] y = ys.get(j); - - for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0).getLength(); i++) { - double xOrigValue = origSed.getSegment(0).getSpectralAxisValues()[i]; - double yOrigValue = origSed.getSegment(0).getFluxAxisValues()[i]; - assertEquals(xOrigValue, x[i]); - assertEquals(yOrigValue, y[i]); - } + public boolean findSherpa = true; + @Override + public String findSherpa() throws SampException { + if (findSherpa) return ""; + throw new SampException(); } - double[] controlY = new double[]{5.0, 5.36666667, 4.5, 2.66666667, 4.0, 6.5, 1.5, 10.5, 5.0, 10.0}; - double[] controlX = new double[]{0., 2., 4., 6., 8., 10., 16., 22., 50., 100.}; - //double[] controlYerr = new double[] {0.5, 0.501120965, 0.3640055, 0.18104634, 0.4, 0.48102899, 0.15, 1.05, 0.5, 1.0}; // calculated from errors [sqrt(sum(errors^2))/N] - double[] controlYerr = new double[]{0, 6.82169741, 2.5, 1.64991582, 0., 5.21216526, 0., 0., 0., 0.}; // calculated from stddev(flux) - double[] controlCounts = new double[]{1, 3, 2, 3, 1, 3, 1, 1, 1, 1}; - - // test values of stacked Sed - double[] yerrValues = (double[]) result.getSegment(0).getDataValues(UTYPE.FLUX_STAT_ERROR); - double[] counts = (double[]) result.getAttachment(COUNTS); - for (int i = 0; i < result.getSegment(0).getLength(); i++) { - double xValue = result.getSegment(0).getSpectralAxisValues()[i]; - double yValue = result.getSegment(0).getFluxAxisValues()[i]; - assertEquals(controlX[i], xValue, 0.00001); - assertEquals(controlY[i], yValue, 0.00001); - assertEquals(controlYerr[i], yerrValues[i], 0.00001); - assertEquals(controlCounts[i], counts[i]); + public boolean hasException = false; + @Override + public boolean isException(Response rspns) { + return hasException; + } + + @Override + public Exception getException(Response rspns) { + return new RuntimeException("client exception"); } - - controller.stop(); } - - @After - public void tearDown() { + + private static class StackerPayloadStub implements SedStackerStackPayload { + public List segments; + @Override + public Double getBinsize() {return null;} + @Override + public void setBinsize(Double binsize) {} + @Override + public Double getSmoothBinsize() {return null;} + @Override + public void setSmoothBinsize(Double boxSize) {} + @Override + public String getStatistic() {return null;} + @Override + public void setStatistic(String statistic) {} + @Override + public Boolean getLogBin() {return null;} + @Override + public void setLogBin(Boolean logBin) {} + @Override + public Boolean getSmooth() {return null;} + @Override + public void setSmooth(Boolean smooth) {} + @Override + public List getSegments() {return segments;} + @Override + public void addSegment(SegmentPayload segment) {} + } + + private static class SegmentPayloadStub implements SegmentPayload { + @Override + public double[] getX() {return x1;} + @Override + public void setX(double[] x) {} + @Override + public double[] getY() {return y1;} + @Override + public void setY(double[] y) {} + @Override + public double[] getYerr() {return yerr1;} + @Override + public void setYerr(double[] yerr) {} + @Override + public Double getZ() {return 0.0;} + @Override + public void setZ(Double redshift) {} + @Override + public Double getNormConstant() {return 0.0;} + @Override + public void setNormConstant(Double normConstant) {} + @Override + public double[] getCounts() {return yerr1;} + @Override + public void setCounts(double[] counts) {} + @Override + public String getId() {return null;} + @Override + public void setId(String id) {} } - }