Example usage for org.apache.commons.lang.time DateUtils truncate

List of usage examples for org.apache.commons.lang.time DateUtils truncate

Introduction

In this page you can find the example usage for org.apache.commons.lang.time DateUtils truncate.

Prototype

public static Date truncate(Object date, int field) 

Source Link

Document

Truncate this date, leaving the field specified as the most significant field.

For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:00:00.000.

Usage

From source file:org.simbasecurity.core.service.CredentialServiceImplTest.java

@Test
public void markUsersForPasswordChange() {
    Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);

    Date notLongerThenChangeRateAgo = DateUtils.addDays(today, -PASSWORD_EXPIRATION_TIME);
    Date longerThenChangeRateAgo = DateUtils.addDays(today, -(PASSWORD_EXPIRATION_TIME + 1));

    User userWithExpiredPassword = mock(User.class);
    User userWithExpiredPasswordButNotRequired = mock(User.class);
    User userWithValidPassword = mock(User.class);

    when(userWithExpiredPassword.isPasswordChangeRequired()).thenReturn(true);
    when(userWithExpiredPasswordButNotRequired.isPasswordChangeRequired()).thenReturn(false);
    when(userWithValidPassword.isPasswordChangeRequired()).thenReturn(true);

    when(userWithExpiredPassword.getDateOfLastPasswordChange()).thenReturn(longerThenChangeRateAgo);
    when(userWithExpiredPasswordButNotRequired.getDateOfLastPasswordChange())
            .thenReturn(longerThenChangeRateAgo);
    when(userWithValidPassword.getDateOfLastPasswordChange()).thenReturn(notLongerThenChangeRateAgo);

    when(mockConfigurationService.getValue(SimbaConfigurationParameter.PASSWORD_LIFE_TIME))
            .thenReturn(PASSWORD_EXPIRATION_TIME);
    when(mockUserRepository.findAll()).thenReturn(Arrays.asList(userWithExpiredPassword,
            userWithExpiredPasswordButNotRequired, userWithValidPassword));

    credentialService.markUsersForPasswordChange();

    verify(userWithExpiredPassword).setChangePasswordOnNextLogon(true);

    verify(userWithExpiredPasswordButNotRequired, never()).setChangePasswordOnNextLogon(anyBoolean());
    verify(userWithValidPassword, never()).setChangePasswordOnNextLogon(anyBoolean());
}

From source file:org.sipfoundry.sipxconfig.site.cdr.CdrHistory.java

/**
 * By default set start at next midnight
 *///  w w w  .  ja v a 2 s.c o  m
public static Date getDefaultEndTime() {
    Calendar now = Calendar.getInstance();
    now.add(Calendar.DAY_OF_MONTH, 1);
    Calendar end = DateUtils.truncate(now, Calendar.DAY_OF_MONTH);
    return end.getTime();
}

From source file:org.sonar.batch.issue.ModuleIssuesTest.java

@Test
public void add_issue_to_cache() throws Exception {
    ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME);
    activeRulesBuilder.create(SQUID_RULE_KEY).setSeverity(Severity.INFO).activate();
    initModuleIssues();//from w ww.j  a v  a  2 s . c  om

    Date analysisDate = new Date();
    when(project.getAnalysisDate()).thenReturn(analysisDate);

    DefaultIssue issue = new DefaultIssue().setKey("ABCDE").setRuleKey(SQUID_RULE_KEY)
            .setSeverity(Severity.CRITICAL);
    when(filters.accept(issue, null)).thenReturn(true);

    boolean added = moduleIssues.initAndAddIssue(issue);

    assertThat(added).isTrue();
    ArgumentCaptor<DefaultIssue> argument = ArgumentCaptor.forClass(DefaultIssue.class);
    verify(cache).put(argument.capture());
    assertThat(argument.getValue().severity()).isEqualTo(Severity.CRITICAL);
    assertThat(argument.getValue().creationDate()).isEqualTo(DateUtils.truncate(analysisDate, Calendar.SECOND));
}

From source file:org.sonar.batch.issue.ModuleIssuesTest.java

