Re: [Simple-support] Bug when read XML with ArrayList and HashMap created by simple
Brought to you by:
niallg
|
From: user <xx...@ra...> - 2011-06-15 07:31:02
|
You can try the following test. It faults:
package testsimplearraybug;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.Assert.*;
import org.junit.Test;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
@Root
public class ParamContainer {
private static final String FILENAME = "params.xml";
@ElementList
private List<ParamItem> items;
public ParamContainer() {
items = new ArrayList();
items.add(new ParamItem());
items.add(new ParamItem());
}
public void save() {
Serializer saver = new Persister();
File f = new File(FILENAME);
try {
saver.write(this, f);
} catch (Exception ex) {
Logger.getLogger(ParamContainer.class.getName()).log(Level.SEVERE,
null, ex);
}
}
public static ParamContainer load() {
Serializer reader = new Persister();
File f = new File(FILENAME);
try {
return reader.read(ParamContainer.class, f);
} catch (Exception ex) {
Logger.getLogger(ParamContainer.class.getName()).log(Level.SEVERE,
null, ex);
}
return null;
}
@Test
public static void checkIdentity() {
ParamContainer c1 = new ParamContainer();
c1.save();
ParamContainer c2 = ParamContainer.load();
org.junit.Assert.assertTrue(c1.items.size() == c2.items.size());
}
public static void main(String[] args) {
checkIdentity();
}
@Root
public static class ParamItem {
@Attribute
private int columnsCount = 0;
@Attribute
private int rowsCount = 0;
}
}
* Niall Gallagher <gal...@ya...> [Thu, 9 Jun 2011 04:35:40
-0700 (PDT)]:
> Hi,
>
> Can you write this as a unit test?
>
> Thanks,
> Niall
>
> --- On Tue, 7/6/11, user <xx...@ra...> wrote:
>
> > From: user <xx...@ra...>
> > Subject: [Simple-support] Bug when read XML with ArrayList and
HashMap
> created by simple
> > To: sim...@li...
> > Received: Tuesday, 7 June, 2011, 5:38 AM
> > There is a small class which creates
> > default configuration and saves it
> > into file:
> >
> > package videocontroldesk.params;
> >
> > import java.io.File;
> > import java.util.ArrayList;
> > import java.util.HashMap;
> > import java.util.List;
> > import java.util.Map;
> > import java.util.logging.Level;
> > import java.util.logging.Logger;
> > import org.simpleframework.xml.Element;
> > import org.simpleframework.xml.ElementList;
> > import org.simpleframework.xml.ElementMap;
> > import org.simpleframework.xml.Root;
> > import org.simpleframework.xml.Serializer;
> > import org.simpleframework.xml.core.Persister;
> >
> > @Root
> > public class VCDParams {
> >
> > private static VCDParams instance;
> > private static final String filename =
> > "VCDParams.xml";
> > private static final Logger logger =
> > Logger.getLogger(VCDParams.class.getName());
> >
> > @Element
> > private int selectedLayoutIdx;
> >
> > @ElementList
> > private List<LayoutParams> layouts;
> >
> > @ElementMap
> > private Map<String, String>
> > ipForCamera;
> >
> > private VCDParams() {
> > selectedLayoutIdx = 0;
> > layouts = new
> > ArrayList<LayoutParams>();
> > for (int i = 0; i < 4; ++i)
> > {
> > layouts.add(new
> > LayoutParams());
> > }
> > ipForCamera = new
> > HashMap<String, String>();
> > ipForCamera.put("БК1",
> > "192.168.0.101");
> > }
> >
> > public int getLayoutsCount() {
> > return layouts.size();
> > }
> >
> > public LayoutParams getLayout(int layoutIdx)
> > {
> > return layouts.get(layoutIdx);
> > }
> >
> > public static VCDParams getInstance() {
> > if (instance == null) {
> > try {
> >
> > instance = read();
> > }
> > catch (Exception
> > ex) {
> >
> > instance = new VCDParams();
> > }
> > }
> > return instance;
> > }
> >
> > private static VCDParams read() throws
> > Exception {
> > Serializer reader = new
> > Persister();
> > File f = new File(filename);
> > return
> > reader.read(VCDParams.class, f);
> > }
> >
> > public void save() {
> > Serializer saver = new
> > Persister();
> > File f = new File(filename);
> > try {
> > saver.write(this,
> > f);
> > } catch (Exception ex) {
> >
> > logger.log(Level.WARNING, "Не могу сохранить
> > файл {0}",
> > filename);
> > }
> > }
> >
> > public static void main(String[] args) {
> >
> > //VCDParams.getInstance().save();
> > VCDParams params =
> > VCDParams.getInstance();
> > //String[] cams =
> > VCDParams.getInstance().getLayout(0).getAllCamIds();
> > }
> >
> > }
> >
> >
> > If I uncomment "save()" method call I get the following
> > XML:
> >
> > <VCDParams>
> > <selectedLayoutIdx>0</selectedLayoutIdx>
> > <layouts
> > class="java.util.ArrayList">
> > <layoutParams columnsCount="2"
> > rowsCount="1">
> > <cell
> > cellId="0">БК1</cell>
> > </layoutParams>
> > <layoutParams columnsCount="2"
> > rowsCount="1">
> > <cell
> > cellId="0">БК1</cell>
> > </layoutParams>
> > <layoutParams columnsCount="2"
> > rowsCount="1">
> > <cell
> > cellId="0">БК1</cell>
> > </layoutParams>
> > <layoutParams columnsCount="2"
> > rowsCount="1">
> > <cell
> > cellId="0">БК1</cell>
> > </layoutParams>
> > </layouts>
> > <ipForCamera
> > class="java.util.HashMap">
> > <entry>
> >
> > <string>БК1</string>
> >
> > <string>192.168.0.101</string>
> > </entry>
> > </ipForCamera>
> > </VCDParams>
> >
> >
> > It looks OK. Then I comment "save()" and uncomment "
> > VCDParams.getInstance()" call.
> > When I set breakpoint right after this call I check the
> > value of
> > layouts.size(). It return 8 instead of 4.
> > Please fix it.
> > Tested on 2.5.3 and 2.4.1.
> >
> >
>
------------------------------------------------------------------------------
> > EditLive Enterprise is the world's most technically
> > advanced content
> > authoring tool. Experience the power of Track Changes,
> > Inline Image
> > Editing and ensure content is compliant with Accessibility
> > Checking.
> > http://p.sf.net/sfu/ephox-dev2dev
> > _______________________________________________
> > Simple-support mailing list
> > Sim...@li...
> > https://lists.sourceforge.net/lists/listinfo/simple-support
> >
|