text:
package org.daisy.pipeline.pax.exam;

import java.util.ArrayList;
import static java.util.Collections.sort;
import java.util.List;

import static org.daisy.pipeline.pax.exam.Options.logbackClassic;
import static org.daisy.pipeline.pax.exam.Options.mavenBundlesWithDependencies;

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

import org.ops4j.pax.exam.Option;

public class OptionsTest {

	@Test
	public void testLogbackClassicWithDependencies() {
		List<String> bundles = new ArrayList<String>();
		for (Option b : mavenBundlesWithDependencies(logbackClassic()).getBundles())
			bundles.add(b.toString());
		sort(bundles);
		assertEquals(bundles.get(0).toString(), "mavenBundle(\"ch.qos.logback:logback-classic:1.0.11(start@4)\")");
		assertEquals(bundles.get(1).toString(), "mavenBundle(\"ch.qos.logback:logback-core:1.0.11(start@4)\")");
		assertEquals(bundles.get(2).toString(), "mavenBundle(\"org.slf4j:slf4j-api:1.7.4(start@4)\")");
	}
}

redpajama_set_name: RedPajamaGithub


text:
APPLICATION_ID = "amzn1.ask.skill.dd677950-cade-4805-b1f1-ce2e3a3569f0"

# Bible API
BIBLE_TRANSLATION = "GNBDC"  # Can't use NIV - it's still in copyright
BIBLE_API_URL = "https://bibles.org/v2/eng-{translation}/passages.js".format(
    translation=BIBLE_TRANSLATION)

# Sermons
SERMONS_XML_URL = "http://www.christchurchmayfair.org/our-talks/podcast/"
SERMONS_XML_NAMESPACE = {
    "ccm": "http://www.christchurchmayfair.org/",
    "itunes": "http://www.itunes.com/dtds/podcast-1.0.dtd"
}
SERMONS_XML_SERVICE_NAMES = {"morning": "AM Service", "evening": "6PM Service"}
# Alexa audio must be served from https endpoint
HTTP_MP3_TO_HTTPS_M3U_API_URL = ("https://0elu033c2a.execute-api.eu-west-1.amazonaws.com/prod/"
                                 "m3uGenerator")

# Config for correction of AMAZON.Date defaulting to future date if year not given
FUTURE_DAYS_GO_BACK_YEAR_THRESHOLD_SERMONS = 30
FUTURE_DAYS_GO_BACK_YEAR_THRESHOLD_PASSAGES = 150

# CCM Events
EVENTS_JSON_URL = "https://ccmayfair.churchsuite.co.uk/embed/calendar/json"


text:
using namespace std;
using namespace IStrategizer;

const char* NewParamText = "New Param";
const char* NewValText = "New Val";

PlanStepView::PlanStepView(CrossMap<unsigned, string>* p_idLookup, QWidget *parent)
    : QWidget(parent),
    m_paramEditDialog(nullptr)
{
    ui.setupUi(this);
    m_idLookup = p_idLookup;
}
//----------------------------------------------------------------------------------------------
void PlanStepView::on_tblParameters_itemDoubleClicked(QTableWidgetItem* p_item)
{
    EditSelectedParameter();
}
//----------------------------------------------------------------------------------------------
void PlanStepView::ViewPerformance(const PlanStepEx* p_planStep) const
{
    if(p_planStep == NULL)
    {
        ui.txtFailureProbability->setText(tr("0"));
        ui.txtVulnerableCondition->clear();
    }
    else
    {
        if (BELONG(ActionType, p_planStep->StepTypeId()))
        {
            double probability = ((Action*)p_planStep)->GetExecutionHistory().GetFailureProbability();
            ui.txtFailureProbability->setText(QString::number(probability, 'f', 5));

            ui.txtActionTrailCount->setText(QString::number(((Action*)p_planStep)->GetExecutionHistory().TrialCount()));

            ConditionEx* condition = ((Action*)p_planStep)->GetExecutionHistory().GetMostVulnerableAliveCondition();
            const char* conditionName = condition ? condition->ToString().c_str(): "";
            ui.txtVulnerableCondition->setText(QString::fromLocal8Bit(conditionName));
        }
    }
}
//----------------------------------------------------------------------------------------------
void PlanStepView::View(PlanStepEx* p_planStep)
{
    m_planStep = p_planStep;

    ViewPerformance(p_planStep);

    if(m_planStep == NULL)
    {
        ui.lblPlanStep->setText(QString::fromLocal8Bit("Plan Step Name"));
        ui.tblParameters->clear();
    }
    else
    {
        string stepName = m_idLookup->GetByFirst(p_planStep->StepTypeId());
        ui.lblPlanStep->setText(QString::fromLocal8Bit(stepName.c_str()));
        ViewParameters(&p_planStep->Parameters());
        ViewConditions(p_planStep);
    }
}
//----------------------------------------------------------------------------------------------
void PlanStepView::ViewParameters( const PlanStepParameters* p_params )
{
    ui.tblParameters->clear();
    ui.tblParameters->setColumnCount(2);
    ui.tblParameters->setRowCount(p_params->size());

    QTableWidgetItem* cell = NULL;
    int row = 0;

    ui.tblParameters->horizontalHeader()->hide();
    ui.tblParameters->verticalHeader()->hide();

    QFont paramNameCellFont = QFont("Tahoma", 8);
    paramNameCellFont.setBold(true);
    QFont paramValCellFont = QFont("Tahoma", 8);

    QFontMetrics paramNameFontMetric(paramNameCellFont);
    QFontMetrics paramValFontMetric(paramValCellFont);

    int maxParamNameWidth = INT_MIN;
    int maxParamValWidth = INT_MIN;

    for(PlanStepParameters::const_iterator itr = p_params->begin();
        itr != p_params->end();
        ++itr)
    {
        assert(m_idLookup->ContainsFirst(itr->first));
        cell = new QTableWidgetItem(QString::fromLocal8Bit(m_idLookup->GetByFirst(itr->first).c_str()));
        cell->setFont(paramNameCellFont);
        maxParamNameWidth = max(maxParamNameWidth, paramNameFontMetric.width(cell->text()));

        ui.tblParameters->setItem(row, 0, cell);

        if(!m_idLookup->ContainsFirst(itr->second))
        {
            cell = new QTableWidgetItem(tr("%1").arg(itr->second));
        }
        else
        {
            cell = new QTableWidgetItem(QString::fromLocal8Bit(m_idLookup->GetByFirst(itr->second).c_str()));
        }

        cell->setFont(paramValCellFont);
        maxParamValWidth = max(maxParamValWidth, paramValFontMetric.width(cell->text()));

        ui.tblParameters->setItem(row, 1, cell);
        ui.tblParameters->setRowHeight(row, 20);

        ++row;
    }

    ui.tblParameters->setRowHeight(row, 20);
    ui.tblParameters->setColumnWidth(0, maxParamNameWidth + 10);
    ui.tblParameters->setColumnWidth(1, maxParamValWidth + 10);
}

//----------------------------------------------------------------------------------------------
void PlanStepView::EditSelectedParameter()
{
    QList<QTableWidgetItem*> items = ui.tblParameters->selectedItems();
    // param key/value cell selected
    assert(items.size() == 2);

    string keyTxt = items[0]->text().toLocal8Bit();
    string oldValueTxt = items[1]->text().toLocal8Bit();

    ParameterType key = (ParameterType)m_idLookup->GetBySecond(keyTxt);
    ParameterType oldValue = PARAM_END;

    if (m_idLookup->ContainsSecond(oldValueTxt))
    {
        oldValue = (ParameterType)m_idLookup->GetBySecond(oldValueTxt);
    }

    m_paramEditDialog = new ParameterEdit("Param Name", "Param Value", m_idLookup, ui.tblParameters);
    m_paramEditDialog->ParamName(keyTxt);
    m_paramEditDialog->ParamValue(oldValueTxt);

    if(m_paramEditDialog->exec() == QDialog::Accepted)
    {
        string newValueTxt = m_paramEditDialog->ParamValue();
        int newValue = 0;

        if (m_idLookup->ContainsSecond(newValueTxt))
        {
            newValue = m_idLookup->GetBySecond(newValueTxt);
        }
        else
        {
            newValue = QString::fromLocal8Bit(newValueTxt.c_str()).toInt();
        }

        PlanStepParameters& params = m_planStep->Parameters();
        params[key] = newValue;
        this->View(m_planStep);
    }
}
//----------------------------------------------------------------------------------------------
void PlanStepView::ViewConditions(const PlanStepEx* p_planStep)
{
    //if(BELONG(ActionType, p_planStep->StepTypeId()))
    //{
    //    const Action* action = static_cast<const Action*>(p_planStep);
    //}
}
//----------------------------------------------------------------------------------------------
PlanStepView::~PlanStepView()
{
    Toolbox::MemoryClean(m_paramEditDialog);
}


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
Reports on the Cryptogamic Study in Nepal (Tokyo) 36 (1982)

#### Original name
Thielavia expansa Udagawa & Y. Sugiy.

### Remarks
null


text:
"""
Tests for TemplateProcessor
"""
from template_processor import TemplateProcessor
from constants import COMMAND_START, COMMAND_END, LINE_COMMAND,  \
    LINE_NONE

