001/*
002 *   Copyright (C) Christian Schulte, 2005-206
003 *   All rights reserved.
004 *
005 *   Redistribution and use in source and binary forms, with or without
006 *   modification, are permitted provided that the following conditions
007 *   are met:
008 *
009 *     o Redistributions of source code must retain the above copyright
010 *       notice, this list of conditions and the following disclaimer.
011 *
012 *     o Redistributions in binary form must reproduce the above copyright
013 *       notice, this list of conditions and the following disclaimer in
014 *       the documentation and/or other materials provided with the
015 *       distribution.
016 *
017 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 *   $JOMC: ModelValidationReportTest.java 4613 2012-09-22 10:07:08Z schulte $
029 *
030 */
031package org.jomc.modlet.test;
032
033import java.io.IOException;
034import java.io.ObjectInputStream;
035import java.util.logging.Level;
036import org.jomc.modlet.ModelValidationReport;
037import org.junit.Test;
038import static org.junit.Assert.assertEquals;
039import static org.junit.Assert.assertNotNull;
040import static org.junit.Assert.assertNull;
041
042/**
043 * Test cases for class {@code org.jomc.modlet.ModelValidationReport}.
044 *
045 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
046 * @version $JOMC: ModelValidationReportTest.java 4613 2012-09-22 10:07:08Z schulte $
047 */
048public class ModelValidationReportTest
049{
050
051    /** Constant to prefix relative resource names with. */
052    private static final String ABSOLUTE_RESOURCE_NAME_PREFIX = "/org/jomc/modlet/test/";
053
054    /** Creates a new {@code ModelValidationReportTest} instance. */
055    public ModelValidationReportTest()
056    {
057        super();
058    }
059
060    @Test
061    public final void testSerializabe() throws Exception
062    {
063        final ModelValidationReport report =
064            this.readObject( ABSOLUTE_RESOURCE_NAME_PREFIX + "ModelValidationReport.ser", ModelValidationReport.class );
065
066        final ModelValidationReport.Detail detail =
067            this.readObject( ABSOLUTE_RESOURCE_NAME_PREFIX + "ModelValidationReportDetail.ser",
068                             ModelValidationReport.Detail.class );
069
070        System.out.println( report );
071        System.out.println( detail );
072
073        assertEquals( 1, report.getDetails( "Identifier 1" ).size() );
074        assertEquals( 1, report.getDetails( "Identifier 2" ).size() );
075        assertEquals( 1, report.getDetails( "Identifier 3" ).size() );
076        assertEquals( 1, report.getDetails( "Identifier 4" ).size() );
077        assertEquals( 1, report.getDetails( "Identifier 5" ).size() );
078        assertEquals( 1, report.getDetails( "Identifier 6" ).size() );
079        assertEquals( 1, report.getDetails( "Identifier 7" ).size() );
080        assertEquals( 1, report.getDetails( "Identifier 8" ).size() );
081        assertEquals( 1, report.getDetails( "Identifier 9" ).size() );
082        assertEquals( 1, report.getDetails( "Identifier 10" ).size() );
083
084        assertEquals( "Identifier", detail.getIdentifier() );
085        assertEquals( Level.OFF, detail.getLevel() );
086        assertEquals( "Message", detail.getMessage() );
087        assertNull( detail.getElement() );
088    }
089
090    private <T> T readObject( final String location, final Class<T> type ) throws IOException, ClassNotFoundException
091    {
092        ObjectInputStream in = null;
093        boolean suppressExceptionOnClose = true;
094
095        try
096        {
097            in = new ObjectInputStream( this.getClass().getResourceAsStream( location ) );
098            assertNotNull( in );
099            final T object = (T) in.readObject();
100            suppressExceptionOnClose = false;
101            return object;
102        }
103        finally
104        {
105            try
106            {
107                if ( in != null )
108                {
109                    in.close();
110                }
111            }
112            catch ( final IOException e )
113            {
114                if ( !suppressExceptionOnClose )
115                {
116                    throw e;
117                }
118            }
119        }
120    }
121
122}