我正在做一些 TDD 将对象编组到 XML 文件中。由于某种原因出现了问题,因为我可以完美地看到我的测试绿色并且完美地创建了文件,但是当我执行生产代码时它不起作用。
让我告诉你这个:
看看下面的测试:
private TermManager manager = new TermManagerImpl();
@Test
public void a_new_term_is_marshalled() {
Term term = new Term("AAA","AAA");
manager.add(term);
assertThat(term.getCreationPath(),is(notNullValue()));
}
因此,此测试在编组完成后检查项目中是否存在文件。类 Term 只是一个用 JAXB 注释的实体,它还包含一个确定创建 xml 的路径的方法(如果创建的话):
@XmlRootElement(name="TERM",namespace = "com.djordje.apps.model.Term")
@XmlAccessorType(XmlAccessType.FIELD)
public class Term {
@XmlElement(name = "NAME")
private String name;
@XmlElement(name = "DESCRIPTION")
private String description;
@XmlElement(name = "CREATIONTIME")
private long creationTime;
//... Some other attributes
public Term() {
//Default constructor required by JAXB
}
public Term(String name, String description) {
this.name = name;
this.description = description;
this.voters = new ArrayList<String>();
}
//...Get and Set methods
public String getCreationPath() {
if(getCreationTime() > 0L){
return "src/main/resources/" + getName() + getCreationTime() + ".xml";
}
return null;
}
}
类 TermManagerImpl 只是数据访问层的一个门面,用于执行一些验证。
public class TermManagerImpl implements TermManager {
private PersistencyManager termsInMemoryStorage = new TermsInMemoryStorage();
@Override
public void add(Term term) {
validateTerm(term, termsInMemoryStorage);
termsInMemoryStorage.add(term);
}
如果验证通过(我认为验证代码不相关,所以我不会将其包含在问题中),则调用持久层的方法 add() 。这有望执行编组。它是如何工作的:
public class TermsInMemoryStorage implements PersistencyManager {
public void add(Term term) {
try {
marshall(term);
} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void marshall(Term term) throws JAXBException, IOException {
term.setCreationTime(System.currentTimeMillis());
File file = new File(term.getCreationPath());
JAXBContext context = JAXBContext.newInstance(Term.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(term,file);
}
}
这就是实施。当我运行测试时,它变为绿色,我看到正在创建文件:
该文件已完美创建,我检查了内容,一切都在那里。问题是当我从生产代码中执行相同的逻辑而不是使用测试时。因此,我有一个 JSF 支持 bean,它根据从某个用户界面接收到的数据创建该 Term 对象。支持 bean 看起来像这样:
@ManagedBean
@RequestScoped
public class CreateController {
private String termName;
private String termDescription;
public CreateController() {
}
//...Get and Set methods
public void saveTerm() {
TermManager termManager = new TermManagerImpl();
termManager.add(new Term(termName,termDescription));
}
}
如您所见, saveTerm() 方法调用与测试完全相同的逻辑。我认为这部分没有问题,我跟着调试器,我看到对象 Term 在里面正确设置了它的值。这个支持bean是从视图中调用的(我想我不需要粘贴视图代码)这是输入的样子:
因此,当尝试编组时,我得到以下 JAXB 异常:
javax.xml.bind.JAXBException
- with linked exception:
[java.io.FileNotFoundException: src/main/resources/ABCDE1384006698756.xml (No such file or directory)]
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:123)
at com.djordje.apps.dataaccess.inmemory.TermsInMemoryStorage.marshall(TermsInMemoryStorage.java:40)
at com.djordje.apps.dataaccess.inmemory.TermsInMemoryStorage.add(TermsInMemoryStorage.java:22)
at com.djordje.apps.utils.termmanagement.TermManagerImpl.add(TermManagerImpl.java:21)
at com.djordje.apps.frontendcontrollers.CreateController.saveTerm(CreateController.java:38)
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 com.sun.el.parser.AstValue.invoke(AstValue.java:275)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:818)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.io.FileNotFoundException: src/main/resources/ABCDE1384006698756.xml (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:116)
... 49 more]]
我完全糊涂了。为什么测试有效,但没有?我必须做些什么来解决这个问题?我跟随调试器直到发生异常之前的点,这就是我所看到的:
在那里我们可以看到 Term 是一个完美创建的对象,它的值......看起来文件由于某种原因没有创建,这是文件对象在调试器中的样子:
调用 marshall 方法后 2 行发生异常。我要提到的最后一件事是我使用的是 Glassfish 4。知道为什么会发生这种情况吗?我应该如何解决它?我应该以不同的方式进行测试吗?