fix store and load method of xml configuration entry store

This commit is contained in:
Sebastian Sdorra
2013-09-16 11:38:17 +02:00
parent 4586d2de0b
commit 10b2221bb6
3 changed files with 209 additions and 4 deletions

View File

@@ -343,7 +343,11 @@ public class JAXBConfigurationEntryStore<V>
Unmarshaller u = context.createUnmarshaller();
reader = xmlInputFactory.createXMLStreamReader(createReader());
// configuration
reader.nextTag();
// entry start
reader.nextTag();
while (reader.isStartElement() && reader.getLocalName().equals(TAG_ENTRY))
@@ -354,13 +358,29 @@ public class JAXBConfigurationEntryStore<V>
String key = reader.getElementText();
// read entry
// read value
reader.nextTag();
V v = (V) u.unmarshal(reader, type).getValue();
JAXBElement<V> element = u.unmarshal(reader, type);
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<V>
);
//J+
writer.writeStartDocument();
// configuration start
writer.writeStartElement(TAG_CONFIGURATION);
Marshaller m = context.createMarshaller();
@@ -400,17 +421,24 @@ public class JAXBConfigurationEntryStore<V>
for (Entry<String, V> 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<V> je = new JAXBElement<V>(QName.valueOf(TAG_VALUE), type,
e.getValue());
m.marshal(je, writer);
// entry end
writer.writeEndElement();
}
// configuration end
writer.writeEndElement();
writer.writeEndDocument();
}

View File

@@ -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<AssignedPermission> 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<AssignedPermission> 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<AssignedPermission> createPermissionStore(
String resource)
throws IOException
{
return createPermissionStore(resource, null);
}
/**
* Method description
*
*
* @param resource
* @param name
*
* @return
*
* @throws IOException
*/
private ConfigurationEntryStore<AssignedPermission> createPermissionStore(
String resource, String name)
throws IOException
{
if (name == null)
{
name = UUID.randomUUID().toString();
}
copy(resource, name);
return createConfigurationStoreFactory().getStore(AssignedPermission.class,
name);
}
}

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" ?>
<configuration>
<entry>
<key>3ZOHKUePB3</key>
<value>
<group-permission>false</group-permission>
<name>tuser</name>
<permission>repository:*:READ</permission>
</value>
</entry>
<entry>
<key>A0OHL3Qqw2</key>
<value>
<group-permission>false</group-permission>
<name>tuser3</name>
<permission>repository:*:OWNER</permission>
</value>
</entry>
<entry>
<key>7COHL2j1G1</key>
<value>
<group-permission>false</group-permission>
<name>tuser2</name>
<permission>repository:*:WRITE</permission>
</value>
</entry>
</configuration>