001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.client;
007
008import java.lang.reflect.Field;
009
010/**
011 * @author acoburn
012 */
013public class TestUtils {
014
015    private TestUtils() { }
016
017    public static final String baseUrl = "http://localhost:8080/rest/foo";
018
019    public static final String rdfTtl = "@prefix dc: <http://purl.org/dc/elements/1.1/> .\n" +
020            "<> dc:title \"Test Object\" . ";
021
022    public static final String rdfXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
023            "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" +
024              "<rdf:Description rdf:about=\"http://localhost:8080/rest/foo\">" +
025                "<mixinTypes xmlns=\"http://fedora.info/definitions/v4/repository#\" " +
026                    "rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">fedora:resource</mixinTypes>" +
027              "</rdf:Description>" +
028            "</rdf:RDF>";
029
030    public static final String sparqlUpdate = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
031            "INSERT { <> dc:title \"Foo\" . } WHERE {}";
032
033    public static final String SPARQL_UPDATE = "application/sparql-update";
034
035    public static final String RDF_XML = "application/rdf+xml";
036
037    public static final String TEXT_TURTLE = "text/turtle";
038
039    public static void setField(final Object parent, final String name,
040        final Object obj) {
041        /* check the parent class too if the field could not be found */
042        try {
043            final Field f = findField(parent.getClass(), name);
044            f.setAccessible(true);
045            f.set(parent, obj);
046        } catch (final Exception e) {
047            e.printStackTrace();
048        }
049    }
050
051    private static Field findField(final Class<?> clazz, final String name)
052            throws NoSuchFieldException {
053        for (final Field f : clazz.getDeclaredFields()) {
054            if (f.getName().equals(name)) {
055                return f;
056            }
057        }
058        if (clazz.getSuperclass() == null) {
059            throw new NoSuchFieldException("Field " + name +
060                                                   " could not be found");
061        }
062        return findField(clazz.getSuperclass(), name);
063    }
064}
065