Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6865b4e09b3615a17abbf874728c8c7f220b233a (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.ui.skynet.test.renderer.imageDetection;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.skynet.render.WordImageChecker;
import org.eclipse.osee.framework.ui.skynet.render.WordmlPicture;
import org.eclipse.osee.framework.ui.skynet.render.imageDetection.WordImageCompare;
import org.junit.Assert;

/**
 * Test case for {@link WordImageCompare}.
 * 
 * @author Jeff C. Phillips
 */
public class WordImageCompareTest {
   private static final String A_MATCH = "support/A_Match.xml";
   private static final String B_MATCH = "support/B_Match.xml";
   private static final String B_NO_MATCH = "support/B_No_Match.xml";
   private static final String WMZ_MATCH = "support/WMZ_File.xml";
   private static final String WMZ_NO_MATCH = "support/WMZ_NO_MATCH.xml";

   @org.junit.Test
   public void testMatchedImages() throws Exception {
      Assert.assertTrue(compareFile(A_MATCH, B_MATCH));
   }

   @org.junit.Test
   public void testNoMatchChangedImages() throws Exception {
      Assert.assertFalse(compareFile(A_MATCH, B_NO_MATCH));
   }

   @org.junit.Test
   public void testWMZMatchedImages() throws Exception {
      Assert.assertTrue(compareFile(WMZ_MATCH, WMZ_MATCH));
   }

   @org.junit.Test
   public void testWMZNoMatchImages() throws Exception {
      Assert.assertFalse(compareFile(WMZ_MATCH, WMZ_NO_MATCH));
   }

   private boolean compareFile(String firstFileName, String secondFileName) throws IOException {
      boolean isEqual = false;
      String firstFile = getContent(firstFileName);
      String secondFile = getContent(secondFileName);

      List<WordmlPicture> firstFileImages = createPictureList(firstFile);
      List<WordmlPicture> secondFileImages = createPictureList(secondFile);
      WordImageCompare compare = new WordImageCompare();

      if (firstFileImages.size() > 0 && secondFileImages.size() == secondFileImages.size()) {
         isEqual = true;
         for (int i = 0; i < firstFileImages.size(); i++) {
            isEqual &=
               compare.compareFiles(firstFileImages.get(i).getBinaryData(), secondFileImages.get(i).getBinaryData());
         }
      }
      return isEqual;
   }

   private static List<WordmlPicture> createPictureList(String wordml) {
      int startIndex = 0;
      List<WordmlPicture> pictures = new LinkedList<WordmlPicture>();
      while (wordml.indexOf("<w:pict>", startIndex) > 0) {
         int currentStartIndex = wordml.indexOf("<w:pict>", startIndex);
         int currentEndIndex = wordml.indexOf("</w:pict", currentStartIndex);
         if (currentEndIndex > 0) {
            try {
               pictures.add(new WordmlPicture(currentStartIndex, wordml.substring(currentStartIndex, currentEndIndex),
                  wordml, null));
            } catch (OseeCoreException ex) {
               OseeLog.log(WordImageChecker.class, Level.WARNING, ex);
            }
         }
         startIndex = currentEndIndex;
      }
      return pictures;
   }

   private static final String getContent(String filePath) throws IOException {
      InputStream inputStream = null;
      try {
         inputStream = new BufferedInputStream(WordImageCompareTest.class.getResourceAsStream(filePath));
         String data = Lib.inputStreamToString(inputStream);
         return data.replaceAll("\r?\n", "\r\n");
      } finally {
         Lib.close(inputStream);
      }
   }
}

Back to the top