Skip to main content
summaryrefslogtreecommitdiffstats
blob: c27b19a9728f3b9fa4364623fa14d52dcec51013 (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
/*******************************************************************************
 * 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.jdk.core.test.text;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Stack;
import junit.framework.TestCase;
import org.eclipse.osee.framework.jdk.core.text.Rule;
import org.eclipse.osee.framework.jdk.core.text.change.ChangeSet;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.junit.Assert;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
 * Tests ability to read files in charsets, particularly UTF8
 * 
 * <pre>
 * {@link Rule}
 * {@link Lib}
 * </pre>
 * 
 * @author Karol M. Wilk
 */
public final class UtfReadingRuleTest extends TestCase {

   private static final String FILE_INPUT = "utf8_input.xml";

   @org.junit.Rule
   public TemporaryFolder tempFolder = new TemporaryFolder();

   private File getInputFile() throws IOException {
      File outfile = tempFolder.newFile("utf8_input_copy.xml");
      URL url = UtfReadingRuleTest.class.getResource(FILE_INPUT);
      InputStream inputStream = null;
      try {
         inputStream = new BufferedInputStream(url.openStream());
         Lib.inputStreamToFile(inputStream, outfile);
      } finally {
         Lib.close(inputStream);
      }
      return outfile;
   }

   private final Utf8TestRule rule = new Utf8TestRule();

   @Test(expected = NullPointerException.class)
   public void testWrongFileName() throws IOException {
      rule.process(new File("./notexistentFile.txt"));
   }

   @Test
   public void testCharset() {
      Stack<String> charsetStack = new Stack<String>();
      charsetStack.add("UnknownCharset");
      charsetStack.add("UTF-8");

      while (!charsetStack.isEmpty()) {
         try {
            File inputFile = getInputFile();
            rule.setCharsetString(charsetStack.pop());
            rule.process(inputFile);
         } catch (Exception ex) {
            Assert.assertTrue("unexpected/wrong exception thrown testCharset(), Exception: " + ex.toString(),
               ex instanceof UnsupportedCharsetException || ex instanceof UnsupportedEncodingException);
         }
      }
   }

   @Test
   public void testUtf8ReadData() throws IOException {
      try {
         File inputFile = getInputFile();
         rule.process(inputFile);
      } catch (UnsupportedCharsetException ex) {
         Assert.assertTrue(true);
      } catch (Exception ex) {
         Assert.fail("unexpected/wrong exception thrown testCharset()");
      }

      //trim off extra data
      String expectedUtf8String = Lib.fileToString(getInputFile());
      String actual = rule.getLastOutput().toString().trim();
      Assert.assertEquals(expectedUtf8String, actual);
   }

   private static final class Utf8TestRule extends Rule {
      private CharSequence lastOutput;

      public Utf8TestRule() {
         lastOutput = null;
      }

      @Override
      public ChangeSet computeChanges(final CharSequence seq) {
         lastOutput = seq;
         return new ChangeSet(seq);
      }

      public CharSequence getLastOutput() {
         return lastOutput;
      }
   }
}

Back to the top