<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.spatial4j.core.io;

import io.jeo.geom.GeomBuilder;

import java.io.IOException;
import java.text.ParseException;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.spatial4j.core.context.jts.JtsSpatialContext;
import com.spatial4j.core.context.jts.JtsSpatialContextFactory;
import com.spatial4j.core.exception.InvalidShapeException;
import com.spatial4j.core.shape.Shape;

public abstract class GeneralReadWriteShapeTest extends BaseRoundTripTest<JtsSpatialContext> {

  protected GeomBuilder gb;

  @Before
  public void setUp() {
    gb = new GeomBuilder();
  }

  @Override
  public JtsSpatialContext initContext() {
    // Like the out-of-the-box GEO, but it wraps datelines
    JtsSpatialContextFactory factory = new JtsSpatialContextFactory();
    factory.geo = true;
    factory.normWrapLongitude = true;
    return new JtsSpatialContext(factory);
  }
  
  
  protected abstract ShapeReader getShapeReader();
  protected abstract ShapeWriter getShapeWriter();

  protected abstract ShapeWriter getShapeWriterForTests();
  
  @Override
  protected void assertRoundTrip(Shape shape, boolean andEquals) {
    try {
      String str = getShapeWriter().toString(shape);
      Shape out = getShapeReader().read(str);

      // GeoJSON has limited numberic precision so the off by .0000001 does not affect its equals
      ShapeWriter writer = getShapeWriterForTests();
      Assert.assertEquals(writer.toString(shape), writer.toString(out));
      
      if(andEquals) {
        Assert.assertEquals(shape, out);
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    } catch (InvalidShapeException e) {
      throw new RuntimeException(e);
    } catch (ParseException e) {
      throw new RuntimeException(e);
    }
  }
  
  @Test
  public void testWriteThenReadPoint() throws Exception {
    assertRoundTrip(point());
  }

  @Test
  public void testWriteThenReadLineString() throws Exception {
    assertRoundTrip(line());
  }

  @Test
  public void testWriteThenReadPolygon() throws Exception {
    assertRoundTrip(polygon1());
    assertRoundTrip(polygon2());
  }

  @Test
  public void testWriteThenReadMultiPoint() throws Exception {
    assertRoundTrip(multiPoint());
  }

  @Test
  public void testWriteThenReadMultiLineString() throws Exception {
    assertRoundTrip(multiLine());
  }

  @Test
  public void testWriteThenReadMultiPolygon() throws Exception {
    assertRoundTrip(multiPolygon());
  }

  @Test
  public void testWriteThenReadRectangle() throws Exception {
    assertRoundTrip(polygon1().getBoundingBox(), false);
  }
  
//  @Test
//  public void testWriteThenReadCollection() throws Exception {
//    assertRoundTrip(collection());
//  }

  String pointText() {
    return strip("{'type': 'Point','coordinates':[100.1,0.1]}");
  }

  com.spatial4j.core.shape.Point point() {
    return ctx.makePoint(100.1, 0.1);
  }

  String lineText() {
    return strip(
      "{'type': 'LineString', 'coordinates': [[100.1,0.1],[101.1,1.1]]}");
  }

  Shape line() {
    return ctx.makeShape(gb.points(100.1, 0.1, 101.1,1.1).toLineString());
  }

  Shape polygon1() {
    return ctx.makeShape(gb.points(100.1, 0.1, 101.1, 0.1, 101.1, 1.1, 100.1, 1.1, 100.1, 0.1).ring().toPolygon());
  }

  String polygonText1() {
    return strip("{ 'type': 'Polygon',"+
    "'coordinates': ["+
    "  [ [100.1, 0.1], [101.1, 0.1], [101.1, 1.1], [100.1, 1.1], [100.1, 0.1] ]"+<commit_msg>use ignore and add comment to read/write collection<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.spatial4j.core.io;

import io.jeo.geom.GeomBuilder;

import java.io.IOException;
import java.text.ParseException;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.spatial4j.core.context.jts.JtsSpatialContext;
import com.spatial4j.core.context.jts.JtsSpatialContextFactory;
import com.spatial4j.core.exception.InvalidShapeException;
import com.spatial4j.core.shape.Shape;

public abstract class GeneralReadWriteShapeTest extends BaseRoundTripTest<JtsSpatialContext> {

  protected GeomBuilder gb;

  @Before
  public void setUp() {
    gb = new GeomBuilder();
  }

  @Override
  public JtsSpatialContext initContext() {
    // Like the out-of-the-box GEO, but it wraps datelines
    JtsSpatialContextFactory factory = new JtsSpatialContextFactory();
    factory.geo = true;
    factory.normWrapLongitude = true;
    return new JtsSpatialContext(factory);
  }
  
  
  protected abstract ShapeReader getShapeReader();
  protected abstract ShapeWriter getShapeWriter();

  protected abstract ShapeWriter getShapeWriterForTests();
  
  @Override
  protected void assertRoundTrip(Shape shape, boolean andEquals) {
    try {
      String str = getShapeWriter().toString(shape);
      Shape out = getShapeReader().read(str);

      // GeoJSON has limited numberic precision so the off by .0000001 does not affect its equals
      ShapeWriter writer = getShapeWriterForTests();
      Assert.assertEquals(writer.toString(shape), writer.toString(out));
      
      if(andEquals) {
        Assert.assertEquals(shape, out);
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    } catch (InvalidShapeException e) {
      throw new RuntimeException(e);
    } catch (ParseException e) {
      throw new RuntimeException(e);
    }
  }
  
  @Test
  public void testWriteThenReadPoint() throws Exception {
    assertRoundTrip(point());
  }

  @Test
  public void testWriteThenReadLineString() throws Exception {
    assertRoundTrip(line());
  }

  @Test
  public void testWriteThenReadPolygon() throws Exception {
    assertRoundTrip(polygon1());
    assertRoundTrip(polygon2());
  }

  @Test
  public void testWriteThenReadMultiPoint() throws Exception {
    assertRoundTrip(multiPoint());
  }

  @Test
  public void testWriteThenReadMultiLineString() throws Exception {
    assertRoundTrip(multiLine());
  }

  @Test
  public void testWriteThenReadMultiPolygon() throws Exception {
    assertRoundTrip(multiPolygon());
  }

  @Test
  public void testWriteThenReadRectangle() throws Exception {
    assertRoundTrip(polygon1().getBoundingBox(), false);
  }
  
  @Ignore // "JtsGeometry does not support GeometryCollection but does support its subclasses."
  @Test
  public void testWriteThenReadCollection() throws Exception {
    assertRoundTrip(collection());
  }

  String pointText() {
    return strip("{'type': 'Point','coordinates':[100.1,0.1]}");
  }

  com.spatial4j.core.shape.Point point() {
    return ctx.makePoint(100.1, 0.1);
  }

  String lineText() {
    return strip(
      "{'type': 'LineString', 'coordinates': [[100.1,0.1],[101.1,1.1]]}");
  }

  Shape line() {
    return ctx.makeShape(gb.points(100.1, 0.1, 101.1,1.1).toLineString());
  }

  Shape polygon1() {
    return ctx.makeShape(gb.points(100.1, 0.1, 101.1, 0.1, 101.1, 1.1, 100.1, 1.1, 100.1, 0.1).ring().toPolygon());
  }

  String polygonText1() {
    return strip("{ 'type': 'Polygon',"+
    "'coordinates': ["+
    "  [ [100.1, 0.1], [101.1, 0.1], [101.1, 1.1], [100.1, 1.1], [100.1, 0.1] ]"+
{'fraction_non_alphanumeric': 0.11334636165076445, 'fraction_numerical': 0.027129555121278306, 'mean_word_length': 3.8226164079822618, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3228201', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2636, 'n_words': 726}