/*
 * Copyright (C) 2015 Google Inc.
 *
 * Licensed 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.google.cloud.dataflow.sdk.values;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

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

/**
 * Tests for {@link TupleTag}.
 */
@RunWith(JUnit4.class)
public class TupleTagTest {

  private static TupleTag<Object> staticTag = new TupleTag<>();
  private static TupleTag<Object> staticBlockTag;
  private static TupleTag<Object> staticMethodTag = createTag();
  private static TupleTag<Object> instanceMethodTag = new AnotherClass().createAnotherTag();

  static {
    staticBlockTag = new TupleTag<>();
  }

  private static TupleTag<Object> createTag() {
    return new TupleTag<>();
  }

  private static class AnotherClass {
    private static TupleTag<Object> anotherTag = new TupleTag<>();
    private TupleTag<Object> createAnotherTag() {
      return new TupleTag<>();
    }
  }

  @Test
  public void testStaticTupleTag() {
    assertEquals("com.google.cloud.dataflow.sdk.values.TupleTagTest#0", staticTag.getId());
    assertEquals("com.google.cloud.dataflow.sdk.values.TupleTagTest#3", staticBlockTag.getId());
    assertEquals("com.google.cloud.dataflow.sdk.values.TupleTagTest#1", staticMethodTag.getId());
    assertEquals("com.google.cloud.dataflow.sdk.values.TupleTagTest#2", instanceMethodTag.getId());
    assertEquals(
        "com.google.cloud.dataflow.sdk.values.TupleTagTest$AnotherClass#0",
        AnotherClass.anotherTag.getId());
  }

  @Test
  public void testNonstaticTupleTag() {
    assertEquals("com.google.cloud.dataflow.sdk.values.TupleTagTest.testNonstaticTupleTag:65",
                 new TupleTag<Object>().getId().split("#")[0]);
    assertNotEquals(new TupleTag<Object>().getId(), new TupleTag<Object>().getId());
  }
}

@@ -632,24 +632,121 @@
 dk.values;%0A%0A
+import static org.hamcrest.Matchers.greaterThan;%0Aimport static org.hamcrest.Matchers.startsWith;%0A
 import stati
@@ -825,16 +825,59 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertThat;
 %0A%0Aimport
@@ -2227,24 +2227,119 @@
 Id());%0A  %7D%0A%0A
+  private TupleTag%3CObject%3E createNonstaticTupleTag() %7B%0A    return new TupleTag%3CObject%3E();%0A  %7D%0A%0A
   @Test%0A  pu
@@ -2388,132 +2388,304 @@
 sert
+Not
 Equals(
-%22com.google.cloud.dataflow.sdk.values.TupleTagTest.testNonstaticTupleTag:65%22,%0A                 new TupleTag%3CObject%3E()
+new TupleTag%3CObject%3E().getId(), new TupleTag%3CObject%3E().getId());%0A    assertNotEquals(createNonstaticTupleTag(), createNonstaticTupleTag());%0A%0A    TupleTag%3CObject%3E tag = createNonstaticTupleTag();%0A%0A    // Check that the name is derived from the method it is created in.%0A    assertThat(tag
 .get
@@ -2706,92 +2706,406 @@
 )%5B0%5D
-);
+,
 %0A    
-assertNotEquals(new TupleTag%3CObject%3E().getId(), new TupleTag%3CObject%3E().getId(
+    startsWith(%22com.google.cloud.dataflow.sdk.values.TupleTagTest.createNonstaticTupleTag%22));%0A%0A    // Check that after the name there is a ':' followed by a line number, and just make%0A    // sure the line number is big enough to be reasonable, so superficial changes don't break%0A    // the test.%0A    assertThat(Integer.parseInt(tag.getId().split(%22#%22)%5B0%5D.split(%22:%22)%5B1%5D),%0A        greaterThan(15
 ));%0A

Make non-static TupleTag test robust to superficial changes