Skip to content

Commit

Permalink
BZ-1211257 - Comments always show the 'current' time
Browse files Browse the repository at this point in the history
(cherry picked from commit 7a51dcd)
  • Loading branch information
wmedvede authored and csadilek committed Oct 23, 2015
1 parent d29dfde commit dc76ebe
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 25 deletions.
@@ -0,0 +1,53 @@
/*
* Copyright 2014 JBoss Inc
*
* 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.
*/

package org.kie.workbench.common.widgets.client.discussion;

import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import org.guvnor.common.services.shared.metadata.model.DiscussionRecord;
import org.kie.workbench.common.widgets.client.util.DateFormatHelper;

public class CommentLinePresenter
implements IsWidget,
CommentLineView.Presenter {

private CommentLineView view;

public CommentLinePresenter() {
this( new CommentLineViewImpl() );
}

public CommentLinePresenter( CommentLineView view ) {
this.view = view;
view.setPresenter( this );
}

protected String formatTimestamp( long timestamp ) {
return DateFormatHelper.shortFormat( timestamp );
}

@Override
public Widget asWidget() {
return view.asWidget();
}

public void setRecord( DiscussionRecord record ) {
view.setAuthor( record.getAuthor() + ":" );
view.setComment( "\"" + record.getNote() + "\"" );
view.setDate( formatTimestamp( record.getTimestamp() ) );
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2015 JBoss Inc
*
* 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.
*/

package org.kie.workbench.common.widgets.client.discussion;

import com.google.gwt.user.client.ui.IsWidget;

public interface CommentLineView
extends IsWidget {

interface Presenter {

}

void setPresenter( Presenter presenter );

void setAuthor( String author );

void setComment( String comment );

void setDate( String date );

}
@@ -1,5 +1,5 @@
/*
* Copyright 2014 JBoss Inc
* Copyright 2015 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,33 +16,23 @@

package org.kie.workbench.common.widgets.client.discussion;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import org.guvnor.common.services.shared.metadata.model.DiscussionRecord;

public class CommentLine
extends Composite
implements IsWidget {
public class CommentLineViewImpl extends Composite
implements CommentLineView {

interface Binder
extends
UiBinder<Widget, CommentLine> {
UiBinder<Widget, CommentLineViewImpl> {

}

private static Binder uiBinder = GWT.create(Binder.class);

@UiField
FocusPanel base;
private static Binder uiBinder = GWT.create( Binder.class );

@UiField
Label author;
Expand All @@ -53,14 +43,29 @@ interface Binder
@UiField
Label comment;

public CommentLine(DiscussionRecord record) {
initWidget(uiBinder.createAndBindUi(this));
private Presenter presenter;

public CommentLineViewImpl( ) {
initWidget( uiBinder.createAndBindUi( this ) );
}

public void setPresenter( Presenter presenter ) {
this.presenter = presenter;
}

@Override
public void setAuthor( String author ) {
this.author.setText( author );
}

this.author.setText(record.getAuthor() + ":");
this.comment.setText("\"" + record.getNote() + "\"");
this.date.setText(
DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_TIME_SHORT).format(
// new Date(record.getTimestamp())
new Date()));
@Override
public void setComment( String comment ) {
this.comment.setText( comment );
}

@Override
public void setDate( String date ) {
this.date.setText( date );
}
}

Expand Up @@ -63,9 +63,10 @@ public void setPresenter( Presenter presenter ) {
this.presenter = presenter;
}

@Override
public void addRow( DiscussionRecord line ) {
lines.add( new CommentLine( line ) );
CommentLinePresenter commentLine = new CommentLinePresenter( );
commentLine.setRecord( line );
lines.add( commentLine );
}

@Override
Expand Down
@@ -0,0 +1,33 @@
/*
* Copyright 2015 JBoss Inc
*
* 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.
*/

package org.kie.workbench.common.widgets.client.util;

import java.util.Date;

import com.google.gwt.i18n.client.DateTimeFormat;

public class DateFormatHelper {

public static String shortFormat( long timestamp ) {
return format( timestamp, DateTimeFormat.PredefinedFormat.DATE_TIME_SHORT );
}

public static String format( long timestamp, DateTimeFormat.PredefinedFormat format ) {
return DateTimeFormat.getFormat( format ).format(
new Date( timestamp ) );
}
}
@@ -0,0 +1,81 @@
/*
* Copyright 2015 JBoss Inc
*
* 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.
*/

package org.kie.workbench.common.widgets.client.discussion;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.guvnor.common.services.shared.metadata.model.DiscussionRecord;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.*;

@RunWith( MockitoJUnitRunner.class )
public class CommentLinePresenterTest {

@Mock
private CommentLineView view;

@InjectMocks
CommentLinePresenter presenter;

@Test
public void testVisualization() {

CommentLineView view = mock( CommentLineView.class );
CommentLinePresenter presenter = new CommentLinePresenterWithNOGWTCode( view );

Date commentDate = new Date( );
DiscussionRecord record = new DiscussionRecord( commentDate.getTime(), "test user", "test note" );
presenter.setRecord( record );

verify( view, times( 1 ) ).setAuthor( eq ( expectedAuthorFormat( "test user" ) ) );
verify( view, times( 1 ) ).setComment( eq( expectedCommentFormat( "test note" ) ) );
verify( view, times( 1 ) ).setDate( expectedDateFormat( commentDate.getTime() ) );
}

private String expectedDateFormat( long timestamp ) {
//expected format to see in the UI -> "2015-10-21 12:09"
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
return sdf.format( new Date( timestamp ) );
}

private String expectedCommentFormat( String comment ) {
return "\"" + comment + "\"";
}

private String expectedAuthorFormat( String author ) {
return author + ":";
}

private class CommentLinePresenterWithNOGWTCode extends CommentLinePresenter {

public CommentLinePresenterWithNOGWTCode( CommentLineView view ) {
super( view );
}

@Override
protected String formatTimestamp( long timestamp ) {
//override method to avoid GWT invocation during test
return expectedDateFormat( timestamp );
}
}
}

0 comments on commit dc76ebe

Please sign in to comment.