Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6545e22ef707fdd55e738b7598f9ed8c749615db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*******************************************************************************
 * Copyright (c) 2010 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.skynet.core.importing.parsers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.ReservedCharacters;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * Tests different forms of outline numbers and titles
 * 
 * @author Karol M. Wilk
 */
@RunWith(Parameterized.class)
public final class WordOutlineTest {

   private static final Pattern PARAGRAPH_REGEX = Pattern.compile("<w:p[ >].*?</w:p>",
      Pattern.DOTALL | Pattern.MULTILINE);
   private static final Pattern OUTLINE_NUMBER_PATTERN = Pattern.compile("((?>\\d+\\.)+\\d*)\\s*");

   private static final String OUTLINE_WITH_NUMBER = "outlineNameWithNumber.xml";
   private static final String OUTLINE_WITH_NUMBER_AND_CONTENT = "outlineNameNumberAndContent.xml";
   private static final String NUMBER_EMBED_IN_CONTENT = "numberEmbeddedInTheContent.xml";

   private final WordOutlineExtractorDelegate delegate;
   private final String dataFileName;
   private final DelegateData[] expectedData;

   public WordOutlineTest(String dataFileName, DelegateData... expectedData) {
      this.delegate = new WordOutlineExtractorDelegate();
      this.dataFileName = dataFileName;
      this.expectedData = expectedData;
   }

   /**
    * Note: some of the data objects need to repeat data from previous test because they are considered to be
    * lastHeaderNumber or lastHeaderName or lastContent
    * 
    * @return collection of data sets used as input for parameterized unit test
    */
   @Parameters
   public static Collection<Object[]> getData() {
      List<Object[]> data = new ArrayList<Object[]>();
      addTest(data, OUTLINE_WITH_NUMBER, data("1.0", "Outline TITLE", ""));
      addTest(data, OUTLINE_WITH_NUMBER_AND_CONTENT, data("5.0", "SCOPE", ""),
         data("5.0", "SCOPE", "content content content more content"));

      StringBuilder builder = new StringBuilder();
      builder.append("This 1.6 is some interesting content 2.3SAMPL");
      builder.append(ReservedCharacters.toCharacter("&acirc;"));
      builder.append(ReservedCharacters.toCharacter("&euro;"));
      builder.append(ReservedCharacters.toCharacter("&ldquo;"));
      builder.append("10.");

      addTest(data, NUMBER_EMBED_IN_CONTENT, data("1.0", "SCOPE", ""), data("1.0", "SCOPE", builder.toString()));
      return data;
   }

   @Test
   public void testDelegate() throws Exception {
      delegate.initialize();

      String rawData = Lib.fileToString(getClass(), dataFileName);
      Matcher matcher = PARAGRAPH_REGEX.matcher(rawData);
      boolean foundSomething = false;

      List<DelegateData> actualData = new ArrayList<DelegateData>();

      while (matcher.find()) {
         foundSomething = true;
         String data = matcher.group();
         delegate.processContent(null, null, false, false, null, null, null, data, false);

         String headerNumber = delegate.getLastHeaderNumber().trim();
         String headerName = delegate.getLastHeaderName().trim();
         String content = delegate.getLastContent().trim();

         actualData.add(data(headerNumber, headerName, content));
      }

      Assert.assertTrue("WordOutlineTester no paragraphs found...", foundSomething);
      for (int index = 0; index < expectedData.length; index++) {
         DelegateData expected = expectedData[index];
         DelegateData actual = actualData.get(index);
         Assert.assertEquals(
            String.format(
               "\nChecking %s of %s,\nEXPECTED: \n\t Number:\"%s\" \n\t Title:\"%s\" \n\t Content:\"%s\"\nACTUAL: \n\t Number:\"%s\" \n\t Title:\"%s\" \n\t Content:\"%s\"\n",
               index, dataFileName, expected.getHeaderNumber(), expected.getHeaderName(), expected.getContent(),
               actual.getHeaderNumber(), actual.getHeaderName(), actual.getContent()), expected, actual);
         if (Strings.isValid(expected.getHeaderNumber())) {
            Assert.assertTrue("WordOutlineTester doesn't look like a outline number...",
               OUTLINE_NUMBER_PATTERN.matcher(actual.getHeaderNumber()).matches());
         }
      }
   }

   @After
   public void testCleanup() {
      delegate.dispose();
      Assert.assertNull(delegate.getLastHeaderNumber());
      Assert.assertNull(delegate.getLastHeaderName());
      Assert.assertNull(delegate.getLastContent());
   }

   private static void addTest(List<Object[]> data, String dataFileName, DelegateData... expectedData) {
      data.add(new Object[] {dataFileName, expectedData});
   }

   private static DelegateData data(String headerNumber, String headerName, String content) {
      return new DelegateData(headerNumber, headerName, content);
   }

   private static final class DelegateData {
      private final String headerNumber;
      private final String headerName;
      private final String content;

      public DelegateData(String headerNumber, String headerName, String content) {
         this.headerNumber = headerNumber;
         this.headerName = headerName;
         this.content = content;
      }

      public String getHeaderNumber() {
         return headerNumber;
      }

      public String getHeaderName() {
         return headerName;
      }

      public String getContent() {
         return content;
      }

      @Override
      public int hashCode() {
         final int prime = 31;
         int result = 1;
         result = prime * result + (content == null ? 0 : content.hashCode());
         result = prime * result + (headerName == null ? 0 : headerName.hashCode());
         result = prime * result + (headerNumber == null ? 0 : headerNumber.hashCode());
         return result;
      }

      @Override
      public boolean equals(Object obj) {
         if (this == obj) {
            return true;
         }
         if (obj == null) {
            return false;
         }
         if (getClass() != obj.getClass()) {
            return false;
         }
         DelegateData other = (DelegateData) obj;
         if (content == null) {
            if (other.content != null) {
               return false;
            }
         } else if (!content.equals(other.content)) {
            return false;
         }
         if (headerName == null) {
            if (other.headerName != null) {
               return false;
            }
         } else if (!headerName.equals(other.headerName)) {
            return false;
         }
         if (headerNumber == null) {
            if (other.headerNumber != null) {
               return false;
            }
         } else if (!headerNumber.equals(other.headerNumber)) {
            return false;
         }
         return true;
      }

      @Override
      public String toString() {
         return String.format("DelegateData [headerNumber=[%s], headerName=[%s], content=[%s]]", headerNumber,
            headerName, content);
      }
   }
}

Back to the top