@Test
public void use_severity_from_active_rule_if_no_severity_on_issue() throws Exception {
    ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME);
    activeRulesBuilder.create(SQUID_RULE_KEY).setSeverity(Severity.INFO).activate();
    initModuleIssues();/*from ww w .  ja  v a2 s .c  om*/

    Date analysisDate = new Date();
    when(project.getAnalysisDate()).thenReturn(analysisDate);

    DefaultIssue issue = new DefaultIssue().setRuleKey(SQUID_RULE_KEY).setSeverity(null);
    when(filters.accept(issue, null)).thenReturn(true);
    moduleIssues.initAndAddIssue(issue);

    ArgumentCaptor<DefaultIssue> argument = ArgumentCaptor.forClass(DefaultIssue.class);
    verify(cache).put(argument.capture());
    assertThat(argument.getValue().severity()).isEqualTo(Severity.INFO);
    assertThat(argument.getValue().creationDate()).isEqualTo(DateUtils.truncate(analysisDate, Calendar.SECOND));
}

From source file:org.sonar.batch.issue.tracking.InitialOpenIssuesSensor.java

@Override
public void analyse(Project project, SensorContext context) {
    ResourceDto module = resourceDao.getResource(ResourceQuery.create().setKey(project.getEffectiveKey()));
    if (module != null) {
        long moduleId = module.getId();
        // Adding one second is a hack for resolving conflicts with concurrent user
        // changes during issue persistence
        final Date now = DateUtils.addSeconds(DateUtils.truncate(new Date(), Calendar.MILLISECOND), 1);
        issueDao.selectNonClosedIssuesByModule(moduleId, new ResultHandler() {
            @Override//w w  w . j  ava  2  s .co m
            public void handleResult(ResultContext rc) {
                IssueDto dto = (IssueDto) rc.getResultObject();
                dto.setSelectedAt(now.getTime());
                initialOpenIssuesStack.addIssue(dto);
            }
        });

        issueChangeDao.selectChangelogOnNonClosedIssuesByModuleAndType(moduleId, new ResultHandler() {
            @Override
            public void handleResult(ResultContext rc) {
                IssueChangeDto dto = (IssueChangeDto) rc.getResultObject();
                initialOpenIssuesStack.addChangelog(dto);
            }
        });
    }
}

From source file:org.sonar.core.issue.workflow.IssueWorkflowTest.java

@Test
public void should_do_automatic_transition() throws Exception {
    workflow.start();//from w  w  w . j av a  2  s.co m

    DefaultIssue issue = new DefaultIssue().setKey("ABCDE").setResolution(Issue.RESOLUTION_FIXED)
            .setStatus(Issue.STATUS_RESOLVED).setNew(false).setEndOfLife(true);
    Date now = new Date();
    workflow.doAutomaticTransition(issue, IssueChangeContext.createScan(now));
    assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FIXED);
    assertThat(issue.status()).isEqualTo(Issue.STATUS_CLOSED);
    assertThat(issue.closeDate()).isNotNull();
    assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND));
}

From source file:org.sonar.core.issue.workflow.IssueWorkflowTest.java

@Test
public void should_close_open_dead_issue() throws Exception {
    workflow.start();/*  w  w w.  jav  a2 s  .c  om*/

    DefaultIssue issue = new DefaultIssue().setKey("ABCDE").setResolution(null).setStatus(Issue.STATUS_OPEN)
            .setNew(false).setEndOfLife(true);
    Date now = new Date();
    workflow.doAutomaticTransition(issue, IssueChangeContext.createScan(now));
    assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FIXED);
    assertThat(issue.status()).isEqualTo(Issue.STATUS_CLOSED);
    assertThat(issue.closeDate()).isNotNull();
    assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND));
}

From source file:org.sonar.core.issue.workflow.IssueWorkflowTest.java

@Test
public void should_close_reopened_dead_issue() throws Exception {
    workflow.start();/*from ww w . j a  v  a  2 s .co m*/

    DefaultIssue issue = new DefaultIssue().setKey("ABCDE").setResolution(null).setStatus(Issue.STATUS_REOPENED)
            .setNew(false).setEndOfLife(true);
    Date now = new Date();
    workflow.doAutomaticTransition(issue, IssueChangeContext.createScan(now));
    assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FIXED);
    assertThat(issue.status()).isEqualTo(Issue.STATUS_CLOSED);
    assertThat(issue.closeDate()).isNotNull();
    assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND));
}

