Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding refactoring listener and scoring

  • Loading branch information...
commit 415524c4977d0d7533006ac2edcb2056153295f7 1 parent c008c49
Sebastian Hermida authored
View
4 META-INF/MANIFEST.MF
@@ -8,6 +8,8 @@ Bundle-Vendor: HAPPYPROG
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.junit,
- org.eclipse.jdt.junit.core;bundle-version="3.6.1"
+ org.eclipse.jdt.junit.core,
+ org.eclipse.ltk.core.refactoring,
+ org.eclipse.core.resources;bundle-version="3.6.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
View
7 src/com/happyprog/tdgotchi/scoreboard/RefactoringObserver.java
@@ -0,0 +1,7 @@
+package com.happyprog.tdgotchi.scoreboard;
+
+public interface RefactoringObserver {
+
+ void onRefactoring();
+
+}
View
31 src/com/happyprog/tdgotchi/scoreboard/Scoreboard.java
@@ -4,36 +4,42 @@
import com.happyprog.tdgotchi.level.TinyLevelManager;
import com.happyprog.tdgotchi.subscriber.JUnitTestSubscriber;
+import com.happyprog.tdgotchi.subscriber.RefactoringSubscriber;
import com.happyprog.tdgotchi.subscriber.TestSubscriber;
+import com.happyprog.tdgotchi.subscriber.TinyRefactoringSubscriber;
import com.happyprog.tdgotchi.views.Tamagotchi;
import com.happyprog.tdgotchi.views.TinyTamagotchi;
import com.happyprog.tdgotchi.views.View;
-public class Scoreboard implements TestObserver, TamagotchiObserver {
+public class Scoreboard implements TestObserver, RefactoringObserver, TamagotchiObserver {
private final View view;
- private final TestSubscriber subscriber;
+ private final TestSubscriber testSubscriber;
+ private final RefactoringSubscriber refactoringSubscriber;
private final Tamagotchi tamagotchi;
+ private final LevelManager levels;
private TestRun previousTestRun;
private int score;
- private final LevelManager levels;
private enum TestRun {
PASS, FAIL
}
public Scoreboard(View view) {
- this(view, new TinyTamagotchi(), new JUnitTestSubscriber(), new TinyLevelManager());
+ this(view, new TinyTamagotchi(), new JUnitTestSubscriber(), new TinyRefactoringSubscriber(), new TinyLevelManager());
}
- public Scoreboard(View view, Tamagotchi tamagotchi, TestSubscriber subscriber, LevelManager levels) {
+ public Scoreboard(View view, Tamagotchi tamagotchi, TestSubscriber testSubscriber,
+ RefactoringSubscriber refactoringSubscriber, LevelManager levels) {
this.view = view;
this.tamagotchi = tamagotchi;
- this.subscriber = subscriber;
+ this.testSubscriber = testSubscriber;
+ this.refactoringSubscriber = refactoringSubscriber;
this.levels = levels;
subscribeToJUnitEvents();
+ subscribeToRefactoringEvents();
startTamagotchi(tamagotchi);
}
@@ -56,6 +62,12 @@ public void onFailingTest() {
}
@Override
+ public void onRefactoring() {
+ tamagotchi.beHappy();
+ updateScoreWith(1);
+ }
+
+ @Override
public void updateMood(Image image) {
view.setImage(image);
}
@@ -97,7 +109,11 @@ private void startTamagotchi(Tamagotchi tamagotchi) {
}
private void subscribeToJUnitEvents() {
- subscriber.subscribe(this);
+ testSubscriber.subscribe(this);
+ }
+
+ private void subscribeToRefactoringEvents() {
+ refactoringSubscriber.subscribe(this);
}
public void onImageSetCallback() {
@@ -107,4 +123,5 @@ public void onImageSetCallback() {
public Image getDefaultHealth() {
return levels.getBeginnerHealth();
}
+
}
View
9 src/com/happyprog/tdgotchi/subscriber/RefactoringSubscriber.java
@@ -0,0 +1,9 @@
+package com.happyprog.tdgotchi.subscriber;
+
+import com.happyprog.tdgotchi.scoreboard.RefactoringObserver;
+
+public interface RefactoringSubscriber {
+
+ void subscribe(RefactoringObserver observer);
+
+}
View
64 src/com/happyprog/tdgotchi/subscriber/TinyRefactoringSubscriber.java
@@ -0,0 +1,64 @@
+package com.happyprog.tdgotchi.subscriber;
+
+import java.util.Calendar;
+
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.ltk.core.refactoring.RefactoringCore;
+import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
+import org.eclipse.ltk.core.refactoring.history.IRefactoringHistoryService;
+import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
+import org.eclipse.ui.PlatformUI;
+
+import com.happyprog.tdgotchi.scoreboard.RefactoringObserver;
+
+public class TinyRefactoringSubscriber implements RefactoringSubscriber {
+ private static final NullProgressMonitor MONITOR = new NullProgressMonitor();
+ private static final int SINCE_INTERVAL = -1;
+ private static final int SECOND_INTERVAL = 1000;
+ private RefactoringObserver observer;
+ private final IRefactoringHistoryService refactoringHistoryService;
+
+ public TinyRefactoringSubscriber() {
+ this(RefactoringCore.getHistoryService());
+ }
+
+ public TinyRefactoringSubscriber(IRefactoringHistoryService refactoringHistoryService) {
+ this.refactoringHistoryService = refactoringHistoryService;
+ }
+
+ @Override
+ public void subscribe(RefactoringObserver observer) {
+ this.observer = observer;
+ startListener();
+ }
+
+ void extractRefactoringHistory() {
+ refactoringHistoryService.connect();
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.add(Calendar.SECOND, SINCE_INTERVAL);
+ long timeInterval = calendar.getTimeInMillis();
+ long now = Calendar.getInstance().getTimeInMillis();
+
+ RefactoringHistory history = refactoringHistoryService.getWorkspaceHistory(timeInterval, now, MONITOR);
+
+ RefactoringDescriptorProxy[] descriptors = history.getDescriptors();
+ for (int i = 0; i < descriptors.length; i++) {
+ observer.onRefactoring();
+ }
+
+ refactoringHistoryService.disconnect();
+ }
+
+ void startListener() {
+ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay().asyncExec(new RefactoringRunnable());
+ }
+
+ class RefactoringRunnable implements Runnable {
+ @Override
+ public void run() {
+ extractRefactoringHistory();
+ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay().timerExec(SECOND_INTERVAL, this);
+ }
+ }
+}
View
31 test/com/happyprog/tdgotchi/scoreboard/ScoreboardTest.java
@@ -11,13 +11,15 @@
import com.happyprog.tdgotchi.level.Intermediate;
import com.happyprog.tdgotchi.level.Pro;
import com.happyprog.tdgotchi.level.Zombie;
+import com.happyprog.tdgotchi.subscriber.RefactoringSubscriber;
import com.happyprog.tdgotchi.subscriber.TestSubscriber;
import com.happyprog.tdgotchi.views.Tamagotchi;
import com.happyprog.tdgotchi.views.View;
public class ScoreboardTest {
- private TestSubscriber subscriber;
+ private TestSubscriber testSubscriber;
+ private RefactoringSubscriber refactoringSubscriber;
private Tamagotchi tamagotchi;
private View view;
@@ -28,7 +30,8 @@
public void before() {
view = mock(View.class);
tamagotchi = mock(Tamagotchi.class);
- subscriber = mock(TestSubscriber.class);
+ testSubscriber = mock(TestSubscriber.class);
+ refactoringSubscriber = mock(RefactoringSubscriber.class);
levels = mock(LevelManager.class);
when(levels.getZombie()).thenReturn(new Zombie());
@@ -36,12 +39,17 @@ public void before() {
when(levels.getIntermediate()).thenReturn(new Intermediate());
when(levels.getPro()).thenReturn(new Pro());
- scoreboard = new Scoreboard(view, tamagotchi, subscriber, levels);
+ scoreboard = new Scoreboard(view, tamagotchi, testSubscriber, refactoringSubscriber, levels);
}
@Test
public void subscribesToTestEvents() throws Exception {
- verify(subscriber).subscribe(scoreboard);
+ verify(testSubscriber).subscribe(scoreboard);
+ }
+
+ @Test
+ public void subscribesToRefactoringEvents() throws Exception {
+ verify(refactoringSubscriber).subscribe(scoreboard);
}
@Test
@@ -84,6 +92,21 @@ public void onGreenToRed_tamagotchiDoesNotChangeMood() throws Exception {
}
@Test
+ public void onRefactoring_tamagotchiIsHappy() throws Exception {
+ scoreboard.onRefactoring();
+
+ verify(tamagotchi).beHappy();
+ }
+
+ @Test
+ public void onRefactoring_tamagotchiScoresOnePoint() throws Exception {
+ scoreboard.onRefactoring();
+
+ // times 2, since the level is set to beginner when the game starts
+ verify(tamagotchi, times(2)).setLevel(isA(Beginner.class));
+ }
+
+ @Test
public void ifScoreLessThanZero_tamagotchiLevelIsZombie() throws Exception {
scoreboard.onFailingTest();
scoreboard.onFailingTest();
View
82 test/com/happyprog/tdgotchi/subscriber/TinyRefactoringSubscriberTest.java
@@ -0,0 +1,82 @@
+package com.happyprog.tdgotchi.subscriber;
+
+import static org.mockito.Matchers.*;
+import static org.mockito.Mockito.*;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
+import org.eclipse.ltk.core.refactoring.history.IRefactoringHistoryService;
+import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.happyprog.tdgotchi.scoreboard.RefactoringObserver;
+
+public class TinyRefactoringSubscriberTest {
+
+ private RefactoringObserver observer;
+ private IRefactoringHistoryService historyService;
+ private RefactoringHistory refactoringHistory;
+ private StubbedRefactorSubscriber refactorSubscriber;
+
+ @Before
+ public void before() {
+ observer = mock(RefactoringObserver.class);
+ historyService = mock(IRefactoringHistoryService.class);
+ refactoringHistory = mock(RefactoringHistory.class);
+ refactorSubscriber = new StubbedRefactorSubscriber(historyService);
+ }
+
+ @Test
+ public void connectsAndDisconnectToRefactoringService() throws Exception {
+ when(historyService.getWorkspaceHistory(anyLong(), anyLong(), isA(IProgressMonitor.class))).thenReturn(
+ refactoringHistory);
+
+ RefactoringDescriptorProxy[] proxy = {};
+ when(refactoringHistory.getDescriptors()).thenReturn(proxy);
+
+ refactorSubscriber.extractRefactoringHistory();
+
+ verify(historyService).connect();
+ verify(historyService).disconnect();
+ }
+
+ @Test
+ public void doesNotNotifyObserverIfNoRefactoringHasHappened() throws Exception {
+ when(historyService.getWorkspaceHistory(anyLong(), anyLong(), isA(IProgressMonitor.class))).thenReturn(
+ refactoringHistory);
+
+ RefactoringDescriptorProxy[] proxy = {};
+ when(refactoringHistory.getDescriptors()).thenReturn(proxy);
+
+ refactorSubscriber.subscribe(observer);
+ refactorSubscriber.extractRefactoringHistory();
+
+ verify(observer, never()).onRefactoring();
+ }
+
+ @Test
+ public void notifiesObserverIfARefactorHappened() throws Exception {
+ when(historyService.getWorkspaceHistory(anyLong(), anyLong(), isA(IProgressMonitor.class))).thenReturn(
+ refactoringHistory);
+
+ RefactoringDescriptorProxy[] proxy = { mock(RefactoringDescriptorProxy.class) };
+ when(refactoringHistory.getDescriptors()).thenReturn(proxy);
+
+ refactorSubscriber.subscribe(observer);
+ refactorSubscriber.extractRefactoringHistory();
+
+ verify(observer).onRefactoring();
+ }
+
+ class StubbedRefactorSubscriber extends TinyRefactoringSubscriber {
+ public StubbedRefactorSubscriber(IRefactoringHistoryService historyService) {
+ super(historyService);
+ }
+
+ @Override
+ void startListener() {
+ // Do nothing on purpose to avoid eclipse async to kick in
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.