import copy
import unittest
import numpy as np
import os

IGNORE_TEST = False
DEFINITIONS = {'a': ['a', 'b', 'c'],
               'm': [1, 2, 3],
               'c': ['c', '']}
COMMAND = \
'''%s DefineVariables Begin %s
DEFINITIONS = %s
api.addDefinitions(DEFINITIONS)
%s DefineVariables End %s '''  \
    %(COMMAND_START, COMMAND_END, str(DEFINITIONS),
    COMMAND_START, COMMAND_END)
SUBSTITUTION1 = "J1: S1 -> S2; k1*S1"
SUBSTITUTION2 = "J{a}1: S{a}1 -> S{a}2; k1*S{a}1"
SUBSTITUTION3 = "J{c}1: S{c}1 -> S{c}2; k1*S{c}1"
SUBSTITUTION4 = "J{m}: S{m} -> S{m+1}; k1*S{m}"
TEMPLATE_STG1 = '''
%s
%s
''' % (COMMAND, SUBSTITUTION1)
TEMPLATE_BAD = '''%s
%s''' % (COMMAND_START, SUBSTITUTION1)
TEMPLATE_STG2 = '''%s
%s''' % (COMMAND, SUBSTITUTION2)
TEMPLATE_STG3 = '''%s
%s''' % (COMMAND, SUBSTITUTION3)
TEMPLATE_NO_DEFINITION = SUBSTITUTION3
TEMPLATE_STG4 = '''%s
%s''' % (COMMAND, SUBSTITUTION4)
TEMPLATE_NO_DEFINITION = SUBSTITUTION4

def isSubDict(dict_super, dict_sub):
  """
  Tests if the second dictonary is a subset of the first.
  :param dict dict_super:
  :param dict dict_sub:
  :return bool:
  """
  for key in dict_sub.keys():
    if not key in dict_super:
      return False
    if not dict_sub[key] == dict_super[key]:
      return False
  return True

#############################
# Tests
#############################
# pylint: disable=W0212,C0111,R0904
class TestTemplateProcessor(unittest.TestCase):

  def setUp(self):
    self.processor = TemplateProcessor(TEMPLATE_STG2)

  def testConstructor(self):
    if IGNORE_TEST:
      return
    self.assertIsNotNone(self.processor._extractor)

  def _testExpand(self, template, variable):
    """
    :param str template: template to expand
    :param str variable: variable to check
    """
    self.processor = TemplateProcessor(template)
    lines = self.processor.do()
    try:
      for val in DEFINITIONS[variable]:
        str_val = "J%s" % str(val)
        if not str_val in lines:
          import pdb; pdb.set_trace()
        self.assertTrue(str_val in lines)
    except Exception as exp:
      import pdb; pdb.set_trace()
      pass

  def testDo(self):
    if IGNORE_TEST:
      return
    self._testExpand(TEMPLATE_STG2, 'a')
    self._testExpand(TEMPLATE_STG3, 'c')
    self._testExpand(TEMPLATE_STG4, 'm')

  def testDo2(self):
    template = '''
%s DefineVariables Begin %s
api.addDefinitions({'s':  ['a', 'b'],
                    't':  ['x', 'y']
                  })
%s DefineVariables End %s
J{s}{t}: S1{s}{t} -> S2{s}{t}; k1*S1{s}{t}'''  %  \
        (COMMAND_START, COMMAND_END, COMMAND_START, COMMAND_END)
    self.processor = TemplateProcessor(template)
    lines = self.processor.do()
    actual_lines = 1 + lines.count('\n')
    expected_lines = 4 + template.count('\n')
    self.assertEqual(actual_lines, expected_lines)
    self.assertTrue("addDefinitions" in lines)
    substitutions = ["Jax", "Jbx", "Jay", "Jby"]
    result = all([s in lines for s in substitutions])
    self.assertTrue(result)

  def testExpandErrorInDefinition(self):
    if IGNORE_TEST:
      return
    with self.assertRaises(ValueError):
      processor = TemplateProcessor(TEMPLATE_BAD)
      result = processor.do()

  def testNoDefinintion(self):
    if IGNORE_TEST:
      return
    self.processor = TemplateProcessor(TEMPLATE_NO_DEFINITION)
    with self.assertRaises(ValueError):
      lines = self.processor.do()

  def testFile(self):
    if IGNORE_TEST:
      return
    dir_path = os.path.dirname(os.path.realpath(__file__))
    parent_path = os.path.dirname(dir_path)
    src_path = os.path.join(parent_path, "Example")
    src_path = os.path.join(src_path, "Demo")
    src_path = os.path.join(src_path, "sample.tmpl")
    TemplateProcessor.processFile(src_path, "/tmp/out.mdl")

  def _testProcessCommand(self, template, is_processed, is_command, processor=None):
    """
    Evaluates the processing of a template line
    :return TemplateProcessor:
    """
    if processor is None:
      processor = TemplateProcessor(template)
    line, line_type = processor._extractor.do()
    self.assertEqual(processor._processCommand(), is_processed)
    is_not_none = processor._command is not None
    self.assertEqual(is_command, line_type == LINE_COMMAND)
    return processor

  def testProcessCommand(self):
    if IGNORE_TEST:
      return
    self._testProcessCommand("line 1", False, False)
    self._testProcessCommand("%s DefineVariables Begin %s"
        % (COMMAND_START, COMMAND_END), True, True)
    line = '''%s DefineVariables Begin %s
a = 32
%s DefineVariables End %s
aa{a} + bb{d}'''  \
        % (COMMAND_START, COMMAND_END, COMMAND_START, COMMAND_END)
    processor = self._testProcessCommand(line, True, True)
    self._testProcessCommand(line, True, False, processor=processor)
    self._testProcessCommand(line, True, True, processor=processor)
    self._testProcessCommand(line, False, False, processor=processor)

if __name__ == '__main__':
  unittest.main()


text:
export default {
    data(){
        return {
            isShowDist: false,
            distInfo: null
        }
    },

    mounted(){},

    methods: {
        chooseDistOk(){   // 根据showEditCtn状态把distInfo绑定到editInfo或者searchInfo上
            var key = this.showEditCtn ? 'editInfo' : 'searchInfo';
            this[key].distInfo = this.distInfo

            this[key].address = this.distInfo.districtValue;   // 默认展示address字段内容,所以这里复制给 address

            // 处理当前选中区域编码
            var arr = ['provinceCode', 'cityCode', 'townCode', 'streetCode']
            info[arr[this.distInfo.levelNum - 2]] = this.distInfo.districtCode

            this.isShowDist = false;
        },
        cancelChooseDist(){
            this.distInfo = null;
            this.isShowDist = false;
        },
        treeNodeClick(data){      //  地区树选择
            this.distInfo = data;
        },
    }
}


text:
'use strict';

const RelayContainer = require('RelayContainer');
const RelayMutation = require('RelayMutation');
const RelayNetworkLayer = require('RelayNetworkLayer');
const RelayPropTypes = require('RelayPropTypes');
const RelayQL = require('RelayQL');
const RelayRootContainer = require('RelayRootContainer');
const RelayRoute = require('RelayRoute');
const RelayStore = require('RelayStore');
const RelayTaskScheduler = require('RelayTaskScheduler');
const RelayInternals = require('RelayInternals');

const createRelayQuery = require('createRelayQuery');
const getRelayQueries = require('getRelayQueries');
const isRelayContainer = require('isRelayContainer');

if (typeof global.__REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined') {
  global.__REACT_DEVTOOLS_GLOBAL_HOOK__._relayInternals = RelayInternals;
}

/**
 * Relay contains the set of public methods used to initialize and orchestrate
 * a React application that uses GraphQL to declare data dependencies.
 */
var RelayPublic = {
  Mutation: RelayMutation,
  PropTypes: RelayPropTypes,
  QL: RelayQL,
  RootContainer: RelayRootContainer,
  Route: RelayRoute,
  Store: RelayStore,

  createContainer: RelayContainer.create,
  createQuery: createRelayQuery,
  getQueries: getRelayQueries,
  injectNetworkLayer: RelayNetworkLayer.injectNetworkLayer,
  injectTaskScheduler: RelayTaskScheduler.injectScheduler,
  isContainer: isRelayContainer,
};

module.exports = RelayPublic;


text:
@import ObjectiveC;
#import "UIView+OALayoutAnchor.h"
#import "NSLayoutConstraint+SuppressActive.h"

@interface OALayoutAnchor ()
- (instancetype)initWithAttribute:(NSLayoutAttribute)attribute view:(UIView*)view;
@end

@implementation UIView (OALayoutAnchor)
#ifndef __IPHONE_9_0
@dynamic leadingAnchor, trailingAnchor, leftAnchor, rightAnchor,
topAnchor, bottomAnchor, widthAnchor, heightAnchor,
centerXAnchor, centerYAnchor, firstBaselineAnchor, lastBaselineAnchor;
#endif

- (OALayoutXAxisAnchor *)___leadingAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutXAxisAnchor class]
                                   layoutAttribute:NSLayoutAttributeLeading];
}

- (OALayoutXAxisAnchor *)___trailingAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutXAxisAnchor class]
                                   layoutAttribute:NSLayoutAttributeTrailing];
}

