Skip to content

Commit

Permalink
Declare immutable types for XStream only when writing files
Browse files Browse the repository at this point in the history
Add the immutable types only when writing the file because there are
files out in the wild that still have referenced objects (probably due
to bugs in previous version and/or manual manipulation of files)

Issue: #4117
  • Loading branch information
buchen committed Aug 5, 2024
1 parent a102143 commit 2ee455e
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 113 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fileversions;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.junit.Test;

import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.ClientFactory;
import name.abuchen.portfolio.model.ClientTestUtilities;

@SuppressWarnings("nls")
public class XStreamReferencesTest
{
@Test
public void testReadingWithReferencesAndWritingWithoutReferences() throws IOException
{
Client withReferences = ClientFactory
.load(XStreamReferencesTest.class.getResourceAsStream("client_with_relative_references.xml"));

// check that the first two securities have the same latest price object
// (is a reference in the XML file)

assertThat(withReferences.getSecurities().get(0).getLatest() == withReferences.getSecurities().get(1)
.getLatest(), is(true));

String xml = ClientTestUtilities.toString(withReferences);
Client without = ClientFactory.load(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
assertThat(without.getSecurities().get(0).getLatest() == without.getSecurities().get(1).getLatest(), is(false));
}

@Test
public void testReadingWithIdReferencesAndWritingWithoutIdReferences() throws IOException
{
Client withReferences = ClientFactory
.load(XStreamReferencesTest.class.getResourceAsStream("client_with_id_references.xml"), true);

// check that the first two securities have the same latest price object
// (is a reference with "id" attribute in the XML file)

assertThat(withReferences.getSecurities().get(0).getLatest() == withReferences.getSecurities().get(1)
.getLatest(), is(true));

String xml = ClientTestUtilities.toString(withReferences, true);
Client without = ClientFactory.load(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
assertThat(without.getSecurities().get(0).getLatest() == without.getSecurities().get(1).getLatest(), is(false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<client id="1">
<version>63</version>
<baseCurrency>EUR</baseCurrency>
<securities>
<security id="2">
<uuid>987f74dd-2308-4b6b-a5b8-4918e3a73d95</uuid>
<currencyCode>EUR</currencyCode>
<prices/>
<latest id="3" t="2024-08-05" v="10">
<high>0</high>
<low>0</low>
<volume>0</volume>
</latest>
<attributes>
<map/>
</attributes>
<isRetired>false</isRetired>
</security>
<security id="4">
<uuid>35b3ba7d-20f4-45be-8804-9d5778f40b52</uuid>
<currencyCode>EUR</currencyCode>
<prices/>
<latest reference="3"/>
<attributes>
<map/>
</attributes>
<isRetired>false</isRetired>
</security>
</securities>
<watchlists/>
<accounts/>
<portfolios/>
<plans/>
<taxonomies/>
<dashboards/>
<properties/>
<settings>
<bookmarks/>
<attributeTypes
</attributeTypes>
<configurationSets/>
</settings>
</client>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<client>
<version>63</version>
<baseCurrency>EUR</baseCurrency>
<securities>
<security>
<uuid>987f74dd-2308-4b6b-a5b8-4918e3a73d95</uuid>
<currencyCode>EUR</currencyCode>
<prices/>
<latest t="2024-08-05" v="10">
<high>0</high>
<low>0</low>
<volume>0</volume>
</latest>
<attributes>
<map/>
</attributes>
<isRetired>false</isRetired>
</security>
<security>
<uuid>35b3ba7d-20f4-45be-8804-9d5778f40b52</uuid>
<currencyCode>EUR</currencyCode>
<prices/>
<latest reference="../../security/latest"/>
<attributes>
<map/>
</attributes>
<isRetired>false</isRetired>
</security>
</securities>
<watchlists/>
<accounts/>
<portfolios/>
<plans/>
<taxonomies/>
<dashboards/>
<properties/>
<settings>
<bookmarks/>
<attributeTypes
</attributeTypes>
<configurationSets/>
</settings>
</client>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
public class ClientTestUtilities
{
public static String toString(Client client)
{
return toString(client, false);
}

public static String toString(Client client, boolean withIdReferences)
{
// prune client - remove empty yet lazily created objects that should
// not show up in XML
Expand All @@ -26,7 +31,7 @@ public static String toString(Client client)
try
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
new ClientFactory.XmlSerialization().save(client, stream);
new ClientFactory.XmlSerialization(withIdReferences).save(client, stream);
stream.close();
return new String(stream.toByteArray());
}
Expand Down
Loading

0 comments on commit 2ee455e

Please sign in to comment.