Author: vyemialyanchyk
Date: 2009-04-09 10:16:36 -0400 (Thu, 09 Apr 2009)
New Revision: 14636
Added:
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.veditor.test/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.veditor.test/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/test/
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.veditor.test/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/test/ExportImageActionTest.java
Modified:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/META-INF/MANIFEST.MF
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/ExportImageAction.java
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.veditor.test/META-INF/MANIFEST.MF
Log:
JBIDE-4148 & JBIDE-4138
Modified:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/META-INF/MANIFEST.MF
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/META-INF/MANIFEST.MF 2009-04-09
14:06:58 UTC (rev 14635)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/META-INF/MANIFEST.MF 2009-04-09
14:16:36 UTC (rev 14636)
@@ -20,6 +20,7 @@
Bundle-Version: 2.0.0
Export-Package: org.jboss.tools.hibernate.ui.veditor,
org.jboss.tools.hibernate.ui.veditor.editors,
+ org.jboss.tools.hibernate.ui.veditor.editors.actions,
org.jboss.tools.hibernate.ui.veditor.editors.command,
org.jboss.tools.hibernate.ui.veditor.editors.figures,
org.jboss.tools.hibernate.ui.veditor.editors.model,
Modified:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/ExportImageAction.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/ExportImageAction.java 2009-04-09
14:06:58 UTC (rev 14635)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/ExportImageAction.java 2009-04-09
14:16:36 UTC (rev 14636)
@@ -1,3 +1,13 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
package org.jboss.tools.hibernate.ui.veditor.editors.actions;
import java.io.ByteArrayOutputStream;
@@ -2,2 +12,3 @@
import java.io.FileOutputStream;
+import java.io.IOException;
@@ -25,8 +36,12 @@
public class ExportImageAction extends Action {
public static final String ACTION_ID = "Export as Image"; //$NON-NLS-1$
+ public static final String[] dialogFilterExtensions = new String[] { "*.png",
"*.jpg", "*.bmp" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ public static final String[] dialogFilterNames = new String[] {
UIVEditorMessages.ExportImageAction_png_format,
+ UIVEditorMessages.ExportImageAction_jpg_format,
UIVEditorMessages.ExportImageAction_bmp_format };
private VisualEditor editor;
+ private FileDialog saveDialog = null;
public ExportImageAction(VisualEditor editor) {
this.editor = editor;
@@ -35,17 +50,28 @@
setImageDescriptor(ImageDescriptor.createFromFile(
VisualEditor.class,"icons/export.png")); //$NON-NLS-1$
}
+
+ /**
+ * main goal of this function to allow tests for
+ * ExportImageAction functionality
+ *
+ * @param saveDialog
+ */
+ public void setSaveDialog(FileDialog saveDialog) {
+ this.saveDialog = saveDialog;
+ }
public void run() {
- FileDialog saveDialog = new FileDialog(
+ if (saveDialog == null) {
+ saveDialog = new FileDialog(
this.editor.getSite().getShell(), SWT.SAVE);
- saveDialog
- .setFilterExtensions(new String[] { "*.png", "*.jpg",
"*.bmp" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- saveDialog.setFilterNames(new String[] {
UIVEditorMessages.ExportImageAction_png_format,
- UIVEditorMessages.ExportImageAction_jpg_format,
UIVEditorMessages.ExportImageAction_bmp_format });
+ }
+ saveDialog.setFilterExtensions(dialogFilterExtensions);
+ saveDialog.setFilterNames(dialogFilterNames);
String filePath = saveDialog.open();
+ saveDialog = null;
if (filePath == null || filePath.trim().length() == 0) {
return;
}
@@ -53,24 +79,32 @@
IFigure fig = ((ScalableFreeformRootEditPart) this.editor
.getEditPartViewer().getRootEditPart())
.getLayer(LayerConstants.PRINTABLE_LAYERS);
+ int imageType = SWT.IMAGE_BMP;
+ if (filePath.toLowerCase().endsWith(".jpg")) { //$NON-NLS-1$
+ imageType = SWT.IMAGE_JPEG;
+ } else if (filePath.toLowerCase().endsWith(".png")) { //$NON-NLS-1$
+ imageType = SWT.IMAGE_PNG;
+ }
+ FileOutputStream outStream = null;
try {
- int imageType = SWT.IMAGE_BMP;
- if (filePath.toLowerCase().endsWith(".jpg")) { //$NON-NLS-1$
- imageType = SWT.IMAGE_JPEG;
- } else if (filePath.toLowerCase().endsWith(".png")) { //$NON-NLS-1$
- imageType = SWT.IMAGE_PNG;
- }
-
byte[] imageData = createImage(fig, imageType);
- FileOutputStream outStream = new FileOutputStream(filePath);
+ outStream = new FileOutputStream(filePath);
outStream.write(imageData);
outStream.flush();
- outStream.close();
- } catch (Throwable e) {
+ } catch (Exception e) {
MessageDialog.openInformation(this.editor.getSite().getShell(),
UIVEditorMessages.ExportImageAction_error,
UIVEditorMessages.ExportImageAction_failed_to_export_image + e.getMessage());
return;
}
+ finally {
+ if (outStream != null) {
+ try {
+ outStream.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
}
/***
Modified:
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.veditor.test/META-INF/MANIFEST.MF
===================================================================
---
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.veditor.test/META-INF/MANIFEST.MF 2009-04-09
14:06:58 UTC (rev 14635)
+++
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.veditor.test/META-INF/MANIFEST.MF 2009-04-09
14:16:36 UTC (rev 14636)
@@ -6,6 +6,7 @@
Require-Bundle: org.junit;bundle-version="3.8.2",
org.eclipse.wst.common.project.facet.ui;bundle-version="1.3.0",
org.eclipse.jdt.core,
+ org.eclipse.gef,
org.hibernate.eclipse.console.test,
org.jboss.tools.hibernate.ui.veditor,
org.hibernate.eclipse
Added:
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.veditor.test/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/test/ExportImageActionTest.java
===================================================================
---
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.veditor.test/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/test/ExportImageActionTest.java
(rev 0)
+++
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.veditor.test/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/test/ExportImageActionTest.java 2009-04-09
14:16:36 UTC (rev 14636)
@@ -0,0 +1,102 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hibernate.ui.veditor.editors.actions.test;
+
+import org.eclipse.draw2d.IFigure;
+import org.eclipse.draw2d.SWTGraphics;
+import org.eclipse.draw2d.geometry.Rectangle;
+import org.eclipse.gef.GraphicalViewer;
+import org.eclipse.gef.LayerConstants;
+import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
+import org.eclipse.swt.graphics.Device;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.FileDialog;
+import org.jboss.tools.hibernate.ui.veditor.editors.VisualEditor;
+import org.jboss.tools.hibernate.ui.veditor.editors.actions.ExportImageAction;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.lib.legacy.ClassImposteriser;
+
+import junit.framework.TestCase;
+
+/**
+ * for ExportImageAction class functionality test
+ *
+ * @author Vitali Yemialyanchyk
+ */
+public class ExportImageActionTest extends TestCase {
+
+ public Mockery context = new Mockery() {
+ {
+ setImposteriser(ClassImposteriser.INSTANCE);
+ }
+ };
+
+ public void testAction() {
+
+ final VisualEditor editor = context.mock(VisualEditor.class);;
+ final FileDialog saveDialog = context.mock(FileDialog.class);;
+ final GraphicalViewer graphicalViewer = context.mock(GraphicalViewer.class);;
+ final ScalableFreeformRootEditPart scalableFreeformRootEditPart =
context.mock(ScalableFreeformRootEditPart.class);;
+ final IFigure figure = context.mock(IFigure.class);;
+ final Control control = context.mock(Control.class);;
+ final Display display = context.mock(Display.class);;
+ final Rectangle rectangle = new Rectangle(0, 0, 20, 10);
+
+ context.checking(new Expectations() {
+ {
+ allowing(saveDialog).setFilterExtensions(ExportImageAction.dialogFilterExtensions);
+ allowing(saveDialog).setFilterNames(ExportImageAction.dialogFilterNames);
+
+ oneOf(saveDialog).open();
+ will(returnValue("test.jpg")); //$NON-NLS-1$
+
+ allowing(editor).getEditPartViewer();
+ will(returnValue(graphicalViewer));
+
+ allowing(graphicalViewer).getRootEditPart();
+ will(returnValue(scalableFreeformRootEditPart));
+
+ allowing(scalableFreeformRootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);
+ will(returnValue(figure));
+
+ allowing(graphicalViewer).getControl();
+ will(returnValue(control));
+
+ allowing(control).getDisplay();
+ will(returnValue(display));
+
+ allowing(figure).getBounds();
+ will(returnValue(rectangle));
+
+ allowing(display).internal_new_GC(null);
+ will(returnValue(0));
+
+ allowing(display).internal_dispose_GC(0, null);
+
+ oneOf(figure).paint(with(any(SWTGraphics.class)));
+
+ allowing(display).isDisposed();
+ will(returnValue(true));
+ }
+ });
+ final ExportImageAction exportImageAction = new ExportImageAction(editor);
+ exportImageAction.setSaveDialog(saveDialog);
+ exportImageAction.run();
+ // GENERAL TEST:
+ // check for all expectations
+ context.assertIsSatisfied();
+
+ }
+
+
+}