- (OALayoutXAxisAnchor *)___leftAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutXAxisAnchor class]
                                   layoutAttribute:NSLayoutAttributeLeft];
}

- (OALayoutXAxisAnchor *)___rightAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutXAxisAnchor class]
                                   layoutAttribute:NSLayoutAttributeRight];
}

- (OALayoutYAxisAnchor *)___topAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutYAxisAnchor class]
                                   layoutAttribute:NSLayoutAttributeTop];
}

- (OALayoutYAxisAnchor *)___bottomAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutYAxisAnchor class]
                                   layoutAttribute:NSLayoutAttributeBottom];
}

- (OALayoutDimension *)___widthAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutDimension class]
                                   layoutAttribute:NSLayoutAttributeWidth];
}

- (OALayoutDimension *)___heightAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutDimension class]
                                   layoutAttribute:NSLayoutAttributeHeight];
}

- (OALayoutXAxisAnchor *)___centerXAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutXAxisAnchor class]
                                   layoutAttribute:NSLayoutAttributeCenterX];
}

- (OALayoutYAxisAnchor *)___centerYAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutYAxisAnchor class]
                                   layoutAttribute:NSLayoutAttributeCenterY];
}

- (OALayoutYAxisAnchor *)___firstBaselineAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutYAxisAnchor class]
                                   layoutAttribute:NSLayoutAttributeFirstBaseline];
}

- (OALayoutYAxisAnchor *)___lastBaselineAnchor {
  return [self getAnchorAndCreateItIfNeededWithKey:_cmd anchorClass:[OALayoutYAxisAnchor class]
                                   layoutAttribute:NSLayoutAttributeLastBaseline];
}

#pragma mark - adding the methods

+ (void)addAnchorMethodsIfNeeded {
  BOOL hasMethods = [UIView instancesRespondToSelector:@selector(leadingAnchor)];

  // If the methods are already available on UIView that means addAnchorMethodsIfNeeded has already been called
  // Or that we are running on iOS9 and up
  if (!hasMethods) {

    NSArray *methods = @[@"leadingAnchor", @"trailingAnchor", @"leftAnchor", @"rightAnchor",
                         @"topAnchor", @"bottomAnchor", @"widthAnchor", @"heightAnchor",
                         @"centerXAnchor", @"centerYAnchor", @"firstBaselineAnchor", @"lastBaselineAnchor"];

    //Iterate all the method, creating new methods that point to their ___ prefixed ones
    for (NSString *methodName in methods) {
      [self addMethodWithName:methodName toClass:[UIView class]];
    }
  }

  hasMethods = [NSLayoutConstraint instancesRespondToSelector:@selector(setActive:)];
  //We need to add methods for isActive and setActive to be able to transparently use constraints
  if (!hasMethods) {
    [self addMethodWithName:@"isActive" toClass:[NSLayoutConstraint class]];
    [self addMethodWithName:@"setActive:" toClass:[NSLayoutConstraint class]];
  }
}

+ (void)addMethodWithName:(NSString*)methodName toClass:(Class)cls {
  SEL selector = NSSelectorFromString(methodName);
  SEL newSelector = NSSelectorFromString([NSString stringWithFormat:@"___%@", methodName]);

  Method method = class_getInstanceMethod(cls, newSelector);
  IMP imp = [cls instanceMethodForSelector:newSelector];

  class_addMethod(cls, selector, imp, method_getTypeEncoding(method));
}

#pragma mark - helpers

- (id)getAnchorAndCreateItIfNeededWithKey:(SEL)anchorSelector anchorClass:(Class)anchorClass
                          layoutAttribute:(NSLayoutAttribute)layoutAttribute {
  id anchor = [self associatedObjectForKey:anchorSelector];

  if (!anchor) {
    anchor = [[anchorClass alloc] initWithAttribute:layoutAttribute view:self];
    [self setAssociatedObject:anchor forKey:anchorSelector];
  }

  return anchor;
}

- (id)associatedObjectForKey:(SEL)key {
  return objc_getAssociatedObject(self, key);
}

- (void)setAssociatedObject:(id)object forKey:(SEL)key {
  objc_setAssociatedObject(self, key, object, OBJC_ASSOCIATION_RETAIN);
}

@end


text:
namespace clang {
namespace driver {
namespace toolchains {

class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
public:
  Linux(const Driver &D, const llvm::Triple &Triple,
        const llvm::opt::ArgList &Args);

  bool HasNativeLLVMSupport() const override;

  void
  AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
                            llvm::opt::ArgStringList &CC1Args) const override;
  void addLibCxxIncludePaths(
      const llvm::opt::ArgList &DriverArgs,
      llvm::opt::ArgStringList &CC1Args) const override;
  void addLibStdCxxIncludePaths(
      const llvm::opt::ArgList &DriverArgs,
      llvm::opt::ArgStringList &CC1Args) const override;
  void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
                          llvm::opt::ArgStringList &CC1Args) const override;
  void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs,
                           llvm::opt::ArgStringList &CC1Args) const override;
  bool isPIEDefault() const override;
  SanitizerMask getSupportedSanitizers() const override;
  void addProfileRTLibs(const llvm::opt::ArgList &Args,
                        llvm::opt::ArgStringList &CmdArgs) const override;
  virtual std::string computeSysRoot() const;

  virtual std::string getDynamicLinker(const llvm::opt::ArgList &Args) const;

  std::vector<std::string> ExtraOpts;

protected:
  Tool *buildAssembler() const override;
  Tool *buildLinker() const override;
};

} // end namespace toolchains
} // end namespace driver
} // end namespace clang

#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_LINUX_H


text:
using FFImageLoading.Work;
using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using System.IO;
using System.Collections.Generic;

namespace FFImageLoading.DataResolvers
{
    public class ResourceDataResolver : IDataResolver
    {
        private static readonly SemaphoreSlim _cacheLock = new SemaphoreSlim(1, 1);
        private static Dictionary<string, StorageFile> _cache = new Dictionary<string, StorageFile>(128);

        public async virtual Task<DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            StorageFile file = null;
            await _cacheLock.WaitAsync(token).ConfigureAwait(false);
            token.ThrowIfCancellationRequested();

            try
            {
                string resPath = identifier.TrimStart('\\', '/');

                if (!resPath.StartsWith(@"Assets\") && !resPath.StartsWith("Assets/"))
                {
                    resPath = @"Assets\" + resPath;
                }

                var imgUri = new Uri("ms-appx:///" + resPath);
                var key = imgUri.ToString();
                if (!_cache.TryGetValue(key, out file))
                {
                    file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);

                    if (_cache.Count >= 128)
                        _cache.Clear();

                    _cache[key] = file;
                }
            }
            catch (Exception)
            {
                try
                {
                    var imgUri = new Uri("ms-appx:///" + identifier);
                    var key = imgUri.ToString();
                    if (!_cache.TryGetValue(key, out file))
                    {
                        file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);

                        if (_cache.Count >= 128)
                            _cache.Clear();

                        _cache[key] = file;
                    }
                }
                catch (Exception)
                {
                }
            }
            finally
            {
                _cacheLock.Release();
            }

            if (file != null)
            {
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(file.Path);

                token.ThrowIfCancellationRequested();
                var stream = await file.OpenStreamForReadAsync();

                return new DataResolverResult(stream, LoadingResult.CompiledResource, imageInformation);
            }

            throw new FileNotFoundException(identifier);
        }
    }
}


text:
import Gaffer
import GafferScene

Gaffer.Metadata.registerNode(

	GafferScene.FilterMixinBase,

	"description",
	"""
	Base class used for implementing FilterSwitch.
	""",

	plugs = {

		"in" : [

			"description",
			"""
			The input filter.
			""",

		]

	}

)


text:
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('broadcasts', '0007_auto_20141116_1522'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='highlight',
            options={'ordering': ['twid']},
        ),
        migrations.AlterField(
            model_name='highlight',
            name='game',
            field=models.ForeignKey(related_name=b'highlited_on', blank=True, to='games.Game', null=True),
        ),
        migrations.AlterField(
            model_name='highlight',
            name='twid',
            field=models.CharField(help_text="The highlight's ID on Twitch; used for API calls, etc.", max_length=200, verbose_name=b'Twitch ID'),
        ),
        migrations.AlterField(
            model_name='highlight',
            name='url',
            field=models.URLField(verbose_name=b'URL', blank=True),
        ),
    ]


text:
import config
config.import_libs()

import unittest
import websocket
import json
import uuid

from gws import assureIsClosed, assureNotClosed, registerGateway, ZMQConnection

