test/java/time/test/java/time/format/TestZoneOffsetPrinter.java

Print this page




  42  *    and/or other materials provided with the distribution.
  43  *
  44  *  * Neither the name of JSR-310 nor the names of its contributors
  45  *    may be used to endorse or promote products derived from this software
  46  *    without specific prior written permission.
  47  *
  48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59  */
  60 package test.java.time.format;
  61 
  62 import static java.time.temporal.ChronoField.OFFSET_SECONDS;
  63 import static org.testng.Assert.assertEquals;
  64 
  65 import java.time.DateTimeException;
  66 import java.time.ZoneOffset;
  67 import java.time.format.DateTimeBuilder;
  68 
  69 import org.testng.annotations.DataProvider;
  70 import org.testng.annotations.Test;
  71 
  72 /**
  73  * Test ZoneOffsetPrinterParser.
  74  */
  75 @Test(groups={"implementation"})
  76 public class TestZoneOffsetPrinter extends AbstractTestPrinterParser {
  77 
  78     private static final ZoneOffset OFFSET_0130 = ZoneOffset.of("+01:30");
  79 
  80     //-----------------------------------------------------------------------
  81     @DataProvider(name="offsets")
  82     Object[][] provider_offsets() {
  83         return new Object[][] {
  84             {"+HH", "NO-OFFSET", ZoneOffset.UTC},
  85             {"+HH", "+01", ZoneOffset.ofHours(1)},
  86             {"+HH", "-01", ZoneOffset.ofHours(-1)},
  87 


 133             {"+HH:MM:ss", "+18:00", ZoneOffset.ofHoursMinutesSeconds(18, 0, 0)},
 134             {"+HH:MM:ss", "-01:00", ZoneOffset.ofHoursMinutesSeconds(-1, 0, 0)},
 135             {"+HH:MM:ss", "-02:00", ZoneOffset.ofHoursMinutesSeconds(-2, 0, 0)},
 136             {"+HH:MM:ss", "-18:00", ZoneOffset.ofHoursMinutesSeconds(-18, 0, 0)},
 137 
 138             {"+HHMMSS", "NO-OFFSET", ZoneOffset.UTC},
 139             {"+HHMMSS", "+010203", ZoneOffset.ofHoursMinutesSeconds(1, 2, 3)},
 140             {"+HHMMSS", "-010203", ZoneOffset.ofHoursMinutesSeconds(-1, -2, -3)},
 141             {"+HHMMSS", "+010200", ZoneOffset.ofHoursMinutesSeconds(1, 2, 0)},
 142             {"+HHMMSS", "-010200", ZoneOffset.ofHoursMinutesSeconds(-1, -2, 0)},
 143 
 144             {"+HH:MM:SS", "NO-OFFSET", ZoneOffset.UTC},
 145             {"+HH:MM:SS", "+01:02:03", ZoneOffset.ofHoursMinutesSeconds(1, 2, 3)},
 146             {"+HH:MM:SS", "-01:02:03", ZoneOffset.ofHoursMinutesSeconds(-1, -2, -3)},
 147             {"+HH:MM:SS", "+01:02:00", ZoneOffset.ofHoursMinutesSeconds(1, 2, 0)},
 148             {"+HH:MM:SS", "-01:02:00", ZoneOffset.ofHoursMinutesSeconds(-1, -2, 0)},
 149         };
 150     }
 151 
 152     @Test(dataProvider="offsets")
 153     public void test_print(String pattern, String expected, ZoneOffset offset) throws Exception {
 154         buf.append("EXISTING");
 155         getFormatter(pattern, "NO-OFFSET").printTo(new DateTimeBuilder(OFFSET_SECONDS, offset.getTotalSeconds()), buf);
 156         assertEquals(buf.toString(), "EXISTING" + expected);
 157     }
 158 
 159     @Test(dataProvider="offsets")
 160     public void test_toString(String pattern, String expected, ZoneOffset offset) throws Exception {
 161         assertEquals(getFormatter(pattern, "NO-OFFSET").toString(), "Offset('NO-OFFSET'," + pattern + ")");
 162     }
 163 
 164     //-----------------------------------------------------------------------
 165     @Test(expectedExceptions=DateTimeException.class)
 166     public void test_print_emptyCalendrical() throws Exception {
 167         getFormatter("+HH:MM:ss", "Z").printTo(EMPTY_DTA, buf);
 168     }
 169 
 170     public void test_print_emptyAppendable() throws Exception {
 171         getFormatter("+HH:MM:ss", "Z").printTo(new DateTimeBuilder(OFFSET_SECONDS, OFFSET_0130.getTotalSeconds()), buf);
 172         assertEquals(buf.toString(), "+01:30");
 173     }
 174 
 175 }


  42  *    and/or other materials provided with the distribution.
  43  *
  44  *  * Neither the name of JSR-310 nor the names of its contributors
  45  *    may be used to endorse or promote products derived from this software
  46  *    without specific prior written permission.
  47  *
  48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59  */
  60 package test.java.time.format;
  61 

  62 import static org.testng.Assert.assertEquals;
  63 
  64 import java.time.DateTimeException;
  65 import java.time.ZoneOffset;

  66 
  67 import org.testng.annotations.DataProvider;
  68 import org.testng.annotations.Test;
  69 
  70 /**
  71  * Test ZoneOffsetPrinterParser.
  72  */
  73 @Test(groups={"implementation"})
  74 public class TestZoneOffsetPrinter extends AbstractTestPrinterParser {
  75 
  76     private static final ZoneOffset OFFSET_0130 = ZoneOffset.of("+01:30");
  77 
  78     //-----------------------------------------------------------------------
  79     @DataProvider(name="offsets")
  80     Object[][] provider_offsets() {
  81         return new Object[][] {
  82             {"+HH", "NO-OFFSET", ZoneOffset.UTC},
  83             {"+HH", "+01", ZoneOffset.ofHours(1)},
  84             {"+HH", "-01", ZoneOffset.ofHours(-1)},
  85 


 131             {"+HH:MM:ss", "+18:00", ZoneOffset.ofHoursMinutesSeconds(18, 0, 0)},
 132             {"+HH:MM:ss", "-01:00", ZoneOffset.ofHoursMinutesSeconds(-1, 0, 0)},
 133             {"+HH:MM:ss", "-02:00", ZoneOffset.ofHoursMinutesSeconds(-2, 0, 0)},
 134             {"+HH:MM:ss", "-18:00", ZoneOffset.ofHoursMinutesSeconds(-18, 0, 0)},
 135 
 136             {"+HHMMSS", "NO-OFFSET", ZoneOffset.UTC},
 137             {"+HHMMSS", "+010203", ZoneOffset.ofHoursMinutesSeconds(1, 2, 3)},
 138             {"+HHMMSS", "-010203", ZoneOffset.ofHoursMinutesSeconds(-1, -2, -3)},
 139             {"+HHMMSS", "+010200", ZoneOffset.ofHoursMinutesSeconds(1, 2, 0)},
 140             {"+HHMMSS", "-010200", ZoneOffset.ofHoursMinutesSeconds(-1, -2, 0)},
 141 
 142             {"+HH:MM:SS", "NO-OFFSET", ZoneOffset.UTC},
 143             {"+HH:MM:SS", "+01:02:03", ZoneOffset.ofHoursMinutesSeconds(1, 2, 3)},
 144             {"+HH:MM:SS", "-01:02:03", ZoneOffset.ofHoursMinutesSeconds(-1, -2, -3)},
 145             {"+HH:MM:SS", "+01:02:00", ZoneOffset.ofHoursMinutesSeconds(1, 2, 0)},
 146             {"+HH:MM:SS", "-01:02:00", ZoneOffset.ofHoursMinutesSeconds(-1, -2, 0)},
 147         };
 148     }
 149 
 150     @Test(dataProvider="offsets")
 151     public void test_format(String pattern, String expected, ZoneOffset offset) throws Exception {
 152         buf.append("EXISTING");
 153         getFormatter(pattern, "NO-OFFSET").formatTo(offset, buf);
 154         assertEquals(buf.toString(), "EXISTING" + expected);
 155     }
 156 
 157     @Test(dataProvider="offsets")
 158     public void test_toString(String pattern, String expected, ZoneOffset offset) throws Exception {
 159         assertEquals(getFormatter(pattern, "NO-OFFSET").toString(), "Offset(" + pattern + ",'NO-OFFSET')");
 160     }
 161 
 162     //-----------------------------------------------------------------------
 163     @Test(expectedExceptions=DateTimeException.class)
 164     public void test_print_emptyCalendrical() throws Exception {
 165         getFormatter("+HH:MM:ss", "Z").formatTo(EMPTY_DTA, buf);
 166     }
 167 
 168     public void test_print_emptyAppendable() throws Exception {
 169         getFormatter("+HH:MM:ss", "Z").formatTo(OFFSET_0130, buf);
 170         assertEquals(buf.toString(), "+01:30");
 171     }
 172 
 173 }