isac-java/src/java-tests/isac/gui/mawen/TestWorksheetForMawen.java
author mmahringer <s1520454056@students.fh-hagenberg.at>
Tue, 21 Nov 2017 08:42:47 +0100
changeset 5221 abfba9a07261
parent 5101 82e74b46e4fb
child 5223 ba3233a38b4d
permissions -rw-r--r--
prep Testcode for demo \label{UC:user-guide}
     1 /*
     2  * @author Natalie Karl
     3  * Copyright (c) 2015 by Natalie Karl
     4  * FH Hagenberg, Software Engineering 
     5  * 
     6  * Use is subject to license terms.
     7  */
     8 package isac.gui.mawen;
     9 
    10 import static isac.gui.IGuiConstants.WINDOW_APPLICATION_INITIAL_SIZE_X;
    11 import static isac.gui.IGuiConstants.WINDOW_APPLICATION_INITIAL_SIZE_Y;
    12 import static isac.gui.IGuiConstants.WORKSHEET_DESKTOP_PANE_INITIAL_SIZE_X;
    13 import static isac.gui.IGuiConstants.WORKSHEET_DESKTOP_PANE_INITIAL_SIZE_Y;
    14 import static isac.gui.IGuiConstants.WORKSHEET_FRAME_INITIAL_OFFSET_Y;
    15 import static isac.gui.IGuiConstants.WORKSHEET_FRAME_INITIAL_SIZE_X;
    16 import isac.gui.IToWorksheetUser;
    17 import isac.gui.Worksheet;
    18 import isac.interfaces.ICalcIterator;
    19 import isac.useractions.EUIElement;
    20 import isac.useractions.IUIAction;
    21 import isac.util.CalcChanged;
    22 import isac.util.formulae.CalcFormula;
    23 import isac.util.formulae.Position;
    24 import isac.wsdialog.IWorksheetDialog;
    25 
    26 import java.awt.BorderLayout;
    27 import java.awt.Container;
    28 import java.awt.Cursor;
    29 import java.awt.Dimension;
    30 import java.awt.event.WindowAdapter;
    31 import java.awt.event.WindowEvent;
    32 import java.awt.event.WindowListener;
    33 import java.beans.PropertyVetoException;
    34 import java.rmi.RemoteException;
    35 import java.util.HashMap;
    36 import java.util.Vector;
    37 
    38 import javax.swing.JButton;
    39 import javax.swing.JFrame;
    40 import javax.swing.JDesktopPane;
    41 import javax.swing.JInternalFrame;
    42 import javax.swing.JMenuBar;
    43 import javax.swing.plaf.basic.BasicInternalFrameUI;
    44 import javax.swing.SwingUtilities;
    45 
    46 import junit.framework.TestCase;
    47 
    48 /**
    49  * @author Natalie Karl
    50  * 
    51  *         With a minimum of code present the terms of the example for reference
    52  *         on screen.
    53  * 
    54  *         <code>Worksheet</code> is launched by
    55  *         <code>WindowApplication#openNewWorksheet</code>, which involves a
    56  *         <code>IWorksheetDialog</code>, which in turn involves RMI (the
    57  *         <code>WorksheetDialog</code> runs in the <code>ObjectManager</code>).
    58  * 
    59  *         We don't want RMI in this TestCase and modify code of
    60  *         <code>WindowApplication#openNewWorksheet</code> such that RMI is
    61  *         excluded.
    62  * 
    63  */
    64 public class TestWorksheetForMawen extends TestCase implements IToWorksheetUser 
    65 {
    66 
    67 	private static JFrame frame_;
    68 	private JDesktopPane desktop_pane_;
    69 	protected static Worksheet currentSession;
    70 
    71 	@SuppressWarnings("unused") // is used below; deletion causes error
    72 	private HashMap<JButton, EUIElement> map_buttons_actions_;
    73 	@SuppressWarnings("unused") // is used below; deletion causes error
    74 	private HashMap<EUIElement, JButton> map_actions_buttons_;
    75 	private JMenuBar menu_bar_;
    76 	@SuppressWarnings("unused") // is used below; deletion causes error
    77 	private int num_browser_frames_;
    78 	@SuppressWarnings("unused") // is used below; deletion causes error
    79 	private boolean menu_separator_added_;
    80 
    81 	// from "implements IToWorksheetUser"
    82 	@Override
    83 	public void addToMenuBar(IUIAction action) {
    84 	}
    85 
    86 	@Override
    87 	public boolean removeFromMenuBar(IUIAction action) {
    88 		return false;
    89 	}
    90 
    91 	@Override
    92 	public void addCloseListener(Worksheet ws) {
    93 	}
    94 
    95 	@Override
    96 	public void removeCloseListener(Worksheet ws) {
    97 	}
    98 
    99 	// ======= copies from constructor of WindowApplication
   100 	private JFrame constructorWindowApplication() {
   101 		menu_separator_added_ = false;
   102 
   103 		frame_ = new JFrame();
   104 		frame_.setSize(new Dimension(WINDOW_APPLICATION_INITIAL_SIZE_X,
   105 				WINDOW_APPLICATION_INITIAL_SIZE_Y));
   106 		// locate frame in the middle of the screen
   107 		frame_.setLocationRelativeTo(null);
   108 
   109 		// initialize hashMaps
   110 		map_buttons_actions_ = new HashMap<JButton, EUIElement>();
   111 		map_actions_buttons_ = new HashMap<EUIElement, JButton>();
   112 
   113 		menu_bar_ = new JMenuBar();
   114 		frame_.setJMenuBar(menu_bar_);
   115 		num_browser_frames_ = 0;
   116 
   117 		this.createWorksheetPane();
   118 		frame_.add(desktop_pane_);
   119 		JFrame.setDefaultLookAndFeelDecorated(true);
   120 
   121 		frame_.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
   122 		frame_.setVisible(true);
   123 		return frame_;
   124 	}
   125 
   126 	// ------ methods from WindowApplication
   127 	private static WindowListener windowListener = new WindowAdapter() {
   128 
   129 		public void windowClosing(WindowEvent w) {
   130 			frame_.setVisible(false);
   131 			frame_.dispose();
   132 			System.exit(0);
   133 		}
   134 	};
   135 
   136 	protected static void startApplication(/* final JFrame windowApplication */) {
   137 		SwingUtilities.invokeLater(new Runnable() { // Start the application
   138 					public void run() {
   139 						frame_.addWindowListener(windowListener);
   140 					}
   141 				});
   142 	}
   143 
   144 	public void createWorksheetPane() {
   145 		desktop_pane_ = new JDesktopPane();
   146 		Dimension d = new Dimension();
   147 		d.setSize(frame_.getWidth() * 0.60, 0.0);
   148 		desktop_pane_.setMinimumSize(d);
   149 		desktop_pane_.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
   150 	}
   151 
   152 	private void addInternalFrame(JInternalFrame frame) {
   153 		desktop_pane_.add(frame);
   154 		desktop_pane_.getDesktopManager().activateFrame(frame);
   155 		frame.toFront();
   156 		frame.grabFocus();
   157 	}
   158 
   159 	private JInternalFrame createInternalFrame(String title, boolean resizable,
   160 			boolean closable, boolean maximizable, boolean iconifiable) {
   161 		JInternalFrame internalFrame = new JInternalFrame(title, resizable,
   162 				closable, maximizable, iconifiable);
   163 		return internalFrame;
   164 	}
   165 
   166 	private void openNewWorksheet(IWorksheetDialog dialog)
   167 			throws RemoteException {
   168 		// InternalFrame, holding the Frames for the Worksheet and the
   169 		// corresponding CalcHeadPanel
   170 		JInternalFrame internalFrame = createInternalFrame("Worksheet", true,
   171 				true, false, false);
   172 		internalFrame.setResizable(true);
   173 		internalFrame.setLayout(new BorderLayout());
   174 
   175 		// DesktopPane, added to internalFrame to be able to handle multiple
   176 		// InternalFrames in this InternalFrame
   177 		final JDesktopPane desktop = new JDesktopPane();
   178 		desktop.setSize(WORKSHEET_DESKTOP_PANE_INITIAL_SIZE_X,
   179 				WORKSHEET_DESKTOP_PANE_INITIAL_SIZE_Y);
   180 
   181 		desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
   182 		internalFrame.setContentPane(desktop); // , BorderLayout.CENTER);
   183 
   184 		frame_.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
   185 
   186 		Worksheet ws = new Worksheet((IToWorksheetUser) this, dialog, desktop);
   187 		// new InternalFrame for the Worksheet
   188 		JInternalFrame ws_frame = new JInternalFrame();
   189 		((BasicInternalFrameUI) ws_frame.getUI()).setNorthPane(null);
   190 		ws_frame.setSize(WORKSHEET_FRAME_INITIAL_SIZE_X, frame_.getHeight()
   191 				- WORKSHEET_FRAME_INITIAL_OFFSET_Y);
   192 
   193 		Container c;
   194 		c = ws_frame.getContentPane();
   195 		c.setLayout(new BorderLayout());
   196 		c.add(ws, BorderLayout.CENTER);
   197 		desktop.add(ws_frame);
   198 		try {
   199 			ws_frame.setMaximum(true);
   200 		} catch (PropertyVetoException e) {
   201 			e.printStackTrace();
   202 		}
   203 		ws_frame.setFocusable(false);
   204 		ws_frame.setVisible(true);
   205 
   206 		internalFrame.addInternalFrameListener(ws);
   207 
   208 //		int width, height = 0;
   209 //		height = internalFrame.getPreferredSize().height;
   210 //		width = internalFrame.getPreferredSize().width;
   211 //		// make Frame a little smaller than the window
   212 //		height = frame_.getHeight() - WORKSHEET_FRAME_INITIAL_OFFSET_Y;
   213 //		width = width < 600 ? WORKSHEET_FRAME_INITIAL_SIZE_X : width;
   214 //
   215 //		JInternalFrame[] frames = desktop_pane_.getAllFrames();
   216 //		int num_frames = frames.length - 4;
   217 		this.addInternalFrame(internalFrame);
   218 //		internalFrame.setLocation(num_frames * 20, num_frames * 20);
   219 //		internalFrame.setPreferredSize(new Dimension(width, height));
   220 		internalFrame.setLocation(20, 10);
   221 		internalFrame.setPreferredSize(new Dimension(500, 300));
   222 		internalFrame.pack();
   223 		internalFrame.setVisible(true);
   224 
   225 		frame_.setCursor(Cursor.getDefaultCursor());
   226 	}
   227 
   228 	/* protocol of building testWorksheetIsac (below): getFormulaeFromTo */
   229 	public void testModDialogIterator1() {
   230 		System.out.println("/--BEGIN isac.gui.mawen.testModDialogIterator1");
   231 		
   232 		Vector<Integer> p = new Vector<Integer>(); p.add(1);
   233 		Position pos = new Position(p, "Res");
   234 		ICalcIterator from = new MockDialogIterator(pos);
   235 		p = new Vector<Integer>(); p.add(2);
   236 	    pos = new Position(p, "Res");
   237 		ICalcIterator to = new MockDialogIterator(pos);
   238 		
   239 		Vector<CalcFormula> fs = null;
   240 		try { // in the test NO rmi is involved
   241 			fs = from.getFormulaeFromTo(to, 0, false);
   242 		} catch (RemoteException e) {
   243 			e.printStackTrace();
   244 		}
   245 		assertEquals(fs.get(0).getPosition().toSMLString(), "([2],Res)");
   246 		
   247 		// -------------------------------------------------------------------
   248 		p = new Vector<Integer>(); p.add(1);
   249 		pos = new Position(p, "Frm");
   250 		from = new MockDialogIterator(pos);
   251 		p = new Vector<Integer>(); p.add(2);
   252 	    pos = new Position(p, "Res");
   253 		to = new MockDialogIterator(pos);
   254 		
   255 		fs = null;
   256 		try { // in the test NO rmi is involved
   257 			fs = from.getFormulaeFromTo(to, 0, false);
   258 		} catch (RemoteException e) {
   259 			e.printStackTrace();
   260 		}
   261 		assertEquals(fs.get(0).getPosition().toSMLString(), "([1],Res)");
   262 		assertEquals(fs.get(1).getPosition().toSMLString(), "([2],Res)");
   263 
   264 		System.out.println("\\--END isac.gui.mawen.testModDialogIterator1");
   265 	}
   266 
   267 	/* protocol of building testWorksheetIsac (below): CalcChanged */
   268 	public void testModDialogIterator2() {
   269 		System.out.println("/--BEGIN isac.gui.mawen.testModDialogIterator2");
   270 		
   271 		Vector<Integer> p = new Vector<Integer>();
   272 		Position pos = new Position(p, "Pbl");
   273 		ICalcIterator ci = new MockDialogIterator(pos);
   274 
   275 		CalcChanged e = new CalcChanged(4711, false, ci, ci, ci);
   276     	ICalcIterator from = e.getLastUnchangedFormula();
   277     	@SuppressWarnings("unused") // is used below; deletion causes error
   278     	ICalcIterator del = e.getLastDeletedFormula();
   279     	ICalcIterator to = e.getLastGeneratedFormula();
   280 		
   281     	Vector<CalcFormula> fs = null;
   282 		try {
   283 			fs = from.getFormulaeFromTo(to, 0, false);
   284 		} catch (RemoteException e1) {
   285 			// TODO Auto-generated catch block
   286 			e1.printStackTrace();
   287 		}
   288 		assertEquals(fs.size(), 1);
   289 		assertEquals(fs.get(0).getPosition().toSMLString(), "([],Pbl)");
   290 //		assertEquals(fs.get(0).getFormula().toSMLString(), "Simplify (1 + (2 * x * (y + 3)) / (4 * z * (y + 3)) + 5)");
   291 		assertEquals(fs.get(0).getFormula().toSMLString(), "1+((2*x*(y+3)+((y^2+6*y+9)/(y+3)))/(4*z*(y+3)))+(5::real)");
   292 		
   293 		// -------------------------------------------------------------------
   294 		p = new Vector<Integer>(); p.add(1);
   295 	    pos = new Position(p, "Res");
   296 	    ICalcIterator unc = new MockDialogIterator(pos);
   297 		p = new Vector<Integer>(); p.add(2);
   298 	    pos = new Position(p, "Res");
   299 	    ICalcIterator gen = new MockDialogIterator(pos);
   300 		
   301 		e = new CalcChanged(4711, false, unc, unc, gen);
   302 		from = e.getLastUnchangedFormula();
   303     	del = e.getLastDeletedFormula();
   304     	to = e.getLastGeneratedFormula();
   305 		
   306     	fs = null;
   307 		try {
   308 			fs = from.getFormulaeFromTo(to, 0, false);
   309 		} catch (RemoteException e1) {
   310 			e1.printStackTrace();
   311 		}
   312 		System.out.println(fs.size());
   313 		System.out.println(fs.get(0).getPosition().toSMLString());
   314 		System.out.println(fs.get(0).getFormula().toSMLString());
   315 
   316 		assertEquals(fs.size(), 1);
   317 		assertEquals(fs.get(0).getPosition().toSMLString(), "([2],Res)");
   318 //		assertEquals(fs.get(0).getFormula().toSMLString(), "1 + x / (2 * z) + 5");
   319 		assertEquals(fs.get(0).getFormula().toSMLString(), "1+(((2*x+1)*(y+3))/(4*z*(y+3)))+(5::real)");
   320 
   321 		
   322 		System.out.println("\\--END isac.gui.mawen.testModDialogIterator2");
   323 	}
   324 
   325 	/* display example for reference in a Worksheet.
   326 	 * THIS TEST WORKS ONLY IN DEBUG MODE, JUnit hangs here.
   327 	 */
   328 	public void testWorksheetIsac() {
   329 			System.out.println("/--BEGIN isac.gui.mawen.testWorksheetIsac");
   330 
   331 		frame_ = constructorWindowApplication();
   332 		startApplication(/* windowApplication */);
   333 		MockWorksheetDialog wd = new MockWorksheetDialog();
   334 
   335 		try {
   336 			this.openNewWorksheet(wd); // <<<<<<<-----------------------------------
   337 			
   338 		} catch (RemoteException e) {
   339 			e.printStackTrace();
   340 		}
   341 		this.createWorksheetPane();
   342 		
   343 		Position from; Position to;
   344 		//-->ISA: getFormulaeFromTo 1 ([],Pbl) ([],Pbl) 0 false;
   345 		from = new Position(); from.setKind("Pbl");
   346 		to = new Position(); to.setKind("Pbl");
   347 		try {
   348       Thread.sleep(10);
   349     } catch (InterruptedException e1) {
   350       // TODO Auto-generated catch block
   351       e1.printStackTrace();
   352     }
   353 		wd.sendFormulaToWorksheet (from, to); // the root-node in CalcModelHierarchy
   354 //		try {
   355 //      Thread.sleep(10);
   356 //    } catch (InterruptedException e1) {
   357 //      // TODO Auto-generated catch block
   358 //      e1.printStackTrace();
   359 //    }
   360 //		//-->ISA: getFormulaeFromTo 1 ([],Met) ([1],Frm) 0 false;
   361 //		from = new Position(); from.setKind("Met");
   362 //		to = new Position(); to.addInt("1"); to.setKind("Frm");
   363 //		try {
   364 //      Thread.sleep(100);
   365 //    } catch (InterruptedException e1) {
   366 //      // TODO Auto-generated catch block
   367 //      e1.printStackTrace();
   368 //    }
   369 //		wd.sendFormulaToWorksheet (from, to);
   370 //		try {
   371 //      Thread.sleep(100);
   372 //    } catch (InterruptedException e1) {
   373 //      // TODO Auto-generated catch block
   374 //      e1.printStackTrace();
   375 //    }
   376 //		//-->ISA: getFormulaeFromTo 1 ([1],Frm) ([1],Res) 0 false;
   377 //		from = new Position(); from.addInt("1"); from.setKind("Frm");
   378 //		to = new Position(); to.addInt("1"); to.setKind("Res");
   379 //		try {
   380 //      Thread.sleep(10);
   381 //    } catch (InterruptedException e1) {
   382 //      // TODO Auto-generated catch block
   383 //      e1.printStackTrace();
   384 //    }
   385 //		wd.sendFormulaToWorksheet (from, to);
   386 //		try {
   387 //      Thread.sleep(10);
   388 //    } catch (InterruptedException e1) {
   389 //      // TODO Auto-generated catch block
   390 //      e1.printStackTrace();
   391 //    }
   392 //		//-->ISA: getFormulaeFromTo 1 ([1],Res) ([2],Res) 0 false;
   393 //		from = new Position(); from.addInt("1"); from.setKind("Res");
   394 //		to = new Position(); to.addInt("2"); to.setKind("Res");
   395 //		try {
   396 //      Thread.sleep(10);
   397 //    } catch (InterruptedException e1) {
   398 //      // TODO Auto-generated catch block
   399 //      e1.printStackTrace();
   400 //    }
   401 //		wd.sendFormulaToWorksheet (from, to);
   402 //		try {
   403 //      Thread.sleep(10);
   404 //    } catch (InterruptedException e1) {
   405 //      // TODO Auto-generated catch block
   406 //      e1.printStackTrace();
   407 //    }
   408 //		//-->ISA: getFormulaeFromTo 1 ([2],Res) ([3],Res) 0 false;
   409 //		from = new Position(); from.addInt("2"); from.setKind("Res");
   410 //		to = new Position(); to.addInt("3"); to.setKind("Res");
   411 //		try {
   412 //      Thread.sleep(10);
   413 //    } catch (InterruptedException e1) {
   414 //      // TODO Auto-generated catch block
   415 //      e1.printStackTrace();
   416 //    }
   417 //		
   418 //		wd.sendFormulaToWorksheet (from, to);
   419 //		try {
   420 //      Thread.sleep(10);
   421 //    } catch (InterruptedException e1) {
   422 //      // TODO Auto-generated catch block
   423 //      e1.printStackTrace();
   424 //    }
   425 //		//ONLY SCEN 5-->ISA: getFormulaeFromTo 1 ([3],Res) ([4],Res) 0 false;
   426 //	    from = new Position(); from.addInt("3"); from.setKind("Res");
   427 //	    to = new Position(); to.addInt("4"); to.setKind("Res");
   428 //	    try {
   429 //	      Thread.sleep(10);
   430 //	    } catch (InterruptedException e1) {
   431 //	      // TODO Auto-generated catch block
   432 //	      e1.printStackTrace();
   433 //	    }
   434 //	    wd.sendFormulaToWorksheet (from, to);
   435 //	    try {
   436 //	      Thread.sleep(10);
   437 //	    } catch (InterruptedException e1) {
   438 //	      // TODO Auto-generated catch block
   439 //	      e1.printStackTrace();
   440 //	    }
   441 //	    from = new Position(); from.addInt("4"); from.setKind("Res");
   442 //	    to = new Position(); to.addInt("4"); to.setKind("Res");
   443 //	    
   444 //	    try {
   445 //	      Thread.sleep(10);
   446 //	    } catch (InterruptedException e1) {
   447 //	      // TODO Auto-generated catch block
   448 //	      e1.printStackTrace();
   449 //	    }
   450 //	    wd.sendFormulaToWorksheet (from, to);
   451 	    //END ONLY SCEN 5-->ISA: getFormulaeFromTo 1 ([3],Res) ([4],Res) 0 false;
   452 		try { Thread.sleep(100000); } catch (InterruptedException e) { e.printStackTrace(); }
   453 		System.out.println("\\--END isac.gui.mawen.testWorksheetIsac");
   454 	}
   455 }