class TestDeviceList(unittest.TestCase):
	def setUp(self):
		self.zmq = ZMQConnection(config.gws_zmq_endpoint)
		self.zmq.accept(lambda m: "gateway_id" in m)
		self.zmq.open()

		self.ws = websocket.WebSocket()
		self.ws.connect(config.gws_ws_uri)

		registerGateway(self, self.ws, config.gateway_id)

		event = self.zmq.pop_data()
		self.assertEqual("on-connected", event["event"])
		self.assertEqual(config.gateway_id, event["gateway_id"])

	def tearDown(self):
		self.ws.close()

		try:
			event = self.zmq.pop_data()
			self.assertEqual("on-disconnected", event["event"])
			self.assertEqual(config.gateway_id, event["gateway_id"])
		finally:
			self.zmq.close()

	"""
	Gateway 1284174504043136 has already associated 4 devices via the testing-data.sql
	init script. 3 of them are active (paired). Ask server for the paired virtual devices
	and test it give us them all.
	"""
	def test1_device_list_success_3_virtual(self):
		id = str(uuid.uuid4())

		msg = json.dumps(
			{
				"message_type" : "device_list_request",
				"id" : id,
				"device_prefix" : "VirtualDevice"
			}
		)

		self.ws.send(msg)
		self.ws.settimeout(5) # this can take more time, suboptimal implementation
		msg = json.loads(self.ws.recv())

		self.assertEqual("device_list_response", msg["message_type"])
		self.assertEqual(id, msg["id"])
		self.assertEqual(1, msg["status"])
		self.assertEqual(3, len(msg["devices"]))
		self.assertEqual("0xa32d27aa5e94ecfd", msg["devices"][0]["device_id"])
		self.assertEqual("0xa335d00019f5234e", msg["devices"][1]["device_id"])
		self.assertEqual("0xa37e0f7f0302324d", msg["devices"][2]["device_id"])

		self.assertEqual(2, len(msg["values"]))
		self.assertIn("0xa32d27aa5e94ecfd", msg["values"])
		self.assertIn("0", msg["values"]["0xa32d27aa5e94ecfd"])
		self.assertNotIn("1", msg["values"]["0xa32d27aa5e94ecfd"])
		self.assertEqual(20, msg["values"]["0xa32d27aa5e94ecfd"]["0"])

		self.assertIn("0xa335d00019f5234e", msg["values"])
		self.assertIn("0", msg["values"]["0xa335d00019f5234e"])
		self.assertIn("1", msg["values"]["0xa335d00019f5234e"])
		self.assertIn("2", msg["values"]["0xa335d00019f5234e"])
		self.assertNotIn("3", msg["values"]["0xa335d00019f5234e"])
		self.assertEqual(19.5, msg["values"]["0xa335d00019f5234e"]["0"])
		self.assertEqual(0, msg["values"]["0xa335d00019f5234e"]["1"])
		# 0xa335d00019f5234e[2] is a random value and cannot be easily tested

		self.assertEqual("10.0.0.1", msg["config"]["0xa32d27aa5e94ecfd"]["ip-address"])
		self.assertEqual("super-secret", msg["config"]["0xa32d27aa5e94ecfd"]["password"])
		self.assertEqual("v1.0-6453", msg["config"]["0xa335d00019f5234e"]["firmware"])

		assureNotClosed(self, self.ws)

	"""
	The gateway_id 1284174504043136 has no Jablotron device registered. Test we
	are given no such device by the server.
	"""
	def test2_device_list_success_0_jablotron(self):
		id = str(uuid.uuid4())

		msg = json.dumps(
			{
				"message_type" : "device_list_request",
				"id" : id,
				"device_prefix" : "Jablotron"
			}
		)

		self.ws.send(msg)
		msg = json.loads(self.ws.recv())

		self.assertEqual("device_list_response", msg["message_type"])
		self.assertEqual(id, msg["id"])
		self.assertEqual(1, msg["status"])
		self.assertEqual(0, len(msg["devices"]))
		self.assertNotIn("values", msg)
		assureNotClosed(self, self.ws)

	"""
	When asking for devices of an unknown device prefix, the server encounters
	an exception which leads to closing the connection. This behaviour would be
	changed in the future. Now, make sure that an unknown prefix breaks the
	connection of the gateway.
	"""
	def test3_device_list_disconnect_bad_prefix(self):
		id = str(uuid.uuid4())

		msg = json.dumps(
			{
				"message_type" : "device_list_request",
				"id" : id,
				"device_prefix" : "Cool"
			}
		)

		self.ws.send(msg)
		assureIsClosed(self, self.ws)

if __name__ == '__main__':
	import sys
	import taprunner
	unittest.main(testRunner=taprunner.TAPTestRunner(stream = sys.stdout))


text:
<?php

namespace Route\Web\Login;

use Model\Model;
use Model\User;
use Route\Web;

class Callback extends Web
{
    public function post()
    {
        if (!isset($this->input->auth)) {
            $this->alert('Authorize failed!');
        }

        $auth = $this->input->auth['auth'];

        try {
            if ($passport = Model::factory('Passport')
                ->where('provider', $auth['provider'])
                ->where('uid', $auth['uid'])->find_one()
            ) {
                $passport->access_token = $auth['credentials']['token'];
                $passport->expired_at = !empty($auth['credentials']['expires']) ? $auth['credentials']['expires'] : null;
                $passport->save();
            } else {
                // 检查当前是否有登陆用户
                if (!$user_id = $this->input->session('login')) {
                    // 没有用户创建一个
                    $user = Model::factory('User')->create();
                    $name = preg_replace('/\s/', '', $auth['info']['name']);

                    if (Model::factory('User')->where('name', $name)->find_one()) {
                        $name .= rand(100, 999);
                    }

                    $user->name = $name;

                    if (isset($auth['info']['description']) && ($bio = $auth['info']['description'])
                        || isset($auth['raw']['description']) && ($bio = $auth['raw']['description'])
                    ) {
                        $user->bio = $bio;
                    }

                    if (isset($auth['info']['email']) && ($email = $auth['info']['email'])) {
                        $user->email = $email;
                    }

                    $user->status = User::OK;
                    $user->save();
                    $user_id = $user->id;
                }

                // 绑定Passport
                $passport = Model::factory('Passport')->create();
                $passport->provider = $auth['provider'];
                $passport->uid = $auth['uid'];
                $passport->display_name = isset($auth['info']['nickname']) ? $auth['info']['nickname'] : $auth['info']['name'];
                $passport->access_token = $auth['credentials']['token'];
                $passport->expired_at = !empty($auth['credentials']['expires']) ? $auth['credentials']['expires'] : null;
                $passport->user_id = $user_id;
                $passport->save();

                if (!empty($user)) {
                    $user->create_passport_id = $passport->id;
                    $user->save();
                }
            }
        } catch (\Exception $e) {
            $this->alert("Create user error: " . $e->getMessage());
        }

        $this->input->session('login', $passport->user_id);
        $this->redirect('/');
    }
}


text:
package com.amazonaws.services.codedeploy.model.transform;

import static com.amazonaws.util.StringUtils.UTF8;
import static com.amazonaws.util.StringUtils.COMMA_SEPARATOR;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.regex.Pattern;

import com.amazonaws.AmazonClientException;
import com.amazonaws.Request;
import com.amazonaws.DefaultRequest;
import com.amazonaws.http.HttpMethodName;
import com.amazonaws.services.codedeploy.model.*;
import com.amazonaws.transform.Marshaller;
import com.amazonaws.util.BinaryUtils;
import com.amazonaws.util.StringUtils;
import com.amazonaws.util.IdempotentUtils;
import com.amazonaws.util.StringInputStream;
import com.amazonaws.util.json.*;

/**
 * RegisterApplicationRevisionRequest Marshaller
 */
public class RegisterApplicationRevisionRequestMarshaller
        implements
        Marshaller<Request<RegisterApplicationRevisionRequest>, RegisterApplicationRevisionRequest> {

    public Request<RegisterApplicationRevisionRequest> marshall(
            RegisterApplicationRevisionRequest registerApplicationRevisionRequest) {

        if (registerApplicationRevisionRequest == null) {
            throw new AmazonClientException(
                    "Invalid argument passed to marshall(...)");
        }

        Request<RegisterApplicationRevisionRequest> request = new DefaultRequest<RegisterApplicationRevisionRequest>(
                registerApplicationRevisionRequest, "AmazonCodeDeploy");
        request.addHeader("X-Amz-Target",
                "CodeDeploy_20141006.RegisterApplicationRevision");

        request.setHttpMethod(HttpMethodName.POST);

        request.setResourcePath("");

        try {
            final SdkJsonGenerator jsonGenerator = new SdkJsonGenerator();

            jsonGenerator.writeStartObject();

            if (registerApplicationRevisionRequest.getApplicationName() != null) {
                jsonGenerator.writeFieldName("applicationName")
                        .writeValue(
                                registerApplicationRevisionRequest
                                        .getApplicationName());
            }
            if (registerApplicationRevisionRequest.getDescription() != null) {
                jsonGenerator.writeFieldName("description").writeValue(
                        registerApplicationRevisionRequest.getDescription());
            }
            if (registerApplicationRevisionRequest.getRevision() != null) {
                jsonGenerator.writeFieldName("revision");
                RevisionLocationJsonMarshaller.getInstance().marshall(
                        registerApplicationRevisionRequest.getRevision(),
                        jsonGenerator);
            }

            jsonGenerator.writeEndObject();

            byte[] content = jsonGenerator.getBytes();
            request.setContent(new ByteArrayInputStream(content));
            request.addHeader("Content-Length",
                    Integer.toString(content.length));
            request.addHeader("Content-Type", "application/x-amz-json-1.1");
        } catch (Throwable t) {
            throw new AmazonClientException(
                    "Unable to marshall request to JSON: " + t.getMessage(), t);
        }

        return request;
    }

}


text:
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Project Oxford: http://ProjectOxford.ai
//
// ProjectOxford SDK Github:
// https://github.com/Microsoft/ProjectOxfordSDK-Windows
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

