package com.google.openlocationcode.tests;

import com.google.openlocationcode.OpenLocationCode;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/** Tests shortening functionality of Open Location Code. */
@RunWith(JUnit4.class)
public class ShorteningTest {

  private class TestData {

    private final String code;
    private final double referenceLatitude;
    private final double referenceLongitude;
    private final String shortCode;
    private final String testType;

    public TestData(String line) {
      String[] parts = line.split(",");
      if (parts.length != 5) {
        throw new IllegalArgumentException("Wrong format of testing data.");
      }
      this.code = parts[0];
      this.referenceLatitude = Double.valueOf(parts[1]);
      this.referenceLongitude = Double.valueOf(parts[2]);
      this.shortCode = parts[3];
      this.testType = parts[4];
    }
  }

  private static final String TEST_PATH =
      System.getenv("JAVA_RUNFILES") + "/openlocationcode/test_data";
  private final List<TestData> testDataList = new ArrayList<>();

  @Before
  public void setUp() throws Exception {
    File testFile = new File(TEST_PATH, "shortCodeTests.csv");
    InputStream testDataStream = new FileInputStream(testFile);
    BufferedReader reader = new BufferedReader(new InputStreamReader(testDataStream));
    String line;
    while ((line = reader.readLine()) != null) {
      if (line.startsWith("#")) {
        continue;
      }
      testDataList.add(new TestData(line));
    }
  }

  @Test
  public void testShortening() {
    for (TestData testData : testDataList) {
      if (testData.testType != "B" && testData.testType != "S") {
        continue;
      }
      OpenLocationCode olc = new OpenLocationCode(testData.code);
      OpenLocationCode shortened =
          olc.shorten(testData.referenceLatitude, testData.referenceLongitude);
      Assert.assertEquals(
          "Wrong shortening of code " + testData.code,
          testData.shortCode,
          shortened.getCode());
    }
  }

  @Test
  public void testRecovering() {
    for (TestData testData : testDataList) {
      if (testData.testType != "B" && testData.testType != "R") {
        continue;
      }
      OpenLocationCode olc = new OpenLocationCode(testData.shortCode);
      OpenLocationCode recovered =
          olc.recover(testData.referenceLatitude, testData.referenceLongitude);
      Assert.assertEquals(testData.code, recovered.getCode());
    }
  }
}

@@ -48,192 +48,108 @@
 ort 
-com.google.openlocationcode.OpenLocationCode;%0A%0Aimport org.junit.Assert;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.junit.runners.JUnit4
+static java.nio.charset.StandardCharsets.UTF_8;%0A%0Aimport com.google.openlocationcode.OpenLocationCode
 ;%0A%0Ai
@@ -343,16 +343,155 @@
 il.List;
+%0Aimport org.junit.Assert;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.junit.runners.JUnit4;
 %0A%0A/** Te
@@ -609,16 +609,23 @@
 private 
+static 
 class Te
@@ -1676,16 +1676,23 @@
 aStream)
+, UTF_8
 );%0A    S
@@ -1954,32 +1954,44 @@
 ist) %7B%0A      if 
+(!%22B%22.equals
 (testData.testTy
@@ -1988,35 +1988,41 @@
 ata.testType
- != %22B%22 && 
+) && !%22S%22.equals(
 testData.tes
@@ -2030,15 +2030,9 @@
 Type
- != %22S%22
+)
 ) %7B%0A
@@ -2488,16 +2488,28 @@
      if 
+(!%22B%22.equals
 (testDat
@@ -2522,19 +2522,25 @@
 Type
- != %22B%22 && 
+) && !%22R%22.equals(
 test
@@ -2556,15 +2556,9 @@
 Type
- != %22R%22
+)
 ) %7B%0A

Update ShorteningTest.java