From 10b2221bb6819a4a729b68708c97af60ffbc476f Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Mon, 16 Sep 2013 11:38:17 +0200 Subject: [PATCH] fix store and load method of xml configuration entry store --- .../store/JAXBConfigurationEntryStore.java | 34 +++- .../JAXBConfigurationEntryStoreTest.java | 152 +++++++++++++++++- .../sonia/scm/store/fixed.format.xml | 27 ++++ 3 files changed, 209 insertions(+), 4 deletions(-) create mode 100644 scm-dao-xml/src/test/resources/sonia/scm/store/fixed.format.xml diff --git a/scm-dao-xml/src/main/java/sonia/scm/store/JAXBConfigurationEntryStore.java b/scm-dao-xml/src/main/java/sonia/scm/store/JAXBConfigurationEntryStore.java index 0d5bcc2540..46e51d97b6 100644 --- a/scm-dao-xml/src/main/java/sonia/scm/store/JAXBConfigurationEntryStore.java +++ b/scm-dao-xml/src/main/java/sonia/scm/store/JAXBConfigurationEntryStore.java @@ -343,7 +343,11 @@ public class JAXBConfigurationEntryStore Unmarshaller u = context.createUnmarshaller(); reader = xmlInputFactory.createXMLStreamReader(createReader()); + + // configuration reader.nextTag(); + + // entry start reader.nextTag(); while (reader.isStartElement() && reader.getLocalName().equals(TAG_ENTRY)) @@ -354,12 +358,28 @@ public class JAXBConfigurationEntryStore String key = reader.getElementText(); - // read entry + // read value reader.nextTag(); - V v = (V) u.unmarshal(reader, type).getValue(); + JAXBElement element = u.unmarshal(reader, type); - entries.put(key, v); + if (!element.isNil()) + { + V v = element.getValue(); + System.out.println( "value: " + v ); + + entries.put(key, v); + } + else + { + logger.warn("could not unmarshall object of entry store"); + } + + // closed entry tag + reader.nextTag(); + + // start new entry + reader.nextTag(); } } catch (Exception ex) @@ -392,6 +412,7 @@ public class JAXBConfigurationEntryStore ); //J+ writer.writeStartDocument(); + // configuration start writer.writeStartElement(TAG_CONFIGURATION); Marshaller m = context.createMarshaller(); @@ -400,17 +421,24 @@ public class JAXBConfigurationEntryStore for (Entry e : entries.entrySet()) { + // entry start writer.writeStartElement(TAG_ENTRY); + // key start writer.writeStartElement(TAG_KEY); writer.writeCharacters(e.getKey()); + // key end writer.writeEndElement(); + // value JAXBElement je = new JAXBElement(QName.valueOf(TAG_VALUE), type, e.getValue()); m.marshal(je, writer); + // entry end + writer.writeEndElement(); } + // configuration end writer.writeEndElement(); writer.writeEndDocument(); } diff --git a/scm-dao-xml/src/test/java/sonia/scm/store/JAXBConfigurationEntryStoreTest.java b/scm-dao-xml/src/test/java/sonia/scm/store/JAXBConfigurationEntryStoreTest.java index f509a617de..a2057e87a3 100644 --- a/scm-dao-xml/src/test/java/sonia/scm/store/JAXBConfigurationEntryStoreTest.java +++ b/scm-dao-xml/src/test/java/sonia/scm/store/JAXBConfigurationEntryStoreTest.java @@ -30,19 +30,99 @@ */ + package sonia.scm.store; //~--- non-JDK imports -------------------------------------------------------- +import com.google.common.io.Closeables; +import com.google.common.io.Resources; + +import org.junit.Test; + +import sonia.scm.security.AssignedPermission; import sonia.scm.security.UUIDKeyGenerator; +import static org.junit.Assert.*; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import java.net.URL; + +import java.util.UUID; + /** * * @author Sebastian Sdorra */ -public class JAXBConfigurationEntryStoreTest extends ConfigurationEntryStoreTestBase +public class JAXBConfigurationEntryStoreTest + extends ConfigurationEntryStoreTestBase { + /** Field description */ + private static final String RESOURCE_FIXED = + "sonia/scm/store/fixed.format.xml"; + + /** Field description */ + private static final String RESOURCE_WRONG = + "sonia/scm/store/wrong.format.xml"; + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @throws IOException + */ + @Test + public void testLoad() throws IOException + { + ConfigurationEntryStore store = + createPermissionStore(RESOURCE_FIXED); + AssignedPermission a1 = store.get("3ZOHKUePB3"); + + assertEquals("tuser", a1.getName()); + + AssignedPermission a2 = store.get("7COHL2j1G1"); + + assertEquals("tuser2", a2.getName()); + + AssignedPermission a3 = store.get("A0OHL3Qqw2"); + + assertEquals("tuser3", a3.getName()); + } + + /** + * Method description + * + * + * @throws IOException + */ + @Test + public void testStoreAndLoad() throws IOException + { + String name = UUID.randomUUID().toString(); + ConfigurationEntryStore store = + createPermissionStore(RESOURCE_FIXED, name); + + store.put("a45", new AssignedPermission("tuser4", "repository:create")); + store = + createConfigurationStoreFactory().getStore(AssignedPermission.class, + name); + + AssignedPermission ap = store.get("a45"); + + assertNotNull(ap); + assertEquals("tuser4", ap.getName()); + assertEquals("repository:create", ap.getPermission()); + } + /** * Method description * @@ -55,4 +135,74 @@ public class JAXBConfigurationEntryStoreTest extends ConfigurationEntryStoreTest return new JAXBConfigurationEntryStoreFactory(new UUIDKeyGenerator(), contextProvider); } + + /** + * Method description + * + * + * @param resource + * @param name + * + * @throws IOException + */ + private void copy(String resource, String name) throws IOException + { + URL url = Resources.getResource(resource); + File confdir = new File(contextProvider.getBaseDirectory(), "config"); + File file = new File(confdir, name.concat(".xml")); + OutputStream output = null; + + try + { + output = new FileOutputStream(file); + Resources.copy(url, output); + } + finally + { + Closeables.close(output, true); + } + } + + /** + * Method description + * + * + * @param resource + * + * @return + * + * @throws IOException + */ + private ConfigurationEntryStore createPermissionStore( + String resource) + throws IOException + { + return createPermissionStore(resource, null); + } + + /** + * Method description + * + * + * @param resource + * @param name + * + * @return + * + * @throws IOException + */ + private ConfigurationEntryStore createPermissionStore( + String resource, String name) + throws IOException + { + if (name == null) + { + name = UUID.randomUUID().toString(); + } + + copy(resource, name); + + return createConfigurationStoreFactory().getStore(AssignedPermission.class, + name); + } } diff --git a/scm-dao-xml/src/test/resources/sonia/scm/store/fixed.format.xml b/scm-dao-xml/src/test/resources/sonia/scm/store/fixed.format.xml new file mode 100644 index 0000000000..3f8e4bd21f --- /dev/null +++ b/scm-dao-xml/src/test/resources/sonia/scm/store/fixed.format.xml @@ -0,0 +1,27 @@ + + + + 3ZOHKUePB3 + + false + tuser + repository:*:READ + + + + A0OHL3Qqw2 + + false + tuser3 + repository:*:OWNER + + + + 7COHL2j1G1 + + false + tuser2 + repository:*:WRITE + + +