github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/test_framework/util.go (about)

     1  package test_framework
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/balzaczyy/golucene/core/index"
     7  	"github.com/balzaczyy/golucene/core/store"
     8  	"github.com/balzaczyy/golucene/core/util"
     9  	. "github.com/balzaczyy/golucene/test_framework/util"
    10  	"log"
    11  )
    12  
    13  // util/_TestUtil.java
    14  
    15  func CheckIndex(dir store.Directory, crossCheckTermVectors bool) *index.CheckIndexStatus {
    16  	var buf bytes.Buffer
    17  	checker := index.NewCheckIndex(dir, crossCheckTermVectors, &buf)
    18  	indexStatus := checker.CheckIndex(nil)
    19  	if indexStatus == nil || !indexStatus.Clean {
    20  		fmt.Println("CheckIndex failed")
    21  		fmt.Println(buf.String())
    22  		panic("CheckIndex failed")
    23  	}
    24  	if INFOSTREAM {
    25  		log.Println(buf.String())
    26  	}
    27  	return indexStatus
    28  }
    29  
    30  // util/CloseableDirectory.java
    31  
    32  // Attempts to close a BaseDirectoryWrapper
    33  func NewCloseableDirectory(dir BaseDirectoryWrapper, failureMarker *TestRuleMarkFailure) func() error {
    34  	return func() error {
    35  		// We only attempt to check open/closed state if there were no other test
    36  		// failures.
    37  		// TODO: perform real close of the delegate: LUCENE-4058
    38  		// defer dir.Close()
    39  		if failureMarker.WasSuccessful() && dir.IsOpen() {
    40  			panic(fmt.Sprintf("Directory not closed: %v", dir))
    41  		}
    42  		return nil
    43  	}
    44  }
    45  
    46  // util/NullInfoStream.java
    47  
    48  // Prints nothing. Just to make sure tests pass w/ and w/o enabled
    49  // InfoStream without actually making noise.
    50  type NullInfoStream int
    51  
    52  var nullInfoStream = NullInfoStream(0)
    53  
    54  func NewNullInfoStream() util.InfoStream {
    55  	return nullInfoStream
    56  }
    57  
    58  func (is NullInfoStream) Message(component, message string, args ...interface{}) {
    59  	assert(component != "")
    60  	assert(message != "")
    61  }
    62  
    63  func (is NullInfoStream) IsEnabled(component string) bool {
    64  	assert(component != "")
    65  	return true // to actually enable logging, we just ignore on message()
    66  }
    67  
    68  func (is NullInfoStream) Close() error {
    69  	return nil
    70  }
    71  
    72  func (is NullInfoStream) Clone() util.InfoStream {
    73  	return is
    74  }
    75  
    76  // func assert(ok bool) {
    77  // 	if !ok {
    78  // 		panic("assert fail")
    79  // 	}
    80  // }