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