001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.client;
017
018import java.lang.reflect.Field;
019
020/**
021 * @author acoburn
022 */
023public class TestUtils {
024
025    private TestUtils() { }
026
027    public static final String baseUrl = "http://localhost:8080/rest/foo";
028
029    public static final String rdfTtl = "@prefix dc: <http://purl.org/dc/elements/1.1/> .\n" +
030            "<> dc:title \"Test Object\" . ";
031
032    public static final String rdfXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
033            "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" +
034              "<rdf:Description rdf:about=\"http://localhost:8080/rest/foo\">" +
035                "<mixinTypes xmlns=\"http://fedora.info/definitions/v4/repository#\" " +
036                    "rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">fedora:resource</mixinTypes>" +
037              "</rdf:Description>" +
038            "</rdf:RDF>";
039
040    public static final String sparqlUpdate = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
041            "INSERT { <> dc:title \"Foo\" . } WHERE {}";
042
043    public static final String SPARQL_UPDATE = "application/sparql-update";
044
045    public static final String RDF_XML = "application/rdf+xml";
046
047    public static final String TEXT_TURTLE = "text/turtle";
048
049    public static void setField(final Object parent, final String name,
050        final Object obj) {
051        /* check the parent class too if the field could not be found */
052        try {
053            final Field f = findField(parent.getClass(), name);
054            f.setAccessible(true);
055            f.set(parent, obj);
056        } catch (final Exception e) {
057            e.printStackTrace();
058        }
059    }
060
061    private static Field findField(final Class<?> clazz, final String name)
062            throws NoSuchFieldException {
063        for (final Field f : clazz.getDeclaredFields()) {
064            if (f.getName().equals(name)) {
065                return f;
066            }
067        }
068        if (clazz.getSuperclass() == null) {
069            throw new NoSuchFieldException("Field " + name +
070                                                   " could not be found");
071        }
072        return findField(clazz.getSuperclass(), name);
073    }
074}
075