2015-11-22 20 views
6

ho un JTable disabile che offre un menu a comparsa:Cliccando sulla disabilità componenti Swing

import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

import javax.swing.JFrame; 
import javax.swing.JMenuItem; 
import javax.swing.JPopupMenu; 
import javax.swing.JTable; 

public class DisabledTableFrame extends JFrame { 

    public DisabledTableFrame() { 

     setSize(200, 100); 
     setTitle(getClass().getCanonicalName()); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     JTable table = new JTable(); 
     add(table); 

     table.addMouseListener(new MouseAdapter() { 

      @Override 
      public void mousePressed(MouseEvent e) { 
       mouseReleased(e); 
      } 

      @Override 
      public void mouseReleased(MouseEvent e) { 
       new PopupMenu(); 
      } 

     }); 

     table.setEnabled(false); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     new DisabledTableFrame(); 

    } 

    private class PopupMenu extends JPopupMenu { 

     public PopupMenu() { 

      JMenuItem menuItem = new JMenuItem("TEST"); 
      add(menuItem); 
      setVisible(true); 
     } 
    } 
} 

Così, quando il test questa funzione con AssertJ swing utilizzando:

import org.assertj.swing.edt.GuiActionRunner; 
import org.assertj.swing.edt.GuiQuery; 
import org.assertj.swing.fixture.FrameFixture; 
import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase; 
import org.junit.Test; 

public class PopupTestCase extends AssertJSwingJUnitTestCase { 

    protected FrameFixture window; 

    @Override 
    protected void onSetUp() { 
     DisabledTableFrame mainFrame = GuiActionRunner 
       .execute(new GuiQuery<DisabledTableFrame>() { 
        protected DisabledTableFrame executeInEDT() { 

         return new DisabledTableFrame(); 
        } 
       }); 

     window = new FrameFixture(robot(), mainFrame); 
    } 

    @Test 
    public void popupShouldBeOpened() { 

     window.table().showPopupMenu(); 
    } 
} 

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>AssertJSwingInactiveTest</groupId> 
    <artifactId>AssertJSwingInactiveTest</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <dependencies> 
     <dependency> 
      <groupId>org.assertj</groupId> 
      <artifactId>assertj-swing-junit-4.5</artifactId> 
      <version>1.2.0</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <sourceDirectory>src</sourceDirectory> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.3</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

Funziona correttamente quando la tabella è abilitata. Ma il test() popupShoudBeOpened sul tavolo disabile lancia la seguente eccezione:

java.lang.IllegalStateException: Expecting component javax.swing.JTable[name=null, rowCount=0, columnCount=0, enabled=false, visible=true, showing=true] to be enabled 
at org.assertj.swing.driver.ComponentPreconditions.checkEnabled(ComponentPreconditions.java:68) 
at org.assertj.swing.driver.ComponentPreconditions.checkEnabledAndShowing(ComponentPreconditions.java:48) 
at org.assertj.swing.driver.ComponentDriver$2.executeInEDT(ComponentDriver.java:555) 
at org.assertj.swing.edt.GuiTask.run(GuiTask.java:38) 
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745) 
at java.awt.EventQueue.access$300(EventQueue.java:103) 
at java.awt.EventQueue$3.run(EventQueue.java:706) 
at java.awt.EventQueue$3.run(EventQueue.java:704) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 
at org.assertj.swing.edt.GuiActionRunner.execute(GuiActionRunner.java:103) 
at org.assertj.swing.driver.ComponentDriver.checkInEdtEnabledAndShowing(ComponentDriver.java:552) 
at org.assertj.swing.driver.ComponentDriver.invokePopupMenu(ComponentDriver.java:519) 
at org.assertj.swing.fixture.AbstractJPopupMenuInvokerFixture.showPopupMenu(AbstractJPopupMenuInvokerFixture.java:95) 
at GUITestCase.popupShouldBeOpened(GUITestCase.java:27) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41) 
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:220) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

Come il menu a comparsa funziona ovviamente anche sul tavolo disabili, come posso ottenere AssertJ a "fare clic destro" il tavolo disabile?

+0

potete inserire il vostro codice per favore? – bircastri

+0

È assertj chi lancia l'eccezione poiché si aspetta che la tabella sia abilitata. Si prega di fornire la traccia di eccezione completa. –

+0

@bircastri Ho distillato il problema alle basi e fornito l'intero stacktrace. È bello vedere che ci sono ancora persone che usano AssertJ Swing. –

risposta

1

AsserJ atteso che il componente testato sia abilitato.
Una volta disabilitato, genererà un IllegalStateException.
Controllare AbstractComponentFixture API here.

Un modo per risolvere questo problema è quello di dividere per 2 test e contengono l'eccezione quando si prevede che la tabella di essere disabilitato:

@Test 
public void popupShouldBeOpenedIfTableIsDisabled() { 
    try { 
     window.table().showPopupMenu(); 
    } catch (IllegalStateException e) { 
     // Continue normally if IllegalStateException was thrown since the table is disabled on purpose. 
    } 
} 

@Test 
public void popupShouldBeOpenedIfTableIsEnabled() { 
    window.table().showPopupMenu(); 
    // IllegalStateException will fail the test since the table is enabled. 
} 
+0

Grazie per aver risolto il mio problema. Il problema è che il menu popup non verrà aperto comunque. Ma ho bisogno di aprirlo per fare clic su una voce di menu. C'è un modo per disattivare i controlli di precondizione di AssertJ? –

+0

Prego. Il problema è che il componente driver controllerà sempre la visualizzazione + EDT + attivata. A meno che non si modifichi il codice sorgente, questi 3 pre-condizione devono essere soddisfatti prima del test. –

Problemi correlati