package com.microsoft.ttshttpoxford.ttssample;

import java.io.File;
import java.io.FileOutputStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class TTSSample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String textToSynthesize = "This is a demo to call microsoft text to speach service in java.";
		String outputFormat = AudioOutputFormat.Riff16Khz16BitMonoPcm;
        String deviceLanguage = "en-US";
        String genderName = Gender.Female;
        String voiceName = "Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)";

        try{
        	byte[] audioBuffer = TTSService.Synthesize(textToSynthesize, outputFormat, deviceLanguage, genderName, voiceName);

        	// write the pcm data to the file
        	String outputWave = ".\\output.pcm";
        	File outputAudio = new File(outputWave);
        	FileOutputStream fstream = new FileOutputStream(outputAudio);
            fstream.write(audioBuffer);
            fstream.flush();
            fstream.close();

            // specify the audio format
           	AudioFormat audioFormat = new AudioFormat(
           			AudioFormat.Encoding.PCM_SIGNED,
               		16000,
               		16,
               		1,
               		1 * 2,
               		16000,
               		false);

               AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(outputWave));

               DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,
                       audioFormat, AudioSystem.NOT_SPECIFIED);
               SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem
                       .getLine(dataLineInfo);
               sourceDataLine.open(audioFormat);
               sourceDataLine.start();
               System.out.println("start to play the wave:");
               /*
                * read the audio data and send to mixer
                */
               int count;
               byte tempBuffer[] = new byte[4096];
               while ((count = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) >0) {
                       sourceDataLine.write(tempBuffer, 0, count);
               }

               sourceDataLine.drain();
               sourceDataLine.close();
               audioInputStream.close();

        }catch(Exception e){
        	e.printStackTrace();
        }
   }

}


text:
using System;
using System.Configuration;
using System.Globalization;

namespace Morell.Scriptor.Core
{
	/// <summary>
	/// Provides methods to initialize the monitor.
	/// </summary>
	public static class Monitor
	{
		/// <summary>
		/// Initializes the file monitor.
		/// </summary>
		/// <param name="parent">The parent.</param>
		/// <param name="errorMessage">The error message.</param>
		/// <returns><c>true</c> if the monitor could be initialized, <c>false</c> otherwise.</returns>
		public static void Initialize(IMonitorUpdate parent)
		{
			string errorMessage;

			// Read configured monitor
			string monitorName = ConfigurationManager.AppSettings["monitor"];
			if (String.IsNullOrEmpty(monitorName))
			{
				errorMessage = "Please configure the monitor type in the section appSettings of the app.config";
				Logger.Error(errorMessage);
				throw new ConfigurationErrorsException(errorMessage);
			}

			// Get dynamic instance of the configured monitor
			IMonitor monitor;
			try
			{
				monitor = Activator.CreateInstance(Type.GetType(monitorName)) as Core.IMonitor;
				if (monitor == null)
				{
					errorMessage = String.Format(CultureInfo.InvariantCulture, "The class '{0}' does not implement the IMonitor interface.", monitorName);
					Logger.Error(errorMessage);
					throw new ConfigurationErrorsException(errorMessage);
				}
			}
			catch (ArgumentException ex)
			{
				Logger.Error(ex.Message);
				throw new ConfigurationErrorsException(String.Format(CultureInfo.InvariantCulture, "Could not load '{0}' class. Please check assembly configuration for the monitor.", monitorName));
			}

			try
			{
				// Start the monitor
				monitor.Start(parent);
				Logger.Info("Monitor initialized");
			}
			catch (ArgumentException ex)
			{
				errorMessage = String.Format(CultureInfo.InvariantCulture, "Please check the project configuration in app.config.\n\r{0}.", ex.Message);
				Logger.Error(errorMessage);
				throw new ConfigurationErrorsException(errorMessage);
			}
		}

		/// <summary>
		/// Disables the monitor.
		/// </summary>
		public static void Disable()
		{
			// TODO: Clean up
			Logger.Info("Monitor disabled");
		}
	}
}


text:
/* global describe, it, beforeEach, afterEach */
/* eslint-disable no-unused-expressions */

const expect = window.chai.expect;
const OdoDevice = window.OdoDevice;

describe('The OdoDevice Component', function device() {
  this.timeout(4000);

  it('can prefix properties', () => {
    expect(OdoDevice.prefixed('color')).to.equal('color');

    // Get the property name when a specific value.
    expect(OdoDevice.prefixed('color', 'blue')).to.equal('color');

    // Get the property name with a specific value which doesn't exit.
    expect(OdoDevice.prefixed('position', 'foo')).to.equal(false);

    // Get the property name of something that doesn't exist.
    expect(OdoDevice.prefixed('odopod')).to.equal(false);

    // Hit cached values.
    expect(OdoDevice.prefixed('odopod')).to.equal(false);
  });

  it('can hyphenate camel cased strings', () => {
    expect(OdoDevice.hyphenate('WebkitTapHighlightColor')).to.equal('-webkit-tap-highlight-color');

    expect(OdoDevice.hyphenate('msTransform')).to.equal('-ms-transform');
  });

  it('will return an empty string when hyphenating a falsy value', () => {
    expect(OdoDevice.hyphenate(null)).to.equal('');
  });

  it('has properties', () => {
    expect(OdoDevice.HAS_TRANSITIONS).to.be.a('boolean');
    expect(OdoDevice.HAS_CSS_ANIMATIONS).to.be.a('boolean');
    expect(OdoDevice.HAS_TRANSFORMS).to.be.a('boolean');
    expect(OdoDevice.CAN_TRANSITION_TRANSFORMS).to.be.a('boolean');
    expect(OdoDevice.HAS_TOUCH_EVENTS).to.be.a('boolean');
    expect(OdoDevice.HAS_POINTER_EVENTS).to.be.a('boolean');
    expect(OdoDevice.HAS_LOCAL_STORAGE).to.be.a('boolean');
    expect(OdoDevice.Dom).to.be.an('object');
    expect(OdoDevice.Css).to.be.an('object');
  });
});


text:
package org.waveprotocol.wave.model.testing;

import org.waveprotocol.wave.model.id.IdGenerator;
import org.waveprotocol.wave.model.id.WaveId;
import org.waveprotocol.wave.model.id.WaveletId;
import org.waveprotocol.wave.model.operation.SilentOperationSink;
import org.waveprotocol.wave.model.operation.wave.WaveletOperation;
import org.waveprotocol.wave.model.operation.wave.WaveletOperationContext;
import org.waveprotocol.wave.model.schema.SchemaProvider;
import org.waveprotocol.wave.model.wave.ObservableWavelet;
import org.waveprotocol.wave.model.wave.ParticipantId;
import org.waveprotocol.wave.model.wave.WaveViewListener;
import org.waveprotocol.wave.model.wave.data.DocumentFactory;
import org.waveprotocol.wave.model.wave.data.impl.WaveletDataImpl;
import org.waveprotocol.wave.model.wave.opbased.ObservableWaveView;
import org.waveprotocol.wave.model.wave.opbased.OpBasedWavelet;
import org.waveprotocol.wave.model.wave.opbased.WaveViewImpl;

/**
 * Dummy implementation of a wave view.
 *
 */
public final class FakeWaveView implements ObservableWaveView, Factory<OpBasedWavelet> {

  public final static class Builder {
    private final SchemaProvider schemas;
    private IdGenerator idGenerator;
    private WaveId waveId;
    private ParticipantId viewer;
    private SilentOperationSink<? super WaveletOperation> sink;
    private WaveViewImpl.WaveletConfigurator configurator;
    private DocumentFactory<?> docFactory;

    private Builder(SchemaProvider schemas) {
      this.schemas = schemas;
    }

    public Builder with(DocumentFactory<?> docFactory) {
      this.docFactory = docFactory;
      return this;
    }

    public Builder with(IdGenerator idGenerator) {
      this.idGenerator = idGenerator;
      return this;
    }

    public Builder with(WaveId wid) {
      this.waveId = wid;
      return this;
    }

    public Builder with(ParticipantId viewer) {
      this.viewer = viewer;
      return this;
    }

    public Builder with(SilentOperationSink<? super WaveletOperation> sink) {
      this.sink = sink;
      return this;
    }

    public Builder with(WaveViewImpl.WaveletConfigurator configurator) {
      this.configurator = configurator;
      return this;
    }

    public FakeWaveView build() {
      if (idGenerator == null) {
        idGenerator = FakeIdGenerator.create();
      }
      if (waveId == null) {
        waveId = idGenerator.newWaveId();
      }
      if (viewer == null) {
        viewer = FAKE_PARTICIPANT;
      }
      if (sink == null) {
        sink = SilentOperationSink.VOID;
      }
      if (configurator == null) {
        configurator = WaveViewImpl.WaveletConfigurator.ADD_CREATOR;
      }
      if (docFactory == null) {
        // Document factory that accepts output-sink registrations.
        docFactory = FakeDocument.Factory.create(schemas);
      }

      // Wavelet factory that does all the work.
      OpBasedWaveletFactory waveletFactory = OpBasedWaveletFactory // \u2620
          .builder(schemas) // \u2620
          .with(WaveletDataImpl.Factory.create(docFactory)) // \u2620
          .with(sink) // \u2620
          .with(viewer) // \u2620
          .build();

      // And the view implementation using that factory.
      WaveViewImpl<OpBasedWavelet> view =
          WaveViewImpl.create(waveletFactory, waveId, idGenerator, viewer, configurator);

      return new FakeWaveView(waveletFactory, view);
    }
  }

