Skip to content

Instantly share code, notes, and snippets.

/MyClock.java Secret

Created February 18, 2016 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/56efd087f3d52afd167e to your computer and use it in GitHub Desktop.
Save anonymous/56efd087f3d52afd167e to your computer and use it in GitHub Desktop.
StefMa @Medium - Developers, please write tests!
public class MyClock {
public String getCurrentTime(TimeFormat format) {
switch (format) {
case GERMAN:
return getStringFormattedTimeWithPatternAndLocale("HH:mm 'Uhr'", Locale.GERMANY);
case AMERICAN:
return getStringFormattedTimeWithPatternAndLocale("hh:mm a", Locale.US);
case GERMAN_WITH_DATE:
return getStringFormattedTimeWithPatternAndLocale("HH:mm 'Uhr,' EEEE dd.MM.yyyy", Locale.GERMANY);
case AMERICAN_WITH_DATE:
return getStringFormattedTimeWithPatternAndLocale("hh:mm a',' EEEE MM.dd.yyyy", Locale.US);
default:
return "";
}
}
@VisibleForTesting
long currentTimeMillis() {
return System.currentTimeMillis();
}
private String getStringFormattedTimeWithPatternAndLocale(String pattern, Locale locale) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, locale);
Date currentDate = new Date(currentTimeMillis());
return simpleDateFormat.format(currentDate);
}
}
public class MyClockTest {
private MyClock mMyClock;
private long mTimeMillis = System.currentTimeMillis();
@Before
public void setUp() throws Exception {
mMyClock = new MyClock() {
long currentTimeMillis() {
return mTimeMillis;
}
};
}
@Test
public void testGetCurrentName_ShouldReturnTimeInGermanFormat() throws Exception {
String germanTime = mMyClock.getCurrentTime(TimeFormat.GERMAN);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm 'Uhr'", Locale.GERMANY);
String formattedTime = simpleDateFormat.format(new Date(mTimeMillis));
assertThat(germanTime, is(equalTo(formattedTime)));
}
@Test
public void testGetCurrentName_ShouldReturnTimeInAmericanFormat() throws Exception {
String americanTime = mMyClock.getCurrentTime(TimeFormat.AMERICAN);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a", Locale.US);
String formattedTime = simpleDateFormat.format(new Date(mTimeMillis));
assertThat(americanTime, is(equalTo(formattedTime)));
}
@Test
public void testGetCurrentName_ShouldReturnTimeInGermanFormatWithDate() throws Exception {
String americanTime = mMyClock.getCurrentTime(TimeFormat.GERMAN_WITH_DATE);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm 'Uhr,' EEEE dd.MM.yyyy", Locale.GERMANY);
String formattedTime = simpleDateFormat.format(new Date(mTimeMillis));
assertThat(americanTime, is(equalTo(formattedTime)));
}
@Test
public void testGetCurrentName_ShouldReturnTimeInAmericanFormatWithDate() throws Exception {
String americanTime = mMyClock.getCurrentTime(TimeFormat.AMERICAN_WITH_DATE);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a',' EEEE MM.dd.yyyy", Locale.US);
String formattedTime = simpleDateFormat.format(new Date(mTimeMillis));
assertThat(americanTime, is(equalTo(formattedTime)));
}
@Test
public void testCurrentTimeMillis_ShouldReturnCurrentTimeMillis() throws Exception {
long clockTime = mMyClock.currentTimeMillis();
assertThat(mTimeMillis, is(equalTo(clockTime)));
}
}
public enum TimeFormat {
GERMAN, AMERICAN, GERMAN_WITH_DATE, AMERICAN_WITH_DATE
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment