创建自定义工具包时,为什么createXrame在OSX上失败?

时间:2009-12-08 05:15:15

标签: java unit-testing swing macos ui-testing

我们正在尝试扩展UISpec4j测试框架,以便在运行测试时显示用户界面。因此,我们有一个自定义工具包,它包含相关平台的本机工具包。我们设法在Windows XP上使一切运行良好,但是在OSX上测试我们的更改时,我们对createFrame的扩展似乎表现得很奇怪:

 public FramePeer createFrame(Frame target) {
   FramePeer resultc = new UISpecFramePeer(target);
   //super.createFrame(target);

   return resultc;
 }

基本上,我们希望创建一个UISpecFramePeer作为拦截类......这样UISpec4j就可以查询UI中发生的事情。但在我们的更改中,我们还希望显示UI。因此,我们尝试使用本机工具包创建一个框架并将其包装在UISpecFramePeer中(未在上面的代码中显示)。

当我们运行上面的代码时,测试运行(正如我们所期望的那样),但显然没有显示任何内容。但是,当我们包含super.createFrame(target)时。我们得到以下例外:

SEVERE: Application class org.openshapa.OpenSHAPA failed to launch
java.lang.ClassCastException: org.uispec4j.interception.toolkit.UISpecFramePeer
    at apple.awt.CWindow$8.convertJComponentToTarget(CWindow.java:236)
    at apple.awt.CWindow$8.convertJComponentToTarget(CWindow.java:233)
    at apple.awt.ClientPropertyApplicator.attachAndApplyClientProperties(ClientPropertyApplicator.java:24)
    at apple.awt.CWindow$1.propertyChange(CWindow.java:190)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:333)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:341)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:270)
    at java.awt.Component.firePropertyChange(Component.java:7277)
    at javax.swing.JComponent.addNotify(JComponent.java:4479)
    at javax.swing.JRootPane.addNotify(JRootPane.java:680)
    at java.awt.Container.addNotify(Container.java:2544)
    at java.awt.Window.addNotify(Window.java:467)
    at java.awt.Frame.addNotify(Frame.java:501)
    at java.awt.Window.pack(Window.java:485)
    at org.jdesktop.application.SingleFrameApplication.initRootPaneContainer(SingleFrameApplication.java:216)
    at org.jdesktop.application.SingleFrameApplication.show(SingleFrameApplication.java:463)
    at org.openshapa.OpenSHAPA.startup(OpenSHAPA.java:444)
    at org.jdesktop.application.Application$1.run(Application.java:171)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-2" java.lang.Error: Application class org.openshapa.OpenSHAPA failed to launch
    at org.jdesktop.application.Application$1.run(Application.java:177)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Caused by: java.lang.ClassCastException: org.uispec4j.interception.toolkit.UISpecFramePeer
    at apple.awt.CWindow$8.convertJComponentToTarget(CWindow.java:236)
    at apple.awt.CWindow$8.convertJComponentToTarget(CWindow.java:233)
    at apple.awt.ClientPropertyApplicator.attachAndApplyClientProperties(ClientPropertyApplicator.java:24)
    at apple.awt.CWindow$1.propertyChange(CWindow.java:190)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:333)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:341)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:270)
    at java.awt.Component.firePropertyChange(Component.java:7277)
    at javax.swing.JComponent.addNotify(JComponent.java:4479)
    at javax.swing.JRootPane.addNotify(JRootPane.java:680)
    at java.awt.Container.addNotify(Container.java:2544)
    at java.awt.Window.addNotify(Window.java:467)
    at java.awt.Frame.addNotify(Frame.java:501)
    at java.awt.Window.pack(Window.java:485)
    at org.jdesktop.application.SingleFrameApplication.initRootPaneContainer(SingleFrameApplication.java:216)
    at org.jdesktop.application.SingleFrameApplication.show(SingleFrameApplication.java:463)
    at org.openshapa.OpenSHAPA.startup(OpenSHAPA.java:444)
    at org.jdesktop.application.Application$1.run(Application.java:171)
    ... 7 more

为什么简单地使用本机工具包来创建一个框架(但不返回它)会产生一个异常,否则当评论super.createFrame时它会表现正常?

1 个答案:

答案 0 :(得分:0)

我还没有在OSX上工作,但在Windows上也会出现同样的问题,原因是:

执行super.createFrame()时,实际上是在创建本地对等体,最终会尝试显示,因此调用addNotify方法。现在里面有很多方法可以直接将对等体转换为WFramePeer(这可能是Windows,OSX可能是CWindowPeer或其他),因此是ClassCastException。在你的情况下,我的猜测是,CWindow类试图投射你的UISpecFramePeer to WFramePeer(or CFramePeer or whatever)。只需通过反编译检查CWindow代码即可。请不要忘记发表你的发现。