  private static final ParticipantId FAKE_PARTICIPANT = new ParticipantId("fake@example.com");

  private final OpBasedWaveletFactory factory;
  private final WaveViewImpl<? extends OpBasedWavelet> view;

  /**
   * Creates a wave view.
   *
   * @param factory  factory exposing testing hacks
   * @param view     real view implementation
   */
  private FakeWaveView(OpBasedWaveletFactory factory, WaveViewImpl<? extends OpBasedWavelet> view) {
    this.factory = factory;
    this.view = view;
  }

  /**
   * @return a builder for a fake wave view.
   */
  public static Builder builder(SchemaProvider schemas) {
    return new Builder(schemas);
  }

  //
  // Expose as basic wavelet factory for wavelet-specific tests.
  //

  @Override
  public OpBasedWavelet create() {
    return createWavelet();
  }

  //
  // Testing hacks.
  //

  public MockParticipationHelper getLastAuthoriser() {
    return factory.getLastAuthoriser();
  }

  public WaveletOperationContext.Factory getLastContextFactory() {
    return factory.getLastContextFactory();
  }

  public OpBasedWavelet createWavelet(WaveletId id) {
    return view.createWavelet(id);
  }

  public void removeWavelet(ObservableWavelet wavelet) {
    view.removeWavelet(wavelet);
  }

  //
  // Delegate view implementation to view.
  //

  @Override
  public OpBasedWavelet createRoot() {
    return view.createRoot();
  }

  @Override
  public OpBasedWavelet createUserData() {
    return view.createUserData();
  }

  @Override
  public OpBasedWavelet createWavelet() {
    return view.createWavelet();
  }

  @Override
  public OpBasedWavelet getRoot() {
    return view.getRoot();
  }

  @Override
  public OpBasedWavelet getUserData() {
    return view.getUserData();
  }

  @Override
  public OpBasedWavelet getWavelet(WaveletId waveletId) {
    return view.getWavelet(waveletId);
  }

  @Override
  public Iterable<? extends OpBasedWavelet> getWavelets() {
    return view.getWavelets();
  }

  @Override
  public WaveId getWaveId() {
    return view.getWaveId();
  }

  @Override
  public void addListener(WaveViewListener listener) {
    view.addListener(listener);
  }

  @Override
  public void removeListener(WaveViewListener listener) {
    view.removeListener(listener);
  }
}


text:
package org.apache.lucene.search.postingshighlight;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.CustomScoreQuery;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.text.BreakIterator;
import java.util.Arrays;
import java.util.Map;

public class TestPostingsHighlighter extends LuceneTestCase {

