Permalink
Browse files

Harleigh108: Changes to MCT such that MCT can be built (successfully …

…with Maven on the command line) with respect to Java 6 or Java 7
  • Loading branch information...
1 parent a5905de commit 5014da47641cd800368eee86b42f7935a41b89cb @harleigh harleigh committed Aug 12, 2013
@@ -12,6 +12,7 @@
private XYPlotLine line;
+ @SuppressWarnings("serial")//Harleigh108: this removes the warning received when we build with respect to java 7:for XYPlotLine
protected void setUp() {
line = new XYPlotLine() {
@Override
@@ -1,34 +1,42 @@
-/*******************************************************************************
- * Mission Control Technologies, Copyright (c) 2009-2012, United States Government
- * as represented by the Administrator of the National Aeronautics and Space
- * Administration. All rights reserved.
- *
- * The MCT platform is 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.
- *
- * MCT includes source code licensed under additional open source licenses. See
- * the MCT Open Source Licenses file included with this distribution or the About
- * MCT Licenses dialog available at runtime from the MCT Help menu for additional
- * information.
- *******************************************************************************/
+/*******************************************************************************
+ * Mission Control Technologies, Copyright (c) 2009-2012, United States Government
+ * as represented by the Administrator of the National Aeronautics and Space
+ * Administration. All rights reserved.
+ *
+ * The MCT platform is 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.
+ *
+ * MCT includes source code licensed under additional open source licenses. See
+ * the MCT Open Source Licenses file included with this distribution or the About
+ * MCT Licenses dialog available at runtime from the MCT Help menu for additional
+ * information.
+ *******************************************************************************/
package gov.nasa.arc.mct.canvas.view;
import gov.nasa.arc.mct.canvas.panel.Panel;
+import gov.nasa.arc.mct.gui.MCTViewManifestationInfoImpl;
import gov.nasa.arc.mct.canvas.view.MarqueSelectionListener.MultipleSelectionProvider;
+import gov.nasa.arc.mct.canvas.view.PanelBorderSelectionTest.MockTitleManifestation;
+import gov.nasa.arc.mct.components.AbstractComponent;
+import gov.nasa.arc.mct.gui.View;
+import gov.nasa.arc.mct.services.component.ViewInfo;
+import gov.nasa.arc.mct.services.component.ViewType;
+import java.awt.Color;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.Collection;
+import java.util.Collections;
import javax.swing.JPanel;
@@ -44,6 +52,15 @@
@Mock
MultipleSelectionProvider mockSelectionProvider;
+ /*
+ * Harleigh108: these two variables we need for fixing this test, so that it can be run with
+ * respect to Java7 (as well as still work in Java6)
+ */
+ private View panelManifestation = null;
+ @Mock
+ private AbstractComponent mockComponent;
+ //End new variables--Harleigh108
+
private JPanel rootPanel;
private MarqueSelectionListener listener;
@@ -54,6 +71,13 @@ public void setup() {
MockitoAnnotations.initMocks(this);
rootPanel = new JPanel();
listener = new MarqueSelectionListener(rootPanel, mockSelectionProvider);
+
+
+ ///////Harleigh108
+ Mockito.when(mockComponent.getViewInfos(ViewType.TITLE)).thenReturn(Collections.singleton(new ViewInfo(MockTitleManifestation.class,"", ViewType.TITLE)));
+ Mockito.when(mockComponent.getDisplayName()).thenReturn("test comp");
+ Mockito.when(mockComponent.getComponents()).thenReturn(Collections.<AbstractComponent> emptyList());
+ //
}
@@ -141,13 +165,39 @@ public void testMouseDraggedStart(Rectangle expectedBounds, Point mouseClicked)
Assert.assertEquals(rootPanel.getComponents()[0].getBounds(),expectedBounds);
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({ "unchecked", "serial" })
@Test
public void testMouseDragIncremental() {
JPanel panel = new JPanel();
- Panel child = Mockito.mock(Panel.class);
- Mockito.when(child.getBounds()).thenReturn(new Rectangle(2,2,1,1));
+
+ /*
+ * Harleigh108: MarqueSelectionListenerTest was failing when we built with respect to Java 7, the root cause (pun intended)
+ * was that we were adding a mocked panel to the rootPanel. As it turns out 'Container.add(mock(Container.class)) ' fails
+ * in Java 7 but works in Java 6. Why? Well, java.awt.Container.addImpl has changed between Java 6 and 7.
+ * The Solution: instead of adding a mock component (using Mockito), we have instead made a real View object when creating
+ * a panel. Also, we used to have Mockito.when(child.getBounds()).thenReturn(new Rectangle(2,2,1,1)); but because of
+ * the java 6-to-7 issue, we chose to make an abstract class here and override the getBounds method.
+ * The old code was beautiful:
+ * Panel child = Mockito.mock(Panel.class);
+ * Mockito.when(child.getBounds()).thenReturn(new Rectangle(2,2,1,1));
+ * rootPanel.add(child);
+ * But now we have the following:
+ */
+ View v = new View(){};
+ v.putClientProperty(CanvasManifestation.MANIFEST_INFO, new MCTViewManifestationInfoImpl() );
+
+ panelManifestation = new MockManifestation(mockComponent, new ViewInfo(CanvasManifestation.class, "", ViewType.OBJECT));
+ MCTViewManifestationInfoImpl info = new MCTViewManifestationInfoImpl();
+ panelManifestation.putClientProperty(CanvasManifestation.MANIFEST_INFO, info);
+
+ Panel child = new Panel(panelManifestation,Mockito.mock(PanelFocusSelectionProvider.class) ) {
+ public Rectangle getBounds() {
+ return new Rectangle(2,2,1,1);
+ }
+ };
+
rootPanel.add(child);
+
MouseEvent e =
new MouseEvent(panel,123,System.currentTimeMillis(),0,2,2,1,false);
Mockito.when(mockSelectionProvider.pointInTopLevelPanel((Point)Mockito.anyObject())).thenReturn(true);
@@ -156,4 +206,17 @@ public void testMouseDragIncremental() {
Mockito.verify(mockSelectionProvider).selectPanels((Collection<Panel>)Mockito.anyObject());
}
+
+ /* Harleigh108:
+ * This is a little helper class so I can make a mock-view component without using Mockito (see method 'testMouseDragIncremental'
+ * above for more detail.
+ */
+ @SuppressWarnings("serial")
+ public static class MockManifestation extends View {
+ public MockManifestation(AbstractComponent component, ViewInfo vi) {
+ super(component,vi);
+ this.setBackground(Color.GREEN);
+ }
+ }
+
}
@@ -1,33 +1,38 @@
-/*******************************************************************************
- * Mission Control Technologies, Copyright (c) 2009-2012, United States Government
- * as represented by the Administrator of the National Aeronautics and Space
- * Administration. All rights reserved.
- *
- * The MCT platform is 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.
- *
- * MCT includes source code licensed under additional open source licenses. See
- * the MCT Open Source Licenses file included with this distribution or the About
- * MCT Licenses dialog available at runtime from the MCT Help menu for additional
- * information.
- *******************************************************************************/
+/*******************************************************************************
+ * Mission Control Technologies, Copyright (c) 2009-2012, United States Government
+ * as represented by the Administrator of the National Aeronautics and Space
+ * Administration. All rights reserved.
+ *
+ * The MCT platform is 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.
+ *
+ * MCT includes source code licensed under additional open source licenses. See
+ * the MCT Open Source Licenses file included with this distribution or the About
+ * MCT Licenses dialog available at runtime from the MCT Help menu for additional
+ * information.
+ *******************************************************************************/
package gov.nasa.arc.mct.canvas.view;
+import gov.nasa.arc.mct.canvas.view.PanelBorderSelectionTest.MockTitleManifestation;
import gov.nasa.arc.mct.components.AbstractComponent;
+import gov.nasa.arc.mct.gui.MCTViewManifestationInfoImpl;
import gov.nasa.arc.mct.gui.View;
import gov.nasa.arc.mct.services.component.ViewInfo;
+import gov.nasa.arc.mct.services.component.ViewType;
+import java.awt.Color;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.Collections;
import javax.swing.JComponent;
import javax.swing.JPanel;
@@ -43,15 +48,53 @@
private PanelInspector panelInspector;
+ /*
+ * Harleigh108: The following two variables we need for fixing this test, so that it can be run with
+ * respect to Java7 (as well as still work in Java6). Object 'view' used to be Mocked, but
+ * this causes test 'testEnterLockedState()' to fail when built with respect to Java 7.
+ */
+ private View view = null;
@Mock
- private View view;
+ private AbstractComponent mockComponent;
Method showHideControllerMethod;
@SuppressWarnings("serial")
@BeforeClass
public void setup() {
MockitoAnnotations.initMocks(this);
+
+ /*Harleigh108:
+ * Rather than mocking a view, we create one with dummy-content, this is because adding a mocked container to a container
+ * will cause a NPE. See testMouseDragIncremental in MarqueSelectionListenerTest.java
+ */
+ view = new MockManifestation(mockComponent, new ViewInfo(CanvasManifestation.class, "", ViewType.OBJECT)) {
+ /*
+ * Originally, in 'testLockedState()' we had Mockito.verify(view, Mockito.times(1)).exitLockedState(), that is we verified
+ * that the method 'exitLockedState()' is called exactly once during the test 'testLockedState()'. Since we can not use
+ * mock-types (see comments in 'testLockedState()') we implemented the following instead:
+ */
+ private int NumberCalls = 0; //Number of calls to method exitLockedState
+
+ @Override
+ public void exitLockedState() {
+ NumberCalls= NumberCalls +1;
+ super.exitLockedState();
+ }
+
+ @Override
+ public int getNumberCallsToExitLockedState() {
+ return NumberCalls;
+ }
+
+ };//--end class
+
+ MCTViewManifestationInfoImpl info = new MCTViewManifestationInfoImpl();
+ view.putClientProperty(CanvasManifestation.MANIFEST_INFO, info);
+
+ Mockito.when(mockComponent.getViewInfos(ViewType.TITLE)).thenReturn(Collections.singleton(new ViewInfo(MockTitleManifestation.class,"", ViewType.TITLE)));
+ Mockito.when(mockComponent.getDisplayName()).thenReturn("test comp");
+ Mockito.when(mockComponent.getComponents()).thenReturn(Collections.<AbstractComponent> emptyList());
AbstractComponent ac = Mockito.mock(AbstractComponent.class);
ViewInfo vi = Mockito.mock(ViewInfo.class);
@@ -91,14 +134,28 @@ protected JComponent getViewControls() {
@Test
public void testEnterLockedState() {
try {
+
// set canvas locked state (isLocked) to true
- Field cf = PanelInspector.class.getDeclaredField("isLocked");
- cf.setAccessible(true);
+ Field cf = PanelInspector.class.getDeclaredField("isLocked");
+ cf.setAccessible(true);
cf.set(panelInspector, Boolean.TRUE);
-
+
+ /* Harleigh108
+ * In Java 7 we can not add mocked things to a container. In this case: in method 'showOrHideController' in PanelInspector.java
+ * we received a NullPointerException when we added the view to the scroll_pane (i.e. scrollPane.setViewportView(view);). We got
+ * the NPE since 'view' was originally a Mock--google 'Container.add(mock(Container.class)) fails in Java 7'. The fix was to
+ * create a real View object rather than a mock one. Why all the NPEs? java.awt.Container.addImp has been changed between Java 6 and Java 7.
+ */
showHideControllerMethod.invoke(panelInspector, true);
+
+ System.out.println("The number of times exitLockedState() has been called: " + ((MockManifestation)view).getNumberCallsToExitLockedState());
+
+ //Harleigh108:
+ /* During the test for LockedState, we only want the method 'exitLockedState' to be called once, originally we had
+ * Mockito.verify(view, Mockito.times(1)).exitLockedState(); but as we no longer have a mock-type, we have the following instead.
+ */
+ Assert.assertEquals(((MockManifestation)view).getNumberCallsToExitLockedState(), 1);
- Mockito.verify(view, Mockito.times(1)).exitLockedState();
} catch (SecurityException e) {
Assert.fail(e.getMessage(), e);
} catch (NoSuchFieldException e) {
@@ -109,8 +166,29 @@ public void testEnterLockedState() {
Assert.fail(e.getMessage(), e);
} catch (InvocationTargetException e) {
Assert.fail(e.getMessage(), e);
+ } catch (AssertionError e) {
+ Assert.fail(e.getMessage(), e);
}
}
+
+ /* Harleigh108:
+ * This is a little helper class so I can make a mock-view component without using Mockito (see method 'testMouseDragIncremental'
+ * above for more detail.
+ */
+ @SuppressWarnings("serial")
+ public static class MockManifestation extends View {
+ public MockManifestation(AbstractComponent component, ViewInfo vi) {
+ super(component,vi);
+ this.setBackground(Color.GREEN);
+ }
+
+ public int getNumberCallsToExitLockedState() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ }//End MockManifestation class
+
}
@@ -264,6 +264,10 @@ public void removeSelectionChangeListener(PropertyChangeListener listener) {
removePropertyChangeListener(SelectionProvider.SELECTION_CHANGED_PROP, listener);
}
+ //Harleigh108: this removes the warning received when we build with respect to java 7; getSelectedValues is now Deprecated in java7
+ //from java7 javadocs (for JList) 'As of JDK 1.7, replaced by getSelectedValuesList()'
+ //Note: we supress this warning to stay compatable with java6
+ @SuppressWarnings("deprecation")
@Override
public Collection<View> getSelectedManifestations() {
Object[] selectedValues = list.getSelectedValues();
@@ -25,6 +25,7 @@ public void testPaint() {
truncatingLabel.setText(TEXT);
plainLabel.setText(TEXT);
+ @SuppressWarnings("serial")//Harleigh108: this removes the warning received when we build with respect to java 7:for JPanel
JPanel testPanel = new JPanel() {
public int getWidth() {
return width;
@@ -134,7 +134,8 @@ public void testCheckBaseComponentRequirements() {
};
}
- @SuppressWarnings("rawtypes")
+ //Harleigh108: this removes the warning received when we build with respect to java 7: deprecation is for PREFERRED_VIEW
+ @SuppressWarnings({ "rawtypes", "deprecation" })
@Test(dataProvider="viewInfoData")
public void testGetViewInfos(Set<ViewInfo> viewInfos, final Set<ViewInfo> filterOut, ViewType type, Set<ViewInfo> expected) {
AbstractComponent ac = new BaseComponentSub2();
Oops, something went wrong.

0 comments on commit 5014da4

Please sign in to comment.