001/*
002 * ModeShape (http://www.modeshape.org)
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.modeshape.common.collection;
017
018import static org.hamcrest.core.Is.is;
019import static org.junit.Assert.assertThat;
020import org.modeshape.common.CommonI18n;
021import org.modeshape.common.collection.Problem.Status;
022import org.modeshape.common.i18n.I18n;
023import org.junit.Before;
024import org.junit.Test;
025
026/**
027 * @author Randall Hauch
028 */
029public class ProblemTest {
030
031    private Problem error;
032    private Problem warning;
033    private Problem info;
034    private I18n message;
035    private Object[] messageParameters;
036    private Throwable throwable;
037    private String location;
038    private String resource;
039
040    @Before
041    public void beforeEach() throws Exception {
042        message = CommonI18n.argumentMayNotBeNull;
043        throwable = new IllegalArgumentException(message.text("throwable"));
044        messageParameters = new Object[] {"message"};
045        resource = "SomeResource";
046        location = "/Meaningless/location";
047        error = new Problem(Status.ERROR, 1, message, messageParameters, resource, location, throwable);
048        warning = new Problem(Status.WARNING, 1, message, messageParameters, resource, location, throwable);
049        info = new Problem(Status.INFO, 1, message, messageParameters, resource, location, throwable);
050    }
051
052    @Test
053    public void shouldHaveToString() {
054        assertThat(error.toString(), is("ERROR: (1) " + message.text("message") + " Resource=\"" + resource + "\" At \""
055                                        + location + "\" (threw " + throwable.getLocalizedMessage() + ")"));
056    }
057
058    @Test
059    public void shouldHaveToStringWithoutDefaultCode() {
060        error = new Problem(Status.ERROR, Problem.DEFAULT_CODE, message, new Object[] {"message"}, null, null, null);
061        assertThat(error.toString(), is("ERROR: " + message.text("message")));
062    }
063
064    @Test
065    public void shouldHaveMessageString() {
066        messageParameters = new Object[] {"error msg"};
067        error = new Problem(Status.ERROR, 1, message, messageParameters, resource, location, throwable);
068        messageParameters = new Object[] {"warning msg"};
069        warning = new Problem(Status.WARNING, 1, message, messageParameters, resource, location, throwable);
070        messageParameters = new Object[] {"info msg"};
071        info = new Problem(Status.INFO, 1, message, messageParameters, resource, location, throwable);
072        assertThat(error.getMessageString(), is(message.text("error msg")));
073        assertThat(warning.getMessageString(), is(message.text("warning msg")));
074        assertThat(info.getMessageString(), is(message.text("info msg")));
075    }
076}