func TestHelperProcess(*testing.T) {
	if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
		return
	}
	defer os.Exit(0)
	args := os.Args
	for len(args) > 0 {
		if args[0] == "--" {
			args = args[1:]
			break
		}
		args = args[1:]
	}
	if args[0] == "sleep" {
		l, err := strconv.Atoi(args[1])
		if err != nil {
			os.Exit(1)
		}
		time.Sleep(time.Duration(l) * time.Second)
	}
}
TestHelperProcess isn't a real test. It's used as a helper process
for TestParameterRun.
Adapted from os/exec/exec_test.go.
package tika
import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"os"
	"os/exec"
	"strconv"
	"testing"
	"time"
)
func init() {
	command = func(string, ...string) *exec.Cmd {
		c := exec.Command(os.Args[0], "-test.run=TestHelperProcess", "--", "sleep", "2")
		c.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
		return c
	}
}
func TestNewServerError(t *testing.T) {
	tests := []struct {
		name string
		jar  string
		port string
	}{
		{name: "no jar path"},
		{name: "invalid port", jar: "jar/path", port: "%31"},
	}
	for _, test := range tests {
		if _, err := NewServer(test.jar, test.port); err == nil {
			t.Errorf("NewServer(%s) got no error", test.name)
		}
	}
}
func TestStart(t *testing.T) {
	path, err := os.Executable()  
	if err != nil {
		t.Skip("cannot find current test executable")
	}
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		fmt.Fprint(w, "1.14")
	}))
	defer ts.Close()
	tsURL, err := url.Parse(ts.URL)
	if err != nil {
		t.Fatalf("error creating test server: %v", err)
	}
	s, err := NewServer(path, tsURL.Port())
	if err != nil {
		t.Fatalf("NewServer got error: %v", err)
	}
	err = s.Start(context.Background())
	if err != nil {
		t.Fatalf("Start got error: %v", err)
	}
	s.Stop()
}
func bouncyServer(bounce int) *httptest.Server {
	bounced := 0
	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		if bounced < bounce {
			bounced++
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		fmt.Fprint(w, "1.14")
	}))
}
func TestStartError(t *testing.T) {
	path, err := os.Executable()
	if err != nil {
		t.Skip("cannot find current test executable")
	}
	ts := bouncyServer(4)
	defer ts.Close()
	tsURL, err := url.Parse(ts.URL)
	if err != nil {
		t.Fatalf("error creating test server: %v", err)
	}
	s, err := NewServer(path, tsURL.Port())
	if err != nil {
		t.Fatalf("NewServer got error: %v", err)
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	if err := s.Start(ctx); err == nil {
		t.Fatalf("s.Start got no error, want error")
	}
	s.jar = "nonexistentFile.jar"
	if err := s.Start(ctx); err == nil {
		t.Fatalf("s.Start got no error, want error for missing Jar file")
	}
}
func TestURL(t *testing.T) {
	tests := []string{"", "test"}
	for _, test := range tests {
		s := &Server{url: test}
		if got := s.URL(); got != test {
			t.Errorf("URL() = %q, want %q", got, test)
		}
	}
}
func TestWaitForStart(t *testing.T) {
	tests := []struct {
		name        string
		reqToBounce int
		wantError   bool
		timeout     time.Duration
	}{
		{"not bounced", 0, false, 5 * time.Second},
		{"bounced twice", 2, false, 5 * time.Second},
		{"bounced for too long", 4, true, 2 * time.Second},
	}
	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()
			ts := bouncyServer(test.reqToBounce)
			defer ts.Close()
			s := &Server{url: ts.URL}
			ctx, cancel := context.WithTimeout(context.Background(), test.timeout)
			defer cancel()
			got := s.waitForStart(ctx)
			if test.wantError && got == nil {
				t.Errorf("waitForStart(%s) got no error, want error", test.name)
			}
			if test.wantError {
				ts.Close()
				return
			}
			if got != nil {
				t.Errorf("waitForStart(%s) got %v, want no error", test.name, got)
			}
		})
	}
}