Skip to content

Instantly share code, notes, and snippets.

@tonussi
Last active August 14, 2017 04:01
Show Gist options
  • Save tonussi/ebacf46fea91ebe26af1c2351f46b677 to your computer and use it in GitHub Desktop.
Save tonussi/ebacf46fea91ebe26af1c2351f46b677 to your computer and use it in GitHub Desktop.
Exercício 1 - JodaTime Tarefa - INE5448-08208 (20172) - Tópicos Especiais em Aplicações Tecnológicas I
package test;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Locale;
import java.util.TimeZone;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.joda.time.Instant;
import org.joda.time.Interval;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @author lucas.tonussi
*
* @see Test-Fixture
*
* A test fixture is a fixed state in code which is tested used as input
* for a test. Another way to describe this is a test precondition. For
* example, a test fixture might be a a fixed string, which is used as
* input for a method. The test would validate if the method behaves
* correctly with this input.
*
* @see Unit-Test
*
* A unit test is a piece of code written by a developer that executes a
* specific functionality in the code to be tested and asserts a certain
* behavior or state. @ Well Written
*
*
* @see Well-written
*
* Test code should not assume any order, i.e., tests should not depend on
* other tests. JUnit assumes that all test methods can be executed in an
* arbitrary order.
*/
public class JUnitAulaUm {
static DateTimeZone SAO_PAULO;
@BeforeClass
public static void setUpClass() {
SAO_PAULO = DateTimeZone.forID("America/Sao_Paulo");
}
@AfterClass
public static void tearDownClass() {
SAO_PAULO = null;
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test(timeout=200)
public void testDateCreation() {
// Fixture Setup
LocalDate localDateTest;
// Exercise SUT
localDateTest = new LocalDate(2012, 2, 5);
// Result Verification
assertEquals(localDateTest.getYear(), 2012);
assertEquals(localDateTest.getDayOfMonth(), 5);
assertEquals(localDateTest.getMonthOfYear(), 2);
// Fixture Teardown
localDateTest = null;
}
@Test(timeout=200)
public void testDateSum() {
// Fixture Setup
LocalDate natal2017 = new LocalDate(2017, 12, 25);
LocalDate reveillon2018 = new LocalDate(2018, 1, 1);
// Exercise SUT
LocalDate testDate = natal2017.plusDays(7);
// Result Verification
assertEquals(reveillon2018, testDate);
// Fixture Teardown
natal2017 = null;
reveillon2018 = null;
}
@Test(timeout=200)
public void testDateSubstraction() {
// Fixture Setup
LocalDate janeiro1 = new LocalDate(2017, 1, 1);
LocalDate janeiro2 = new LocalDate(2017, 4, 1);
// Exercise SUT
janeiro2 = janeiro2.minusMonths(3);
// Result Verification
assertEquals(janeiro1, janeiro2);
// Fixture Teardown
janeiro1 = null;
janeiro2 = null;
}
@Test(timeout=200)
public void testHourCreation() {
// Fixture Setup
DateTimeFormatter formatedHourTest;
// Exercise SUT
formatedHourTest = DateTimeFormat.forPattern("yyyy--dd MM HH");
// Result Verification
assertEquals (
new DateTime(2010, 6, 30, 13, 0, ISOChronology.getInstance(SAO_PAULO)).toInstant(),
Instant.parse("2010--30 06 13", formatedHourTest)
);
// Fixture Teardown
formatedHourTest = null;
}
@Test(timeout=200)
public void testHourSum() {
// Fixture Setup
DateTime dateTimeBeingTested = new DateTime(2006, 6, 9, 15, 0, 0, 0, SAO_PAULO);
// Exercise SUT
DateTime plusFiveHours = new DateTime(2006, 6, 9, 10, 0, 0, 0, SAO_PAULO).plusHours(5);
// Result Verification
assertEquals(dateTimeBeingTested, plusFiveHours);
// Fixture Teardown
dateTimeBeingTested = null;
plusFiveHours = null;
}
@Test(timeout=200)
public void testHoursSubtraction() {
// Fixture Setup
DateTime start;
DateTime end1;
// Exercise SUT
start = new DateTime(2006, 6, 9, 15, 0, 0, 0, SAO_PAULO).minusHours(5);
end1 = new DateTime(2006, 6, 9, 10, 0, 0, 0, SAO_PAULO);
// Result Verification
assertEquals(start, end1);
// Fixture Teardown
start = null;
end1 = null;
}
@Test(timeout=200)
public void testBeforeAndAfter() {
// Fixture Setup
Interval intervalBetween3And7;
// Exercise SUT
intervalBetween3And7 = new Interval(1, 2);
// Result Verification
assertEquals(true, intervalBetween3And7.isBefore(new Instant(2)));
assertEquals(true, intervalBetween3And7.isBefore(new Instant(7)));
assertEquals(false, intervalBetween3And7.isAfter(new Instant(6)));
assertEquals(false, intervalBetween3And7.isAfter(new Instant(6)));
// Fixture Teardown
intervalBetween3And7 = null;
}
@Test(timeout=200)
public void testDateTimeCreation() {
// Fixture Setup
DateTime dateBeingTested;
// Exercise SUT
dateBeingTested = new DateTime(2006, 6, 9, 12, 4, 3, 1, SAO_PAULO);
// Result Verification
assertEquals(dateBeingTested.getYear(), 2006);
assertEquals(dateBeingTested.getDayOfMonth(), 9);
assertEquals(dateBeingTested.getMonthOfYear(), 6);
assertEquals(dateBeingTested.getHourOfDay(), 12);
assertEquals(dateBeingTested.getMinuteOfHour(), 4);
assertEquals(dateBeingTested.getSecondOfMinute(), 3);
assertEquals(dateBeingTested.getMillisOfSecond(), 1);
// Fixture Teardown
dateBeingTested = null;
}
@Test(timeout=200)
public void testDateInterval() {
// Fixture Setup
Interval internalTest;
long TEST_TIME1 = 30L
* DateTimeConstants.MILLIS_PER_DAY + 12L
* DateTimeConstants.MILLIS_PER_HOUR + 24L
* DateTimeConstants.MILLIS_PER_MINUTE;
long TEST_TIME2 = 100L
* DateTimeConstants.MILLIS_PER_DAY + 14L
* DateTimeConstants.MILLIS_PER_HOUR + 28L
* DateTimeConstants.MILLIS_PER_MINUTE;
// Exercise SUT
internalTest = new Interval(TEST_TIME1, TEST_TIME2);
// Result Verification
assertEquals(TEST_TIME2 - TEST_TIME1, internalTest.toDurationMillis());
assertEquals(TEST_TIME2 - TEST_TIME1, internalTest.toDuration().getMillis());
// Fixture Teardown
internalTest = null;
}
@Test(timeout=200)
public void testPeriodsCreation() {
// Fixture Setup
Period periodBeingTested;
// Exercise SUT
periodBeingTested = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime());
// Result Verification
assertEquals(6, periodBeingTested.size());
assertEquals(1, periodBeingTested.getValue(0));
assertEquals(4, periodBeingTested.getValue(1));
assertEquals(5, periodBeingTested.getValue(2));
assertEquals(6, periodBeingTested.getValue(3));
assertEquals(7, periodBeingTested.getValue(4));
assertEquals(8, periodBeingTested.getValue(5));
assertEquals(true, Arrays.equals(new int[] { 1, 4, 5, 6, 7, 8 }, periodBeingTested.getValues()));
// Fixture Teardown
periodBeingTested = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment