View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package test.de.smartics.properties.reports;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.allOf;
20  import static org.hamcrest.Matchers.containsString;
21  import static org.hamcrest.Matchers.equalTo;
22  import static org.hamcrest.Matchers.is;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.when;
25  
26  import java.io.ByteArrayOutputStream;
27  import java.io.UnsupportedEncodingException;
28  
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  import de.smartics.properties.api.core.domain.DocumentMetaData;
34  import de.smartics.properties.api.core.domain.PropertyDescriptor;
35  import de.smartics.properties.api.core.domain.PropertyExpression;
36  import de.smartics.properties.api.core.domain.PropertyKey;
37  import de.smartics.properties.report.data.PropertyReportItem;
38  import de.smartics.properties.report.data.PropertyReportSet;
39  import de.smartics.properties.reports.PropertiesPropertyReport;
40  import de.smartics.properties.spi.core.metadata.projectdoc.ProjectdocMetaData;
41  import de.smartics.testdoc.annotations.Uut;
42  import de.smartics.testdoc.categories.Technical;
43  import de.smartics.util.lang.NullArgumentException;
44  
45  /**
46   * Tests {@link PropertiesPropertyReport}.
47   */
48  public class PropertiesPropertyReportTest
49  {
50    // ********************************* Fields *********************************
51  
52    // --- constants ------------------------------------------------------------
53  
54    private static final String KEY_STRING_1 = "test.name.x";
55  
56    private static final String EXPRESSION_1 = null;
57  
58    private static final String KEY_STRING_2 = "test.name.y";
59  
60    private static final String EXPRESSION_2 = "42";
61  
62    // --- members --------------------------------------------------------------
63  
64    @Uut
65    private PropertiesPropertyReport uut;
66  
67    private ByteArrayOutputStream out;
68  
69    // ****************************** Inner Classes *****************************
70  
71    // ********************************* Methods ********************************
72  
73    // --- prepare --------------------------------------------------------------
74  
75    @Before
76    public void setUp()
77    {
78      out = new ByteArrayOutputStream(2048);
79      uut = new PropertiesPropertyReport(out);
80    }
81  
82    // --- helper ---------------------------------------------------------------
83  
84    private static PropertyReportItem.Builder createBuilder(
85        final String keyString, final String expression)
86    {
87      final PropertyDescriptor descriptor = mock(PropertyDescriptor.class);
88      final PropertyKey key = new PropertyKey(keyString);
89      when(descriptor.getKey()).thenReturn(key);
90      if (expression != null)
91      {
92        final PropertyExpression ex = PropertyExpression.create(expression);
93        when(descriptor.getDefaultExpression()).thenReturn(ex);
94      }
95      final DocumentMetaData metaData = new ProjectdocMetaData();
96      when(descriptor.getDocumentMetaData()).thenReturn(metaData);
97  
98      final PropertyReportItem.Builder builder = new PropertyReportItem.Builder();
99      builder.with(descriptor);
100     return builder;
101   }
102 
103   private void addItem(final String keyString, final String expression)
104   {
105     final PropertyReportItem.Builder builder =
106         createBuilder(keyString, expression);
107     final PropertyReportItem item = builder.build();
108     uut.handle(item);
109   }
110 
111   // --- tests ----------------------------------------------------------------
112 
113   @Test(expected = NullArgumentException.class)
114   @Category(Technical.class)
115   public void requiresANonNullStream()
116   {
117     new PropertiesPropertyReport(null);
118   }
119 
120   @Test
121   @Category(Technical.class)
122   public void allowsFlushingAnEmptyPropertySet()
123   {
124     uut.flush();
125     final int writtenBytes = out.toByteArray().length;
126     assertThat(writtenBytes, is(equalTo(0)));
127   }
128 
129   @Test
130   @SuppressWarnings("unchecked")
131   public void addsItemsWithoutPropertySet() throws UnsupportedEncodingException
132   {
133     addItem(KEY_STRING_1, EXPRESSION_1);
134     addItem(KEY_STRING_2, EXPRESSION_2);
135 
136     uut.flush();
137 
138     final String written = out.toString("ISO8859_1");
139     assertThat(
140         written,
141         allOf(containsString(KEY_STRING_1), containsString(KEY_STRING_2),
142             containsString(EXPRESSION_2)));
143   }
144 
145   @Test
146   @SuppressWarnings("unchecked")
147   public void addsItemsWithPropertySet() throws UnsupportedEncodingException
148   {
149     final PropertyReportSet.Builder builder = new PropertyReportSet.Builder();
150     builder.withComment("comment").withType("type");
151     final PropertyReportSet reportSet = builder.build();
152     uut.handle(reportSet);
153 
154     addItem(KEY_STRING_1, EXPRESSION_1);
155     addItem(KEY_STRING_2, EXPRESSION_2);
156 
157     uut.flush();
158 
159     final String written = out.toString("ISO8859_1");
160     assertThat(
161         written,
162         allOf(containsString("type"), containsString("comment"),
163             containsString(KEY_STRING_1), containsString(KEY_STRING_2),
164             containsString(EXPRESSION_2)));
165   }
166 }