From source file:org.sonar.core.issue.workflow.IssueWorkflowTest.java

@Test
public void should_close_confirmed_dead_issue() throws Exception {
    workflow.start();/*www  . jav a 2  s  .com*/

    DefaultIssue issue = new DefaultIssue().setKey("ABCDE").setResolution(null)
            .setStatus(Issue.STATUS_CONFIRMED).setNew(false).setEndOfLife(true);
    Date now = new Date();
    workflow.doAutomaticTransition(issue, IssueChangeContext.createScan(now));
    assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FIXED);
    assertThat(issue.status()).isEqualTo(Issue.STATUS_CLOSED);
    assertThat(issue.closeDate()).isNotNull();
    assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND));
}

From source file:org.sonar.db.issue.IssueDtoTest.java

@Test
public void set_issue_fields() {
    Date createdAt = DateUtils.addDays(new Date(), -5);
    Date updatedAt = DateUtils.addDays(new Date(), -3);
    Date closedAt = DateUtils.addDays(new Date(), -1);

    IssueDto dto = new IssueDto().setKee("100").setType(RuleType.VULNERABILITY).setRuleId(1)
            .setRuleKey("squid", "AvoidCycle").setLanguage("xoo").setComponentKey("org.sonar.sample:Sample")
            .setComponentUuid("CDEF").setProjectUuid("GHIJ").setModuleUuid("BCDE")
            .setModuleUuidPath("ABCD.BCDE.").setProjectKey("org.sonar.sample").setStatus(Issue.STATUS_CLOSED)
            .setResolution(Issue.RESOLUTION_FALSE_POSITIVE).setGap(15.0).setEffort(10L).setLine(6)
            .setSeverity("BLOCKER").setMessage("message").setManualSeverity(true).setAssignee("perceval")
            .setIssueAttributes("key=value").setAuthorLogin("pierre").setIssueCreationDate(createdAt)
            .setIssueUpdateDate(updatedAt).setIssueCloseDate(closedAt);

    DefaultIssue issue = dto.toDefaultIssue();
    assertThat(issue.key()).isEqualTo("100");
    assertThat(issue.type()).isEqualTo(RuleType.VULNERABILITY);
    assertThat(issue.ruleKey().toString()).isEqualTo("squid:AvoidCycle");
    assertThat(issue.language()).isEqualTo("xoo");
    assertThat(issue.componentUuid()).isEqualTo("CDEF");
    assertThat(issue.projectUuid()).isEqualTo("GHIJ");
    assertThat(issue.componentKey()).isEqualTo("org.sonar.sample:Sample");
    assertThat(issue.moduleUuid()).isEqualTo("BCDE");
    assertThat(issue.moduleUuidPath()).isEqualTo("ABCD.BCDE.");
    assertThat(issue.projectKey()).isEqualTo("org.sonar.sample");
    assertThat(issue.status()).isEqualTo(Issue.STATUS_CLOSED);
    assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FALSE_POSITIVE);
    assertThat(issue.gap()).isEqualTo(15.0);
    assertThat(issue.effort()).isEqualTo(Duration.create(10L));
    assertThat(issue.line()).isEqualTo(6);
    assertThat(issue.severity()).isEqualTo("BLOCKER");
    assertThat(issue.message()).isEqualTo("message");
    assertThat(issue.manualSeverity()).isTrue();
    assertThat(issue.assignee()).isEqualTo("perceval");
    assertThat(issue.attribute("key")).isEqualTo("value");
    assertThat(issue.authorLogin()).isEqualTo("pierre");
    assertThat(issue.creationDate()).isEqualTo(DateUtils.truncate(createdAt, Calendar.SECOND));
    assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(updatedAt, Calendar.SECOND));
    assertThat(issue.closeDate()).isEqualTo(DateUtils.truncate(closedAt, Calendar.SECOND));
    assertThat(issue.isNew()).isFalse();
}