  public void testBasics() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);

    body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
    iw.addDocument(doc);
    body.setStringValue("Highlighting the first term. Hope it works.");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    Query query = new TermQuery(new Term("body", "highlighting"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
    assertEquals(2, snippets.length);
    assertEquals("Just a test highlighting from postings. ", snippets[0]);
    assertEquals("Highlighting the first term. ", snippets[1]);

    ir.close();
    dir.close();
  }

  public void testFormatWithMatchExceedingContentLength2() throws Exception {

    String bodyText = "123 TEST 01234 TEST";

    String[] snippets = formatWithMatchExceedingContentLength(bodyText);

    assertEquals(1, snippets.length);
    assertEquals("123 TEST 01234 TE", snippets[0]);
  }

  public void testFormatWithMatchExceedingContentLength3() throws Exception {

    String bodyText = "123 5678 01234 TEST TEST";

    String[] snippets = formatWithMatchExceedingContentLength(bodyText);

    assertEquals(1, snippets.length);
    assertEquals("123 5678 01234 TE", snippets[0]);
  }

  public void testFormatWithMatchExceedingContentLength() throws Exception {

    String bodyText = "123 5678 01234 TEST";

    String[] snippets = formatWithMatchExceedingContentLength(bodyText);

    assertEquals(1, snippets.length);
    // LUCENE-5166: no snippet
    assertEquals("123 5678 01234 TE", snippets[0]);
  }

  private String[] formatWithMatchExceedingContentLength(String bodyText) throws IOException {

    int maxLength = 17;

    final Analyzer analyzer = new MockAnalyzer(random());

    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(analyzer);
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    final FieldType fieldType = new FieldType(TextField.TYPE_STORED);
    fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    final Field body = new Field("body", bodyText, fieldType);

    Document doc = new Document();
    doc.add(body);

    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);

    Query query = new TermQuery(new Term("body", "test"));

    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(1, topDocs.totalHits);

    PostingsHighlighter highlighter = new PostingsHighlighter(maxLength);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs);

    ir.close();
    dir.close();
    return snippets;
  }

  // simple test highlighting last word.
  public void testHighlightLastWord() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);

    body.setStringValue("This is a test");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    Query query = new TermQuery(new Term("body", "test"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(1, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
    assertEquals(1, snippets.length);
    assertEquals("This is a test", snippets[0]);

    ir.close();
    dir.close();
  }

  // simple test with one sentence documents.
  public void testOneSentence() throws Exception {
    Directory dir = newDirectory();
    // use simpleanalyzer for more natural tokenization (else "test." is a token)
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);

    body.setStringValue("This is a test.");
    iw.addDocument(doc);
    body.setStringValue("Test a one sentence document.");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    Query query = new TermQuery(new Term("body", "test"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
    assertEquals(2, snippets.length);
    assertEquals("This is a test.", snippets[0]);
    assertEquals("Test a one sentence document.", snippets[1]);

    ir.close();
    dir.close();
  }

  // simple test with multiple values that make a result longer than maxLength.
  public void testMaxLengthWithMultivalue() throws Exception {
    Directory dir = newDirectory();
    // use simpleanalyzer for more natural tokenization (else "test." is a token)
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Document doc = new Document();

    for(int i = 0; i < 3 ; i++) {
      Field body = new Field("body", "", offsetsType);
      body.setStringValue("This is a multivalued field");
      doc.add(body);
    }

    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter(40);
    Query query = new TermQuery(new Term("body", "field"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(1, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
    assertEquals(1, snippets.length);
    assertTrue("Snippet should have maximum 40 characters plus the pre and post tags",
        snippets[0].length() == (40 + "".length()));

    ir.close();
    dir.close();
  }

  public void testMultipleFields() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Field title = new Field("title", "", offsetsType);
    Document doc = new Document();
    doc.add(body);
    doc.add(title);

    body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
    title.setStringValue("I am hoping for the best.");
    iw.addDocument(doc);
    body.setStringValue("Highlighting the first term. Hope it works.");
    title.setStringValue("But best may not be good enough.");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new TermQuery(new Term("body", "highlighting")), BooleanClause.Occur.SHOULD);
    query.add(new TermQuery(new Term("title", "best")), BooleanClause.Occur.SHOULD);
    TopDocs topDocs = searcher.search(query.build(), 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    Map<String,String[]> snippets = highlighter.highlightFields(new String [] { "body", "title" }, query.build(), searcher, topDocs);
    assertEquals(2, snippets.size());
    assertEquals("Just a test highlighting from postings. ", snippets.get("body")[0]);
    assertEquals("Highlighting the first term. ", snippets.get("body")[1]);
    assertEquals("I am hoping for the best.", snippets.get("title")[0]);
    assertEquals("But best may not be good enough.", snippets.get("title")[1]);
    ir.close();
    dir.close();
  }

  public void testMultipleTerms() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);

    body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
    iw.addDocument(doc);
    body.setStringValue("Highlighting the first term. Hope it works.");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new TermQuery(new Term("body", "highlighting")), BooleanClause.Occur.SHOULD);
    query.add(new TermQuery(new Term("body", "just")), BooleanClause.Occur.SHOULD);
    query.add(new TermQuery(new Term("body", "first")), BooleanClause.Occur.SHOULD);
    TopDocs topDocs = searcher.search(query.build(), 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query.build(), searcher, topDocs);
    assertEquals(2, snippets.length);
    assertEquals("Just a test highlighting from postings. ", snippets[0]);
    assertEquals("Highlighting the first term. ", snippets[1]);

    ir.close();
    dir.close();
  }

  public void testMultiplePassages() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);

    body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
    iw.addDocument(doc);
    body.setStringValue("This test is another test. Not a good sentence. Test test test test.");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    Query query = new TermQuery(new Term("body", "test"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 2);
    assertEquals(2, snippets.length);
    assertEquals("This is a test. Just a test highlighting from postings. ", snippets[0]);
    assertEquals("This test is another test. ... Test test test test.", snippets[1]);

    ir.close();
    dir.close();
  }

  public void testUserFailedToIndexOffsets() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType positionsType = new FieldType(TextField.TYPE_STORED);
    positionsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
    Field body = new Field("body", "", positionsType);
    Field title = new StringField("title", "", Field.Store.YES);
    Document doc = new Document();
    doc.add(body);
    doc.add(title);

    body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
    title.setStringValue("test");
    iw.addDocument(doc);
    body.setStringValue("This test is another test. Not a good sentence. Test test test test.");
    title.setStringValue("test");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    Query query = new TermQuery(new Term("body", "test"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    try {
      highlighter.highlight("body", query, searcher, topDocs, 2);
      fail("did not hit expected exception");
    } catch (IllegalArgumentException iae) {
      // expected
    }

    try {
      highlighter.highlight("title", new TermQuery(new Term("title", "test")), searcher, topDocs, 2);
      fail("did not hit expected exception");
    } catch (IllegalArgumentException iae) {
      // expected
    }
    ir.close();
    dir.close();
  }

  public void testBuddhism() throws Exception {
    String text = "This eight-volume set brings together seminal papers in Buddhist studies from a vast " +
                  "range of academic disciplines published over the last forty years. With a new introduction " +
                  "by the editor, this collection is a unique and unrivalled research resource for both " +
                  "student and scholar. Coverage includes: - Buddhist origins; early history of Buddhism in " +
                  "South and Southeast Asia - early Buddhist Schools and Doctrinal History; Theravada Doctrine " +
                  "- the Origins and nature of Mahayana Buddhism; some Mahayana religious topics - Abhidharma " +
                  "and Madhyamaka - Yogacara, the Epistemological tradition, and Tathagatagarbha - Tantric " +
                  "Buddhism (Including China and Japan); Buddhism in Nepal and Tibet - Buddhism in South and " +
                  "Southeast Asia, and - Buddhism in China, East Asia, and Japan.";
    Directory dir = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, analyzer);

    FieldType positionsType = new FieldType(TextField.TYPE_STORED);
    positionsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", text, positionsType);
    Document document = new Document();
    document.add(body);
    iw.addDocument(document);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher searcher = newSearcher(ir);
    PhraseQuery query = new PhraseQuery("body", "buddhist", "origins");
    TopDocs topDocs = searcher.search(query, 10);
    assertEquals(1, topDocs.totalHits);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 2);
    assertEquals(1, snippets.length);
    assertTrue(snippets[0].contains("Buddhist origins"));
    ir.close();
    dir.close();
  }

  public void testCuriousGeorge() throws Exception {
    String text = "It's the formula for success for preschoolers—Curious George and fire trucks! " +
                  "Curious George and the Firefighters is a story based on H. A. and Margret Rey's " +
                  "popular primate and painted in the original watercolor and charcoal style. " +
                  "Firefighters are a famously brave lot, but can they withstand a visit from one curious monkey?";
    Directory dir = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, analyzer);
    FieldType positionsType = new FieldType(TextField.TYPE_STORED);
    positionsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", text, positionsType);
    Document document = new Document();
    document.add(body);
    iw.addDocument(document);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher searcher = newSearcher(ir);
    PhraseQuery query = new PhraseQuery("body", "curious", "george");
    TopDocs topDocs = searcher.search(query, 10);
    assertEquals(1, topDocs.totalHits);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 2);
    assertEquals(1, snippets.length);
    assertFalse(snippets[0].contains("CuriousCurious"));
    ir.close();
    dir.close();
  }

  public void testCambridgeMA() throws Exception {
    BufferedReader r = new BufferedReader(new InputStreamReader(
                     this.getClass().getResourceAsStream("CambridgeMA.utf8"), StandardCharsets.UTF_8));
    String text = r.readLine();
    r.close();
    Directory dir = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, analyzer);
    FieldType positionsType = new FieldType(TextField.TYPE_STORED);
    positionsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", text, positionsType);
    Document document = new Document();
    document.add(body);
    iw.addDocument(document);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher searcher = newSearcher(ir);
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new TermQuery(new Term("body", "porter")), BooleanClause.Occur.SHOULD);
    query.add(new TermQuery(new Term("body", "square")), BooleanClause.Occur.SHOULD);
    query.add(new TermQuery(new Term("body", "massachusetts")), BooleanClause.Occur.SHOULD);
    TopDocs topDocs = searcher.search(query.build(), 10);
    assertEquals(1, topDocs.totalHits);
    PostingsHighlighter highlighter = new PostingsHighlighter(Integer.MAX_VALUE-1);
    String snippets[] = highlighter.highlight("body", query.build(), searcher, topDocs, 2);
    assertEquals(1, snippets.length);
    assertTrue(snippets[0].contains("Square"));
    assertTrue(snippets[0].contains("Porter"));
    ir.close();
    dir.close();
  }

  public void testPassageRanking() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);

    body.setStringValue("This is a test.  Just highlighting from postings. This is also a much sillier test.  Feel free to test test test test test test test.");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    Query query = new TermQuery(new Term("body", "test"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(1, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 2);
    assertEquals(1, snippets.length);
    assertEquals("This is a test.  ... Feel free to test test test test test test test.", snippets[0]);

    ir.close();
    dir.close();
  }

  public void testBooleanMustNot() throws Exception {
    Directory dir = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, analyzer);
    FieldType positionsType = new FieldType(TextField.TYPE_STORED);
    positionsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "This sentence has both terms.  This sentence has only terms.", positionsType);
    Document document = new Document();
    document.add(body);
    iw.addDocument(document);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher searcher = newSearcher(ir);
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new TermQuery(new Term("body", "terms")), BooleanClause.Occur.SHOULD);
    BooleanQuery.Builder query2 = new BooleanQuery.Builder();
    query.add(query2.build(), BooleanClause.Occur.SHOULD);
    query2.add(new TermQuery(new Term("body", "both")), BooleanClause.Occur.MUST_NOT);
    TopDocs topDocs = searcher.search(query.build(), 10);
    assertEquals(1, topDocs.totalHits);
    PostingsHighlighter highlighter = new PostingsHighlighter(Integer.MAX_VALUE-1);
    String snippets[] = highlighter.highlight("body", query.build(), searcher, topDocs, 2);
    assertEquals(1, snippets.length);
    assertFalse(snippets[0].contains("both"));
    ir.close();
    dir.close();
  }

  public void testHighlightAllText() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);

    body.setStringValue("This is a test.  Just highlighting from postings. This is also a much sillier test.  Feel free to test test test test test test test.");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter(10000) {
      @Override
      protected BreakIterator getBreakIterator(String field) {
        return new WholeBreakIterator();
      }
    };
    Query query = new TermQuery(new Term("body", "test"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(1, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 2);
    assertEquals(1, snippets.length);
    assertEquals("This is a test.  Just highlighting from postings. This is also a much sillier test.  Feel free to test test test test test test test.", snippets[0]);

    ir.close();
    dir.close();
  }

  public void testSpecificDocIDs() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);

    body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
    iw.addDocument(doc);
    body.setStringValue("Highlighting the first term. Hope it works.");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    Query query = new TermQuery(new Term("body", "highlighting"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    ScoreDoc[] hits = topDocs.scoreDocs;
    int[] docIDs = new int[2];
    docIDs[0] = hits[0].doc;
    docIDs[1] = hits[1].doc;
    String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 1 }).get("body");
    assertEquals(2, snippets.length);
    assertEquals("Just a test highlighting from postings. ", snippets[0]);
    assertEquals("Highlighting the first term. ", snippets[1]);

    ir.close();
    dir.close();
  }

  public void testCustomFieldValueSource() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    Document doc = new Document();

    FieldType offsetsType = new FieldType(TextField.TYPE_NOT_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    final String text = "This is a test.  Just highlighting from postings. This is also a much sillier test.  Feel free to test test test test test test test.";
    Field body = new Field("body", text, offsetsType);
    doc.add(body);
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);

    PostingsHighlighter highlighter = new PostingsHighlighter(10000) {
        @Override
        protected String[][] loadFieldValues(IndexSearcher searcher, String[] fields, int[] docids, int maxLength) throws IOException {
          assert fields.length == 1;
          assert docids.length == 1;
          String[][] contents = new String[1][1];
          contents[0][0] = text;
          return contents;
        }

        @Override
        protected BreakIterator getBreakIterator(String field) {
          return new WholeBreakIterator();
        }
      };

    Query query = new TermQuery(new Term("body", "test"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(1, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 2);
    assertEquals(1, snippets.length);
    assertEquals("This is a test.  Just highlighting from postings. This is also a much sillier test.  Feel free to test test test test test test test.", snippets[0]);

    ir.close();
    dir.close();
  }

  /** Make sure highlighter returns first N sentences if
   *  there were no hits. */
  public void testEmptyHighlights() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Document doc = new Document();

    Field body = new Field("body", "test this is.  another sentence this test has.  far away is that planet.", offsetsType);
    doc.add(body);
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    Query query = new TermQuery(new Term("body", "highlighting"));
    int[] docIDs = new int[] {0};
    String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 2 }).get("body");
    assertEquals(1, snippets.length);
    assertEquals("test this is.  another sentence this test has.  ", snippets[0]);

    ir.close();
    dir.close();
  }

  /** Make sure highlighter we can customize how emtpy
   *  highlight is returned. */
  public void testCustomEmptyHighlights() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Document doc = new Document();

    Field body = new Field("body", "test this is.  another sentence this test has.  far away is that planet.", offsetsType);
    doc.add(body);
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter() {
        @Override
        public Passage[] getEmptyHighlight(String fieldName, BreakIterator bi, int maxPassages) {
          return new Passage[0];
        }
      };
    Query query = new TermQuery(new Term("body", "highlighting"));
    int[] docIDs = new int[] {0};
    String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 2 }).get("body");
    assertEquals(1, snippets.length);
    assertNull(snippets[0]);

    ir.close();
    dir.close();
  }

  /** Make sure highlighter returns whole text when there
   *  are no hits and BreakIterator is null. */
  public void testEmptyHighlightsWhole() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Document doc = new Document();

    Field body = new Field("body", "test this is.  another sentence this test has.  far away is that planet.", offsetsType);
    doc.add(body);
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter(10000) {
      @Override
      protected BreakIterator getBreakIterator(String field) {
        return new WholeBreakIterator();
      }
    };
    Query query = new TermQuery(new Term("body", "highlighting"));
    int[] docIDs = new int[] {0};
    String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 2 }).get("body");
    assertEquals(1, snippets.length);
    assertEquals("test this is.  another sentence this test has.  far away is that planet.", snippets[0]);

    ir.close();
    dir.close();
  }

  /** Make sure highlighter is OK with entirely missing
   *  field. */
  public void testFieldIsMissing() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Document doc = new Document();

    Field body = new Field("body", "test this is.  another sentence this test has.  far away is that planet.", offsetsType);
    doc.add(body);
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    Query query = new TermQuery(new Term("bogus", "highlighting"));
    int[] docIDs = new int[] {0};
    String snippets[] = highlighter.highlightFields(new String[] {"bogus"}, query, searcher, docIDs, new int[] { 2 }).get("bogus");
    assertEquals(1, snippets.length);
    assertNull(snippets[0]);

    ir.close();
    dir.close();
  }

  public void testFieldIsJustSpace() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);

    Document doc = new Document();
    doc.add(new Field("body", "   ", offsetsType));
    doc.add(new Field("id", "id", offsetsType));
    iw.addDocument(doc);

    doc = new Document();
    doc.add(new Field("body", "something", offsetsType));
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    int docID = searcher.search(new TermQuery(new Term("id", "id")), 1).scoreDocs[0].doc;

    Query query = new TermQuery(new Term("body", "highlighting"));
    int[] docIDs = new int[1];
    docIDs[0] = docID;
    String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 2 }).get("body");
    assertEquals(1, snippets.length);
    assertEquals("   ", snippets[0]);

    ir.close();
    dir.close();
  }

  public void testFieldIsEmptyString() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);

    Document doc = new Document();
    doc.add(new Field("body", "", offsetsType));
    doc.add(new Field("id", "id", offsetsType));
    iw.addDocument(doc);

    doc = new Document();
    doc.add(new Field("body", "something", offsetsType));
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    int docID = searcher.search(new TermQuery(new Term("id", "id")), 1).scoreDocs[0].doc;

    Query query = new TermQuery(new Term("body", "highlighting"));
    int[] docIDs = new int[1];
    docIDs[0] = docID;
    String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 2 }).get("body");
    assertEquals(1, snippets.length);
    assertNull(snippets[0]);

    ir.close();
    dir.close();
  }

  public void testMultipleDocs() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);

    int numDocs = atLeast(100);
    for(int i=0;i<numDocs;i++) {
      Document doc = new Document();
      String content = "the answer is " + i;
      if ((i & 1) == 0) {
        content += " some more terms";
      }
      doc.add(new Field("body", content, offsetsType));
      doc.add(newStringField("id", ""+i, Field.Store.YES));
      iw.addDocument(doc);

      if (random().nextInt(10) == 2) {
        iw.commit();
      }
    }

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    Query query = new TermQuery(new Term("body", "answer"));
    TopDocs hits = searcher.search(query, numDocs);
    assertEquals(numDocs, hits.totalHits);

    String snippets[] = highlighter.highlight("body", query, searcher, hits);
    assertEquals(numDocs, snippets.length);
    for(int hit=0;hit<numDocs;hit++) {
      Document doc = searcher.doc(hits.scoreDocs[hit].doc);
      int id = Integer.parseInt(doc.get("id"));
      String expected = "the answer is " + id;
      if ((id  & 1) == 0) {
        expected += " some more terms";
      }
      assertEquals(expected, snippets[hit]);
    }

    ir.close();
    dir.close();
  }

  public void testMultipleSnippetSizes() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Field title = new Field("title", "", offsetsType);
    Document doc = new Document();
    doc.add(body);
    doc.add(title);

    body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
    title.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new TermQuery(new Term("body", "test")), BooleanClause.Occur.SHOULD);
    query.add(new TermQuery(new Term("title", "test")), BooleanClause.Occur.SHOULD);
    Map<String,String[]> snippets = highlighter.highlightFields(new String[] { "title", "body" }, query.build(), searcher, new int[] { 0 }, new int[] { 1, 2 });
    String titleHighlight = snippets.get("title")[0];
    String bodyHighlight = snippets.get("body")[0];
    assertEquals("This is a test. ", titleHighlight);
    assertEquals("This is a test. Just a test highlighting from postings. ", bodyHighlight);
    ir.close();
    dir.close();
  }

  public void testEncode() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);

    body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter() {
      @Override
      protected PassageFormatter getFormatter(String field) {
        return new DefaultPassageFormatter("", "", "... ", true);
      }
    };
    Query query = new TermQuery(new Term("body", "highlighting"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(1, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
    assertEquals(1, snippets.length);
    assertEquals("Just a test highlighting from <i>postings</i>. ", snippets[0]);

    ir.close();
    dir.close();
  }

  /** customizing the gap separator to force a sentence break */
  public void testGapSeparator() throws Exception {
    Directory dir = newDirectory();
    // use simpleanalyzer for more natural tokenization (else "test." is a token)
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Document doc = new Document();

    Field body1 = new Field("body", "", offsetsType);
    body1.setStringValue("This is a multivalued field");
    doc.add(body1);

    Field body2 = new Field("body", "", offsetsType);
    body2.setStringValue("This is something different");
    doc.add(body2);

    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter() {
      @Override
      protected char getMultiValuedSeparator(String field) {
        assert field.equals("body");
        return '\u2029';
      }
    };
    Query query = new TermQuery(new Term("body", "field"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(1, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
    assertEquals(1, snippets.length);
    assertEquals("This is a multivalued field\u2029", snippets[0]);

    ir.close();
    dir.close();
  }

  // LUCENE-4906
  public void testObjectFormatter() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);

    body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    IndexSearcher searcher = newSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter() {
      @Override
      protected PassageFormatter getFormatter(String field) {
        return new PassageFormatter() {
          PassageFormatter defaultFormatter = new DefaultPassageFormatter();

          @Override
          public String[] format(Passage passages[], String content) {
            // Just turns the String snippet into a length 2
            // array of String
            return new String[] {"blah blah", defaultFormatter.format(passages, content).toString()};
          }
        };
      }
    };

    Query query = new TermQuery(new Term("body", "highlighting"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(1, topDocs.totalHits);
    int[] docIDs = new int[1];
    docIDs[0] = topDocs.scoreDocs[0].doc;
    Map<String,Object[]> snippets = highlighter.highlightFieldsAsObjects(new String[]{"body"}, query, searcher, docIDs, new int[] {1});
    Object[] bodySnippets = snippets.get("body");
    assertEquals(1, bodySnippets.length);
    assertTrue(Arrays.equals(new String[] {"blah blah", "Just a test highlighting from postings. "}, (String[]) bodySnippets[0]));

    ir.close();
    dir.close();
  }

  public void testFieldSometimesMissingFromSegment() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "foo", offsetsType);
    Document doc = new Document();
    doc.add(body);
    iw.addDocument(doc);

    // Make a 2nd segment where body is only stored:
    iw.commit();
    doc = new Document();
    doc.add(new StoredField("body", "foo"));
    iw.addDocument(doc);

    IndexReader ir = DirectoryReader.open(iw.w, true);
    iw.close();

    IndexSearcher searcher = new IndexSearcher(ir);
    PostingsHighlighter highlighter = new PostingsHighlighter();
    Query query = new MatchAllDocsQuery();
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
    assertEquals(2, snippets.length);
    assertEquals("foo", snippets[0]);
    assertNull(snippets[1]);
    ir.close();
    dir.close();
  }

  public void testCustomScoreQueryHighlight() throws Exception{
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);

    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);

    body.setStringValue("This piece of text refers to Kennedy at the beginning then has a longer piece of text that is very long in the middle and finally ends with another reference to Kennedy");
    iw.addDocument(doc);

    IndexReader ir = iw.getReader();
    iw.close();

    TermQuery termQuery = new TermQuery(new Term("body", "very"));
    PostingsHighlighter highlighter = new PostingsHighlighter();
    CustomScoreQuery query = new CustomScoreQuery(termQuery);

    IndexSearcher searcher = newSearcher(ir);
    TopDocs hits = searcher.search(query, 10);
    assertEquals(1, hits.totalHits);

    String snippets[] = highlighter.highlight("body", query, searcher, hits);
    assertEquals(1, snippets.length);
    assertEquals("This piece of text refers to Kennedy at the beginning then has a longer piece of text that is very long in the middle and finally ends with another reference to Kennedy",
                 snippets[0]);

    ir.close();
    dir.close();
  }
}