View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 2005-206
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: ModelValidationReportTest.java 4613 2012-09-22 10:07:08Z schulte $
29   *
30   */
31  package org.jomc.modlet.test;
32  
33  import java.io.IOException;
34  import java.io.ObjectInputStream;
35  import java.util.logging.Level;
36  import org.jomc.modlet.ModelValidationReport;
37  import org.junit.Test;
38  import static org.junit.Assert.assertEquals;
39  import static org.junit.Assert.assertNotNull;
40  import static org.junit.Assert.assertNull;
41  
42  /**
43   * Test cases for class {@code org.jomc.modlet.ModelValidationReport}.
44   *
45   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
46   * @version $JOMC: ModelValidationReportTest.java 4613 2012-09-22 10:07:08Z schulte $
47   */
48  public class ModelValidationReportTest
49  {
50  
51      /** Constant to prefix relative resource names with. */
52      private static final String ABSOLUTE_RESOURCE_NAME_PREFIX = "/org/jomc/modlet/test/";
53  
54      /** Creates a new {@code ModelValidationReportTest} instance. */
55      public ModelValidationReportTest()
56      {
57          super();
58      }
59  
60      @Test
61      public final void testSerializabe() throws Exception
62      {
63          final ModelValidationReport report =
64              this.readObject( ABSOLUTE_RESOURCE_NAME_PREFIX + "ModelValidationReport.ser", ModelValidationReport.class );
65  
66          final ModelValidationReport.Detail detail =
67              this.readObject( ABSOLUTE_RESOURCE_NAME_PREFIX + "ModelValidationReportDetail.ser",
68                               ModelValidationReport.Detail.class );
69  
70          System.out.println( report );
71          System.out.println( detail );
72  
73          assertEquals( 1, report.getDetails( "Identifier 1" ).size() );
74          assertEquals( 1, report.getDetails( "Identifier 2" ).size() );
75          assertEquals( 1, report.getDetails( "Identifier 3" ).size() );
76          assertEquals( 1, report.getDetails( "Identifier 4" ).size() );
77          assertEquals( 1, report.getDetails( "Identifier 5" ).size() );
78          assertEquals( 1, report.getDetails( "Identifier 6" ).size() );
79          assertEquals( 1, report.getDetails( "Identifier 7" ).size() );
80          assertEquals( 1, report.getDetails( "Identifier 8" ).size() );
81          assertEquals( 1, report.getDetails( "Identifier 9" ).size() );
82          assertEquals( 1, report.getDetails( "Identifier 10" ).size() );
83  
84          assertEquals( "Identifier", detail.getIdentifier() );
85          assertEquals( Level.OFF, detail.getLevel() );
86          assertEquals( "Message", detail.getMessage() );
87          assertNull( detail.getElement() );
88      }
89  
90      private <T> T readObject( final String location, final Class<T> type ) throws IOException, ClassNotFoundException
91      {
92          ObjectInputStream in = null;
93          boolean suppressExceptionOnClose = true;
94  
95          try
96          {
97              in = new ObjectInputStream( this.getClass().getResourceAsStream( location ) );
98              assertNotNull( in );
99              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 }